(DS1302+超声波)上电显示时钟,按下S7切换为超声波,按下S6切换,当切换到时钟时,串口发送时间;当切换到超声波时,串口发送距离。
main.c
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "chaoshengbo.h"
#include "ds1302.h"
sbit s7=P3^0;
sbit s6=P3^1;
void Timer0Init(void);
void xianshi_csb();
void xianshi_ds();
void Delay10ms();
void anjian();
void sendstring(unsigned char *s);
void UartInit(void);
void sengbyte(unsigned char dat);
//void chuankou();
unsigned char date;
extern unsigned int distance,intr,t;
unsigned char display[]={
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char smg[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int s;
unsigned char work=0;//模式选择
unsigned char kou_flag;
void main()
{
Timer0Init();EA=1;ET0=1;
P2=(P2&0X1F)|0XA0;
P0=0X00;
P2&=0X1F;
UartInit();ES=1;
Ds1302Init();
sendstring("任务");sendstring("十四");
while(1)
{
// chuankou();
anjian();
xianshi_csb();
xianshi_ds();
}
}
void anjian()
{
if(s7==0)
{
Delay10ms();
if(s7==0)
{
work=2;
}
while(!s7);
}
if(s6==0)
{
Delay10ms();
if(s6==0)
{
work=1;
}
while(!s6);
}
}
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
}
void uartint() interrupt 4
{

该博客介绍了一种使用STC15F2K60S2单片机结合DS1302实时时钟模块和超声波传感器的设计方案。通过上电显示时钟,用户可通过按键在时钟和超声波测距之间切换。在时钟模式下,串口发送当前时间;在超声波模式下,发送测量的距离信息。
最低0.47元/天 解锁文章
899

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



