6419|4

2056

帖子

0

TA的资源

五彩晶圆(初级)

楼主
 

msp430LAUNCHPAD的5种ADC滤波算法验证--有PCB哦!! [复制链接]

国庆放假10天,除了玩也做了一点东西!
很久以前我们论坛的网友“常见泽”分享430的5种ADC滤波算法,我闲来无事就为MSP430LAUNCHPAD做了一个扩展板!









下面这个是PCB文件和代码:
ADC滤波测试-1.zip (7.54 MB, 下载次数: 1077)
ADC滤波测试-1.zip (7.54 MB, 下载次数: 1077)
  1. /*
  2. 算术平均滤波法
  3.   A、方法:
  4.     连续取N个采样值进行算术平均运算
  5.     N值较大时:信号平滑度较高,但灵敏度较低
  6.     N值较小时:信号平滑度较低,但灵敏度较高
  7.     N值的选取:一般流量,N=12;压力:N=4
  8.   B、优点:
  9.     适用于对一般具有随机干扰的信号进行滤波
  10.     这样信号的特点是有一个平均值,信号在某一数值范围附近上下波动
  11.   C、缺点:
  12.     对于测量速度较慢或要求数据计算速度较快的实时控制不适用
  13.     比较浪费RAM
  14. */
  15. #include
  16. #define uchar unsigned char
  17. #define uint unsigned int

  18. unsigned char i,j;
  19. long result,resultsum,resultq = 0;
  20. uchar ge,shi,bai,qian;

  21. #define SER ( 1 << 1 ) //也是所谓的DS
  22. #define SRCLK ( 1 << 0 ) //也是所谓的SHCP
  23. #define RCLK ( 1 << 2 ) //也是所谓的STCP

  24. void delay_1ms(void)
  25. {
  26. unsigned int i;
  27. for (i=0;i<1000;i++);
  28. }

  29. void delay_nms(unsigned int n)
  30. {
  31. unsigned int i=0;
  32. for (i=0;i
  33. delay_1ms();
  34. }
  35. uchar table[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 };

  36. void Write595(uchar data)
  37. {

  38. P2OUT &= ~RCLK;
  39. for( i=0;i<8;i++ )
  40. {
  41. if(data&0x80)
  42. {
  43. P2OUT |= SER;
  44. }
  45. else
  46. {
  47. P2OUT &= ~SER;
  48. }
  49. P2OUT &= ~SRCLK;
  50. P2OUT |= SRCLK;
  51. data <<= 1;
  52. }
  53. P2OUT |= RCLK;
  54. }

  55. void ADC_init(void)
  56. {
  57. P1SEL |= 0x01;
  58. ADC10AE0 |= 0x01; // P1.0 ADC option select
  59. ADC10CTL1 = INCH_0+SHS_1;
  60. }
  61. void main(void)
  62. {
  63. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  64. if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
  65. {
  66. while(1); // If calibration constants erased
  67. // do not load, trap CPU!
  68. }
  69. //8Mhz
  70. BCSCTL1 = CALBC1_8MHZ; // Set range
  71. DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */

  72. P1DIR |= (BIT2|BIT3|BIT4|BIT5);
  73. P2DIR |= 0xff;
  74. TACCTL0 = CCIE; // Enable interrupt
  75. TACCR0 = 32-1; // PWM Period
  76. TACCTL1 = OUTMOD_3; // TACCR1 set/reset
  77. TACCR1 = 2; // TACCR1 PWM Duty Cycle
  78. TACTL = TASSEL_1 + MC_1; // ACLK, up mode

  79. ADC_init();
  80. _EINT();
  81. while(1)
  82. {
  83. result = resultq/30;
  84. result = result*1500/1024;
  85. qian = result/1000;
  86. bai = result/100%10;
  87. shi = result/10%10;
  88. ge = result%10;

  89. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  90. Write595(table[ge]);
  91. P1OUT &= ~( 1 << 2 );
  92. delay_nms(1);

  93. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  94. Write595(table[shi]);
  95. P1OUT &= ~( 1 << 3 );
  96. delay_nms(1);

  97. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  98. Write595(table[bai]);
  99. P1OUT &= ~( 1 << 4 );
  100. delay_nms(1);

  101. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  102. Write595(table[qian]);
  103. P1OUT &= ~( 1 << 5 );
  104. delay_nms(1);
  105. }
  106. }
  107. /********************************************************************/
  108. // ADC10 interrupt service routine
  109. #pragma vector=ADC10_VECTOR
  110. __interrupt void ADC10_ISR(void)
  111. {

  112. ADC10CTL0 &= ~ENC; // ADC10 disabled
  113. ADC10CTL0 = 0; // ADC10, Vref disabled completely
  114. if(j<31)
  115. {
  116. if(j!=0)
  117. {
  118. resultsum += ADC10MEM;
  119. }
  120. j++;
  121. }
  122. else
  123. {
  124. resultq = resultsum;
  125. resultsum = 0;
  126. j = 0;
  127. }
  128. }
  129. /****************************************************************/
  130. // Timer A0 interrupt service routine
  131. #pragma vector=TIMER0_A0_VECTOR
  132. __interrupt void Timer_A(void)
  133. {
  134. ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
  135. ADC10CTL0 |= ENC; // ADC10 enable set
  136. }
复制代码
  1. /*
  2. 加权递推平均滤波法
  3.   A、方法:
  4.     是对递推平均滤波法的改进,即不同时刻的数据加以不同的权
  5.     通常是,越接近现时刻的数据,权取得越大。
  6.     给予新采样值的权系数越大,则灵敏度越高,但信号平滑度越低
  7.   B、优点:
  8.     适用于有较大纯滞后时间常数的对象
  9.     和采样周期较短的系统
  10.   C、缺点:
  11.     对于纯滞后时间常数较小,采样周期较长,变化缓慢的信号
  12.     不能迅速反应系统当前所受干扰的严重程度,滤波效果差
  13. */
  14. #include
  15. #define uchar unsigned char
  16. #define uint unsigned int
  17. const int coefficient[10]= {1,2,3,4,5,6,7,8,9,10};
  18. const int coesum = 55;

  19. unsigned char i,j,n;
  20. long resultsum,resultend,resultsum1,resultq = 0;
  21. unsigned int result[11];
  22. uchar ge,shi,bai,qian;

  23. #define SER ( 1 << 1 ) //也是所谓的DS
  24. #define SRCLK ( 1 << 0 ) //也是所谓的SHCP
  25. #define RCLK ( 1 << 2 ) //也是所谓的STCP

  26. void delay_1ms(void)
  27. {
  28. unsigned int i;
  29. for (i=0;i<1000;i++);
  30. }
  31. void delay_nms(unsigned int n)
  32. {
  33. unsigned int i=0;
  34. for (i=0;i
  35. delay_1ms();
  36. }

  37. uchar table[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 };

  38. void Write595(uchar data)
  39. {

  40. P2OUT &= ~RCLK;
  41. for( i=0;i<8;i++ )
  42. {
  43. if(data&0x80)
  44. {
  45. P2OUT |= SER;
  46. }
  47. else
  48. {
  49. P2OUT &= ~SER;
  50. }
  51. P2OUT &= ~SRCLK;
  52. P2OUT |= SRCLK;
  53. data <<= 1;
  54. }
  55. P2OUT |= RCLK;
  56. }

  57. void ADC_init(void)
  58. {
  59. P1SEL |= 0x01;
  60. ADC10AE0 |= 0x01; // P1.0 ADC option select
  61. ADC10CTL1 = INCH_0+SHS_1;
  62. }

  63. void main(void)
  64. {
  65. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  66. if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
  67. {
  68. while(1); // If calibration constants erased
  69. // do not load, trap CPU!
  70. }
  71. //8Mhz
  72. BCSCTL1 = CALBC1_8MHZ; // Set range
  73. DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */

  74. P1DIR |= (BIT2|BIT3|BIT4|BIT5);
  75. P2DIR |= 0xff;
  76. TACCTL0 = CCIE; // Enable interrupt
  77. TACCR0 = 32-1; // PWM Period
  78. TACCTL1 = OUTMOD_3; // TACCR1 set/reset
  79. TACCR1 = 2; // TACCR1 PWM Duty Cycle
  80. TACTL = TASSEL_1 + MC_1; // ACLK, up mode

  81. ADC_init();
  82. _EINT();

  83. while(1)
  84. {
  85. resultend = resultq*1500/1024;
  86. qian = resultend/1000;
  87. bai = resultend/100%10;
  88. shi = resultend/10%10;
  89. ge = resultend%10;

  90. P1OUT &= ~(( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  91. Write595(table[ge]);
  92. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  93. Write595(table[ge]);
  94. P1OUT &= ~( 1 << 2 );
  95. delay_nms(1);

  96. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  97. Write595(table[shi]);
  98. P1OUT &= ~( 1 << 3 );
  99. delay_nms(1);

  100. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  101. Write595(table[bai]);
  102. P1OUT &= ~( 1 << 4 );
  103. delay_nms(1);

  104. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  105. Write595(table[qian]);
  106. P1OUT &= ~( 1 << 5 );
  107. delay_nms(1);
  108. }
  109. }
  110. /********************************************************************/
  111. // ADC10 interrupt service routine
  112. #pragma vector=ADC10_VECTOR
  113. __interrupt void ADC10_ISR(void)
  114. {

  115. ADC10CTL0 &= ~ENC; // ADC10 disabled
  116. ADC10CTL0 = 0; // ADC10, Vref disabled completely
  117. if(j<11)
  118. {
  119. result[j] = ADC10MEM;
  120. j++;
  121. }
  122. else
  123. {
  124. for(n=0;n<10;n++)
  125. {
  126. result[n] = result[n+1];
  127. resultsum += result[n]*coefficient[n];
  128. }
  129. resultsum /= coesum;
  130. resultq = resultsum;
  131. resultsum = 0;
  132. j = 0;
  133. }

  134. //ADC10CTL0 |= ENC; // ADC10 enable set
  135. }
  136. /****************************************************************/
  137. // Timer A0 interrupt service routine
  138. #pragma vector=TIMER0_A0_VECTOR
  139. __interrupt void Timer_A(void)
  140. {
  141. ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
  142. ADC10CTL0 |= ENC; // ADC10 enable set
  143. }
复制代码
  1. /*
  2. 限幅滤波法(又称程序判断滤波法)
  3.   A、方法:
  4.     根据经验判断,确定两次采样允许的最大偏差值(设为A)
  5.     每次检测到新值时判断:
  6.     如果本次值与上次值之差<=A,则本次值有效
  7.     如果本次值与上次值之差>A,则本次值无效,放弃本次值,用上次值代替本次值
  8.   B、优点:
  9.     能有效克服因偶然因素引起的脉冲干扰
  10.   C、缺点
  11.     无法抑制周期性的干扰
  12.     平滑度差
  13. */
  14. #include
  15. #define uchar unsigned char
  16. #define uint unsigned int
  17. #define A 2
  18. unsigned char j = 0;
  19. long result[31],resultsum,resultend,resultq = 0;
  20. uchar ge,shi,bai,qian;

  21. #define SER ( 1 << 1 ) //也是所谓的DS
  22. #define SRCLK ( 1 << 0 ) //也是所谓的SHCP
  23. #define RCLK ( 1 << 2 ) //也是所谓的STCP

  24. void delay_1ms(void)
  25. {
  26. unsigned int i;
  27. for (i=0;i<1000;i++);
  28. }

  29. void delay_nms(unsigned int n)
  30. {
  31. unsigned int i=0;
  32. for (i=0;i
  33. delay_1ms();
  34. }
  35. uchar table[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 };

  36. //uchar table[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E };
  37. void Write595(uchar data)
  38. {
  39. uchar i;
  40. P2OUT &= ~RCLK;
  41. for( i=0;i<8;i++ )
  42. {
  43. if(data&0x80)
  44. {
  45. P2OUT |= SER;
  46. }
  47. else
  48. {
  49. P2OUT &= ~SER;
  50. }
  51. P2OUT &= ~SRCLK;
  52. P2OUT |= SRCLK;
  53. data <<= 1;
  54. }
  55. P2OUT |= RCLK;
  56. }

  57. /*void ADC_init(void)
  58. {
  59. P1SEL |= 0x01;
  60. ADC10CTL0 = SREF_1 + REFON + ADC10ON + ADC10IE + ADC10SHT_2; //
  61. ADC10AE0 |= 0x01; // P1.0 ADC option select
  62. ADC10CTL1 = INCH_0+ADC10SSEL_2;
  63. ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start

  64. }*/
  65. void ADC_init(void)
  66. {
  67. P1SEL |= 0x01;
  68. ADC10AE0 |= 0x01; // P1.0 ADC option select
  69. ADC10CTL1 = INCH_0+SHS_1;
  70. }
  71. void main(void)
  72. {
  73. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  74. if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
  75. {
  76. while(1); // If calibration constants erased // do not load, trap CPU!
  77. }
  78. //8Mhz
  79. BCSCTL1 = CALBC1_8MHZ; // Set range
  80. DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */

  81. P1DIR |= (BIT2|BIT3|BIT4|BIT5);
  82. P2DIR |= 0xff;
  83. TACCTL0 = CCIE; // Enable interrupt
  84. TACCR0 = 32-1; // PWM Period
  85. TACCTL1 = OUTMOD_3; // TACCR1 set/reset
  86. TACCR1 = 2; // TACCR1 PWM Duty Cycle
  87. TACTL = TASSEL_1 + MC_1; // ACLK, up mode

  88. ADC_init();
  89. _EINT();

  90. while(1)
  91. {
  92. resultend = resultq*1500/1024;
  93. qian = resultend/1000;
  94. bai = resultend/100%10;
  95. shi = resultend/10%10;
  96. ge = resultend%10;

  97. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  98. Write595(table[ge]);
  99. P1OUT &= ~( 1 << 2 );
  100. delay_nms(1);

  101. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  102. Write595(table[shi]);
  103. P1OUT &= ~( 1 << 3 );
  104. delay_nms(1);

  105. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  106. Write595(table[bai]);
  107. P1OUT &= ~( 1 << 4 );
  108. delay_nms(1);

  109. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  110. Write595(table[qian]);
  111. P1OUT &= ~( 1 << 5 );
  112. delay_nms(1);
  113. }
  114. }
  115. /********************************************************************/
  116. // ADC10 interrupt service routine
  117. #pragma vector=ADC10_VECTOR
  118. __interrupt void ADC10_ISR(void)
  119. {
  120. ADC10CTL0 &= ~ENC; // ADC10 disabled
  121. ADC10CTL0 = 0; // ADC10, Vref disabled completely
  122. if(j<31)
  123. {
  124. result[j] = ADC10MEM;
  125. if(j>=2)
  126. {
  127. if((result[j] - result[j-1]>A)||(result[j-1] - result[j]>A))
  128. resultsum = result[j-1];
  129. else
  130. resultsum = result[j];
  131. }

  132. j++;
  133. }
  134. else
  135. {
  136. resultq = resultsum;
  137. j = 0;
  138. }
  139. }
  140. /****************************************************************/
  141. // Timer A0 interrupt service routine
  142. #pragma vector=TIMER0_A0_VECTOR
  143. __interrupt void Timer_A(void)
  144. {
  145. ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
  146. ADC10CTL0 |= ENC; // ADC10 enable set
  147. }
复制代码
  1. /*
  2. 中位值滤波法
  3.   A、方法:
  4.     连续采样N次(N取奇数)
  5.     把N次采样值按大小排列
  6.     取中间值为本次有效值
  7.   B、优点:
  8.     能有效克服因偶然因素引起的波动干扰
  9.     对温度、液位的变化缓慢的被测参数有良好的滤波效果
  10.   C、缺点:
  11.     对流量、速度等快速变化的参数不宜
  12. */
  13. #include
  14. #define uchar unsigned char
  15. #define uint unsigned int
  16. #define X 31
  17. unsigned char i,j,k,n;
  18. long resultsum,temp,resultq = 0;
  19. uint result[31];
  20. uchar ge,shi,bai,qian;

  21. #define SER ( 1 << 1 ) //也是所谓的DS
  22. #define SRCLK ( 1 << 0 ) //也是所谓的SHCP
  23. #define RCLK ( 1 << 2 ) //也是所谓的STCP

  24. void delay_1ms(void)
  25. {
  26. unsigned int i;
  27. for (i=0;i<1000;i++);
  28. }

  29. void delay_nms(unsigned int n)
  30. {
  31. unsigned int i=0;
  32. for (i=0;i
  33. delay_1ms();
  34. }
  35. uchar table[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 };

  36. void Write595(uchar data)
  37. {

  38. P2OUT &= ~RCLK;
  39. for( i=0;i<8;i++ )
  40. {
  41. if(data&0x80)
  42. {
  43. P2OUT |= SER;
  44. }
  45. else
  46. {
  47. P2OUT &= ~SER;
  48. }
  49. P2OUT &= ~SRCLK;
  50. P2OUT |= SRCLK;
  51. data <<= 1;
  52. }
  53. P2OUT |= RCLK;
  54. }

  55. void ADC_init(void)
  56. {
  57. P1SEL |= 0x01;
  58. ADC10AE0 |= 0x01; // P1.0 ADC option select
  59. ADC10CTL1 = INCH_0+SHS_1;
  60. }

  61. void main(void)
  62. {
  63. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  64. if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
  65. {
  66. while(1); // If calibration constants erased // do not load, trap CPU!
  67. }
  68. //8Mhz
  69. BCSCTL1 = CALBC1_8MHZ; // Set range
  70. DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */

  71. P1DIR |= (BIT2|BIT3|BIT4|BIT5);
  72. P2DIR |= 0xff;
  73. TACCTL0 = CCIE; // Enable interrupt
  74. TACCR0 = 32-1; // PWM Period
  75. TACCTL1 = OUTMOD_3; // TACCR1 set/reset
  76. TACCR1 = 2; // TACCR1 PWM Duty Cycle
  77. TACTL = TASSEL_1 + MC_1; // ACLK, up mode

  78. ADC_init();
  79. _EINT();
  80. while(1)
  81. {
  82. resultsum = resultq;
  83. resultsum = resultsum*1500/1024;
  84. qian = resultsum/1000;
  85. bai = resultsum/100%10;
  86. shi = resultsum/10%10;
  87. ge = resultsum%10;

  88. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  89. Write595(table[ge]);
  90. P1OUT &= ~( 1 << 2 );
  91. delay_nms(1);

  92. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  93. Write595(table[shi]);
  94. P1OUT &= ~( 1 << 3 );
  95. delay_nms(1);

  96. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  97. Write595(table[bai]);
  98. P1OUT &= ~( 1 << 4 );
  99. delay_nms(1);

  100. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  101. Write595(table[qian]);
  102. P1OUT &= ~( 1 << 5 );
  103. delay_nms(1);
  104. }
  105. }
  106. /********************************************************************/
  107. // ADC10 interrupt service routine
  108. #pragma vector=ADC10_VECTOR
  109. __interrupt void ADC10_ISR(void)
  110. {

  111. ADC10CTL0 &= ~ENC; // ADC10 disabled
  112. ADC10CTL0 = 0; // ADC10, Vref disabled completely
  113. if(j
  114. {
  115. result[j] = ADC10MEM;
  116. j++;
  117. }
  118. else
  119. {
  120. for(n=0;n
  121. {
  122. for(k=0;k
  123. {
  124. if(result[k]>result[k+1])
  125. {
  126. temp = result[k];
  127. result[k] = result[k+1];
  128. result[k+1] = temp;
  129. }
  130. }
  131. }
  132. resultq = result[(X-1)/2];
  133. j = 0;
  134. }

  135. //ADC10CTL0 |= ENC; // ADC10 enable set
  136. }
  137. /****************************************************************/
  138. // Timer A0 interrupt service routine
  139. #pragma vector=TIMER0_A0_VECTOR
  140. __interrupt void Timer_A(void)
  141. {
  142. ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
  143. ADC10CTL0 |= ENC; // ADC10 enable set
  144. }
复制代码
  1. /*
  2. 中位值平均滤波法(又称防脉冲干扰平均滤波法)
  3.   A、方法:
  4.     相当于“中位值滤波法”+“算术平均滤波法”
  5.     连续采样N个数据,去掉一个最大值和一个最小值
  6.     然后计算N-2个数据的算术平均值
  7.     N值的选取:3~14
  8.   B、优点:
  9.     融合了两种滤波法的优点
  10.     对于偶然出现的脉冲性干扰,可消除由于脉冲干扰所引起的采样值偏差
  11.   C、缺点:
  12.     测量速度较慢,和算术平均滤波法一样
  13.     比较浪费RAM
  14. */
  15. #include
  16. #define uchar unsigned char
  17. #define uint unsigned int
  18. #define X 31
  19. unsigned char i,j,k,n,m;
  20. long resultsum,temp,resultend,resultq = 0;
  21. uint result[31];
  22. uchar ge,shi,bai,qian;

  23. #define SER ( 1 << 1 ) //也是所谓的DS
  24. #define SRCLK ( 1 << 0 ) //也是所谓的SHCP
  25. #define RCLK ( 1 << 2 ) //也是所谓的STCP

  26. void delay_1ms(void)
  27. {
  28. unsigned int i;
  29. for (i=0;i<1000;i++);
  30. }
  31. void delay_nms(unsigned int n)
  32. {
  33. unsigned int i=0;
  34. for (i=0;i
  35. delay_1ms();
  36. }

  37. uchar table[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 };

  38. void Write595(uchar data)
  39. {
  40. P2OUT &= ~RCLK;
  41. for( i=0;i<8;i++ )
  42. {
  43. if(data&0x80)
  44. {
  45. P2OUT |= SER;
  46. }
  47. else
  48. {
  49. P2OUT &= ~SER;
  50. }
  51. P2OUT &= ~SRCLK;
  52. P2OUT |= SRCLK;
  53. data <<= 1;
  54. }
  55. P2OUT |= RCLK;
  56. }

  57. void ADC_init(void)
  58. {
  59. P1SEL |= 0x01;
  60. ADC10AE0 |= 0x01; // P1.0 ADC option select
  61. ADC10CTL1 = INCH_0+SHS_1;
  62. }

  63. void main(void)
  64. {
  65. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  66. if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
  67. {
  68. while(1); // If calibration constants erased
  69. // do not load, trap CPU!!
  70. }
  71. //8Mhz
  72. BCSCTL1 = CALBC1_8MHZ; // Set range
  73. DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */

  74. P1DIR |= (BIT2|BIT3|BIT4|BIT5);
  75. P2DIR |= 0xff;
  76. TACCTL0 = CCIE; // Enable interrupt
  77. TACCR0 = 32-1; // PWM Period
  78. TACCTL1 = OUTMOD_3; // TACCR1 set/reset
  79. TACCR1 = 2; // TACCR1 PWM Duty Cycle
  80. TACTL = TASSEL_1 + MC_1; // ACLK, up mode

  81. ADC_init();
  82. _EINT();

  83. while(1)
  84. {
  85. resultend = resultq/29;
  86. resultend = resultend*1500/1024;
  87. qian = resultend/1000;
  88. bai = resultend/100%10;
  89. shi = resultend/10%10;
  90. ge = resultend%10;

  91. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  92. Write595(table[ge]);
  93. P1OUT &= ~( 1 << 2 );
  94. delay_nms(1);

  95. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  96. Write595(table[shi]);
  97. P1OUT &= ~( 1 << 3 );
  98. delay_nms(1);

  99. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  100. Write595(table[bai]);
  101. P1OUT &= ~( 1 << 4 );
  102. delay_nms(1);

  103. P1OUT |= (( 1 << 2 )|(1<<3)|(1<<4)|(1<<5));
  104. Write595(table[qian]);
  105. P1OUT &= ~( 1 << 5 );
  106. delay_nms(1);
  107. }
  108. }
  109. /********************************************************************/
  110. // ADC10 interrupt service routine
  111. #pragma vector=ADC10_VECTOR
  112. __interrupt void ADC10_ISR(void)
  113. {

  114. ADC10CTL0 &= ~ENC; // ADC10 disabled
  115. ADC10CTL0 = 0; // ADC10, Vref disabled completely
  116. if(j
  117. {
  118. result[j]= ADC10MEM;
  119. j++;
  120. }
  121. else
  122. {
  123. for(n=0;n
  124. {
  125. for(k=0;k
  126. {
  127. if(result[k]>result[k+1])
  128. {
  129. temp = result[k];
  130. result[k] = result[k+1];
  131. result[k+1] = temp;
  132. }
  133. }
  134. }
  135. for(m=1;m<30;m++)
  136. {
  137. resultsum += result[m];
  138. }
  139. resultq = resultsum;
  140. resultsum = 0;
  141. j = 0;
  142. }
  143. }
  144. /****************************************************************/
  145. // Timer A0 interrupt service routine
  146. #pragma vector=TIMER0_A0_VECTOR
  147. __interrupt void Timer_A(void)
  148. {
  149. ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;
  150. ADC10CTL0 |= ENC; // ADC10 enable set
  151. }
复制代码

[ 本帖最后由 IC爬虫 于 2012-10-8 18:17 编辑 ]

msp430g2452--adc滤波.zip

8.57 KB, 下载次数: 46

此帖出自模拟电子论坛

最新回复

不错啊  详情 回复 发表于 2013-6-6 22:58

赞赏

1

查看全部赞赏

点赞 关注(3)

回复
举报

2万

帖子

341

TA的资源

版主

沙发
 
支持楼主这种奉献精神,,,,做工不错哦
此帖出自模拟电子论坛

点评

在论坛也有些时间了,获得大家很多的资料,也到自己做一些力所能及的事,回馈给朋友们了。  详情 回复 发表于 2012-10-8 18:14
 
 

回复

2056

帖子

0

TA的资源

五彩晶圆(初级)

板凳
 
我写的代码是改自"常见泽"分享的,但是和他的有些不一样。有兴趣的话可以参照对比一下!

我画的板子是为了方便自己DIY的,大家可以直接用。所有代码都是经过本人验证的,至于各种滤波算法的差别自己去试,程序开头的文字介绍大家把他当做一个参考就好,实际效果还是要自己来验证。
此帖出自模拟电子论坛
 
 
 
 

回复

2056

帖子

0

TA的资源

五彩晶圆(初级)

4
 

回复 沙发 qwqwqw2088 的帖子

在论坛也有些时间了,获得大家很多的资料,也到自己做一些力所能及的事,回馈给朋友们了。
此帖出自模拟电子论坛
 
 
 
 

回复

12

帖子

0

TA的资源

一粒金砂(初级)

5
 
不错啊
此帖出自模拟电子论坛
 
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表