#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
/////////////////////////////////////
uchar n='0',num0=0,num1=0;
char zuo_pwm=35,you_pwm=35 ;
sbit zuo1=P0^0;//in1
sbit zuo2=P0^1;//in2
sbit you1=P0^2;
sbit you2=P0^3;
sbit ENA=P0^4;
sbit ENB=P0^5;
//*-*-*-*
/* 函数功能:设置串口*/
void init() //time0
{
TMOD=0x21;
SCON=0x50;
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x91; //设置定时初值
TH0 = 0xFF; //设置定时初值
TH1=0xfd;
TL1=0xfd;
EA=1; //打开总中断
ET0=1; //打开定时器0中断允许
TR1=1; //定时器1开始计时
TR0=1; //定时器0开始计时
ES=1; //打开接收中断
}
//*-*+*-+-+-
//**********************
void qian ()
{
zuo_pwm=17;
you_pwm=17;
zuo1=1;
zuo2=0;
you1=1;
you2=0;
}
void jiansu () //智能灯熄灭
{
zuo_pwm=8;
you_pwm=8;
zuo1=1;
zuo2=0;
you1=1;
you2=0;
// zuo1=0;
// zuo2=0;
// you1=0;
// you2=0;
//zuo_pwm=0;
//you_pwm=0;
}
void jiasu ()
{
zuo_pwm=35;
you_pwm=35;
zuo1=1;
zuo2=0;
you1=1;
you2=0;
}
void hou () //智能灯1/3亮度
{
zuo_pwm=17;
you_pwm=17;
zuo1=0;
zuo2=1;
you1=0;
you2=1;
}
void turnleft () //智能灯2/3亮度
{
zuo_pwm = 13;
you_pwm = 16;
zuo1=1;
zuo2=0;
you1=1;
you2=0;
}
void turnright ()
{
zuo_pwm = 16;
you_pwm = 13;
zuo1=1;
zuo2=0;
you1=1;
you2=0;
}
void stop ()
{
zuo1=0;
zuo2=0;
you1=0;
you2=0;
}
void zhuandong ()
{
zuo_pwm=35;
you_pwm=35;
zuo1=1;
zuo2=0;
you1=0;
you2=1;
}
void zhuandong_jian ()
{
zuo_pwm=12;
you_pwm=12;
zuo1=1;
zuo2=0;
you1=0;
you2=1;
}
void main ()
{
init (); //初始化
while (1)
{
{
switch (n) //蓝牙控制
{
case '2':qian (); break;
case '3':jiansu (); break;
case '4':jiasu (); break;
case '5':hou (); break;
case '6':turnleft (); break;
case '7':turnright (); break;
case '8':zhuandong (); break;
case '9':zhuandong_jian (); break;
case '0':stop (); break;
default:break;
}
}
}
}
//*-*-*-*
/* 函数功能:设置串口*/
void UART_SER()interrupt 4
{
if (RI)
{
RI=0;
n=SBUF;
TI=1;
SBUF=n;
}
if (TI)
TI=0;
}
//定时器0中断函数
void Timer1Interrupt(void) interrupt 1
{
TL0 = 0x91; //设置定时初值
TH0 = 0xFF; //设置定时初值
//PWM调占空比
num0++;
num0++;
num1++;
if(num0<=zuo_pwm)
{ENA=1;}
else
ENA=0;
if(num0==40)
{
ENA=~ENA;
num0=0;
}
if(num1<=you_pwm)
{
ENB=1;
}
else
ENB=0;
if(num1==40)
{ENB=~ENB;num1=0;}
}
|