Subversion Repositories svnkaklik

Rev

Rev 508 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 508 Rev 509
Line 1... Line -...
1
//*****************************************************************************
-
 
2
// File Name	: a2dtest.c
-
 
3
// 
-
 
4
// Title		: example usage of some avr library functions
-
 
5
// Revision		: 1.0
-
 
6
// Notes		:	
-
 
7
// Target MCU	: Atmel AVR series
-
 
8
// Editor Tabs	: 4
-
 
9
// 
-
 
10
// Revision History:
-
 
11
// When			Who			Description of change
-
 
12
// -----------	-----------	-----------------------
-
 
13
// 20-Oct-2002	pstang		Created the program
-
 
14
//*****************************************************************************
-
 
15
 
-
 
16
//----- Include Files ---------------------------------------------------------
1
//----- Include Files ---------------------------------------------------------
17
#include <avr/io.h>		// include I/O definitions (port names, pin names, etc)
2
#include <avr/io.h>		// include I/O definitions (port names, pin names, etc)
18
#include <avr/interrupt.h>	// include interrupt support
3
#include <avr/interrupt.h>	// include interrupt support
19
#include <math.h>
4
#include <math.h>
20
 
5
 
Line 25... Line 10...
25
#include "a2d.h"		// include A/D converter function library
10
#include "a2d.h"		// include A/D converter function library
26
 
11
 
27
//----- Begin Code ------------------------------------------------------------
12
//----- Begin Code ------------------------------------------------------------
28
#define BUFLEN 64
13
#define BUFLEN 64
29
 
14
 
-
 
15
void radtodeg(double fi, u16 *deg, u08 *min, u08 *sec)	//convert radians to degrees mins and seconds
-
 
16
{
-
 
17
  double pom;
-
 
18
 
-
 
19
	fi=fi*180/PI;
-
 
20
	*deg=(u16)trunc(fi);
-
 
21
	pom=(fi-(*deg))*60;
-
 
22
	*min=(u08)trunc(pom);
-
 
23
	*sec=(u08)round((pom-(*min))*60);
-
 
24
}
-
 
25
 
-
 
26
inline double quadraticerror(double average, double buf[], u16 size)
-
 
27
{
-
 
28
  u16 i;
-
 
29
  double err=0;
-
 
30
 
-
 
31
	for(i=0; i<size; i++) err += square(buf[i]-average);		// sum quadratic errors
-
 
32
	err = sqrt(err/(size-1))/sqrt(size);	// compute average quadratic error
-
 
33
	return err;
-
 
34
}
-
 
35
 
30
int main(void)
36
int main(void)
31
{
37
{
32
	u16 i,x,y;
38
	u16 i,x,y;
33
	double fi, err, fibuf[BUFLEN];
39
	double fi, err, fibuf[BUFLEN];
34
	s16 fia, erra;
40
	u08 fi_min, fi_sec, err_min, err_sec;
35
	u16 fib, errb;
41
	u16 fi_deg, err_deg;
36
 
42
 
37
	// initialize our libraries
43
	// initialize our libraries
38
	// initialize the UART (serial port)
44
	// initialize the UART (serial port)
39
	uartInit();
45
	uartInit();
40
	uartSetBaudRate(0,9600);
46
	uartSetBaudRate(0,9600);
Line 44... Line 50...
44
	timerInit();
50
	timerInit();
45
	// turn on and initialize A/D converter
51
	// turn on and initialize A/D converter
46
	a2dInit();	
52
	a2dInit();	
47
	// configure a2d port (PORTA) as input
53
	// configure a2d port (PORTA) as input
48
	// so we can receive analog signals
54
	// so we can receive analog signals
49
	DDRA = 0x00;
55
	DDRF = 0x00;
50
	// make sure pull-up resistors are turned off
56
	// make sure pull-up resistors are turned off
51
	PORTA = 0x00;
57
	PORTF = 0x00;
52
 
58
 
53
	// set the a2d prescaler (clock division ratio)
59
	// set the a2d prescaler (clock division ratio)
54
	// - a lower prescale setting will make the a2d converter go faster
60
	// - a lower prescale setting will make the a2d converter go faster
55
	// - a higher setting will make it go slower but the measurements
61
	// - a higher setting will make it go slower but the measurements
56
	//   will be more accurate
62
	//   will be more accurate
Line 63... Line 69...
63
	a2dSetReference(ADC_REFERENCE_AREF);
69
	a2dSetReference(ADC_REFERENCE_AREF);
64
 
70
 
65
	// use a2dConvert8bit(channel#) to get an 8bit a2d reading
71
	// use a2dConvert8bit(channel#) to get an 8bit a2d reading
66
	// use a2dConvert10bit(channel#) to get a 10bit a2d reading
72
	// use a2dConvert10bit(channel#) to get a 10bit a2d reading
67
 
73
 
-
 
74
	rprintf("inklinometr 2009\r\n");
-
 
75
 
68
	while(1)
76
	while(1)
69
	{
77
	{
70
		fi=0;
78
		fi=0;
71
		err=0;
79
		err=0;
72
		for(i=0; i<BUFLEN; i++)
80
		for(i=0; i<BUFLEN; i++)
Line 74... Line 82...
74
			x = a2dConvert10bit(ADC_CH_ADC0);
82
			x = a2dConvert10bit(ADC_CH_ADC0);
75
			y = a2dConvert10bit(ADC_CH_ADC1);
83
			y = a2dConvert10bit(ADC_CH_ADC1);
76
			fibuf[i] = atan2((double)x-511,(double)y-511);		// record computed angles to buffer for post processing
84
			fibuf[i] = atan2((double)x-511,(double)y-511);		// record computed angles to buffer for post processing
77
		}
85
		}
78
		for(i=0; i<BUFLEN; i++) fi += fibuf[i];		// sum recorded angles
86
		for(i=0; i<BUFLEN; i++) fi += fibuf[i];		// sum recorded angles
-
 
87
		fi = (fi/BUFLEN)+PI;		// average recorded angles and expand product to whole circle
79
 
88
 
80
		fi = ((fi/BUFLEN)+PI) * 180.0 / PI;		// average recorded angles and convert product to degrees
89
/*for(i=0; i<BUFLEN; i++)
81
 
90
{
82
		for(i=0; i<BUFLEN; i++) err += (fibuf[i]-fi)*(fibuf[i]-fi);		// sum cubic errors
-
 
83
		err = sqrt(err/(BUFLEN-1))/sqrt(BUFLEN);	// compute average cubic error
-
 
84
		erra = floor(err);
91
	fibuf[i]=i;
85
		errb = floor((err - erra)*1000);
-
 
-
 
92
}*/
86
		
93
		
87
		fia = floor(fi);
94
		err=quadraticerror(fi,fibuf,BUFLEN);
88
		fib = floor((fi - fia)*1000);
95
		radtodeg(fi,&fi_deg,&fi_min,&fi_sec);
89
 
-
 
-
 
96
		radtodeg(err,&err_deg,&err_min,&err_sec);
90
		
97
		
91
		rprintf("fi:%d.%d  +- %d.%d \r\n", fia, fib, erra, errb);
98
		rprintf("fi:%d.%d.%d  +- %d.%d.%d \r\n", fi_deg, fi_min, fi_sec, err_deg, err_min, err_sec);
-
 
99
		delay_ms(20);
92
	}
100
	}
93
	return 0;
101
	return 0;
94
}
102
}