PWM波的频率用数码管显示的问题
时间:10-02
整理:3721RD
点击:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char dspbuf[] = {16,16,16,16,16,16,16,16};
code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,~0x40};
float fosc=12000000;
uchar qian,bai,shi,ge;
uchar dspcom = 0;
bit k1=0;
sbit key1=P3^1;
sbit key2=P3^2;
sbit key3=P3^3;
sbit OUT=P1^6;
sbit OUT1=P1^7;
void seg_display();
void display(uchar X);
uchar zkb;
uint freq;
uchar TIMER0_L,TIMER0_H,TIMER1_L,TIMER1_H; //定时器0和1的定时初值;
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
void seg_display()
{
P2 = 0xe0;
P0 = 0xff; //消隐
P2= 0x00;
P2 = 0xc0;
P0 = 1 << dspcom;
P2 = 0x00;
P2 = 0xe0;
P0 = tab[dspbuf[dspcom]];
P2 = 0x00;
if (++dspcom == 8)
dspcom = 0;
}
void display(uchar X)
{
bai=X/100;
shi=X%100/10;
ge=X%10;
dspbuf[0]=bai;
dspbuf[1]=shi;
dspbuf[2]=ge;
}
void init()
{
freq=50; //频率
zkb=50; //占空比
TIMER0_L=0xE0;
TIMER0_H=0XB1;
TIMER1_L=0XF0;
TIMER1_H=0XD8;
TL0=0XE0;
TH1=0XD8;
TMOD=0X11;
IT0=1;
EX0=1;
ET0=1;
ET1=1;
EA=1;
TR0=1;
TR1=1;
}
void main()
{
init();
seg_display();
while(1)
{
if(k1==1)
{
display(freq);
k1=0;
}
}
}
void Timer0_freq() interrupt 1
{
TR1=1;
TL0=TIMER0_L;
TH0=TIMER0_H;
OUT=1;
OUT1=1;
}
void Timer1_zkb() interrupt 3
{
TR1=0;
TL1=TIMER1_L;
TH1=TIMER1_H;
OUT=0;
OUT1=0;
}
void keyscan() interrupt 0 using 1
{
float TF0,TZ1;
EX0=0;
if(key1==0)
{
Delay10ms();
if(key1==0)
{
while(!key1);
freq+=100;
if(freq>500) freq=50;
k1=1;
}
}
if(key2==0)
{
Delay10ms();
if(key2==0)
{
while(!key2);
zkb++;
if(zkb>100) zkb=1;
}
}
if(key3==0)
{
Delay10ms();
if(key3==0)
{
while(!key3);
zkb--;
if(zkb<1) zkb=50;
}
}
TF0=(65536-fosc/(12.0*freq));
TZ1=(65536-(fosc*zkb)/(12.0*100*freq));
TIMER0_H=(uint)TF0/256;
TIMER0_L=(uint)TF0%256;
TIMER1_H=(uint)TZ1/256;
TIMER1_L=(uint)TZ1%256;
EX0=1;
TR0=1;
TR1=1;
}
在这个程序里我实现了两路输出波形,但是最后 数码管上显示频率出问题了, 数码管上什么也没有,而且我按键加频率,频率也没反应。请问大佬,这是什么原因?
#define uchar unsigned char
#define uint unsigned int
unsigned char dspbuf[] = {16,16,16,16,16,16,16,16};
code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,~0x40};
float fosc=12000000;
uchar qian,bai,shi,ge;
uchar dspcom = 0;
bit k1=0;
sbit key1=P3^1;
sbit key2=P3^2;
sbit key3=P3^3;
sbit OUT=P1^6;
sbit OUT1=P1^7;
void seg_display();
void display(uchar X);
uchar zkb;
uint freq;
uchar TIMER0_L,TIMER0_H,TIMER1_L,TIMER1_H; //定时器0和1的定时初值;
void Delay10ms() //@12.000MHz
{
unsigned char i, j;
i = 117;
j = 184;
do
{
while (--j);
} while (--i);
}
void seg_display()
{
P2 = 0xe0;
P0 = 0xff; //消隐
P2= 0x00;
P2 = 0xc0;
P0 = 1 << dspcom;
P2 = 0x00;
P2 = 0xe0;
P0 = tab[dspbuf[dspcom]];
P2 = 0x00;
if (++dspcom == 8)
dspcom = 0;
}
void display(uchar X)
{
bai=X/100;
shi=X%100/10;
ge=X%10;
dspbuf[0]=bai;
dspbuf[1]=shi;
dspbuf[2]=ge;
}
void init()
{
freq=50; //频率
zkb=50; //占空比
TIMER0_L=0xE0;
TIMER0_H=0XB1;
TIMER1_L=0XF0;
TIMER1_H=0XD8;
TL0=0XE0;
TH1=0XD8;
TMOD=0X11;
IT0=1;
EX0=1;
ET0=1;
ET1=1;
EA=1;
TR0=1;
TR1=1;
}
void main()
{
init();
seg_display();
while(1)
{
if(k1==1)
{
display(freq);
k1=0;
}
}
}
void Timer0_freq() interrupt 1
{
TR1=1;
TL0=TIMER0_L;
TH0=TIMER0_H;
OUT=1;
OUT1=1;
}
void Timer1_zkb() interrupt 3
{
TR1=0;
TL1=TIMER1_L;
TH1=TIMER1_H;
OUT=0;
OUT1=0;
}
void keyscan() interrupt 0 using 1
{
float TF0,TZ1;
EX0=0;
if(key1==0)
{
Delay10ms();
if(key1==0)
{
while(!key1);
freq+=100;
if(freq>500) freq=50;
k1=1;
}
}
if(key2==0)
{
Delay10ms();
if(key2==0)
{
while(!key2);
zkb++;
if(zkb>100) zkb=1;
}
}
if(key3==0)
{
Delay10ms();
if(key3==0)
{
while(!key3);
zkb--;
if(zkb<1) zkb=50;
}
}
TF0=(65536-fosc/(12.0*freq));
TZ1=(65536-(fosc*zkb)/(12.0*100*freq));
TIMER0_H=(uint)TF0/256;
TIMER0_L=(uint)TF0%256;
TIMER1_H=(uint)TZ1/256;
TIMER1_L=(uint)TZ1%256;
EX0=1;
TR0=1;
TR1=1;
}
在这个程序里我实现了两路输出波形,但是最后 数码管上显示频率出问题了, 数码管上什么也没有,而且我按键加频率,频率也没反应。请问大佬,这是什么原因?
那你就先让它显示了啊
我写了让它显示的程序, 但是板子上不显示.
你需要好好理一理思路,整理一下程序和程序所要实现功能的基本知识。看你的程序数码管应该是动态扫描方式,的确是有了显示程序void seg_display(),但没看到什么地方有定时的调用这个程序,这个程序也只是在主程序的while()前调用了一次。
其他程序比较乱,尤其是键盘扫描程序,似乎是键盘扫描和键盘的功能处理混在了一起。
我键盘是用的外部中断写的
先解决显示的问题吧,没看到实现动态扫描显示的功能
兄弟, 我就是解决不了才来问的呀
兄弟, 我就是解决不了才来问的呀