Home > other >  Big help to see me this based on at89c51 adjustable frequency and duty ratio of the square wave gene
Big help to see me this based on at89c51 adjustable frequency and duty ratio of the square wave gene

Time:04-21

This is I do of square wave generator, to square wave output 5 k to 25 k, adjustable frequency and duty ratio, but still not achieve them, ask bosses help.

The code is as follows:
#include
# define uchar unsigned char
# define uint unsigned int
Float f=12000000;
P1 sbit KEY1=^ 0;
P1 sbit KEY2=^ 1;
Sbit KEY3=p ^ 2;
P1 sbit KEY4=^ 3;
Sbit OUTPUT=P2 ^ 0;
Uchar ZKB;
Uint freq.
Uchar TIMER0_L TIMER0_H, TIMER1_L TIMER1_H;

Void delay (uchar n)
{
Uchar I;
While (n -)
{the for (I=255; I> 0; I -)
{}
}}

Void SysInit (void)
{
P1=0 x0f;
Freq=5000;
ZKB=50;
TIMER0_L=0 xe0-0xfc;
TIMER0_H=0 xb1;
TIMER1_L=0 xf0;
TIMER1_H=0 xd8;
TL0=0 xe0-0xfc;
TH0=0 xb1;
TL1=0 xf0;
TH1=0 xd8;
TMOD=0 x11;
IT0=1;
EX0=1;
ET0=1;
ET1=1;
EA=1;
TR0=1;
TR1=1;
}

Void main (void)
{
SysInit ();
While (1)
{}
}

Void Timer0_freq () interrupt 1
{
TR1=1;
TL0=TIMER0_L;
TH0=TIMER0_H;
The OUTPUT=1;
}

Void Timer1_zkb () interrupt 3
{
TR1=0;
TL1=TIMER1_L;
TH1=TIMER1_H;
The OUTPUT=0;
}

Void Keyscan () using interrupt 0 1
{
Float TF0 TZ1;
EX0=0;
Delay (1);
If (P1!=0 x0f)
{
If (KEY1==0) freq +=1000;
If (KEY2==0) freq +=100;
If (KEY3==0) ZKB +=10;
If (KEY4==0) ZKB +=1;
If (freq> 25000) freq=5000;
If (zkb> 100) ZKB=10;
TF0=(65536 - f/(12 * freq));
TZ1=(65536 - (f * ZKB)/(12 * 100 * freq));
TIMER0_H=(uint) TF0/256;
TIMER0_L=(uint) TF0%256;
TIMER1_H=(uint) TZ1/256;
TIMER1_L=(uint) TZ1%256;
}
P1=0 x0f;
EX0=1;
TR0=1;
TR1=1;
}

CodePudding user response:

TIMER0_H=(uint) TF0/256;
This casts, my image is, the compiler will not do transformation, but the original variables as you directly to the specified type to use, don't know whether I understand is wrong, if, as I understand it, you won't get the right result,

CodePudding user response:

I'm sorry, if my understanding is wrong, the compiler will transform,
  • Related