Complete Introduction to AVR Microcontroller Programming: From Setup to First Program
Atmel AVR 8-bit and 32-bit microcontrollers deliver a unique combination of performance, power efficiency, and design …
By Prabeesh Keezhathra
- 3 minutes read - 530 wordsThe time constant(sec) of an RC circuit is equal to the product of the resistance and the capacitance of the circuit.
It is the time required to charge the capacitor through the resistor to 63. 2% of full charge,or to discharge it to 36.8% of its initial voltage.
The voltage of the RC circuit is measured using adc of the ATmega8, input voltage for RC circuit is given from PB0. The timer is started at the time of the PB0 making 1 .
The adc of ATmega8(ADCH) is 8 bit long so corresponding to 5V get 255 in ADCH. The TCNT1 value is taken to a variable when the output voltage of the RC circuit become 63.2% of the input voltage.That is 3.16 v corresponding to these voltage ADCH show 161(appr).
Using an LCD can show the TCNT1 value. TCNT1 is 16 bit long.Here ATmega8 running in 8MHz clock,timer prescaled by 1024.
So if you get the real time multiply the TCNT1 value to (1024/8000000).
Some test examples:
R=1kΩ C=100µF the calculated RC constant is 0.1S.
The value of TCNT1 is 846.Which is equal to 0.108288s.
R=2kΩ C=100µF the calculated RC constant is 0.2S.
The value of TCNT1 is 1864.Which is equal to 0.238592s.
1
2#include<avr/io.h>
3#define F_CPU 8000000UL
4#include <util/delay.h>
5
6#define RS 6 //PD6
7#define EN 7 //PD7
8#define databits PORTC //PC0 to PC3
9#define row1 cmd(0x80)
10#define row2 cmd(0xc0)
11
12void adc_init()
13{
14 //select AVCC reference voltage , left alignment of data and ADC4
15 ADMUX=((1<<REFS0)|(1<<ADLAR)|(1<<MUX2));
16
17 //enable ADC, set prescaler to divide the clock by 64 and auto triggering mode
18 ADCSRA=((1<<ADEN)|(1<<ADFR)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0));
19
20}
21
22void conversion()
23{
24 //start conversion
25 ADCSRA|=(1<<ADSC);
26}
27
28void port_init()
29{
30 DDRC = 0xcf;
31 DDRD = (1 << RS)|(1 << EN);
32}
33
34void LCD_STROBE(void)
35{
36 PORTD |= (1 << EN);
37 _delay_us(1);
38 PORTD &= ~(1 << EN);
39}
40
41void data(unsigned char c)
42{
43 PORTD |= (1 << RS);
44 _delay_us(50);
45 databits = (c >> 4);
46 LCD_STROBE();
47 databits = (c);
48 LCD_STROBE();
49}
50
51void cmd(unsigned char c)
52{
53 PORTD &= ~(1 << RS);
54 _delay_us(50);
55 databits = (c >> 4);
56 LCD_STROBE();
57 databits = (c);
58 LCD_STROBE();
59}
60
61void clear(void)
62{
63 cmd(0x01);
64 _delay_ms(5);
65}
66
67void lcd_init()
68{
69 _delay_ms(15);
70 cmd(0x30);
71 _delay_ms(1);
72 cmd(0x30);
73 _delay_us(100);
74 cmd(0x30);
75 cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
76 cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
77 cmd(0x0c); // Make cursorinvisible
78 clear(); // Clear screen
79 cmd(0x6); // Set entry Mode(auto increment of cursor)
80}
81
82void print(char *p)
83{
84 while(*p) data(*p++);
85}
86
87void main()
88{
89 char a[5],b[5];
90 int c,d;
91 DDRB=0x01;
92 _delay_ms(50);
93 TCCR1B|=(1<<CS10)|(1<<CS12);//prescale 1024
94 port_init();
95 adc_init();
96 lcd_init();
97 PORTB=0x01;//applying vcc to RC circuit
98 TCNT1=0x00;//start the timer
99
100 while(1)
101 {
102 conversion();
103
104 while(!( ADIF));
105
106 if(ADCH==161)//63% of the input voltage
107 {
108 c=TCNT1;
109 d=ADCH;
110 itoa(c,a,10);//integer to ASCII
111 itoa(d,b,10);//integer to ASCII
112 row1;
113 print(a);//showing timer value
114 row2;
115 print(b);//showing adc value
116 }
117 }
118}