Not getting O/P:PIC18F4520 project using Timer0 module

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32

Hi. Guys I'm working with MikroC pro. How z u all, I hope all good. Now i am working on Timer and actually i have done my coding using a pic 18f4520 microcontroller with internal oscillator having freq= 8 MHz.Here it is necessary to provide a delay long enough to notice a change on a port. TMR0 with assigned prescaler is used for this purpose.An interrupt is generated on every timer register overflow and every interrupt routine automatically increments the cnt variables by 1.
Hope u may have got my point, But I'm not getting output. :confused:


[
Rich (BB 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
    T0CON = 0b00100000;            // Bit T0IE is set, bit T0IF is cleared
}

void main() {
    T0CON = 0b11000100;        // Prescaler is assigned to timer TMR0
    ADRESL = 0b00000000;                // All I/O pins are configured as digital
    ADRESH = 0b00000000;
    TRISB = 0b00000000;                // All port B pins are configured as outputs
    PORTB = 0b00000000;              // Reset port B
    CMCON =07;
    TMR0 = 96;                // Timer T0 counts from 96 to 255
    T0CON = 0b10100000;            // 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);
}
SIZE="5"][/SIZE]
 

tshuck

Joined Oct 18, 2012
3,534
You need to turn on interrupts. - see the INTCON register, bit GIE on page 95.

Also, T0CON doesn't have T0IE...look at the register definition on page 123.

Also, also, you have turned off the timer in the ISR...
 
Last edited:

Thread Starter

PIYUSH SONI

Joined Nov 15, 2013
32
Thank x for your suggestion. I got your point & let you know soon.
I think you are talking about T0SE: Timer0 Source Edge Select bit. This bit must be enable to work .;);)
 

tshuck

Joined Oct 18, 2012
3,534
Thank x for your suggestion. I got your point & let you know soon.
I think you are talking about T0SE: Timer0 Source Edge Select bit. This bit must be enable to work .;);)
Your code comments suggest that you think T0IE is in T0CON...

No, no it doesn't...were that what bit 5 in T0CON was. Bit 5 controls the source of the clock for the timer, which you have set to be a counter - it derives its clock from the T0CKI pin. T0SE is bit 4 of T0CON.

You need to reference the datasheet when trying to program a microcontroller...
 

THE_RB

Joined Feb 11, 2008
5,438
"PORTB = PORTB++; // Increment number on port B by 1"

And you should write to LATB to get output on PIC18F series, not to PORTB.
 
Top