cmicrocontroller8051

am I missing something in C code? programming 8051 MCUs


I am currently learning C, so I tried to program my AT89S52 MCU with programmer.

Compiling with sdcc works, and upload is also successful. but when I tried to make blink program, it is not doing the thing I expected. I made my own header file with blink function. Its purpose is to blink LED on P1.0 with given delay. but it looks like the delay does not work and it is really fast switching P1.0 on and off.

Here is basicIO.h

#include <8052.h>

unsigned int i = 0;
void delay(int dl)
{
    for(i=0;i<=dl;i++) { //repeat 1ms delay x times
        TMOD = 0x01;    // Timer0 mode1
        TH0 = 0xFC;      //initial value for 1ms
        TL0 = 0x66;
        TR0 = 1;      // timer start
        while(TF0==0);  // check overflow condition
        TR0 = 0;     // Stop Timer
        TF0 = 0;    // Clear flag
    }
    
}
void on(void) {
P1_0 = 0x00;
}
void off(void) {
P1_0 = 0xFF;
}
void blink(int valdl) {
    P1_0 = 0x00;
    delay(valdl);
    P1_0 = 0xFF;
}

and here is srccode.c:

#include <8052.h>
#include "basicIO.h"
void main(void)
{
    off();
    while(1)
    { 
        blink(100);
        blink(100);
    }
}

There is no error when running the on, off or delay functions, so what is happening with blink?


Solution

  • You have all the functions but your main loop should be

    while(1)
    {   on()
        delay(100);
        off();
        delay(100);
    }
    

    or use

    void blink(int valdl) 
    {
        P1_0 = 0x00;
        delay(valdl);
        P1_0 = 0xFF;
        delay(valdl);
    }