代码如下:
#include <msp430g2553.h>
#include "12864.h"
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
unsigned long int mk=0;
uint num=0;
/************************主函数****************************/
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; //关闭看门狗
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ; // DCO 1MHz
lcdinit(); //复位1602液晶
// LCD_dsp_string(1,0,"占空比:");
LCD_dsp_string(1,0,"脉宽:");
LCD_dsp_string(2,0,"脉宽:");
// LCD_dsp_string(3,0,"脉宽:");
// LCD_dsp_string(4,0,"脉宽:");
P1IES = 0; // P1.4为 IO中断,上升沿
P1IE |= BIT4;
// P1REN |= BIT4;
// P1OUT &= ~BIT4; //下拉电阻
P1SEL |= BIT0; //P1.0 ACLK输出
P1DIR |= BIT0;
BCSCTL1 |= DIVA_3;
TACTL |= TACLR;
TACCTL0 |= CCIE;
// TACTL = TASSEL_2+MC_2; //SMCLK 8M,清计数器,使能中断,连续增计模式
while(1)
{
_EINT();
LPM0;
mk=num*65536+mk;
if(mk<5000)
{
LCD_set_xy(1,3);
LCD_write_dat('0'+mk/10000);
LCD_write_dat('0'+mk/1000%10);
LCD_write_dat('0'+mk/100%10);
LCD_write_dat('0'+mk/10%10);
LCD_write_dat('0'+mk%10);
LCD_write_dat('u');
LCD_write_dat('s');
}
else
{
mk=mk/1000;
LCD_set_xy(1,3);
LCD_write_dat('0'+mk/10000);
LCD_write_dat('0'+mk/1000%10);
LCD_write_dat('0'+mk/100%10);
LCD_write_dat('0'+mk/10%10);
LCD_write_dat('0'+mk%10);
LCD_write_dat('m');
LCD_write_dat('s');
}
num=0;
TACTL |=TACLR;
delay_ms(500);
}
}
/************中断函数****************/
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TimerA0 (void)
{
num++;
}
#pragma vector=PORT1_VECTOR // P1口中断处理函数
__interrupt void PORT1_ISR(void)
{
TACTL = TASSEL_2+MC_2;
while(P1IN&BIT4 == BIT4);
TACTL = MC_0;
__disable_interrupt();
mk=TAR;
P1IFG = 0;
LPM0_EXIT; // Clear CPUOFF bit from 0(SR)
}
我是使用的IO中断测的脉宽,可是实际效果不理想,就只是测ACLK8分频后都一直显示五千多ms,求大神助攻,帮忙看看。另外直接使用定时器的捕获测我也试过,但是好像只能测到1K到6K左右。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#include "12864.h"
#include "delay.h"
#define uint unsigned int
#define uchar unsigned char
uint start=0;
uint end=0;
uint tem=0;
uint temp=0,overflow=0;
uint num=0;
void Init_CLK(void)
{
volatile unsigned int index;
BCSCTL1&=~0X00; //打开XT2振荡器
do
{
IFG1 &= ~OFIFG; // 清除振荡器失效标志
for (index = 0xFF; index > 0; index--);// 延时,等待XT2起振
}
while ((IFG1 & OFIFG) != 0);// 判断XT2是否起振
BCSCTL2 =SELM_2+SELS; //选择MCLK、SMCLK为XT2 8MHz
}
/************************主函数****************************/
void main(void)
{
unsigned long int e,f;
unsigned int mk=0,zkb=0;
WDTCTL = WDTPW+WDTHOLD; //关闭看门狗
Init_CLK();
lcdinit(); //复位1602液晶
LCD_dsp_string(1,0,"占空比:");
LCD_dsp_string(2,0,"脉宽:");
P4SEL |= BIT1; //设置P4.1端口为功能模块使用
// P1DIR &=~BIT2;
P2SEL |= BIT0; //P2.0 ACLK输出
P2DIR |= BIT0;
BCSCTL1 |= DIVA_2;
TBCTL |= TBCLR;
TBCTL = TBSSEL_2+MC_2; //SMCLK 8M,清计数寄存器,连续增计模式
TBCCTL1 =CM_1+SCS+CAP+CCIE+CCIS_0; //上升沿捕获,同步,capture模式 CCI1B(P4^1),中断使能
while(1)
{
e=overflow*65536+temp-start;
f=num*65536+end-start;
mk=(unsigned int)(f/8.0);
zkb=(unsigned int)((f*1000.0)/e);
LCD_set_xy(2,3);
LCD_write_dat('0'+mk/1000);
LCD_write_dat('0'+mk/100%10);
LCD_write_dat('0'+mk/10%10);
LCD_write_dat('0'+mk%10);
LCD_write_dat('u');
LCD_write_dat('s');
LCD_set_xy(1,4);
LCD_write_dat('0'+zkb/100);
LCD_write_dat('0'+zkb/10%10);
LCD_write_dat('.');
LCD_write_dat('0'+zkb%10);
LCD_write_dat('%');
delay_ms(500);
_EINT();
LPM0;
}
}
#pragma vector=TIMERB1_VECTOR //定时器A中断处理
__interrupt void timer_b(void)
{
switch(TBIV) //向量查询
{
case 2: //捕获中断
if(TBCCTL1&CM_1) //上升沿
{
tem++;
if(tem==1)
{
TBCCTL1=(TBCCTL1&(~CM_1))|CM_2; //更变设置为下降沿触发
start=TBCCR1; //记录A波初始时间(捕捉产生start)
overflow=0; //溢出计数变量复位
num=0;
}
else if(tem==2)
{
tem=0;
temp=TBCCR1;
_DINT();
LPM0_EXIT;
}
}
else if(TBCCTL1&CM_2) //下降沿
{
TBCCTL1=(TBCCTL1&(~CM_2))|CM_1; //更变设置为上升沿触发
end=TBCCR1; //用start,end,overflow计算脉冲宽度
num=overflow;
}
break;
case 14: //定时器溢出中断
overflow++;
break; //溢出计数加1
default:break;
}
}
这是f149测脉宽的,但是效果不理想,麻烦各位大虾帮忙看看哪不行?3Q
一周热门 更多>