单片机编程:假设晶振频率为11.0592Mhz,试编写一程序,在P1.7引脚输出周期为2s的方波

单片机编程:假设晶振频率为11.0592Mhz,试编写一程序,在P1.7引脚输出周期为2s的方波 求大神指点,给出程序 (80x81)

精确定时,只要你的晶振够准确零误差

(256-56)×144×32=921600=11059200÷12


汇编:

CNT0    EQU    20H
CNT1    EQU    21H
     
    ORG    0000H
    AJMP    MAIN
    ORG    000BH 
    DJNZ    CNT0,ENDT0
    MOV    CNT0,#144
    DJNZ    CNT1,ENDT0
    MOV    CNT1,#32
    CPL    P1.7
ENDT0: RETI    
     
MAIN:
    MOV    TMOD,#02H
    MOV    TH0,#56
    MOV    TL0,#56
    SETB    ET0
    SETB    EA
    SETB    TR0
SLEEP:
    MOV    PCON,#01
    NOP
    SJMP    SLEEP
END

用C语言

//f=11.0592MHz
sbit  pout=P1^7;
unsigned char cnt0=144,cnt1=32;
void main()
{
   Init();
    while(1)
    {
         PCON=0x01; //休眠
    }
}
void Init()
{
    TMOD=0x2;  //方式2:8位自装入
    TH0=0x56;  //基础定时
    TL0=0x56;
    TR0=1;
    ET0=1;
    EA=1;
     
}
    
void Timeon() interrupt 1  //定时器
{  
    if(--cnt0==0) 
    {
        cnt0=144; 
        if(--cnt1==0) 
         {
              cnt1=32;
              pout=~pout;
         }  
    }
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-10-20
#include<reg52.h>
sbit squ_out=P1^7 ;
void main()
{
     bit squ_mirror;
     unsigned int a,b;
     EA =0;
     while(1){
          for(a=106;a;a--)for(b=1085;b;b--); 
          squ_out = squ_mirror = ~ squ_mirror;
     }
}

周期小了一点点,大约少了57微秒左右。

如果要很准,你的晶振也真的很准,就用下面这个:

#include<reg52.h>
sbit squ_out=P1^7 ;
void main()
{
 bit squ_mirror;
 unsigned int a,b;
 unsigned char c;
 EA =0;
 while(1){
  for(a=106;a;a--)for(b=1085;b;b--); 
  for(c=12;c;c--);
  c++;
  squ_out = squ_mirror = ~ squ_mirror;
 }
}

第2个回答  2013-10-20
#include<reg51.h>

sbit sqr=P1^7;
main()
{
unsigned char time=0;
TMOD=0x01;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
TR0=1;
while(1)
{
if(TF1==1)
{
TF1=0;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
time++;
if(time==20)
{
sqr=~sqr;

time=0;
}
}
}
}
第3个回答  2013-10-21
没有指定编程语言。

(80x81),这是什么意思?
相似回答