|
這是主程序,按鍵程序和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);
} //電機停止轉動
}
|
|