谁有单片机驱动直流电机的PWM程序啊?给我一些资料呗,非常感谢!

要C程序,注解要详细一些
2024-12-23 13:25:15
推荐回答(1个)
回答1:

利用延时函数输出pwm
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
sbit dianji=P1^7;
sbit jiakey=P3^5;
sbit jiankey=P3^7;
sbit ting=P3^4;
uchar num,show_num,gao_num=1,di_num=5;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x76,0x79,0x38,0x3f,0
};
void delay(uint xms)
{
uint x,z;
for(x=xms;x>0;x--)
for(z=125;z>0;z--);
}
void display()
{
dula=1;
P0=table[show_num]; //速度千位
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[0];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[0];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[0];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
}
void key()
{
if(jiakey==0)
{
delay(5);
if(jiakey==0)
{

num++;
if(num==6)
num=5;
while(jiakey==0);
}
}
if(jiankey==0)
{
delay(5);
if(jiankey==0)
{
if(num!=0)
num--;
else
num=0;
while(jiankey==0);
}
}
if(ting==0)
{
delay(3);
if(ting==0)
{
num=6;
}
}
}
void dispose()
{
switch(num)
{
case 0:
show_num=1;
gao_num=1;
di_num=5;
break;
case 1:
show_num=2;
gao_num=2;
di_num=4;
break;
case 2:
show_num=3;
gao_num=3;
di_num=3;
break;
case 3:
show_num=4;
gao_num=4;
di_num=2;
break;
case 4:
show_num=5;
gao_num=5;
di_num=1;
break;
case 5:
show_num=6;
gao_num=6;
di_num=0;
break;
case 6:
show_num=6;
gao_num=0 ;
di_num=0;
break;
}
}
void qudong()
{
uchar i;
if(di_num!=0)
{
for(i=0;i {
dianji=0;
display();
}
}
for(i=0;i {
dianji=1;
display();
}
}
void main()
{
while(1)
{
dianji=0;
key();
dispose();
qudong();
}
}