/*请教一个小小的问题,我用msp430g2331超声波测距模块测距,用数码管显示,但发现我测量的数据都比实际值大1.5-1,8倍,我不太清楚程序时序如何,但也不会差距如此大,难道是超声波模块有问题吗?*/
#include"config.h"
int main( void )
{
extern_16m();//内部1M时钟
init_TA(); //启动ta定时器,定时5ms
hc164_init(); //用74hc164并转串,
hc_sr04(); //超声波模块引脚初始化
__bis_SR_register(GIE);
while(1)
{
}
}
#include"io430.h"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define CLK0 P1OUT&=~BIT6
#define CLK1 P1OUT|=BIT6
#define hc_sr040 P1OUT&=~BIT4
#define hc_sr041 P1OUT|=BIT4
#define CPU_F ((double)1000000) //外部高频晶振16MHZ
//#define CPU_F ((double)32768) //外部低频晶振32.768KHZ
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
unsigned char const code_hex[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x39,0x79,0x0e,0x3e};//0,1,2,3,4,5,6,7,8,9,c,e,j,u
uchar t;
uchar hc_sr04_cishu,hc_sr04_flag;
uchar xianshi_flag;
float hc_sr04_time;
uint xishu;
uchar dis_play[4]={10,11,12,13};
void extern_16m()
{
WDTCTL = WDTPW + WDTHOLD;
if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased, trap CPU!!
}
DCOCTL |= DCO0 + DCO1+DCO2; //SMCLK选择LFXT1CLK
// BCSCTL2 |= SELM_0;//MCLK采用1M的内部DCO
// BCSCTL2 |= DIVS_0;//SMCLK采用内部的时钟
}
void hc164_init()
{
P1DIR |=BIT6+BIT7+BIT0+BIT1+BIT2+BIT3; // P1.0 output
//4段共阴数码管,bit0-bit3为片选位,bit6为74hc164slk,bit7为dat位
}
void hc_sr04()
{
P1DIR |=BIT4;
P1DIR &=~BIT5;
P1IE |=0x20; //上升沿触发
P1IES |=0x00;
hc_sr04_flag=3;//默认准备发射状态
}
void sendbyte(uchar byte)
{
uchar c,num;
num=byte;
for(c=0;c<8;c++)
{
P1OUT&=~0x80;
CLK0;
P1OUT|=num&0x80; //(0x80即十进制的128, 二进制的10000000 按位发送
CLK1;
num<<=1;
}
}
void send_char(uchar weizhi,uchar byte)
{
P1OUT |= 0x0f;
uchar c,send_byte;
send_byte=code_hex[byte];
if((weizhi==0)&&(xianshi_flag==0))
send_byte|=0x80;
if((weizhi==1)&&(xianshi_flag==1))
send_byte|=0x80;
sendbyte(send_byte);
c=weizhi&0x03;
P1OUT&=~(1<<c);
}
void init_TA()
{
TACCR0 = 5000; //5ms中断一次
TACTL = TASSEL_2 + MC_1; // SMCLK, upmode,
TACCTL0 = CCIE; // TACCR0 interrupt enabled
}
void shumaguan()
{
++t;
if(t>=4)
t=0;
send_char(t,dis_play[t]);
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMERA0_ISR() // the interrupt source is CC0
{
shumaguan();
hc_sr04_cishu++;
if(( hc_sr04_flag==3)&&(hc_sr04_cishu==100))//500ms测距一次
{
hc_sr041;
P1IE |=0x20;
delay_us(10); //发射端发射高电平超过10us.
hc_sr040;
}
if(hc_sr04_flag==2)
{
float juli;
uint juli1; //计算一次超声波经过时间
juli=(float)(hc_sr04_time/5.8139); //求出距离(mm)
juli1=(uint)(juli);
if(juli1<10000)
{ //给数码管显示
xianshi_flag=0;
dis_play[0]=(juli1/1000);
dis_play[1]=(juli1%1000/100);
dis_play[2]=(juli1%1000%100/10);
dis_play[3]=(juli1%10);
}
else
{
xianshi_flag=1;
dis_play[0]=(juli1/10000);
dis_play[1]=(juli1%10000/1000);
dis_play[2]=(juli1%10000%1000/100);
dis_play[3]=(juli1%10000%1000%100/10);
}
hc_sr04_flag=3;
hc_sr04_cishu=0;
}
}
#pragma vector =PORT1_VECTOR
__interrupt void Port_1(void)//一次上升沿与一次下降沿为高电平的时间
{
switch(P1IES&0x20)
{
case 0:TAR=0;P1IES=0x20;hc_sr04_flag=1;hc_sr04_cishu=0;break;改为上升沿
case 0x20:hc_sr04_time=5000*hc_sr04_cishu+TAR;P1IES=0;hc_sr04_flag=2;P1IE &=~0x20;break;
} //改为下降沿,并进入发射状态,等待500ms
P1IFG=0;
}
此帖出自
小平头技术问答
一周热门 更多>