Tìm kiếmĐăng ký thành viên  >  Đăng nhập    Liên hệ     
  

Trang chủ



Thảo luận



 SỰ KIỆN ĐẶC BIỆT TẠI VAGAM/ EVENTS & SEMINARS
 Thông tin chung
 Điện – điện tử: căn bản và các chuyên đề
 Cảm biến và cơ cấu chấp hành
 Lập trình vi xử lý, vi điều khiển và điều khiển logic
- Vi điều khiển 89c51
- Vi điều khiển AVR.
- Vi điều khiển PIC.
- Các trao đổi về Vi điều khiển khác
 Giao tiếp máy tính - truyền dữ liệu - xử lý ảnh số
 Phần mềm và ngôn ngữ lập trình trong kỹ thuật
 Điều khiển tự động
 Thiết kế và mô phỏng hệ thống cơ khí
 Các chuyên đề ÔTÔ, XE MÁY - ROBOT - AUTOMATION
 Anh văn kỹ thuật
 Chia sẻ tài liệu cùng các thành viên
 Các chủ đề thảo luận chung
 Mã nguồn mở



Tài liệu tham khảo









http://hocdelam.org

http://labview.hocdelam.org

http://vagam.dieukhien.net/index.php?tpid=5&catid=10&chapid=83

http://vagam.dieukhien.net/index.php?tpid=5&catid=24&chapid=75

http://vagam.dieukhien.net/index.php?tpid=5&catid=5&chapid=33

http://vagam.dieukhien.net/index.php?arid=37

http://vagam.dieukhien.net/discuss.php?thid=148&pagenum=1

http://vagam.dieukhien.net/discuss.php?thid=456

http://vagam.dieukhien.net/discuss.php?thid=497&pagenum=1

http://vagam.dieukhien.net/discuss.php?ftopid=12&fcatid=44

http://www.dieukhien.net

http://dieukhientudong.com

http://suno.vn


http://spkt.net/diendan/

http://sanpham.hocdelam.org

http://robot.kut.ac.kr

http://labview.hocdelam.org/vn/Nghien-cuu/Hoi-thao-khoa-hoc.nso

http://www.phone4vn.com




Bài 5 : Dùng ADC nhận tín hiệu từ biến trở

| 1 | 2 | 3 | > Tiếp >> Cuối 
 giotdang | 15:20:01 09-01-08 | Posts: 191 1
Tóm tắt : Qua bài này các bạn có thể sử dụng được ADC của AVR

Giới thiệu :
Phần cứng các bạn cần 1 con biến trở nối vào portA.0
Mạch ATmega16 như bài đầu .


Các chế độ sẽ chọn :
sử dụng chế độ ngắt ADC, tự động scan giá trị analog của port A




Ví dụ : Dùng PORTA.0 nhận giá trị từ biến trở hiển thị lên LCD theo ADC 8bit (từ 0->5V thành 0 ->255)
Chú ý :
trong chương trình có sử dụng hàm lcd_putnum(value); hiển thị chữ số

c code
  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V1.24.8d Professional
  4. Automatic Program Generator
  5. © Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
  6. http://www.hpinfotech.com
  7.  
  8. Project :
  9. Version :
  10. Date : 1/9/2008
  11. Author : LENGOCTUAN
  12. Company : VAGAM
  13. Comments:
  14.  
  15. Comments:
  16.  
  17.  
  18.  
  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 &gt; (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&gt;0)
  69. {
  70. lcd_putchar(48+tram);
  71. lcd_putchar(48+chuc);
  72. lcd_putchar(48+donvi);
  73.  
  74. }
  75. else if(chuc&gt;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. }
  201.  
  202.  


video :
adc_avr




giotdang1985@yahoo.com

 body | 04:39:18 10-01-08 | Posts: 202
cảm ơn các bài viết của bác, em vẫn theo và thực hành thêm.