|
智能小车,红外加超声波避障加测速里程,内加算法,用上51的4个中断优先级了,
- #include <reg52.h>
- #include "lcd1602.h"
- #include "IRLED.h"
- #include "CarControl.h"
- #include "SuperWave.h"
- #include "SysTicks.h"
- #include "MeasureSpeed.h"
- sfr IPH = 0XB7;//优先级配置寄存器
- #define uchar unsigned char
- #define uint unsigned int
- sbit IRIN = P3^3; //红外接收器数据线
- extern uchar IRLedValue ;//红外遥控器值
- /***************************************
-
- 定时器0:计数器模式,用于计算码盘脉冲个数
- 定时器1:定时器模式,用于计算超声波距离
- 定时器2:定时器模式,用于系统滴答计时,与产生PWM波
- 外部中断1:红外接收中断
-
- 优先级:
- 外部中断1 > 定时器2 > 定时器1 > 定时器0
- *****************************************/
- bit IsCarRunning=0;//车是否在跑
- void main()
- {
- uchar i;
- PT0=1; //提升外部中断1高于定时器0
- IPH|=0x22;//提升定时器2优先级仅次于外部中断1
-
- LCD1602_Init();
- IRLED_Init();
- Car_Init();
- SuperWave_Init();
- SysTicks_Init();
- MeasureSpeed_Init();
- LCD1602_ShowString(0,0,"Num:");
- while(1)
- {
- SuperWave_GetCount();
-
- Delay_ms(10);
- }
- }
- #include <reg52.h>
- #include "lcd1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define LCD1602_DATA P0
- sbit lcden=P2^7; //使能控制
- sbit lcdrs=P2^5; //命令数据输入
- sbit lcdrw=P2^6; //命令数据输入
-
- void Delay_ms(unsigned int ms)
- {
- unsigned int x,y;
- for(x=ms;x>0;x--)
- for(y=110;y>0;y--);
- }
-
- bit LCD1602_Busy()
- {
- bit busy;
- lcdrs=0;
- lcdrw=1;
- lcden=1;
- Delay_ms(2);
- busy=(bit)(LCD1602_DATA&0x80);
- lcden=0;
- return(busy);
- }
-
- void LCD1602_Wcmd(unsigned char buffer)
- {
- while(LCD1602_Busy());
- lcdrs = 0;
- lcdrw = 0;
- lcden = 0;
- LCD1602_DATA = buffer;
- lcden = 1;
- Delay_ms(5); //1ms 3ms
- lcden = 0;
- }
- void LCD1602_Wdata(unsigned char dat)
- {
- while(LCD1602_Busy());
- lcdrs = 1;
- lcdrw = 0;
- lcden = 0;
- LCD1602_DATA = dat;
- lcden = 1;
- Delay_ms(5);
- lcden = 0;
- }
- void LCD1602_Init()
- {
- LCD1602_Wcmd(0x38);//数据8位
- Delay_ms(5);
- LCD1602_Wcmd(0x0e);//开启显示屏及光标
- Delay_ms(5);
- LCD1602_Wcmd(0x06);//输入模式:位增,不移
- Delay_ms(5);
- LCD1602_Wcmd(0x01);//清零
- Delay_ms(5);
- }
- void LCD1602_ShowString(unsigned char x,unsigned char y,unsigned char dat[])
- {
- uchar i=0;
- if(y==0)
- LCD1602_Wcmd(0x80+x);//0x80+1
- else if(y==1)
- LCD1602_Wcmd(0xc0+x);//0xc0+1
- while(dat[i]!='\0')
- {
- LCD1602_Wdata(dat[i]);
- Delay_ms(2);
- i++;
- }
- }
- #include <reg52.h>
- #include "ultrasonic.h"
- #include "delay.h"
- #include "lcd.h"
- void getsuper(void)
- {
- trig = 1;
- DelayUs2x(5);
- trig = 0;
- while(!echo);
- TR1 = 1;
- while(echo);
- TR1 = 0;
- time = TH1*256 + TL1;
- TH1 = 0;
- TL1 = 0;
-
- sss=(time*1.7)/100;
- LCD1602_Dis_OneChar(1, 1, sss%1000/100+0x30);
- LCD1602_Dis_OneChar(2, 1, sss%100/10+0x30);
- LCD1602_Dis_OneChar(3, 1, sss%10+0x30);
- LCD1602_Dis_OneChar(4, 1, 'c');
- LCD1602_Dis_OneChar(5, 1, 'm');
-
- }
复制代码 |
|