Huangwenfeng 发表于 2013-4-20 22:58:21

急急!!新手,写了个51的led流水灯程序在仿真中不能实现

#include "reg51.h"
#include "intrins.h"

unsigned char i=0x01;
//unsigned char j=0x01;
void initTimer(void)
{
TMOD=0x01;
TH0=0xd8;
TL0=0xf0;
}

void timer0(void) interrupt 1
{
TH0=0xd8;
TL0=0xf0;
//add your code here.

}

void int0(void) interrupt 0
{
IE0=0;

}


void main(void)
{
while(1)
{
      //0b0000_0001
    P0 = ~i;
    if(i == 0x80)//0b1000_0000
    i = 0x01;
    else
    i = i << 1;        //0b0000_0010
        //i --;       

/*P3 = ~j;
    if(j == 0x80)//0b1000_0000
    j = 0x01;
    else
    j =j << 1;        //0b0000_0010
   */
}




IT0=1;
EX0=1;
initTimer();
TR0=1;
ET0=1;
EA=1;





}

hqsdz1 发表于 2013-4-21 00:45:56

记得当初我写流水灯的时候很正常的啊,就是那种带尾巴的

ap0705307 发表于 2013-4-21 01:30:42

新手更加不能急...

jjj206 发表于 2013-4-21 01:32:51

仿真不过,不一定就不行的,仿真过了,也不一定行的,仿真之作参考,真正要调程序还是找块芯片调吧

阿莫小子~ 发表于 2013-4-21 01:38:58

你把while{}后面的东西放在前面,现在好像是你的程序直接进到while循环里,在这之前没开定时器(也是学习者,班门弄斧,有错还望大家指点)

阿莫小子~ 发表于 2013-4-21 01:44:46

不好意思,你好像没用定时器,每次状态改变都没有延时。这样的话即使闪烁,闪烁频率也太高了,看不出来效果得。可以用定时器计数进行延时,这样的话就应该可以看出闪烁效果了

shangdawei 发表于 2013-4-21 05:44:33

本帖最后由 shangdawei 于 2013-4-21 05:50 编辑

#include "reg51.h"
#include "intrins.h"

unsigned char i = 0x01;
//unsigned char j=0x01;
void initTimer( void )
{
TMOD = 0x01;
TH0 = 0xd8;
TL0 = 0xf0;
}

void timer0( void )
interrupt 1
{
TH0=0xd8;
TL0=0xf0;
//add your code here.
}

void int0( void )
interrupt 0
{
IE0=0;
}

void main( void )
{
<font color="#ff0000">while ( 1 )</font>
{
    P0 = ~i;          //0b0000_0001
    if ( i == 0x80 )//0b1000_0000
      i = 0x01;
    else
      i = i << 1;   //0b0000_0010
      //i --;      

    /*P3 = ~j;
   if(j == 0x80)    //0b1000_0000
   j = 0x01;
   else
   j =j << 1;       //0b0000_0010
   */
}

<font color="#ff0000">// 从这里开始的这些语句从未被执行的, 从而LED状态变化没有延时</font>
<font color="#ff0000">IT0 = 1;
EX0 = 1;
initTimer( );
TR0 = 1;
ET0 = 1;
EA = 1;</font>
}

shangdawei 发表于 2013-4-21 05:52:48

代码无法着色显示 ...{:sweat:}

Huangwenfeng 发表于 2013-4-21 06:19:51

shangdawei 发表于 2013-4-21 05:44 static/image/common/back.gif


但是按照你这个来的话编译出错啊

shangdawei 发表于 2013-4-21 06:29:08

Huangwenfeng 发表于 2013-4-21 06:19 static/image/common/back.gif
但是按照你这个来的话编译出错啊

本来就是引用你的代码而已, 同时想测试一下代码着色功能

Huangwenfeng 发表于 2013-4-21 07:07:16

shangdawei 发表于 2013-4-21 06:29 static/image/common/back.gif
本来就是引用你的代码而已, 同时想测试一下代码着色功能

哦哦,谢谢啦!能帮忙修改下不!嘻嘻

wang80891 发表于 2013-4-21 07:07:35

主要是没有延时,单步仿真应该能看出效果

shangdawei 发表于 2013-4-21 08:43:31

Huangwenfeng 发表于 2013-4-21 07:07 static/image/common/back.gif
哦哦,谢谢啦!能帮忙修改下不!嘻嘻

参考这个代码/* BLINKY.C - LED Flasher for the Keil MCBx51 Evaluation Board with 80C51 device*/

#include <REG8252.H>


// When you have enabled the option Stop Program Execution with Serial
// Interrupt, the Monitor-51 uses the serial interrupt of the UART.
// It is therefore required to reserve the memory locations for the interrupt
// vector.You can do this by adding one of the following code lines:

// char code reserve _at_ 0x23;   // when using on-chip UART for communication
// char code reserve _at_0x3;   // when using off-chip UART for communication

void wait (void){                   /* wait function */
;                                 /* only to delay for LED flashes */
}

void main (void){
unsigned int i;                     /* Delay var */
unsigned char j;                  /* LED var */

while (1) {                         /* Loop forever */
    for (j=0x01; j< 0x80; j<<=1){   /* Blink LED 0, 1, 2, 3, 4, 5, 6 */
      P1 = j;
                            /* Output to LED Port */
      for (i = 0; i < 10000; i++){/* Delay for 10000 Counts */
       wait ();                     /* call wait function */
      }
    }

    for (j=0x80; j> 0x01; j>>=1){   /* Blink LED 6, 5, 4, 3, 2, 1 */
      P1 = j;                         /* Output to LED Port */
      for (i = 0; i < 10000; i++){/* Delay for 10000 Counts */
       wait ();                     /* call wait function */
      }
    }
}
}

Huangwenfeng 发表于 2013-4-21 20:43:11

呵呵。。谢谢各位的帮助,我已经解决这个问题啦!{:tongue:}
页: [1]
查看完整版本: 急急!!新手,写了个51的led流水灯程序在仿真中不能实现