这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 高校专区» 坤创E-Geek/天科大新电社» msp430f149超声波测距代码

共4条 1/1 1 跳转至

msp430f149超声波测距代码

菜鸟
2013-11-20 23:05:03 打赏


#include "msp430x14x.h"
#define CPU_F ((double)8000000)
#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))
#define P55 5
#define P56 6
#define P57 7

#define RS_CLR P5OUT &= ~(1 << P55) //RS置低
#define RS_SET P5OUT |= (1 << P55) //RS置高

#define RW_CLR P5OUT &= ~(1 << P56) //RW置低
#define RW_SET P5OUT |= (1 << P56) //RW置高

#define EN_CLR P5OUT &= ~(1 << P57) //E置低
#define EN_SET P5OUT |= (1 << P57) //E置高
#define DataPort P4OUT //P4口为数据口
#define uchar unsigned char
#define uint unsigned int
unsigned char x=0;
unsigned char table[2];
unsigned char table1[]="jiulishi:";
unsigned int number;

//======================写指令=========================//
void write_com(uchar com)
{
RS_CLR; //RS置低
EN_CLR; //En置低
DataPort=com;
delay_ms(1);
EN_SET; //E置高
delay_ms(2);
EN_CLR;
}

//======================写数据==========================//
void write_data(uchar data)
{
RS_SET; //RS置高
EN_CLR;
DataPort=data;
delay_ms(1);
EN_SET;
delay_ms(2);
EN_CLR;
}
//=======================================================//
//add1为1602显示的行选择切值为1或2),add2为行的位选择,shu为要显示的数;
void write_shu(uchar add1,uchar add2, uchar shu)
{
uchar shi,ge;
shi=shu/10;
ge=shu%10;
switch(add1)
{
case 1:{
write_com(0x80+add2);
write_data(0x30+shi);//注此处若要显示3为可再加write_data(0x30+bai);
write_data(0x30+ge);
}
break;
case 2:{
write_com(0x80+0x40+add2);
write_data(0x30+shi);
write_data(0x30+ge);
}
break;
}

}
//========================1602初始化================================//
void init()
{


P4SEL = 0x00;
P4DIR = 0xFF;
P5SEL = 0x00;
P5DIR|= BIT5 + BIT6 + BIT7; //控制口设置为输出模式
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);

}


void INIT()
{


P3SEL|=0X30;
P3DIR|=0X10;
P3DIR&=~0X20;
UTCTL0|=SSEL0;
UCTL0|=CHAR;

UBR10=0X00;
UBR00=0X03;
UMCTL0=0X4A;

ME1=UTXE0+URXE0;
UCTL0&=~SWRST;
IE1|=URXIE0;
IFG1&=~UTXIFG0;
}

void main( void )
{
unsigned char Thousand, Hundred, Decade, Unit,x;

WDTCTL = WDTPW + WDTHOLD;
init();
INIT();
_EINT();

write_com(0x80);
for(x=0;x<9;x++)
write_data(table1[x]);

write_com(0x80+0x40+6);
write_data('m');
write_data('m');
while(1)
{
while(!UTXIFG0);
TXBUF0=0X55;

number=table[0]*256+table[1];
if(number > 6000)
number = 0;
Thousand = number/1000;
Hundred = number%1000/100;
Decade = number%100/10;
Unit = number%10;


write_com(0x80+0x40+2);
write_data(0x30+Thousand);
write_data(0x30+Hundred);
write_data(0x30+Decade);
write_data(0x30+Unit);


}

}
#pragma vector=USART0RX_VECTOR
__interrupt void uarss(void)
{
table[x++]=RXBUF0;
if(x>sizeof table-1)
x=0;


/*table[x]=RXBUF0;
x++;
if(x==2)
x=0;
*/
}




关键词: 超声波 msp430f149

菜鸟
2013-11-20 23:06:31 打赏
2楼
149?是我11级才子系列

高工
2013-11-22 23:28:26 打赏
3楼

看了程序,说说我自己的习惯呀,作为交流:

代码里有下面这段:

#define P55 5
#define P56 6
#define P57 7

#define RS_CLR P5OUT &= ~(1 << P55) //RS置低
#define RS_SET P5OUT |= (1 << P55) //RS置高

#define RW_CLR P5OUT &= ~(1 << P56) //RW置低
#define RW_SET P5OUT |= (1 << P56) //RW置高

#define EN_CLR P5OUT &= ~(1 << P57) //E置低
#define EN_SET P5OUT |= (1 << P57) //E置高

我一般喜欢这样写:

#define RS 5

#define RW 6

#define EN 7

#define PortOutput P5OUT

#define RS_CLR PortOutput &= ~(1 << RS)

#define RS_SET PortOutput |= (1 << RS)

....

因为我觉着这样如果移植就更方便了,IO端口改了也只需要改这里的几个地方而已...


菜鸟
2013-11-24 21:05:12 打赏
4楼
说的对。

共4条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]