Question

I've been doing a project about home automation in which I have to use timer interrupts with 8051 microcontroller. I've constructed the following code, however I couldn't manage to get interrupt working. It seems that the program does not go into timer ISR at all. I use a buton to simulate PIR input, therefore lampControl is triggered, no worries there. I use as a library.

Any ideas or help will be greately appreciated:

void timer0_isr(void) interrupt 1  //Timer 0 Interrupt
{
        TH0 = 0xDC;
        TL0 = 0x00;
        TR0 = 1;
        if (++lamp_interrupt_count == 6000)
        {
            sendCharacterShowAsHex(0x8F); 

            lamp_interrupt_count = 0;                

            TR0 = 0;
        }   
}



void main()
{  
    unsigned char chr;

    IE = 0x93;    
    while(1)
    {
        serialInput();
        if (getPIRInput() == 0x00)
        {
            lampControl(0x80);
        }

        ....
                ....
                ....
}




void lampControl(unsigned char serial_data_in)
{
    if (serial_data_in == 0x80)
    {
        sendCharacterShowAsHex(0x80);

        //enable interrupts
        IE = 0x93;  

        device_interrupt = 2; //Lamp

        TMOD = 0x21; // Timer0 Gate=0, Mode 1, 16bit timer
        TH0 = 0xDC;
        TL0 = 0x00;
        TR0 = 1; 

    }
    else if(serial_data_in == 0x8F)
    {
        sendCharacterShowAsHex(0x8F);
    }
}
Was it helpful?

Solution

You need to configure the timer and interrupts before you can use them. In main() you need at least the following configuration bits set in order to be able to turn the timer on with "TR0 = 1;" :

Set those bits first thing in main() and this should do the trick:

TMOD = 0x01;        // 16-bit no auto reload
TH0 = 0xDC;         //Set high and low bits to count 0xFFFF - 0xDC00 = 0x23FF counts
TL0 = 0x00;
ET0 = 1;            // Enable timer0 interrupt
EA = 1;             // Enable all interrupts
//TR0 = 1;          //Enable Timer0 immediately

The rest of your code should run fine.

Note: you could change your interrupt function definition to: "void timer0_isr(void) interrupt 1 using 1" to force it to use register bank 1 for the interrupt function operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top