1 #include "timerHz.h" 2 #include "msp430g2553.h" 3 4 #define _DEBUG_TIMERHZ_ 5 6 unsigned int capArray[16] = {0}; 7 unsigned char index = 2, flag = 0; 8 float hz = 0; 9 10 /* 11 * 绝对值 12 */ 13 int abs(int x) 14 { 15 return x>0?x:-x; 16 } 17 18 /* 19 * 系统初始化 20 */ 21 void HZ_Init() 22 { 23 P1SEL |= BIT1; //P1.1复用 24 P1DIR &= ~BIT1; //P1.1输入 25 // P1SEL |= BIT2; //P1.2复用 26 // P1DIR &= ~BIT2; //P1.2输入 27 28 CCTL0 = CM_1 + SCS + CCIS_0 + CAP + CCIE; //上升沿捕获,同步,信号源设置,捕获功能,允许捕获中断 29 TACTL = TASSEL_2 + MC_2; //定时器的时钟源选择SMCLK(16M),连续计数 30 } 31 32 /******************中断服务程序*****************************/ 33 #pragma vector=TIMER0_A0_VECTOR 34 __interrupt void TIME0_A0_ISR(void) 35 { 36 capArray[index--] = TA0CCR0; 37 if(index == 0) 38 { 39 index = 2; 40 TA0CCR0 = 0; // 清零计数器 41 flag = 1; 42 } 43 } 44 45 #ifdef _DEBUG_TIMERHZ_ 46 #include "12864.h" 47 #include "sys.h" 48 49 void main() 50 { 51 double temp = 213.456; 52 // unsigned char i; 53 54 System_Init(); 55 LCD_Init(); 56 HZ_Init(); 57 58 LCD_Show_String("Measure freq:"); 59 LCD_Set_Pos(1,0); 60 LCD_Show_Deci(temp); 61 LCD_Set_Pos(1,5); 62 LCD_Show_String("Hz"); 63 LCD_Set_Pos(1,0); 64 65 _EINT(); 66 while(1) 67 { 68 if (flag == 1) 69 { 70 // for (i = 0; i<=2; i += 2) 71 // temp += abs(capArray[i+1] - capArray[i]); 72 temp = abs(capArray[1] - capArray[2]); 73 hz = ((double )(1000000))/temp; //1MHZ,1/1000000s = 1us 74 LCD_Set_Pos(1,0); 75 LCD_Show_Deci(hz); //显示 76 flag = 0; 77 } 78 } 79 } 80 #endif
MSP430之频率测量,误差1Hz,布布扣,bubuko.com
原文:http://www.cnblogs.com/rongfangliu/p/measurefreq.html