E kiếm được đoạn code sau dùng để xuất tín hiệu AD ra LCD:
biến trở -> bộ ADC -> AVR -> xuất điện áp ra LCD
19. Chip type : ATmega16
20. Program type : Application
21. Clock frequency : 8.000000 MHz
22. Memory model : Small
23. External SRAM size : 0
24. Data Stack size : 256
25. ************************************************** ***/
26.
27. #include <mega16.h>
28.
29. // Alphanumeric LCD Module functions
30. #asm
31. .equ __lcd_port=0x15 ;PORTC
32. #endasm
33. #include <lcd.h>
34.
35. #define FIRST_ADC_INPUT 0
36. #define LAST_ADC_INPUT 7
37. unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
38. #define ADC_VREF_TYPE 0x20
39.
40. // ADC interrupt service routine
41. // with auto input scanning
42. interrupt [ADC_INT] void adc_isr(void)
43. {
44. register static unsigned char input_index=0;
45. // Read the 8 most significant bits
46. // of the AD conversion result
47. adc_data[input_index]=ADCH;
48. // Select next ADC input
49. if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
50. input_index=0;
51. ADMUX=(FIRST_ADC_INPUT|ADC_VREF_TYPE)+input_index;
52. // Start the AD conversion
53. ADCSRA|=0x40;
54. }
55.
56.
57. // Declare your global variables here
58. void lcd_putnum(unsigned int d)
59. {
60. unsigned char donvi, chuc, tram;
61. donvi=d%10;
62. d=d/10;
63. chuc=d%10;
64. d=d/10;
65. tram=d%10;
66. d=d/10;
67. 68. if(tram>0)
69. {
70. lcd_putchar(48+tram);
71. lcd_putchar(48+chuc);
72. lcd_putchar(48+donvi);
73.
74. }
75. else if(chuc>0)
76. {
77. lcd_putchar(48+chuc);
78. lcd_putchar(48+donvi);
79. lcd_putsf(" ");
80.
81. }
82. else {
83. lcd_putchar(48+donvi);
84. lcd_putsf(" ");
85. lcd_putsf(" ");
86.
87. }
88. }
89.
90. void main(void)
91. {
92. // Declare your local variables here
93.
94. // Input/Output Ports initialization
95. // Port A initialization
96. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
97. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
98. PORTA=0x00;
99. DDRA=0x00;
100.
101. // Port B initialization
102. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
103. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
104. PORTB=0x00;
105. DDRB=0x00;
106.
107. // Port C initialization
108. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
109. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
110. PORTC=0x00;
111. DDRC=0x00;
112.
113. // Port D initialization
114. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
115. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
116. PORTD=0x00;
117. DDRD=0x00;
118.
119. // Timer/Counter 0 initialization 120. // Clock source: System Clock
121. // Clock value: Timer 0 Stopped
122. // Mode: Normal top=FFh
123. // OC0 output: Disconnected
124. TCCR0=0x00;
125. TCNT0=0x00;
126. OCR0=0x00;
127.
128. // Timer/Counter 1 initialization
129. // Clock source: System Clock
130. // Clock value: Timer 1 Stopped
131. // Mode: Normal top=FFFFh
132. // OC1A output: Discon.
133. // OC1B output: Discon.
134. // Noise Canceler: Off
135. // Input Capture on Falling Edge
136. // Timer 1 Overflow Interrupt: Off
137. // Input Capture Interrupt: Off
138. // Compare A Match Interrupt: Off
139. // Compare B Match Interrupt: Off
140. TCCR1A=0x00;
141. TCCR1B=0x00;
142. TCNT1H=0x00;
143. TCNT1L=0x00;
144. ICR1H=0x00;
145. ICR1L=0x00;
146. OCR1AH=0x00;
147. OCR1AL=0x00;
148. OCR1BH=0x00;
149. OCR1BL=0x00;
150.
151. // Timer/Counter 2 initialization
152. // Clock source: System Clock
153. // Clock value: Timer 2 Stopped
154. // Mode: Normal top=FFh
155. // OC2 output: Disconnected
156. ASSR=0x00;
157. TCCR2=0x00;
158. TCNT2=0x00;
159. OCR2=0x00;
160.
161. // External Interrupt(s) initialization
162. // INT0: Off
163. // INT1: Off
164. // INT2: Off
165. MCUCR=0x00;
166. MCUCSR=0x00;
167.
168. // Timer(s)/Counter(s) Interrupt(s) initialization
169. TIMSK=0x00;
170.
171. // Analog Comparator initialization
172. // Analog Comparator: Off
173. // Analog Comparator Input Capture by Timer/Counter 1: Off
174. ACSR=0x80;
175. SFIOR=0x00;
176.
177. // ADC initialization
178. // ADC Clock frequency: 1000.000 kHz
179. // ADC Voltage Reference: AREF pin 180. // ADC Auto Trigger Source: None
181. // Only the 8 most significant bits of
182. // the AD conversion result are used
183. ADMUX=FIRST_ADC_INPUT|ADC_VREF_TYPE;
184. ADCSRA=0xCB;
185.
186. // LCD module initialization
187. lcd_init(16);
188.
189. // Global enable interrupts
190. #asm("sei")
191. lcd_gotoxy(0,0);
192. lcd_putsf("ADC");
193.
194. while (1)
195. {
196. // Place your code here
197. lcd_gotoxy(4,1);
198. lcd_putnum(adc_data[0]);
199. };
200. }
Không hiểu giá trị "gt" là gì, mà c/tr cứ đòi phải define, tại sao trong hàm IF tác giả lại dùng dấu ";", mọi người bỉết thì giúp dùm.
biến trở -> bộ ADC -> AVR -> xuất điện áp ra LCD
19. Chip type : ATmega16
20. Program type : Application
21. Clock frequency : 8.000000 MHz
22. Memory model : Small
23. External SRAM size : 0
24. Data Stack size : 256
25. ************************************************** ***/
26.
27. #include <mega16.h>
28.
29. // Alphanumeric LCD Module functions
30. #asm
31. .equ __lcd_port=0x15 ;PORTC
32. #endasm
33. #include <lcd.h>
34.
35. #define FIRST_ADC_INPUT 0
36. #define LAST_ADC_INPUT 7
37. unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
38. #define ADC_VREF_TYPE 0x20
39.
40. // ADC interrupt service routine
41. // with auto input scanning
42. interrupt [ADC_INT] void adc_isr(void)
43. {
44. register static unsigned char input_index=0;
45. // Read the 8 most significant bits
46. // of the AD conversion result
47. adc_data[input_index]=ADCH;
48. // Select next ADC input
49. if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
50. input_index=0;
51. ADMUX=(FIRST_ADC_INPUT|ADC_VREF_TYPE)+input_index;
52. // Start the AD conversion
53. ADCSRA|=0x40;
54. }
55.
56.
57. // Declare your global variables here
58. void lcd_putnum(unsigned int d)
59. {
60. unsigned char donvi, chuc, tram;
61. donvi=d%10;
62. d=d/10;
63. chuc=d%10;
64. d=d/10;
65. tram=d%10;
66. d=d/10;
67. 68. if(tram>0)
69. {
70. lcd_putchar(48+tram);
71. lcd_putchar(48+chuc);
72. lcd_putchar(48+donvi);
73.
74. }
75. else if(chuc>0)
76. {
77. lcd_putchar(48+chuc);
78. lcd_putchar(48+donvi);
79. lcd_putsf(" ");
80.
81. }
82. else {
83. lcd_putchar(48+donvi);
84. lcd_putsf(" ");
85. lcd_putsf(" ");
86.
87. }
88. }
89.
90. void main(void)
91. {
92. // Declare your local variables here
93.
94. // Input/Output Ports initialization
95. // Port A initialization
96. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
97. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
98. PORTA=0x00;
99. DDRA=0x00;
100.
101. // Port B initialization
102. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
103. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
104. PORTB=0x00;
105. DDRB=0x00;
106.
107. // Port C initialization
108. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
109. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
110. PORTC=0x00;
111. DDRC=0x00;
112.
113. // Port D initialization
114. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In
Func0=In
115. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T
State0=T
116. PORTD=0x00;
117. DDRD=0x00;
118.
119. // Timer/Counter 0 initialization 120. // Clock source: System Clock
121. // Clock value: Timer 0 Stopped
122. // Mode: Normal top=FFh
123. // OC0 output: Disconnected
124. TCCR0=0x00;
125. TCNT0=0x00;
126. OCR0=0x00;
127.
128. // Timer/Counter 1 initialization
129. // Clock source: System Clock
130. // Clock value: Timer 1 Stopped
131. // Mode: Normal top=FFFFh
132. // OC1A output: Discon.
133. // OC1B output: Discon.
134. // Noise Canceler: Off
135. // Input Capture on Falling Edge
136. // Timer 1 Overflow Interrupt: Off
137. // Input Capture Interrupt: Off
138. // Compare A Match Interrupt: Off
139. // Compare B Match Interrupt: Off
140. TCCR1A=0x00;
141. TCCR1B=0x00;
142. TCNT1H=0x00;
143. TCNT1L=0x00;
144. ICR1H=0x00;
145. ICR1L=0x00;
146. OCR1AH=0x00;
147. OCR1AL=0x00;
148. OCR1BH=0x00;
149. OCR1BL=0x00;
150.
151. // Timer/Counter 2 initialization
152. // Clock source: System Clock
153. // Clock value: Timer 2 Stopped
154. // Mode: Normal top=FFh
155. // OC2 output: Disconnected
156. ASSR=0x00;
157. TCCR2=0x00;
158. TCNT2=0x00;
159. OCR2=0x00;
160.
161. // External Interrupt(s) initialization
162. // INT0: Off
163. // INT1: Off
164. // INT2: Off
165. MCUCR=0x00;
166. MCUCSR=0x00;
167.
168. // Timer(s)/Counter(s) Interrupt(s) initialization
169. TIMSK=0x00;
170.
171. // Analog Comparator initialization
172. // Analog Comparator: Off
173. // Analog Comparator Input Capture by Timer/Counter 1: Off
174. ACSR=0x80;
175. SFIOR=0x00;
176.
177. // ADC initialization
178. // ADC Clock frequency: 1000.000 kHz
179. // ADC Voltage Reference: AREF pin 180. // ADC Auto Trigger Source: None
181. // Only the 8 most significant bits of
182. // the AD conversion result are used
183. ADMUX=FIRST_ADC_INPUT|ADC_VREF_TYPE;
184. ADCSRA=0xCB;
185.
186. // LCD module initialization
187. lcd_init(16);
188.
189. // Global enable interrupts
190. #asm("sei")
191. lcd_gotoxy(0,0);
192. lcd_putsf("ADC");
193.
194. while (1)
195. {
196. // Place your code here
197. lcd_gotoxy(4,1);
198. lcd_putnum(adc_data[0]);
199. };
200. }
Không hiểu giá trị "gt" là gì, mà c/tr cứ đòi phải define, tại sao trong hàm IF tác giả lại dùng dấu ";", mọi người bỉết thì giúp dùm.
Comment