/*
硬件:stc89c52
软件:keil c
创建日期:2012.12.24 平安夜
最后修改:2012.12.24 平安夜
作者 :Mr Lee
P2^0、P2^1连接L298N的IN3和IN4
电机的两端接L298N的OUT3和OUT4
无线模块的D0-D3、VT接P1.0-P1.4
*/
#include
#define DISTIME_DELAY 100
#define LZ_WIRELESS_DEBUG 0
#define LZ_PWM_DEBUG 0
#define PWM_LENGTH 100
#define PWM_WIDTH 50
sbit motor1=P2^0;
sbit direction=P2^1;
char width=5;
char flag=0;
char flag_send=0;
void timer0_init(void)
{
//TMOD=0x01;//选定时器0,工作方式1,16位计数模式
TMOD|=0x01;
TH0=(65535-DISTIME_DELAY)/255;//设置初始值
TL0=(65535-DISTIME_DELAY)%255;
EA=1;//打开总中断
ET0=1;//打开定时器0中断
TR0=1;//定时器零开始计数
}
void uart_init()
{
TMOD|=0x20; //TMOD=0
TH1=0xf3; //12MHZ ,BPS:4800,N,8,1,0xf3=243
TL1=0xf3;
TR1=1;
PCON=0x80; //SMOD=1,方式一,8位数据位,一位起始位和一位结束位
//
SCON=0x50; //串口通信控制寄存器 模式一
EA=1;
// ES=1; //打开串口中断
//REN=1;
}
void uart_putchar(unsigned char dat)
{
SBUF=dat; //把数据送给sbuf缓存器中
while(TI!=1);//发送标志位 TI如果发送了为1,没发送为0,没发送等待,到了退出循环
TI=0; //到了,TI清为0
}
void uart_printf(unsigned char *buff)
{
while(*buff)
uart_putchar(*buff++);
}
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i
{
for(j=0;j<121;j++)
{;}
}
}
void printf_width()
{
uart_printf("width is ");
uart_putchar((width/100+'0'));
uart_putchar((width%100/10+'0'));
uart_putchar((width%10+'0'));
uart_printf("\r\n");
delay(20);
}
void SC2272_RECV()
{
char in_buff;
//无线模块VT位有效
if((P1&0x10)==0x10)
{
in_buff=(P1&0x0f);
if(flag_send==1)
{
flag_send=0;
switch(in_buff)
{
case 1:
{
uart_printf("key B\r\n");
if(direction==1)
{
width+=1;
}
else if(direction==0)
{
width-=1;
}
printf_width();
}break;
case 2:
{
uart_printf("key D\r\n");direction=1;
printf_width();
}break;
case 4:
{
uart_printf("key A\r\n");
if(direction==1)
{
width-=1;
}
else if(direction==0)
{
width+=1;
}
printf_width();
}break;
case 8:
{
uart_printf("key C\r\n");direction=0;
printf_width();
}break;
default:
{
}break;
}
#if LZ_WIRELESS_DEBUG==1
uart_putchar(in_buff);
uart_printf("\r\n");
#endif
}
}
if(direction==1)
width=width;
if(width<0)
width=0;
if(width>100)
width=100;
if(flag_send==1)
{
flag_send=0;
printf_width();
}
}
void main()
{
unsigned char flag_calc=0;
timer0_init();
uart_init();
while(1)
{
SC2272_RECV();
#if LZ_PWM_DEBUG==1
if(motor1==1)
uart_printf("1\r\n");
else if(motor1==0)
uart_printf("0\r\n");
#endif
}
}
void timer0() interrupt 1
{
//TH0=(65535-DISTIME_DELAY)/255;//设置初始值
//TL0=(65535-DISTIME_DELAY)%255;
TH0=0XFF;
TL0=0X9B;
flag++;
if(flag>PWM_LENGTH)
{
flag=0;
flag_send=1;
}
if((flag>width)||(flag>PWM_LENGTH))
motor1=0;
else
motor1=1;
}
阅读(1191) | 评论(0) | 转发(0) |