Rev Author Line No. Line
1500 kaklik 1 #include "main.h"
2  
3 #define REV "$Rev$"
4  
5 unsigned int32 time; // pocitadlo preteceni casovace
6  
7 #define BUFLEN 100
8  
9 #int_RTCC
10 void RTCC_isr(void) // preruseni od pretekleho casovace
11 {
12 time++;
13 }
14  
15 // Includes all USB code and interrupts, as well as the CDC API
16 #include <usb_cdc.h>
17 #include <math.h>
18  
19 float quadraticerror(float average, float buf[], int16 size) // compute average quadratic error
20 {
21 int16 i;
22 float err=0;
23  
24 for(i=0; i<size; i++) err += (buf[i]-average)*(buf[i]-average); // sum quadratic errors
25 err = sqrt((1/(float)size)*err);
26 return err;
27 }
28  
29 void main()
30 {
31  
32 float x[BUFLEN], y[BUFLEN], z[BUFLEN];
33 float xavg, yavg, zavg;
34 int i;
35 port_b_pullups(FALSE);
36 setup_psp(PSP_DISABLED);
37 setup_spi(SPI_SS_DISABLED);
38 setup_wdt(WDT_OFF);
39 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
40 setup_timer_1(T1_DISABLED);
41 setup_timer_2(T2_DISABLED,0,1);
42 setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
43 setup_comparator(NC_NC_NC_NC);
44 setup_vref(FALSE);
45 enable_interrupts(INT_TIMER0);
46 enable_interrupts(GLOBAL);
47  
48 setup_adc_ports(AN0_TO_AN2|VSS_VREF);
1504 kaklik 49 setup_adc(ADC_CLOCK_DIV_32);
1500 kaklik 50  
51 usb_init(); // initialise USB module
52  
53 while(!usb_cdc_connected());
54 time=0;
55 set_timer0(0);
56 printf(usb_cdc_putc,"time[s] X Xerr Y Yerr Z Zerr \n\r");
57  
58 while(usb_cdc_connected()) // pockej nez se pripoji seriovy port PC
59 {
60 for(i=0; i <BUFLEN; i++)
61 {
62 set_adc_channel(0);
63 delay_us(10);
64 x[i]=read_adc();
65 xavg+=x[i];
66  
67 set_adc_channel(1);
68 delay_us(10);
69 y[i]=read_adc();
70 yavg+=y[i];
71  
72 set_adc_channel(2);
73 delay_us(10);
74 z[i]=read_adc();
75 zavg+=z[i];
76 }
77  
78 xavg=xavg/BUFLEN;
79 yavg=yavg/BUFLEN;
80 zavg=zavg/BUFLEN;
81  
82 // odesli namerene hodnoty
83 printf(usb_cdc_putc, "%7.3f %4.3f %4.3f %4.3f %4.3f %4.3f %4.3f \n\r",((time << 16) + get_timer0())/15625.0, xavg, quadraticerror(xavg,x,BUFLEN), yavg, quadraticerror(yavg,y,BUFLEN), zavg, quadraticerror(zavg,z,BUFLEN)); //konstanta k je kvuli prevodu do rozzumnych jednotek [s]
84 }
85 }