Subversion Repositories svnkaklik

Rev

Rev 658 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log

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