Not getting O/P:PIC18F4520 project using Timer0 module

Discussion in 'Embedded Systems and Microcontrollers' started by PIYUSH SONI, Nov 22, 2013.

  1. PIYUSH SONI

    Thread Starter Member

    Nov 15, 2013
    32
    1

    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:


    [
    Code ( (Unknown Language)):
    1.  
    2. unsigned cnt;                 // Define variable cnt
    3. void interrupt()
    4.  {
    5.     cnt++;                    // Interrupt causes cnt to be incremented by 1
    6.     TMR0= 96;               // Timer TMR0 is returned its initial value
    7.     T0CON = 0b00100000;            // Bit T0IE is set, bit T0IF is cleared
    8. }
    9.  
    10. void main() {
    11.     T0CON = 0b11000100;        // Prescaler is assigned to timer TMR0
    12.     ADRESL = 0b00000000;                // All I/O pins are configured as digital
    13.     ADRESH = 0b00000000;
    14.     TRISB = 0b00000000;                // All port B pins are configured as outputs
    15.     PORTB = 0b00000000;              // Reset port B
    16.     CMCON =07;
    17.     TMR0 = 96;                // Timer T0 counts from 96 to 255
    18.     T0CON = 0b10100000;            // Enable interrupt TMR0
    19.     cnt = 0;                  // Variable cnt is assigned a 0
    20.  
    21.     do {                      // Endless loop
    22.         if (cnt == 400) {     // Increment port B after 400 interrupts
    23.             PORTB = PORTB++;  // Increment number on port B by 1
    24.             cnt = 0;          // Reset variable cnt
    25.         }
    26.     } while(1);
    27. }
    28.  
    SIZE="5"][/SIZE]
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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: Nov 22, 2013
  3. PIYUSH SONI

    Thread Starter Member

    Nov 15, 2013
    32
    1
    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 .;);)
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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...
     
  5. THE_RB

    AAC Fanatic!

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

    And you should write to LATB to get output on PIC18F series, not to PORTB.
     
  6. PIYUSH SONI

    Thread Starter Member

    Nov 15, 2013
    32
    1
    Thanks RB, i will definitely use LAT instead of PORT.
     
Loading...