⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smartcar_9.c

📁 此程序为飞思卡尔智能车校内选拔赛主控为STC51单片机的初始程序
💻 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 + -