Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Small code sequence - problem understanding how works (led's)

Status
Not open for further replies.

alinpion

New Member
Hello

I'm a beginner in mcu programming and I have a small problem understanding how the following code works:

Code:
unsigned cnt;                 // Define variable cnt

void interrupt() {
    cnt++;                    // Interrupt causes cnt to be incremented by 1
    TMR0 = 96;                // Timer TMR0 is returned its initial value
    INTCON = 0x20;            // Bit T0IE is set, bit T0IF is cleared
}

void main() {
    OPTION_REG = 0x84;        // Prescaler is assigned to timer TMR0
    ANSEL = 0;                // All I/O pins are configured as digital
    ANSELH = 0;
    TRISB = 0;                // All port B pins are configured as outputs
    PORTB = 0x0;              // Reset port B
    TMR0 = 96;                // Timer T0 counts from 96 to 255
    INTCON = 0xA0;            // Enable interrupt TMR0
    cnt = 0;                  // Variable cnt is assigned a 0
    
    do {                      // Endless loop
        if (cnt == 400) {     // Increment port B after 400 interrupts
            PORTB = PORTB++;  // Increment number on port B by 1
            cnt = 0;          // Reset variable cnt
        }
    } while(1);
}

It's from mikroe and it's exlained but what I don't understand it's how the cnt is incremented up to 400 if he is assigned to 0 2 lines before and in the do loop the interrupt function isn't call ??
 
Some guys from mikroe say that I don't need to call the interrupt function because it is called internally but still I don't understand how 400 interrupts can occur in such short time for the condition if(cnt==400) to be true .

Your line (if(cnt++==400)) makes a lot more sense but then what is the purpose of the interrupt function? From what I've read the main idea of this code is to count interrupts and after a condition is met portb is incremented with one.
 
The cnt is incremented in the timer interrupt. Only change I would do is to declare that variable "volatile".

You could also do all the work in the interrupt like this:
Code:
void interrupt() {
    cnt++;                    // Interrupt causes cnt to be incremented by 1

    if (cnt == 400) {     // Increment port B after 400 interrupts
        PORTB = PORTB++;  // Increment number on port B by 1
        cnt = 0;          // Reset variable cnt
    }

    TMR0 = 96;                // Timer TMR0 is returned its initial value
    INTCON = 0x20;            // Bit T0IE is set, bit T0IF is cleared
}
 
Last edited:
OK now I understand how the interrupt function is called.

Another question is how can 400 interrupts occur so fast ? And if 400 of interrupts can occur so fast what are the ones that occur the most?
 
Another question is how can 400 interrupts occur so fast ? And if 400 of interrupts can occur so fast what are the ones that occur the most?

I don't quite understand what you mean. The timer interrupt is used to get more accurate control how fast the cnt is incremented. It can be incremented very slowly or very fast. In the original code the cnt variable is compared over and over again if it is equal to 400.. and when it is, the variable is reset and PORTB is incremented.
 
I don't quite understand what you mean. The timer interrupt is used to get more accurate control how fast the cnt is incremented. It can be incremented very slowly or very fast. In the original code the cnt variable is compared over and over again if it is equal to 400.. and when it is, the variable is reset and PORTB is incremented.

What I don't understand is what type of interrupt can occur for 400 times ?

One of the interrupt definition is this:"Interrupts, as the name suggests interrupts the normal execution and Requests and urgent attention of CPU. Interrupts are situations that the CPU can't predict when they will happen, they can happen any time, so the CPU does not wait for them. So the CPU keeps on doing its normal job unless and interrupt occurs." Based on this definition the interrupts can't occur so fast if we don't have something special build to do this. That's why I don't understand how for this example we can have such a lot of interrupts.
 
I think you are thinking some concepts wrong here, but I don't know what. Let me try to explain what is happening in the original code.

First a timer/counter is initialized to generate an interrupt when it reaches 255 (or when it overflows). Timer/counter is a module that keeps on counting in the background while the processor is free to do other stuff.
Code:
    TMR0 = 96;                // Timer T0 counts from 96 to 255
    INTCON = 0xA0;            // Enable interrupt TMR0


In the interrupt we increment the cnt variable and reset the timer/counter module to start counting again.. that way we will keep getting the interrupts at constant rate and the cnt will get incremented at this rate
Code:
void interrupt() {
    cnt++;                    // Interrupt causes cnt to be incremented by 1
    TMR0 = 96;                // Timer TMR0 is returned its initial value
    INTCON = 0x20;            // Bit T0IE is set, bit T0IF is cleared
}


In the main-function, there is an infinite loop. In the loop there is an "if" statement where we check whether the cnt is equal to 400. If cnt is not equal we skip the bracketed code.. but because we are in an infinite loop the code execution goes back to the comparison.. so this way the comparison is repeated over and over again. Meanwhile the cnt variable keeps on incrementing and at some point it reaches the value 400. The comparison will then be true and the code inside the if statement will get executed, cnt will be set back to 0 and PORTB is incremented.
Code:
do {                      // Endless loop
        if (cnt == 400) {     // Increment port B after 400 interrupts
            PORTB = PORTB++;  // Increment number on port B by 1
            cnt = 0;          // Reset variable cnt
        }
    } while(1);
 
I think you are thinking some concepts wrong here, but I don't know what. Let me try to explain what is happening in the original code.

First a timer/counter is initialized to generate an interrupt when it reaches 255 (or when it overflows).

That was the part that I didn't understand ;Now it's all clear Thank you !!!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top