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
|
|
|
22 |
#include "uart.h" // include uart function library
|
|
|
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 ------------------------------------------------------------
|
|
|
28 |
#define BUFLEN 32
|
|
|
29 |
|
|
|
30 |
int main(void)
|
|
|
31 |
{
|
|
|
32 |
u08 i=0;
|
|
|
33 |
s16 x=0,y=0;
|
|
|
34 |
double fi;
|
|
|
35 |
s16 fia;
|
|
|
36 |
u16 fib;
|
|
|
37 |
|
|
|
38 |
// initialize our libraries
|
|
|
39 |
// initialize the UART (serial port)
|
|
|
40 |
uartInit();
|
|
|
41 |
uartSetBaudRate(9600);
|
|
|
42 |
// make all rprintf statements use uart for output
|
|
|
43 |
rprintfInit(uartSendByte);
|
|
|
44 |
// initialize the timer system
|
|
|
45 |
timerInit();
|
|
|
46 |
// turn on and initialize A/D converter
|
|
|
47 |
a2dInit();
|
|
|
48 |
// configure a2d port (PORTA) as input
|
|
|
49 |
// so we can receive analog signals
|
|
|
50 |
DDRC = 0x00;
|
|
|
51 |
// make sure pull-up resistors are turned off
|
|
|
52 |
PORTC = 0x00;
|
|
|
53 |
|
|
|
54 |
// set the a2d prescaler (clock division ratio)
|
|
|
55 |
// - a lower prescale setting will make the a2d converter go faster
|
|
|
56 |
// - a higher setting will make it go slower but the measurements
|
|
|
57 |
// will be more accurate
|
|
|
58 |
// - other allowed prescale values can be found in a2d.h
|
|
|
59 |
a2dSetPrescaler(ADC_PRESCALE_DIV128);
|
|
|
60 |
|
|
|
61 |
// set the a2d reference
|
|
|
62 |
// - the reference is the voltage against which a2d measurements are made
|
|
|
63 |
// - other allowed reference values can be found in a2d.h
|
|
|
64 |
a2dSetReference(ADC_REFERENCE_AREF);
|
|
|
65 |
|
|
|
66 |
// use a2dConvert8bit(channel#) to get an 8bit a2d reading
|
|
|
67 |
// use a2dConvert10bit(channel#) to get a 10bit a2d reading
|
|
|
68 |
|
|
|
69 |
while(1)
|
|
|
70 |
{
|
|
|
71 |
for(i=0; i<BUFLEN; i++)
|
|
|
72 |
{
|
|
|
73 |
x += a2dConvert10bit(0);
|
|
|
74 |
y += a2dConvert10bit(1);
|
|
|
75 |
}
|
|
|
76 |
x = x/BUFLEN - 512;
|
|
|
77 |
y = y/BUFLEN - 512;
|
|
|
78 |
|
|
|
79 |
fi = atan2(y,x) * 180.0 / PI;
|
|
|
80 |
fia = floor(fi);
|
|
|
81 |
fib = floor((fi - fia));
|
|
|
82 |
rprintf("X:%d Y:%d fi:%d.%d \r\n", x, y, fia, fib);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
return 0;
|
|
|
86 |
}
|