|
識(shí)別硬幣車(chē)停2 s聲響燈亮后繼續(xù)行駛,bc段加速,d處進(jìn)內(nèi)道,轉(zhuǎn)到點(diǎn)停車(chē),顯示硬幣數(shù)和停車(chē)時(shí)間
制作出來(lái)的實(shí)物圖如下:
單片機(jī)源程序如下:
- /***************定義頭文件專(zhuān)屬區(qū)域*******************/
- #include <REGX51.H>//右鍵添加51頭文件
- #include "Trace.h"//循跡驅(qū)動(dòng)頭文件
- #include "motor.h"//控制輪子轉(zhuǎn)向頭文件
- #include "oled.h"//OELD驅(qū)動(dòng)頭文件
- #include "bmp.h"//OLED圖片頭文件,沒(méi)用到
- #include "Beep_Led_Ctrl.h"//蜂鳴器和LED的頭文件
- /***************定義引腳專(zhuān)屬區(qū)域*******************/
- sbit ENA = P2^0;//L298n上的ENA、ENB,需要拔掉跳帽,然后和單片機(jī)上面的IO口相接
- sbit ENB = P2^1;
- sbit STBY = P2^6;
- /***************定義變量專(zhuān)屬區(qū)域*******************/
- unsigned char PWM; //占空比
- extern unsigned char Speed_A; //全局變量,引用Trace.c當(dāng)中的Speed_A和Speed_B
- extern unsigned char Speed_B; //在Trace.c中定義為電機(jī)A的速度和電機(jī)B的速度
- extern unsigned char Coin_Number; //硬幣數(shù)量
- unsigned int i; //秒計(jì)時(shí)輔助變量
- unsigned char Second; //數(shù)秒(90秒停止)
- unsigned char Stop_Times; //停止時(shí)間顯示
- /***************定時(shí)器0初始化函數(shù)*******************/
- void Timer0Init(void) //100微秒@12.000MHz
- {
- TMOD &= 0xF0; //設(shè)置定時(shí)器模式
- TMOD |= 0x02; //設(shè)置定時(shí)器模式
- TL0 = 0x9C; //設(shè)置定時(shí)初值
- TH0 = 0x9C; //設(shè)置定時(shí)重載值
- TF0 = 0; //清除TF0標(biāo)志
- TR0 = 1; //定時(shí)器0開(kāi)始計(jì)時(shí)
- ET0 = 1; //定時(shí)器0中斷開(kāi)關(guān)
- EA = 1; //中斷總開(kāi)關(guān)
- }
- /***************定時(shí)器0中斷服務(wù)函數(shù)*******************/
- void Timer0Server() interrupt 1
- {
- /* 占空比調(diào)速程序:*/
- PWM++;
- if(PWM>=200)PWM = 0;
- if(PWM <= Speed_A)
- {
- ENA = 1;
- }
- else
- {
- ENA = 0;
- }
- if(PWM <= Speed_B)
- {
- ENB = 1;
- }
- else
- {
- ENB = 0;
- }
- /*計(jì)時(shí)秒程序*/
- i++;
- if(i >= 10000) {i = 0; Second++;if(Second>=90) Second = 90;}
-
- }
- /***************主函數(shù)Main*******************/
- void main()
- {
- Timer0Init(); //定時(shí)器初始化
- STBY = 1; //tb6612的STBY端口拉高電平,才能工作
- BL_Ctrl_Init(); //蜂鳴器和Led的初始化
-
- /*OLED初始化程序*/
- OLED_Init(); //初始化OLED
- OLED_ColorTurn(0); //0正常顯示,1 反色顯示
- OLED_DisplayTurn(0);//0正常顯示 1 屏幕翻轉(zhuǎn)顯示
- OLED_ShowString(0,0,"YANGYU COOL!",8);//開(kāi)始顯示內(nèi)容
-
- while(1)
- {
- Trace_Car();//引用Trace.c中的函數(shù)(灰度循跡邏輯控制)
-
- if(Second >=34 && Second <35){Stop_Times = Second;}//使停止時(shí)間等于34s(正在數(shù)的時(shí)間),不能只寫(xiě)Stop_Times = Second;不然停止時(shí)間會(huì)隨定時(shí)器數(shù)秒一起變化
-
- if(Second >=34&&Second <= 90)//34秒停止
- {
- Speed_A = 0;
- Speed_B = 0;
- Stop();
- OLED_ShowString(0,0,"Car_Drive_Times:",8);
- OLED_ShowNum(103,0,Second,3,8);
- OLED_ShowString(0,1,"Car_Stop_Times: ",8);
- OLED_ShowNum(103,1,Stop_Times,3,8);
- OLED_ShowString(0,2,"Coin_Number: ",8);
- OLED_ShowNum(103,2,Coin_Number,3,8);
- }
- else if(Second > 90)//九十秒停車(chē),其實(shí)上面可以把&&Second <= 90刪掉,我是怕師傅要看有90s停下的程序
- {
- Speed_A = 0;
- Speed_B = 0;
- Stop();
- OLED_ShowString(0,0,"Car_Drive_Times:",8);
- OLED_ShowNum(103,0,Second,3,8);
- OLED_ShowString(0,1,"Car_Stop_Times: ",8);
- OLED_ShowNum(103,1,Stop_Times,3,8);
- OLED_ShowString(0,2,"Coin_Number: ",8);
- OLED_ShowNum(103,2,Coin_Number,3,8);
- }
- }
-
- }
復(fù)制代碼
Keil代碼下載:
Keil代碼.7z
(44.7 KB, 下載次數(shù): 7)
2023-5-27 16:05 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|