MSP430F5529测频法测量信号频率

MSP430F5529测量频率

-----测频法

信号变换电路

过零比较器,lm393输出上拉电阻,两电阻分压 程序

#include #include \//测频法,上限1Mhz

//看门狗定时1s,开门狗中断处理程序开启捕获器,捕获1s中所有的上升脉冲,脉冲个数即为频率

/*优化的反向可以将开门够定时1S使用定时器去定时1s

* main.c */

long fre=0; char buf[60]=\

void SetVcoreUp (unsigned int level) {

// Open PMM registers for write PMMCTL0_H = PMMPW_H;

// Set SVS/SVM high side new level

SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level; // Set SVM low side to new level

SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level; // Wait till SVM is settled

while ((PMMIFG & SVSMLDLYIFG) == 0); // Clear already set flags

PMMIFG &= ~(SVMLVLRIFG + SVMLIFG); // Set VCore to new level

PMMCTL0_L = PMMCOREV0 * level; // Wait till new level reached if ((PMMIFG & SVMLIFG))

while ((PMMIFG & SVMLVLRIFG) == 0); // Set SVS/SVM low side to new level

SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level; // Lock PMM registers for write access PMMCTL0_H = 0x00; }

void init_clock() { SetVcoreUp (0x01); SetVcoreUp (0x02); SetVcoreUp (0x03); UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO UCSCTL4 |= SELA_2; // Set ACLK = REFO __bis_SR_register(SCG0); // Disable the FLL control loop UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation UCSCTL2 = FLLD_0 + 609; // Set DCO Multiplier for 25MHz // (N + 1) * FLLRef = Fdco // (762 + 1) * 32768 = 25MHz // Set FLL Div = fDCOCLK/2 __bic_SR_register(SCG0); // Enable the FLL control loop __delay_cycles(782000); do { UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags SFRIFG1 &= ~OFIFG; // Clear fault flags }while (SFRIFG1&OFIFG); // Test oscillator fault flag }

void send_char(char sc) { UCA0TXBUF=sc; while(!(UCA0IFG&UCTXIFG)); }

void send_string(char *s) { while(*s!='\\0') { send_char(*s++); } }

init_uart() { P3SEL |= BIT3+BIT4; // P3.3,4 = USCI_A0 TXD/RXD UCA0CTL1 |= UCSWRST; // **Put state machine in reset** UCA0CTL1 |= UCSSEL__SMCLK; // SMCLK UCA0BR0 = 173; // 1MHz 115200 (see User's Guide) UCA0BR1 = 0; // 1MHz 115200 UCA0MCTL |= UCBRS_5 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0 UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** // UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt send_string(\}

int main(void) {

WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer init_clock();//初始化系统时钟为20 init_uart();//串口波特率为115200bps

WDTCTL=WDT_ADLY_1000 ;//开门狗定时1s

P1DIR &= ~BIT2; // P1.2 in P1SEL |= BIT2; //捕获输入

MHz

SFRIE1|=WDTIE; //开看门狗定时器中断

__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts __no_operation(); // For debugger return 0; }

void measure_fre() { if(fre>=500)

sprintf(buf,\频率:%1.1f ',4);\\r\\n\

if(fre<=52)

sprintf(buf,\频率:%1.1f ',4);\\r\\n\

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4