📄 zhuansukongzhi.c
字号:
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD P1
void delayms(void); /*延迟函数*/
uchar key_scan();
static Count=0;
static cnt=0;
static truev=0;
static v=30; /*v为电机设定速度,n为pwm占空比 */
static n;
sbit L298_EN=P2^0;
sbit L298_IN1=P2^1;
sbit L298_IN2=P2^2;
sbit LCD_RS=P3^7; /*定义1602液晶模块接口*/
sbit LCD_RW=P3^6;
sbit LCD_E=P3^5; /*LCD使能端*/
bit flag;
void delay(uint ms) /*延时1ms*/
{
uchar i;
while(ms--)
for(i=0;i<250;i++)
{
_nop_();
_nop_();
}
}
void SELECT_LCD(void) /*选中LCD*/
{
LCD_E=1;
_nop_();
_nop_();
_nop_();
}
void NOSELECT_LCD(void) /*释放LCD*/
{
LCD_E=0;
_nop_();
_nop_();
_nop_();
}
bit LCD_Busy() /*检测LCD的忙碌状态,在对LCD操作的时候必须检测忙碌状态,忙时对其操作无效*/
{
bit result;
LCD_RS = 1; /*设置LCD为忙碌状态的检测*/
LCD_RW = 1;
SELECT_LCD(); /*选中LCD*/
_nop_(); /*待稳定*/
_nop_();
_nop_();
_nop_();
result = (bit)(LCD & 0x80); /*读LCD的DB7的值 1为忙 0为空闲,可写入指令和数据*/
NOSELECT_LCD();
return result; /*返回result,1为忙 0为空闲*/
}
LCD_WriteCommand(uchar com) /*写指令数据到LCD*/
{
while(LCD_Busy()); /*当LCD不忙时,写指令数据*/
NOSELECT_LCD();
LCD_RS = 0; /*设置LCD为写指令数据*/
LCD_RW = 0;
_nop_();
_nop_();
LCD = com; /*把指令数据送到P1口*/
_nop_();
_nop_();
_nop_();
_nop_();
SELECT_LCD(); /*选中LCD*/
_nop_();
_nop_();
_nop_();
_nop_();
NOSELECT_LCD(); /*释放LCD*/
}
LCD_DisplayPosit(uchar pos) /*设定字符在LCD上显示的位置*/
{
LCD_WriteCommand(pos|0x80);
delay(1);
}
LCD_WriteData(uchar dat) /*写字符显示数据到LCD*/
{
while(LCD_Busy()); /*当LCD不忙时,写显示数据*/
LCD_RS = 1; /*设置LCD为写显示数据*/
LCD_RW = 0;
NOSELECT_LCD();
LCD = dat; /*把显示数据送到P1口*/
_nop_();
_nop_();
_nop_();
_nop_();
SELECT_LCD(); /*选中LCD*/
_nop_();
_nop_();
_nop_();
_nop_();
NOSELECT_LCD(); /*释放LCD*/
}
LCD_Init() /*LCD初始化*/
{
LCD_WriteCommand(0x38); /*16*2显示,5*7点阵,8位数据*/
delay(1);
LCD_WriteCommand(0x0c); /*显示开,关光标*/
delay(1);
LCD_WriteCommand(0x06); /*移动光标*/
delay(1);
LCD_WriteCommand(0x01); /*清除LCD的显示内容*/
delay(1);
}
/*显示单个字符*/
void show(uchar n,uchar a) /*n表示显示位置,a为要显示的字符*/
{
LCD_DisplayPosit(n);
LCD_WriteData(a);
}
uchar key_scan()
{ uchar Counter=0;
uchar Counte=0;
uchar key=20;
while(key==20)
{
P0=0xf0;
Counte=P0;
if(Counte==0xe0)
{delay(2);
if(Counte==0xe0) Counter=1;
}
if(Counte==0xd0)
{delay(2);
if(Counte==0xd0) Counter=2;
}
if(Counte==0xb0)
{delay(2);
if(Counte==0xb0) Counter=3;
}
if(Counte==0x70)
{delay(2);
if(Counte==0x70) Counter=4;
}
P0=0x0f;
Counte=P0;
if(Counte==0x0e)
{delay(2);
if(Counte==0x0e) Counte=1;
}
if(Counte==0x0d)
{delay(2);
if(Counte==0x0d) Counte=2;
}
if(Counte==0x0b)
{delay(2);
if(Counte==0x0b) Counte=3;
}
if(Counte==0x07)
{delay(2);
if(Counte==0x07) Counte=4;
}
if(Counter==1&&Counte==1) key=0;
if(Counter==2&&Counte==1) key=1;
if(Counter==3&&Counte==1) key=2;
if(Counter==4&&Counte==1) key=3;
if(Counter==1&&Counte==2) key=4;
if(Counter==2&&Counte==2) key=5;
if(Counter==3&&Counte==2) key=6;
if(Counter==4&&Counte==2) key=7;
if(Counter==1&&Counte==3) key=8;
if(Counter==2&&Counte==3) key=9;
if(Counter==3&&Counte==3) key=10;
if(Counter==4&&Counte==3) key=11;
if(Counter==1&&Counte==4) key=12;
if(Counter==2&&Counte==4) key=13;
if(Counter==3&&Counte==4) key=14;
if(Counter==4&&Counte==4) key=15;
}
P0=0x0f;
Counte=P0;
while(Counte!=0x0f)
{ P0=0x0f;
Counte=P0;
}
return key;
}
void Timer1() interrupt 3
{
TH1=(65636-50000)/256;
TL1=(65636-50000)%256;
Count++;
if(Count>=20)
{ Count=0;
truev=cnt;
cnt=0;
}
}
void Timer0() interrupt 1
{ if(flag==1)
{ TH0=(65636-(100-n)*50)/256;
TL0=(65636-(100-n)*50)%256;
L298_IN1=0;
}
else
{
TH0=(65636-n*50)/256;
TL0=(65636-n*50)%256;
L298_IN1=1;
}
flag=~flag;
}
void Int1() interrupt 2
{
cnt++;
}
void Int0() interrupt 0
{ uchar s,a,b,p,q;
uchar m=v;
uchar k=0;
EX0=0;
s=key_scan();
if (s==12)
{
LCD_WriteCommand(0x01); /*清除LCD的显示内容*/
delay(10);
show(0,'P');
show(1,'u');
show(2,'t');
show(3,' ');
show(4,'i');
show(5,'n');
show(6,' ');
show(7,'v');
show(8,':');
s=key_scan();
while(s!=15)
{
if(s==14)
{k=0; break;}
if((s>=0)&(s<=9)&(k==0))
{ a=s;
k++;
show(9,(a+48));
}
s=key_scan();
if(s==14)
{k=0; break;}
if(s==15) break;
if((s>=0)&(s<=9)&(k==1))
{ b=s;
k++;
show(10,(b+48));
}
s=key_scan();
}
if(k==1) v=a;
if(k==2) v=a*10+b;
if((v<10)|(v>40))
{ v=m;
LCD_WriteCommand(0x01); /*清除LCD的显示内容*/
delay(10);
show(4,'E');
show(5,'R');
show(6,'R');
show(7,'O');
show(8,'R');
delay(200);
}
p=truev;
q=v;
LCD_WriteCommand(0x01);
delay(10);
show(0,'S');
show(1,'e');
show(2,'l');
show(3,'e');
show(4,'c');
show(5,'t');
show(6,' ');
show(7,'V');
show(8,'=');
show(9,(q/10)+48);
show(10,(q%10)+48);
show(11,'r');
show(12,'/');
show(13,'s');
show(0xc0,'T');
show(0xc1,'r');
show(0xc2,'u');
show(0xc3,'e');
show(0xc4,' ');
show(0xc5,' ');
show(0xc6,' ');
show(0xc7,'V');
show(0xc8,'=');
show(0xc9,(p/10)+48);
show(0xca,(p%10)+48);
show(0xcb,'r');
show(0xcc,'/');
show(0xcd,'s');
}
}
void main(void)
{ uchar temp1=0; /*temp1变量保存速度返回值 */
uchar temp2=0; /*temp2变量保存路程返回值*/
P0=0x0f;
n=40;
flag=1;
IE=0x8f;
IP=0x0e;
TCON=0x55;
TMOD=0x11;
TH0=(65636-n*50)/256;
TL0=(65636-n*50)%256;
TH1=(65636-50000)/256;
TL1=(65636-50000)%256;
LCD_Init();
while(1)
{ uchar i;
EX0=1;
L298_IN2=0;
LCD_WriteCommand(0x01);
delay(10);
show(0,'S');
show(1,'e');
show(2,'l');
show(3,'e');
show(4,'c');
show(5,'t');
show(6,' ');
show(7,'V');
show(8,'=');
show(11,'r');
show(12,'/');
show(13,'s');
show(0xc0,'T');
show(0xc1,'r');
show(0xc2,'u');
show(0xc3,'e');
show(0xc4,' ');
show(0xc5,' ');
show(0xc6,' ');
show(0xc7,'V');
show(0xc8,'=');
show(0xcb,'r');
show(0xcc,'/');
show(0xcd,'s');
for(i=0;i<5;i++)
{ P0=0x0f;
temp1=truev;
temp2=v;
if(temp2==0) L298_EN=0;
if(temp2>0) L298_EN=1;
if(n>0&n<100)
{
if((temp1>temp2+1)&(n>1)) n--; /*现实速度大于设定速度+1,占空比减小*/
if((temp1<temp2-1)&(n<99)) n++; /*现实速度小于设定速度-1,占空比增加*/
}
show(9,(temp2/10)+48);
show(10,(temp2%10)+48);
show(0xc9,(temp1/10)+48);
show(0xca,(temp1%10)+48);
delay(600);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -