|
这是主程序,按键程序和1602显示程序应该都没有错误。有proteus仿真,1602速度显示一直是0000
#include<reg51.h>
#include "key.h"
#include "1602.c"
uint i,z,cou,zhuan,msec; //定义参数
unsigned char count=0;
/*-------------------外部中断1计数程序-------------------*/
void counter(void) interrupt 2
{
cou++;
if(cou==30) //30次循环为发动机转一圈
{
cou=0; //初始化计数
z++; //转圈计数加1
}
}
/*-------------------------------------------------------*/
/*-----------------内部中断计时计数程序-----------------*/
void Timer_0(void) interrupt 3
{
TH1=0x3c; //50ms定时
TL1=0xb0;
msec++;
if(msec==20) //50*20=1S
{
msec=0;
zhuan=60*z; //每分钟转速
z=0;
}
}
/*-------------------------------------------------------*/
void Timer_Init()
{
TMOD=0X11; //T0定时方式1
TH1=0X3c;
TL1=0Xb0;
IT1= 1;
EX1= 1;
TH0=0Xfc;
TL0=0X66; //计数初值设置为1ms
ET0=1; //打开定时器0的中断
TR0=1; //打开定时器0
EA=1; //开总中断
}
void main()
{
in1=0;
in2=1;
ena=1;
Timer_Init();
InitLcd1602();
while(1)
{
key();
LcdShowStr(0, 0, "Speed= r/min");
LcdShowDat(6, 0,4, zhuan);
}
}
void Timer() interrupt 1 //特别注意此处,0--外部中断0,1--定时器中断0,2--外部中断1,3--定时器中断1,4--串行口中断1
{
TR0=0;
TH0=0Xfc;
TL0=0X66; //重新赋计数初值为1ms
if(count<=PWM_Count)
{
ena=1;
}
else
{
ena=0;
}
count++;
if(count>=100)
{
count=0;
}
TR0=1;
}
1602显示程序
#define uchar unsigned char
#define uint unsigned int
#define LCD1602_DB P0//数据端,可改为任意Px口
void LcdWriteCmd(uchar cmd);
/* 1602控制端,根据硬件自改IO口 */
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P2^2;
/* 延时函数 x ms */
void delay(uint x)
{
unsigned int i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
/* Lcd1602初始化 */
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);//清屏指令
}
/* LCD等待函数 */
void LcdWaitReady()
{
uchar sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do {
LCD1602_E = 1;
sta = LCD1602_DB;
LCD1602_E = 0;
} while (sta & 0x80);
}
/* LCD写命令函数 */
void LcdWriteCmd(uchar cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* LCD写数据函数 */
void LcdWriteDat(uint dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* LCD数据位置函数 */
void LcdSetCursor(uchar x, uchar y)
{
uchar addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LcdWriteCmd(addr | 0x80);
}
/*
LCDx写字符函数
变量:x, y :(x,y)为第一个字符坐标
例:第2排第3个字符之后显示lcd
LcdShowStr(1,2,"lcd");
*/
void LcdShowStr(uchar x, uchar y, uchar *str)
{
LcdSetCursor(x, y);
while (*str != '\0')
{
LcdWriteDat(*str++);
}
}
/*
LCD写数字函数
变量:x, y :(x,y)为第一个字符坐标
z:显示数字位数
例:第2排第3个字符之后显示1666只显示2位
LcdShowStr(1,2,1666,2);
*/
void LcdShowDat(uchar x, uchar y,uchar z, uint dat)
{
LcdSetCursor(x, y);
if(z==4)
{ LcdWriteDat(0x30+dat/1000);
LcdWriteDat(0x30+dat/100%10);
LcdWriteDat(0x30+dat/10%10);
LcdWriteDat(0x30+dat%10);}
if(z==3)
{
LcdWriteDat(0x30+dat/100);
LcdWriteDat(0x30+dat/10%10);
LcdWriteDat(0x30+dat%10);}
if(z==2){
LcdWriteDat(0x30+dat/10%10);
LcdWriteDat(0x30+dat%10);}
}
按键程序
#ifndef key_h
#define key_h
sbit key_1=P1^0; //顺时针转动
sbit key_2=P1^1; //逆时针转动
sbit key_3=P1^2; // 加速转动
sbit key_4=P1^3; //减速转动
sbit key_5=P1^4; //停止转动
unsigned char PWM_Count=20; //占空比控制字
unsigned char n=5; //速度增减量5
sbit in1=P2^7;
sbit in2=P2^6;
sbit ena=P2^5;
void delays()
{
unsigned char i;
for(i=80;i>0;i--);
}
void key()
{
P1=0XFF;
if(key_1==0)
{
delays();
if(key_1==0)
{
in1=0;
in2=1;
} //顺时针转动
while(!key_1);
}
if(key_2==0)
{
delays();
if(key_2==0)
{
in1=1;
in2=0;
}
while(!key_2);
} //逆时针转动
if(key_3==0)
{
delays();
if(key_3==0)
{
PWM_Count=PWM_Count+n;
if(PWM_Count>=100)
PWM_Count=100;
}
while(!key_3);
} //加速转动 ,如果大于等于100就全速转动
if(key_4==0)
{
delays();
if(key_4==0)
{
if(PWM_Count>10)
{
PWM_Count=PWM_Count-n;
}
else
PWM_Count=5; //减速转动,最低转速为5
}
while(!key_4);
}
if(key_5==0)
{
delays();
if(key_5==0)
{
in1=0;
in2=0;
}
while(!key_5);
} //电机停止转动
}
|
|