📄 main.c
字号:
/**********************************************
/程序说明:nRF24LE1超声波测距,串口显示测量数据
***********************************************/
#include "reg24le1.h"
#include <intrins.h>
/***********************************************
/
/宏定义部分
/
***********************************************/
#define Enableint() do{EA=1;}while(0)
#define Disableint() do{EA=0;}while(0)
#define true 0x01
#define false 0x00
#define RX P00
#define TX P01
#define LED P02
/***********************************************
/类型重新定义
***********************************************/
typedef unsigned char uchar;
typedef unsigned int uint;
/***********************************************
/定义标志位
***********************************************/
bit flag =0; /*溢出标志 */
/********************************************
/
/延时函数
/
*********************************************/
void delayms(unsigned int ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
while(--j);
}
}
}
/*********************************************
/
/nRF24LE1之I/O口初始化函数
/
**********************************************/
void IO_init(void )
{
P0DIR|=BIT_0; /*设置RX为输入*/
P0DIR&=(~(BIT_1)); /*设置TX为输出*/
P0DIR&=(~(BIT_2));
RX=0;
TX=0;
}
/*********************************************
/
/LedDebug函数通过调用显示程序状态
/
**********************************************/
void LedDebug(void)
{
LED=!LED;
delayms(200);
}
/*********************************************
/
/delayus()延时微秒
/
*********************************************/
void delayus(uint us)
{
us-=5;
while(us--)
{
_nop_();
}
}
/*********************************************
/
/nRF24LE1工作时钟的初始化
/
*********************************************/
void ClkSet(void)
{
CLKCTRL=0X28;
CLKLFCTRL = 0x01;
}
/*********************************************
/
/启动超声波模块
/
*********************************************/
void StartModule()
{
TX=1;
delayus(25);
TX=0;
}
/********************************************
/
/定时器0初始化
/
*********************************************/
void T0_init(void)
{
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1; /*允许T0中断 */
TR0=0;
}
/*******************************************
/
/串口初始化
/
*******************************************/
void Uart_init(uint baud)
{
P0DIR &= 0xF7; /*设置P0.3 (TxD)为输出 */
P0DIR |= 0x10; /*设置P0.4 (RxD)为输入 */
P0|=0x18;
S0CON = 0x50;
PCON |= 0x80; /* 波特率倍增 */
WDCON |= 0x80; /* 选定内部波特率发生器*/
if(baud==38400)
{
S0RELL = 0xF3; /* 波特率38400 */
S0RELH = 0x03;
}
else if(baud==9600)
{
S0RELL = 0xCC; /* 波特率9600 */
S0RELH = 0x03;
}
}
/*********************************************
/
/putch()函数通过串口输出一个字符
/
*********************************************/
void putch(char ch)
{
S0BUF=ch;
while(!TI0);
TI0=0;
}
/*********************************************
/
/Puts()串口发送一个字符串
/
**********************************************/
void Puts(char* str)
{
while(*str!='\0')
{
putch(*str++);
}
}
/********************************************
/
/nextline()换行函数
/
*********************************************/
void nextline(void)
{
putch('\n');
}
/**********************************************
/
/计算距离
/
***********************************************/
void Conut(void)
{
static uint time=0,TMP;
static float S;
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.87)/100; /*算出来是CM */
if(flag==1) /*超出测量 */
{
flag=0;
Puts("can,t get the distance!!!");
nextline();
}
else
{
Puts("distance S= ");
TMP=S;
if(TMP>=100&&TMP<1000)
{
putch((TMP/100)+'0');
putch((TMP%100)/10+'0');
putch((TMP%10)+'0');
}
else if(TMP>=10)
{
putch((TMP/10)+'0');
putch((TMP%10)+'0');
}
else if(TMP<10)
{
putch(TMP+'0');
}
putch('.');
TMP=((int)(S*100))%100;
putch((TMP/10)+'0');
putch((TMP%10)+'0');
Puts(" cm");
nextline();
}
}
/*********************************************
/
/定时器中断0服务函数
/
*********************************************/
void TIMER0ISR0() interrupt INTERRUPT_TF0
{
flag=1;
}
/*********************************************
/
/主函数部分
/
**********************************************/
void main(void)
{
static uint timeout=0;
Disableint();
ClkSet(); /*nRF24LE1时钟初始化 */
IO_init(); /*I/O口初始化 */
T0_init(); /*定时器0初始化 */
Uart_init(38400);/*波特率初始化 */
Puts("超声波测距测试程序!");
nextline();
Enableint();
while(1)
{
ST : StartModule();/*启动超声波测距 */
while(0==RX) /*当RX为零时等待 */
{
delayus(5);
timeout++;
if(timeout==3000)
{
timeout=0;
goto ST;
}
}
timeout=0;
if(RX==1)
TR0=1; /*开启计数 */
while(1==RX); /*当RX为1计数并等待 */
if(RX==0)
TR0=0; /*关闭计数 */
Conut(); /*计算距离,同时串口显示 */
LedDebug(); /*LED程序状态显示 */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -