您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > MSP430F5529测频法测量信号频率
MSP430F5529测量频率-----测频法信号变换电路过零比较器,lm393输出上拉电阻,两电阻分压程序#includemsp430f5529.h#includestdio.h//测频法,上限1Mhz//看门狗定时1s,开门狗中断处理程序开启捕获器,捕获1s中所有的上升脉冲,脉冲个数即为频率/*优化的反向可以将开门够定时1S使用定时器去定时1s*main.c*/longfre=0;charbuf[60]=\0;voidSetVcoreUp(unsignedintlevel){//OpenPMMregistersforwritePMMCTL0_H=PMMPW_H;//SetSVS/SVMhighsidenewlevelSVSMHCTL=SVSHE+SVSHRVL0*level+SVMHE+SVSMHRRL0*level;//SetSVMlowsidetonewlevelSVSMLCTL=SVSLE+SVMLE+SVSMLRRL0*level;//WaittillSVMissettledwhile((PMMIFG&SVSMLDLYIFG)==0);//ClearalreadysetflagsPMMIFG&=~(SVMLVLRIFG+SVMLIFG);//SetVCoretonewlevelPMMCTL0_L=PMMCOREV0*level;//Waittillnewlevelreachedif((PMMIFG&SVMLIFG))while((PMMIFG&SVMLVLRIFG)==0);//SetSVS/SVMlowsidetonewlevelSVSMLCTL=SVSLE+SVSLRVL0*level+SVMLE+SVSMLRRL0*level;//LockPMMregistersforwriteaccessPMMCTL0_H=0x00;}voidinit_clock(){SetVcoreUp(0x01);SetVcoreUp(0x02);SetVcoreUp(0x03);UCSCTL3=SELREF_2;//SetDCOFLLreference=REFOUCSCTL4|=SELA_2;//SetACLK=REFO__bis_SR_register(SCG0);//DisabletheFLLcontrolloopUCSCTL0=0x0000;//SetlowestpossibleDCOx,MODxUCSCTL1=DCORSEL_7;//SelectDCOrange50MHzoperationUCSCTL2=FLLD_0+609;//SetDCOMultiplierfor25MHz//(N+1)*FLLRef=Fdco//(762+1)*32768=25MHz//SetFLLDiv=fDCOCLK/2__bic_SR_register(SCG0);//EnabletheFLLcontrolloop__delay_cycles(782000);do{UCSCTL7&=~(XT2OFFG+XT1LFOFFG+DCOFFG);//ClearXT2,XT1,DCOfaultflagsSFRIFG1&=~OFIFG;//Clearfaultflags}while(SFRIFG1&OFIFG);//Testoscillatorfaultflag}voidsend_char(charsc){UCA0TXBUF=sc;while(!(UCA0IFG&UCTXIFG));}voidsend_string(char*s){while(*s!='\0'){send_char(*s++);}}init_uart(){P3SEL|=BIT3+BIT4;//P3.3,4=USCI_A0TXD/RXDUCA0CTL1|=UCSWRST;//**Putstatemachineinreset**UCA0CTL1|=UCSSEL__SMCLK;//SMCLKUCA0BR0=173;//1MHz115200(seeUser'sGuide)UCA0BR1=0;//1MHz115200UCA0MCTL|=UCBRS_5+UCBRF_0;//ModulationUCBRSx=1,UCBRFx=0UCA0CTL1&=~UCSWRST;//**InitializeUSCIstatemachine**//UCA0IE|=UCRXIE;//EnableUSCI_A0RXinterruptsend_string(CLS(0);\r\n);}intmain(void){WDTCTL=WDTPW|WDTHOLD;//Stopwatchdogtimerinit_clock();//初始化系统时钟为20MHzinit_uart();//串口波特率为115200bpsWDTCTL=WDT_ADLY_1000;//开门狗定时1sP1DIR&=~BIT2;//P1.2inP1SEL|=BIT2;//捕获输入SFRIE1|=WDTIE;//开看门狗定时器中断__bis_SR_register(LPM0_bits+GIE);//EnterLPM0,enableinterrupts__no_operation();//Fordebuggerreturn0;}voidmeasure_fre(){if(fre=500)sprintf(buf,DS16(0,60,'频率:%1.1f',4);\r\n,fre*1.008);if(fre=52)sprintf(buf,DS16(0,60,'频率:%1.1f',4);\r\n,(fre)*1.0);elseif(fre=208)sprintf(buf,DS16(0,60,'频率:%1.1f',4);\r\n,(fre+1)*1.0);elseif(fre=496)sprintf(buf,DS16(0,60,'频率:%1.1f',4);\r\n,(fre+3)*1.0);send_string(buf);fre=0;TA0CTL=TASSEL_2+MC_2+TACLR+TAIE;//SMCLK,连续mode,clearTAR8分频下限可以测到8Hz。上限200K左右TA0CCTL1=CM_1+SCS+CAP+CCIE;}#pragmavector=WDT_VECTOR__interruptvoidWDT_ISR(void){measure_fre();}#pragmavector=TIMER0_A1_VECTOR__interruptvoidTIMER0_A1_ISR(void){switch(__even_in_range(TA0IV,14)){case0:break;//Nointerruptcase2://CCR1fre++;break;case4:break;//CCR2notusedcase6:break;//CCR3notusedcase8:break;//CCR4notusedcase10:break;//CCR5notusedcase12:break;//CCR6notusedcase14:break;//定时器溢出default:break;}}
本文标题:MSP430F5529测频法测量信号频率
链接地址:https://www.777doc.com/doc-4830581 .html