?? smartcar_9.c
字號:
#include<reg51.h>
#define temp P0 //光電管接入口
#define middleline 0xe7 //光電檢測中間線
#define startline 0x24 //起跑線
#define outline 0xff //檢測不到黑線
unsigned char num0=0; //計數初值
unsigned char round=14; //舵機參數初值,設置為中間位置
unsigned char speedup=8; //電機參數初值,設置為最大速度
unsigned char num1=0; //計數初值
sbit servopwm=P2^0; //舵機PWM輸出口
sbit motorpwm1=P2^6; //電機PWM1輸出口
sbit motorpwm2=P2^7; //電機PWM2輸出口
void init() //中斷初始化
{
TMOD=0x11; //定時器方式1
EA=1; //開總中斷
ET0=1; //允許定時器0中斷
ET1=1; //允許定時器1中斷
TH0=(65536-100)/256; //裝入初值,高八位取模,100在定時器中12M下表示100us
TL0=(65536-100)%256; //裝入初值,低八位取余
TH1=(65536-50)/256;
TL1=(65536-50)%256;
TR0=1; //啟動中斷
TR1=1; //啟動中斷
servopwm=1; //舵機初始為高電平
motorpwm1=0; //IN1初始為低電平
motorpwm2=1; //IN2初始為高電平
}
void timer0() interrupt 1 //定時器0方式1
{
TH0=(65536-100)/256; //讀初值
TL0=(65536-100)%256;
num0++;
if(num0==round) //高電平持續時間為round
servopwm=0;
if(num0==200) //舵機周期為10ms
{
servopwm=1;
num0=0;
}
}
void timer1() interrupt 3 //定時器1方式3
{
TH1=(65536-50)/256;
TL1=(65536-50)%256;
num1++;
if(num1==speedup)
{
motorpwm1=1;
motorpwm2=0;
}
if(num1==100) //電機周期為1ms
{
motorpwm1=0;
motorpwm2=1;
num1=0;
}
}
void main() //主函數
{
init(); //初始化
while(1)
{
if(temp==0xe7) //檢測為中間線
{
round=14;
speedup=8;
}
else
{
switch(temp)
{
case 0xf7:{round=11;speedup=12;};break; //P0:11110111
case 0xf3:{round=11;speedup=16;};break; //P0:11110011
case 0xfb:{round=10;speedup=20;};break; //P0:11111011
case 0xf9:{round=9;speedup=24;};break; //P0:11111001
case 0xfd:{round=8;speedup=28;};break; //P0:11111101
case 0xfc:{round=7;speedup=36;};break; //P0:11111100
case 0xfe:{round=6;speedup=55;};break; //P0:11111110
case 0x7f:{round=19;speedup=55;};break; //P0:01111111
case 0x3f:{round=17;speedup=36;};break; //P0:00111111
case 0xbf:{round=16;speedup=28;};break; //P0:10111111
case 0x9f:{round=15;speedup=24;};break; //P0:10011111
case 0xdf:{round=14;speedup=20;};break; //P0:11011111
case 0xcf:{round=13;speedup=16;};break; //P0:11001111
case 0xef:{round=13;speedup=12;};break; //P0:11101111
default: break;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -