lcd显示屏上显示的转速和电机下面显示的数字不符,他们有什么关系
程序如下
- #include <AT89X52.h>
- #include <stdio.h>
- #include <string.h>
- #define uint unsigned int
- #define uchar unsigned char //宏定义
- sbit zheng=P1^0; //电机正转控制键
- sbit fan=P1^1; //电机反转控制键
- sbit add=P1^2; //电机加速
- sbit dec=P1^3; //电机减速
- sbit stop=P1^4; //电机停止
- sbit PWMZ=P2^0; //电机正转PWM信号
- sbit PWMF=P2^7; //电机反转PWM信号
- bit run=0; //电机运行标志位,=1时电机运行,=0电机停止
- bit Z=0,F=0; //电机正反标志位
- unsigned char PWM=0xAF; //开机时速度为中间值
- //unsigned char code suduzhi[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF}; //速度等级数值
- void delay(unsigned char n)
- {
- unsigned char i,j;
- for(j=0;j<n;j++)
- for(i=0;i<125;i++);
- }
- /*****主函数*****/
- void main(void)
- {
- TMOD=0x21;
- TH0=0xFF;
- TL0=0x00;
- TH1=PWM;
- TL1=PWM;
- EA=1;
- ET0=1;
- ET1=1;
- EX0=1;
- IT0=1;
- PT0=1; //定时中断优先
- PT1=1;
- PX0=0;
- PWMZ=0; //开机时,电机不转动
- PWMF=0;
- while(1)
- {
-
- }
- }
- void int0(void) interrupt 0
- {
- EX0=0; //关外部中断0
- if(zheng==0)
- {
- TR0=1;
- Z=1;
- F=0;
- PWMF=0;
- }
- if(fan==0)
- {
- TR0=1;
- F=1;
- Z=0;
- PWMZ=0;
- }
- if(add==0)
- {
- delay(10);
- if(add==0)
- {
- if(PWM<0xFF) //定时初值加,脉宽加,速度加
- PWM=PWM+8;
- else
- PWM=0xFF;
- }
- while(add==0);
- }
- if(dec==0) //定时初值减,脉宽减,速度减
- {
- delay(10);
- if(dec==0)
- {
- if(PWM>0x1F)
- PWM=PWM-8;
- else
- PWM=0x1F;
- }
- while(dec==0);
- }
- if(stop==0)
- {
- TR0=0;
- TR1=0;
- PWMZ=0;
- PWMF=0;
- }
- EX0=1;
- }
- void time0(void) interrupt 1
- {
- TR0=0;
- TR1=1;
- TH0=0xFF;
- TL0=0x00;
- TH1=PWM;
- TL1=PWM;
- TR1=1;
- PWMZ=0;
- PWMF=0;
- TR0=1;
- }
复制代码
|