大神:帮我看看我的程序是什么问题,
/**超声波测距离HC-SR04模块,在1602上显示出来,误差为5cm以内********/
#include "msp430.h"
#include "Delayms.H"
#include "lcd.H"
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define CLK_Trig P5OUT &= ~BIT0 //P3.0产生脉冲引脚
#define SET_Trig P5OUT |= BIT0 //P3.0产生脉冲引脚
#define Echo (P5IN & BIT1) //回波引脚
unsigned char tt[]=" hc-sr04 ";
uchar distance[]="distance: CM";
unsigned long int distance_data, M;
uchar succeed_bit;
//uint count=0;
//函数的声明//
/*20us延时函数*/
void Delay20us()
{
uchar i;
for(i=0; i<20; i++);
}
/***测量获取数据**/
void Measure()
{
succeed_bit=0;
CCTL0 &= ~CCIE; //关闭中断使能; 测量前关闭中断,减少误差
// TACTL |= TACLR;//定时器的清零,清除基数的内容,不过TACTL resgiters设置也清除了
SET_Trig;//P3.0产生脉冲引脚
Delay20us();//延时20us,把Trig的引脚的电位拉高20us//之后,超声波内部自动发送8个40KHz的脉冲
CLK_Trig;//再输出低电平
while(Echo == 0);//判断echo是否变为高电平
CCTL0 |= CCIE;//中断使能,打开中断
while(Echo == 1); //等待echo变回低电平
distance_data = TAR;//TA0R是所需时间
while(!succeed_bit);//等待succeed为 1
M =(distance_data * 17)/1000;//测量结果为CM
succeed_bit = 0;
}
//显示函数
void display()
{
distance[12] = M%10+0x30; //取个位
distance[11] = M/10%10+0x30; //取十位
distance[10] = M/100+0x30; //取百位
Write_intruc
tion(0xc0);
LCD1602_Print(distance);
Delayms(60);
}
void main(void)
{
uchar i;
//Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 &= ~XT2OFF;//XT2OFF=0,XT2OFF is on
do
{
IFG1 &= ~OFIFG; //对OFIFG的值取反
for(i=0; i<0xff; i++);
}
while(IFG1 & OFIFG);//判断OFIFG标志位失效
BCSCTL2 |= SELM_2 + SELS +DIVS_3;//使MCLK和SMCLK都为高频振荡器,8分频
CCR0 =61000; //定时61MS
TACTL |= TASSEL_2 + ID_3 + MC_1 ;//选择SMLK时钟频率,8分频,1M
P5DIR &= ~BIT1;//设置P5.1为输入端
P5DIR |= BIT0;//设置P5.1为输出端
P2DIR = 0xff;//LCD的数据端口
P6DIR = 0x38;//控制端口
Delayms(600);
LCD1602_Inter();//1602的初始化
_EINT(); //进入全局中断
Write_intruction(0xc0);//第二行的列地址
LCD1602_Print(distance);
Write_intruction(0x80);//第二行的列地址
LCD1602_Print(tt);
while(1)
{
Measure();
display();
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void vectot_TimeA(void)
{
succeed_bit =1;
CCTL0 &= ~CCIE; //关闭中断使能;
}
/***************************
系统在默认情况下,输出为0
***************************/
#include "msp430.h"
#include "Delayms.h"
#define uchar unsigned char
#define uint unsigned int
#define DataDir P2DIR
#define CtrlDir P6DIR
#define CLR_RS P6OUT &= ~BIT3
#define SET_RS P6OUT|=BIT3
#define CLR_RW P6OUT&=~BIT4
#define SET_RW P6OUT|=BIT4
#define CLR_EN P6OUT&=~BIT5
#define SET_EN P6OUT|=BIT5
/*******LCD1602写指令函数***********/
void Write_intruction(uint intruction)
{
CLR_RS;
CLR_RW;
CLR_EN;
SET_EN;
P2OUT = intruction;
CLR_EN;
SET_RS;
SET_RW;
Delayms(30);
}
/*******LCD1602写数据*******/
void Write_Data(uchar *DATA)
{
SET_RS;
CLR_RW;
CLR_EN;//上升沿
SET_EN;
P2OUT = *DATA;
CLR_EN;
SET_RS;
SET_RW;
Delayms(30);
}
/***********初始化LCD*************/
void LCD1602_Inter()
{
Write_intruction(0x38);
Write_intruction(0x38);
Write_intruction(0x38);
Write_intruction(0x0C);
Write_intruction(0x01);
Write_intruction(0x06);
}
void LCD1602_Print(uchar *str)
{
while( *str!=' ')
{
Write_Data(str);
str++;
}
}
void Delayms(unsigned int m)
{
unsigned int i, j;
for(i=0; i<m; i++)
for(j=0; j<1000; j++);
}
一周热门 更多>