想通过调节滑动变阻器阻值改变数码管显示电压的数值,同时改变直流电机占空比,实现调速。不知道为什么上电之后红灯不亮;按下停止键之后红灯依然不亮;电机转动时黄灯闪烁没有规律。求大佬帮帮忙。
单片机程序如下:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define YS 1000
uchar code zixingma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code weixuan[]={0x10,0x20,0x40,0x80};
sbit pwm=P2^0;
sbit P21=P2^1;
sbit ST=P3^0;
sbit OE=P3^1;
sbit EOC=P3^2;
sbit ALE=P3^3;
sbit P07=P0^7;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;
sbit key4=P3^7;
void key_scan(void);
void caiji(void);
void qidong();
void tingzhi();
uchar flag,k,n;
void t_0() interrupt 1
{
TH0=(65536-500)/256;
TL0=(65536-500)%256;
k++;
}
void delay5ms(void)
{
unsigned char i,j;
for(i=10;i>0;i--)
for(j=248;j>0;j--);
}
void delay(uchar t)
{
while(t--);
}
void disp(uint num)
{
uchar i,zheng,dian1,dian2,dian3;
zheng=num/1000;
dian1=num%1000/100;
dian2=num%100/10;
dian3=num%100%10;
for(i=0;i<4;i++)
{
P1=0xff;
switch(i)
{
case 0:P2=weixuan[ i];P0=zixingma[zheng];P07=0;break;
case 1:P2=weixuan[ i];P0=zixingma[dian1];break;
case 2:P2=weixuan[ i];P0=zixingma[dian2];break;
case 3:P2=weixuan[ i];P0=zixingma[dian3];break;
}
delay(200);
}
}
uint ADC0809(void)
{
uint n;
OE=0;
ST=0;
ALE=0;
ST=1;
ALE=1;
ST=0;
ALE=0;
delay(10);
while(!EOC);
OE=1;
n=P1;
OE=0;
return n;
}
void main()
{
k=0;
flag=0;
P21=1;
TMOD=0x01;
TH0=(65536-500)/256;
TL0=(65536-500)%256;
EA=1;
ET0=1;
TR0=1;
pwm=0;
while(1)
{
key_scan();
switch(flag)
{
case 0:disp(ADC0809()*0);break;
case 1:caiji();disp(ADC0809()*19.6078);break;
case 2:qidong();break;
case 3:tingzhi();break;
}
}
}
void key_scan()
{
if(key1==0)
{
delay5ms();
if(key1==0)
{
flag=1;
while(key1==1)
delay5ms();
}
}
if(key2==0)
{
delay5ms();
if(key2==0)
{
flag=0;
while(key2==1)
delay5ms();
}
}
if(key3==0)
{
delay5ms();
if(key3==0)
{
flag=2;
while(key3==1)
delay5ms();
}
}
if(key4==0)
{
delay5ms();
if(key4==0)
{
flag=3;
while(key4==1)
delay5ms();
}
}
}
void caiji()
{
uint ADC0809(void);
}
void qidong()
{
disp(ADC0809()*19.6078);
pwm=1;
P21=0;
n=ADC0809()*20;
if(k>100)
k=0;
if(k<n)
{
pwm=0;
}
else
{
pwm=1;
}
}
void tingzhi()
{
pwm=0;
P21=1;
disp(ADC0809()*0);
}
|