Rev 1146 Rev 1858
1 //***************************************************************************** 1 //*****************************************************************************
2 // File Name : a2dtest.c 2 // File Name : a2dtest.c
3 // 3 //
4 // Title : example usage of some avr library functions 4 // Title : example usage of some avr library functions
5 // Revision : 1.0 5 // Revision : 1.0
6 // Notes : 6 // Notes :
7 // Target MCU : Atmel AVR series 7 // Target MCU : Atmel AVR series
8 // Editor Tabs : 4 8 // Editor Tabs : 4
9 // 9 //
10 // Revision History: 10 // Revision History:
11 // When Who Description of change 11 // When Who Description of change
12 // ----------- ----------- ----------------------- 12 // ----------- ----------- -----------------------
13 // 20-Oct-2002 pstang Created the program 13 // 20-Oct-2002 pstang Created the program
14 //***************************************************************************** 14 //*****************************************************************************
15 15
16 //----- Include Files --------------------------------------------------------- 16 //----- Include Files ---------------------------------------------------------
17 #include <avr/io.h> // include I/O definitions (port names, pin names, etc) 17 #include <avr/io.h> // include I/O definitions (port names, pin names, etc)
18 #include <avr/interrupt.h> // include interrupt support 18 #include <avr/interrupt.h> // include interrupt support
19 #include <string.h> 19 #include <string.h>
20   20  
21 #include "global.h" // include our global settings 21 #include "global.h" // include our global settings
22 #include "uart.h" // include uart function library 22 #include "uart.h" // include uart function library
23 #include "rprintf.h" // include printf function library 23 #include "rprintf.h" // include printf function library
24 #include "timer.h" // include timer function library (timing, PWM, etc) 24 #include "timer.h" // include timer function library (timing, PWM, etc)
25 #include "a2d.h" // include A/D converter function library 25 #include "a2d.h" // include A/D converter function library
26 #include "vt100.h" // include VT100 terminal support 26 #include "vt100.h" // include VT100 terminal support
27   27  
28 //----- Begin Code ------------------------------------------------------------ 28 //----- Begin Code ------------------------------------------------------------
29 int main(void) 29 int main(void)
30 { 30 {
31 u16 a=0; 31 u16 a=0;
32 u08 i=0; 32 u08 i=0;
33   33  
34 // initialize our libraries 34 // initialize our libraries
35 // initialize the UART (serial port) 35 // initialize the UART (serial port)
36 uartInit(); 36 uartInit();
37 // make all rprintf statements use uart for output 37 // make all rprintf statements use uart for output
38 rprintfInit(uartSendByte); 38 rprintfInit(uartSendByte);
39 // initialize the timer system 39 // initialize the timer system
40 timerInit(); 40 timerInit();
41 // turn on and initialize A/D converter 41 // turn on and initialize A/D converter
42 a2dInit(); 42 a2dInit();
43   43  
44 // print a little intro message so we know things are working 44 // print a little intro message so we know things are working
45 /* vt100ClearScreen(); 45 /* vt100ClearScreen();
46 vt100SetCursorPos(1,1); 46 vt100SetCursorPos(1,1);
47 rprintf("Welcome to the a2d test!\r\n");*/ 47 rprintf("Welcome to the a2d test!\r\n");*/
48 48
49 // configure a2d port (PORTA) as input 49 // configure a2d port (PORTA) as input
50 // so we can receive analog signals 50 // so we can receive analog signals
51 DDRC = 0x00; 51 DDRC = 0x00;
52 // make sure pull-up resistors are turned off 52 // make sure pull-up resistors are turned off
53 PORTC = 0x00; 53 PORTC = 0x00;
54   54  
55 // set the a2d prescaler (clock division ratio) 55 // set the a2d prescaler (clock division ratio)
56 // - a lower prescale setting will make the a2d converter go faster 56 // - a lower prescale setting will make the a2d converter go faster
57 // - a higher setting will make it go slower but the measurements 57 // - a higher setting will make it go slower but the measurements
58 // will be more accurate 58 // will be more accurate
59 // - other allowed prescale values can be found in a2d.h 59 // - other allowed prescale values can be found in a2d.h
60 a2dSetPrescaler(ADC_PRESCALE_DIV32); 60 a2dSetPrescaler(ADC_PRESCALE_DIV32);
61   61  
62 // set the a2d reference 62 // set the a2d reference
63 // - the reference is the voltage against which a2d measurements are made 63 // - the reference is the voltage against which a2d measurements are made
64 // - other allowed reference values can be found in a2d.h 64 // - other allowed reference values can be found in a2d.h
65 a2dSetReference(ADC_REFERENCE_AVCC); 65 a2dSetReference(ADC_REFERENCE_AVCC);
66   66  
67 // use a2dConvert8bit(channel#) to get an 8bit a2d reading 67 // use a2dConvert8bit(channel#) to get an 8bit a2d reading
68 // use a2dConvert10bit(channel#) to get a 10bit a2d reading 68 // use a2dConvert10bit(channel#) to get a 10bit a2d reading
69   69  
70 while(1) 70 while(1)
71 { 71 {
72 u08 c=0; 72 u08 c=0;
73 u08 n,i; 73 u08 n,i;
74 char radiace[10]; 74 char radiace[10];
75 75
76 char radka[201]; 76 char radka[201];
77   77  
78 for(n=0;n<=100;n++) radka[n]=0; // vynuluj bufferovaci pole 78 for(n=0;n<=100;n++) radka[n]=0; // vynuluj bufferovaci pole
79 for(n=0;n<10;n++) radiace[n]=0; // vynuluj bufferovaci pole 79 for(n=0;n<10;n++) radiace[n]=0; // vynuluj bufferovaci pole
80   80  
81 n=0; 81 n=0;
82 while(1) // pockej na $ kterym zacina NMEA radka 82 while(1) // pockej na $ kterym zacina NMEA radka
83 { 83 {
84 uartReceiveByte(&c); 84 uartReceiveByte(&c);
85 if(c == '$') break; 85 if(c == '$') break;
86 } 86 }
87   87  
88 for(i=0;i<100;i++) // nacti maximalne 100 znaku do bufferu 88 for(i=0;i<100;i++) // nacti maximalne 100 znaku do bufferu
89 { 89 {
90 radka[n]=c; 90 radka[n]=c;
91 if(c == '\n') break; // kdyz narazis na konec radku zastav nacitani 91 if(c == '\n') break; // kdyz narazis na konec radku zastav nacitani
92 uartReceiveByte(&c); 92 uartReceiveByte(&c);
93 n++; 93 n++;
94 } 94 }
95   95  
96 radka[n]=0; // naztav na konec retezce pro zpracovani pomoci strcat 96 radka[n]=0; // naztav na konec retezce pro zpracovani pomoci strcat
97   97  
98   98  
99 itoa(a2dConvert10bit(2),&radiace,10); //a2dConvert8bit(1) 99 itoa(a2dConvert10bit(2),&radiace,10); //a2dConvert8bit(1)
100 if(n != 0) 100 if(n != 0)
101 { 101 {
102 strcat(radka, radiace); 102 strcat(radka, radiace);
103 strcat(radka,"\r"); 103 strcat(radka,"\r");
104 } 104 }
105 n=0; 105 n=0;
106 uartFlushReceiveBuffer(); 106 uartFlushReceiveBuffer();
107   107  
108 while (0!=radka[n]) 108 while (0!=radka[n])
109 { 109 {
110 uartSendByte(radka[n]); 110 uartSendByte(radka[n]);
111 n++; 111 n++;
112 timerPause(35); 112 timerPause(35);
113 } 113 }
114   114  
115 } 115 }
116 116
117 return 0; 117 return 0;
118 } 118 }