1 |
|
2 | /**
|
3 | * ADCdisplay displays the value of the adc :-)
|
4 | * version 2 - uses ISR
|
5 | *
|
6 | * Outupt on a 2x16 LCD:
|
7 | * ----------------
|
8 | * ADC-Value
|
9 | * -> 223/222/224
|
10 | * ----------------
|
11 | * which is -> current / min / max
|
12 | *
|
13 | */
|
14 |
|
15 | #define F_CPU 16000000UL // Clock speed
|
16 |
|
17 | #define LED_RED 0b00000001 // PIN Register for the red led
|
18 | #define LED_GREEN 0b00000010 // PIN Register for the green led
|
19 |
|
20 | #include <stdlib.h>
|
21 | #include <avr/io.h>
|
22 | #include <avr/interrupt.h>
|
23 | #include <util/delay.h>
|
24 | #include "lcd.h"
|
25 |
|
26 | volatile int value = 0; // current value
|
27 | int last_value = 0; // value from the last messurement
|
28 | int biggest = 0; // max value
|
29 | int lowest = 1023; // min value
|
30 |
|
31 | char buf[4]; // a buffer for the value to disply
|
32 |
|
33 | /**
|
34 | * On the dev-board there are two
|
35 | * static led's a red one and a
|
36 | * green one.
|
37 | * This routine flashes one off
|
38 | * the static leds.
|
39 | * ---------------------------- //
|
40 | *
|
41 | */
|
42 | void blink_led(int led){ // toggle a PIN on PortB for any blink call
|
43 | PORTB |= led; // LED on
|
44 | _delay_ms(150);
|
45 | PORTB &= ~led; // toggle LED off
|
46 | _delay_ms(300);
|
47 | return;
|
48 | }
|
49 |
|
50 | /**
|
51 | * Interupt calld by change in
|
52 | * ADC register
|
53 | * ---------------------------- //
|
54 | *
|
55 | */
|
56 | ISR(ADC_vect)
|
57 | {
|
58 | blink_led(LED_GREEN); // tell that the conversation is done
|
59 | value = ADCW;
|
60 | }
|
61 |
|
62 | /**
|
63 | * Enable and configure
|
64 | * the analog-digital converter
|
65 | * ---------------------------- //
|
66 | *
|
67 | */
|
68 | void initADC(channel){
|
69 |
|
70 | TCNT0=0x00; // set timer.0 to initial 0
|
71 | TCCR0B = (1<<CS02); // start timer0 with /256 prescaler
|
72 |
|
73 | ADCSRA = (1<<ADEN) // enable ADC
|
74 | | (1<<ADIE) // enable interupt
|
75 | | (1<<ADATE)
|
76 | | (1<<ADPS2) // set 128 to the prescaller
|
77 | | (1<<ADPS1)
|
78 | | (1<<ADPS0);
|
79 |
|
80 | ADCSRB = ((1<<ADTS2) // set timer.0 overflow
|
81 | | (0<<ADTS1)
|
82 | | (0<<ADTS0));
|
83 |
|
84 | ADMUX = channel | (1<<REFS0); // set the channel
|
85 |
|
86 | }
|
87 |
|
88 | /**
|
89 | * Main routine
|
90 | * ---------------------------- //
|
91 | *
|
92 | */
|
93 | int main(void){
|
94 | DDRB = 0b00000011; // set up pin direction PortB as Output
|
95 | blink_led(LED_RED);
|
96 |
|
97 | lcd_init(LCD_DISP_ON); // initialize display, cursor off
|
98 |
|
99 | lcd_puts("*** WELCOME ***\n"); // welcome message
|
100 | lcd_puts(" - ADCV V.2 - ");
|
101 | _delay_ms(2000); // wait for 2sec
|
102 |
|
103 | lcd_clrscr(); // clear display and home cursor
|
104 | lcd_puts("ADC-Value\n"); // show the mode
|
105 | lcd_puts("->"); // show the indicator
|
106 |
|
107 |
|
108 | initADC(5);
|
109 |
|
110 | sei();
|
111 | while(1) { // Mainloop
|
112 | blink_led(LED_RED);
|
113 |
|
114 | if(value != last_value){ // if something has changes
|
115 |
|
116 | itoa(value, buf, 10); // convert the int to string buffer
|
117 |
|
118 | lcd_home(); // display cleanup
|
119 | lcd_gotoxy(2,1);
|
120 | lcd_puts(" ");
|
121 | lcd_home();
|
122 | lcd_gotoxy(2,1);
|
123 | lcd_puts(buf); // show current value
|
124 |
|
125 | if(value > biggest){ // get min and max
|
126 | biggest = value;
|
127 | }
|
128 | if(value < lowest){
|
129 | lowest = value;
|
130 | }
|
131 |
|
132 | lcd_puts("/");
|
133 | itoa(lowest, buf, 10);
|
134 | lcd_puts(buf); // show min
|
135 |
|
136 | lcd_puts("/");
|
137 | itoa(biggest, buf, 10);
|
138 | lcd_puts(buf); // show max
|
139 |
|
140 | last_value = value; // remember the last value
|
141 |
|
142 | }
|
143 | }
|
144 | }
|