今天外接了超声波测距模块试了一下 基本功能实现了 误差稍微有点大
#include "reg52.h"
//超声波测距2011.12.16
//内部振荡器16MHz
//Timer0_实现方波发生器_40.2kHz
//Timer1_波特率发生器_UART0_9600波特率
//Timer2_超声波时间测量计数
//for循环_方波发生器步距_3.4kHz
//Timer0_方波发生器步距_0.2kHz
#define Max 1
#define Step_1 65
#define Step_2 3
#define Step_3 30000
#define Cal_Set 5000
//unsigned char bdata status_byte_0;//状态位
//unsigned int xdata * data px;
unsigned int Cal_1;
unsigned char Data_1[18]={0};//接收存放
sbit LED30 = P3^0; //系统运行周期LED显示
sbit Output_32=P3^2; //超声波驱动脉冲输出
sbit Output_33=P3^3; //超声波驱动脉冲输出
void SYSCLK_Init (void)
{
/* int i; // delay counter
OSCXCN = 0x67; // start external oscillator with 11.0592MHz crystal
for (i=0; i < 256; i++){;}; // Wait for osc. to start up
while (!(OSCXCN & 0x80)); // Wait for crystal osc. to settle
OSCICN = 0x88;//Enable missing clock detector and select external oscillator as SYSCLK source
*/
OSCICN = 0x97;//Enable missing clock detector and select internal 16MHz oscillator as SYSCLK source
}
void PORT_Init (void)
{
XBR0 = 0x6E; //Enable UART0 and UART1
XBR1 = 0x17;
XBR2 = 0x44;
// XBR0 = 0x07; //Enable UART0 and UART1
// XBR1 = 0x02;
// XBR2 = 0x44; //Enable crossbar and weak -up
// XBR2 = 0xC4; //Enable crossbar but disable weak pull-ups
// EMI0CF = 0x27;
// EMI0TC = 0x21;
P0MDOUT = 0x00;
P1MDOUT = 0x00;
// P2MDOUT = 0x00;
//P3MDOUT = 0x0C;
}
void UART01_Init (void)
{ SCON0 = 0x50; // SCON0: mode 1, 8-bit, No Parity Bit, 1 bit start and 1 bit stop
TMOD = 0x22; // timer 1: mode 2, 8-bit reload
// timer 0: mode 2, 8-bit reload
TL1 =0x9e;
TH1 =0x8e; //99_A3=9600 Baud rate
//CKCON=0x08;//Timer0_使用系统时钟,不1/12分频
//CKCON=0x00;//Timer0_使用系统时钟,1/12分频
CKCON = 0x18; //Timer0_使用系统时钟_Not_1/12分频_Timer1 uses SYSCLK as time base
PCON = 0x80; //SMOD0 = 1_UART0波特率加倍
TR1 = 0; // Start Timer1 用于串行通讯Baud rate控制
}
/*
void SPI0_Init (void)
{
SPI0CFG = 0x07; // data sampled on 1st SCK rising edge 8-bit data words
SPI0CFG|=0xC0; // CKPOL =1;
SPI0CN = 0x03; // Master mode; SPI enabled; flags cleared
SPI0CKR = SYSCLK/2/8000000-1; // SPI clock <= 8MHz (limited by EEPROM spec.)
}
*/
/*
void Delay1us(unsigned char Count_us)
{unsigned char t1;
while (Count_us)
{
for(t1=1;t1<5;t1++){;}
Count_us--;
}
}
void Delay1ms(unsigned char Count_ms)
{ while (Count_ms)
{
Delay1us(200);
Delay1us(200);
Delay1us(200);
Delay1us(200);
Count_ms--;
}
}
*/
void Send_Addre_32()
{
ES0=0;
SBUF0='@';while(!TI0);TI0=0;//头
ACC = Data_1[0];//拆成高4位
ACC &= 0xF0;
ACC = ACC/16;
SBUF0=ACC;while(!TI0);TI0=0;
ACC = Data_1[0];//拆成低4位
ACC = ACC & 0x0F;
SBUF0=ACC;while(!TI0);TI0=0;
ACC = Data_1[1];//拆成高4位
ACC &= 0xF0;
ACC = ACC/16;
SBUF0=ACC;while(!TI0);TI0=0;
ACC = Data_1[1];//拆成低4位
ACC = ACC & 0x0F;
SBUF0=ACC;while(!TI0);TI0=0;
SBUF0='A';while(!TI0);TI0=0;//尾
ES0=1;
}
//===main()===
void main (void)
{
unsigned char M1,M2;
unsigned int i_1,i_2;
WDTCN = 0xde; //禁止WDT_disable watchdog timer
WDTCN = 0xad; //关看门狗
//===
// WDTCN = 0xFF; //锁定WDT_一直使用看门狗
// WDTCN = 0xA5; //使能/复位WDT_喂狗
// WDTCN = 0x07; //设置最大WDT超时极限值
//===
SYSCLK_Init (); //初始化系统时钟
UART01_Init (); //初始化串口
PORT_Init (); //初始化IO口
T2CON = 0x01; //初始化Timer2
TH0 =0x46;//Timer0_实现方波发生器_40.2kHz
TL0 =0x46;
TH2 =0x00;
TL2 =0x00;
ET0 = 1;
ET2 = 1;
EX0 = 1;
EA = 1;
//Waiting_1
for(M1=0;M1<9;M1++)
{
LED30=~LED30;
for(i_1=0;i_1<300;i_1++)
{for(i_2=0;i_2<300;i_2++)
{;}
}
}
while(1)
{
M2++;
if(M2>Step_2){M2=0;LED30=~LED30;}
Send_Addre_32();
Output_32=0;
Output_33=1;
ET2 = 1;//Timer2_溢出中断允许
TR2 = 1;//Timer2_计时开始
TR0 = 1;//发出超声波
for(i_1=0;i_1<Step_1;i_1++){;}
TR0 = 0;//停止超声波
Output_32=0;
Output_33=1;
for(M1=0;M1<Step_2;M1++)
{
for(i_1=0;i_1<Step_1;i_1++){;}//延时
}
EX0=1; //外中断0允许接收回波
Cal_1=Data_1[0]*256+Data_1[1];
for(i_1=0;i_1<Step_2;i_1++)
{
for(M1=0;M1<Step_2;M1++)
{
for(i_2=0;i_2<Step_3;i_2++){;}//延时
}
if(Cal_1>Cal_Set){LED30=~LED30;}//如果油位过低_快闪
}
}//while(1)
}//main()
void T2_ISR(void) interrupt 5
{
ET2 = 0;//Timer2_溢出中断禁止
TR2 = 0;//Timer2_计时停止
TF2=0;//溢出中断标志
TH2=0;
TL2=0;
}
void T0_ISR(void) interrupt 1
{
//ET0 = 0;
//TR0 = 0;
Output_32=~Output_32;
Output_33=~Output_33;
//ET0 = 1;
//TR0 = 1;
}
/*
void INIT0_ISR(void) interrupt 0
{
EX0=0;
ET2 = 0;
TR2 = 0;//Timer2_计时停止
*px = TH2;
px++;
*px = TL2;
px++;
// ACC = TH2;
// B = TL2;
TH2=0;
TL2=0;
}
*/
void INIT0_ISR(void) interrupt 0
{
EX0=0;
ET2 = 0;
TR2 = 0;//Timer2_计时停止
Data_1[0] = TH2;//高4位
Data_1[1] = TL2;//低4位
TH2=0;
TL2=0;
}