Subversion Repositories svnkaklik

Rev

Rev 507 | Rev 509 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
507 kaklik 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 ---------------------------------------------------------
17
#include <avr/io.h>		// include I/O definitions (port names, pin names, etc)
18
#include <avr/interrupt.h>	// include interrupt support
19
#include <math.h>
20
 
21
#include "global.h"		// include our global settings
508 kaklik 22
#include "uart2.h"		// include uart function library
507 kaklik 23
#include "rprintf.h"	// include printf function library
24
#include "timer.h"		// include timer function library (timing, PWM, etc)
25
#include "a2d.h"		// include A/D converter function library
26
 
27
//----- Begin Code ------------------------------------------------------------
508 kaklik 28
#define BUFLEN 64
507 kaklik 29
 
30
int main(void)
31
{
508 kaklik 32
	u16 i,x,y;
33
	double fi, err, fibuf[BUFLEN];
34
	s16 fia, erra;
35
	u16 fib, errb;
507 kaklik 36
 
37
	// initialize our libraries
38
	// initialize the UART (serial port)
39
	uartInit();
508 kaklik 40
	uartSetBaudRate(0,9600);
507 kaklik 41
	// make all rprintf statements use uart for output
508 kaklik 42
	rprintfInit(uart0SendByte);
507 kaklik 43
	// initialize the timer system
44
	timerInit();
45
	// turn on and initialize A/D converter
46
	a2dInit();	
47
	// configure a2d port (PORTA) as input
48
	// so we can receive analog signals
508 kaklik 49
	DDRA = 0x00;
507 kaklik 50
	// make sure pull-up resistors are turned off
508 kaklik 51
	PORTA = 0x00;
507 kaklik 52
 
53
	// set the a2d prescaler (clock division ratio)
54
	// - a lower prescale setting will make the a2d converter go faster
55
	// - a higher setting will make it go slower but the measurements
56
	//   will be more accurate
57
	// - other allowed prescale values can be found in a2d.h
58
	a2dSetPrescaler(ADC_PRESCALE_DIV128);
59
 
60
	// set the a2d reference
61
	// - the reference is the voltage against which a2d measurements are made
62
	// - other allowed reference values can be found in a2d.h
63
	a2dSetReference(ADC_REFERENCE_AREF);
64
 
65
	// use a2dConvert8bit(channel#) to get an 8bit a2d reading
66
	// use a2dConvert10bit(channel#) to get a 10bit a2d reading
67
 
68
	while(1)
69
	{
508 kaklik 70
		fi=0;
71
		err=0;
507 kaklik 72
		for(i=0; i<BUFLEN; i++)
508 kaklik 73
		{ 
74
			x = a2dConvert10bit(ADC_CH_ADC0);
75
			y = a2dConvert10bit(ADC_CH_ADC1);
76
			fibuf[i] = atan2((double)x-511,(double)y-511);		// record computed angles to buffer for post processing
507 kaklik 77
		}
508 kaklik 78
		for(i=0; i<BUFLEN; i++) fi += fibuf[i];		// sum recorded angles
507 kaklik 79
 
508 kaklik 80
		fi = ((fi/BUFLEN)+PI) * 180.0 / PI;		// average recorded angles and convert product to degrees
81
 
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);
85
		errb = floor((err - erra)*1000);
86
 
507 kaklik 87
		fia = floor(fi);
508 kaklik 88
		fib = floor((fi - fia)*1000);
89
 
90
 
91
		rprintf("fi:%d.%d  +- %d.%d \r\n", fia, fib, erra, errb);
507 kaklik 92
	}
93
	return 0;
94
}