Subversion Repositories svnkaklik

Compare Revisions

Ignore whitespace Rev 508 → Rev 509

/programy/C/avr/akcelerometr/a2dtest.c
1,18 → 1,3
//*****************************************************************************
// File Name : a2dtest.c
//
// Title : example usage of some avr library functions
// Revision : 1.0
// Notes :
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// Revision History:
// When Who Description of change
// ----------- ----------- -----------------------
// 20-Oct-2002 pstang Created the program
//*****************************************************************************
//----- Include Files ---------------------------------------------------------
#include <avr/io.h> // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h> // include interrupt support
27,12 → 12,33
//----- Begin Code ------------------------------------------------------------
#define BUFLEN 64
 
void radtodeg(double fi, u16 *deg, u08 *min, u08 *sec) //convert radians to degrees mins and seconds
{
double pom;
 
fi=fi*180/PI;
*deg=(u16)trunc(fi);
pom=(fi-(*deg))*60;
*min=(u08)trunc(pom);
*sec=(u08)round((pom-(*min))*60);
}
 
inline double quadraticerror(double average, double buf[], u16 size)
{
u16 i;
double err=0;
 
for(i=0; i<size; i++) err += square(buf[i]-average); // sum quadratic errors
err = sqrt(err/(size-1))/sqrt(size); // compute average quadratic error
return err;
}
 
int main(void)
{
u16 i,x,y;
double fi, err, fibuf[BUFLEN];
s16 fia, erra;
u16 fib, errb;
u08 fi_min, fi_sec, err_min, err_sec;
u16 fi_deg, err_deg;
 
// initialize our libraries
// initialize the UART (serial port)
46,9 → 52,9
a2dInit();
// configure a2d port (PORTA) as input
// so we can receive analog signals
DDRA = 0x00;
DDRF = 0x00;
// make sure pull-up resistors are turned off
PORTA = 0x00;
PORTF = 0x00;
 
// set the a2d prescaler (clock division ratio)
// - a lower prescale setting will make the a2d converter go faster
65,6 → 71,8
// use a2dConvert8bit(channel#) to get an 8bit a2d reading
// use a2dConvert10bit(channel#) to get a 10bit a2d reading
 
rprintf("inklinometr 2009\r\n");
 
while(1)
{
fi=0;
76,19 → 84,19
fibuf[i] = atan2((double)x-511,(double)y-511); // record computed angles to buffer for post processing
}
for(i=0; i<BUFLEN; i++) fi += fibuf[i]; // sum recorded angles
fi = (fi/BUFLEN)+PI; // average recorded angles and expand product to whole circle
 
fi = ((fi/BUFLEN)+PI) * 180.0 / PI; // average recorded angles and convert product to degrees
 
for(i=0; i<BUFLEN; i++) err += (fibuf[i]-fi)*(fibuf[i]-fi); // sum cubic errors
err = sqrt(err/(BUFLEN-1))/sqrt(BUFLEN); // compute average cubic error
erra = floor(err);
errb = floor((err - erra)*1000);
/*for(i=0; i<BUFLEN; i++)
{
fibuf[i]=i;
}*/
fia = floor(fi);
fib = floor((fi - fia)*1000);
 
err=quadraticerror(fi,fibuf,BUFLEN);
radtodeg(fi,&fi_deg,&fi_min,&fi_sec);
radtodeg(err,&err_deg,&err_min,&err_sec);
rprintf("fi:%d.%d +- %d.%d \r\n", fia, fib, erra, errb);
rprintf("fi:%d.%d.%d +- %d.%d.%d \r\n", fi_deg, fi_min, fi_sec, err_deg, err_min, err_sec);
delay_ms(20);
}
return 0;
}