AVR AD 采集转换程序,带数字滤波 1. //本例使用内部参考电压,ADc0 输入,采集的信号通过LED 显示。 2. 3. //ICC-AVR application builder : 2006-9-3 11:42:36 4. // Target : M32 5. // Crystal: 7.3728Mhz 6. // 2.56v 片内基准电压,输入a1:即 adc0 7. // 误差为: 连续测量模式。 8. // 精度 8,输出左对齐 ADLAR=1 9. // 注意:参考电压一定要高于被测电压。 10. #include
11. #include 12. //数字滤波 13. #define a 0xF4 14. // a=0.95 15. #define b 0x0D 16. // b=1-a=0.05 17. const led_table[16]={0xa0,0xfc,0xc1,0xd0,0x9c,0x92,0x82,0xf8,0x80,0x90,0x88,0x86,0xc7,0xc4,0x83,0x8b};// 0~~f 18. typedef unsigned char uint8; 19. uint8 value,value1=0; 20. int x; //最终的值 10 位 21. volatile uint8 i,j=0; 22. 23. //delay 24. /***********************************/ 25. void Delay100us(uint8 n) 26. { 27. uint8 i; 28. for(i=36;n!=0;n--) 29. while(--i); 30. } 31. 32. void Delay1s(uint8 n) 33. { 34. n=n*10; 35. for (;n!=0;n--){ 36. Delay100us(200); 37. } 38. } 39. /******************************/ 40. 41. void port_init(void) 42. { 43. PORTA = 0x00; //做adc 输入时不可上拉。 44. DDRA = 0x00; 45. PORTB = 0xFF; 46. DDRB = 0xFF; 47. PORTC = 0xFF; 48. DDRC = 0x00; 49. PORTD = 0xFF; 50. DDRD = 0x00; 51. } 52. 53. //ADC initialize 54. // Conversion time: 225uS 55. void adc_init(void) 56. { 57. ADCSR = 0x00; //disable adc 58. ADMUX = 0xC0|(1<