超声波
#include <STC15F2K60S2.h>
#include <delay.h>
#include <string.h>
#include <stdio.h>
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define uchar unsigned char
#define uint unsigned int
#define TX P10
#define RX P11
uchar discount=0,count_1=0,ceju_flag=1;
uchar code tab[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar dsbuff[8]={11,11,11,11,11,11,11,11};
uint distance=0,dis;
char shuzu[2];
uchar n=0,dat;
uchar num=20;
pdata uchar a[40];
uchar high,low;
uchar b[12]={"temperture:"};
void senddata(uchar dat);
void sendstring(uchar *p);
void sendwave();
void Timer0Init(void);
void Timer1Init(void);
void display();
void get_distance();
void UartInit(void);
void allinit()
{
P2=0x80;P0=0xff;
P2=0xa0;P0=0x00;
P2=0xc0;P0=0xff;
P2=0xe0;P0=0xff;
}
void main()
{
allinit();
Timer0Init();
Timer1Init();
UartInit();
EA=1;
ET0=1;
ES=1;
while(1)
{
if(ceju_flag==1)
{
ceju_flag=0;
get_distance();
P2=0x80;
P0=0xfe;
sprintf(a,"%s%hd%c%c",b,distance,'\r','\n');
sendstring(a);
}
high=(distance>>8)&0xff; //取其高八位和低八位分别存进E2PROM
low=distance&0xff;
dis=(high<<8)|low;
dsbuff[0]=dis/100;
dsbuff[1]=dis%100/10;
dsbuff[2]=dis%10;
dsbuff[5]=distance/100;
dsbuff[6]=distance%100/10;
dsbuff[7]=distance%10;
}
}
void uart1() interrupt 4
{
}
void sendstring(uchar *p)
{
while(*p!='\0')
{
SBUF=*p;
while(!TI);
TI=0;
p++;
}
}
void get_distance()
{
sendwave();
TR1=1;
while((RX==1)&&(TF1==0));
TR1=0;
if(TF1)
{
TF1=0;
distance=999;
}
else
{
distance=TH1;
distance=distance<<8;
distance|=TL1;
distance=(uint) distance*0.017;
}
TH1=TL1=0;
}
void sendwave()
{
TX=1;
somenop;somenop;somenop;somenop;somenop;
somenop;somenop;somenop;somenop;somenop;
TX=0;
}
void time0() interrupt 1
{
display();
if(++count_1==100)
{
count_1=0;
ceju_flag=1;
}
}
void display()
{
P2=0xe0;
P0=0xff;
P2=0x1f;
P2=0xc0;
P0=1<<discount;
P2=0x1f;
P2=0xe0;
P0=tab[dsbuff[discount]];
P2=0x1f;
if(++discount==8)
discount=0;
}
void Timer1Init(void) //0毫秒@11.0592MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0x00; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 0; //定时器1开始计时
}
void Timer0Init(void) //2毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x9A; //设置定时初值
TH0 = 0xA9; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0xE0; //设定定时初值
T2H = 0xFE; //设定定时初值
AUXR |= 0x10; //启动定时器2
}
ne555
void timer1isr() interrupt 3 //1ms周期执行
{
unsigned int temp;
display_dig();
LED();
cnt_100ms++;
cnt_10ms++;
if (cnt_100ms >= 100)
{
cnt_100ms = 0;
TR0 = 0;
temp = TH0;
temp <<= 8;
temp |= TL0;
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TR0 = 1;
ne555 = temp * 10;
}
}
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟为计数模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x04; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x20; //设置定时初值
TH1 = 0xD1; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
EA = 1;
ET1 = 1;
}
该程序是使用STC15F2K60S2单片机进行超声波测距的实现,通过初始化定时器和UART串口通信,测量并发送距离数据。在中断服务函数中处理计时和数据显示。
4479

被折叠的 条评论
为什么被折叠?



