• AVR Freaks

Hot![SOLVED]Cannot write TMR0 register on hardware; works fine in simulator.

Author
kingargon
Starting Member
  • Total Posts : 41
  • Reward points : 0
  • Joined: 2012/06/11 16:40:44
  • Location: 0
  • Status: offline
2015/05/19 18:22:26 (permalink)
0

[SOLVED]Cannot write TMR0 register on hardware; works fine in simulator.

i can write to TMR0 fine when debugging on simulator; but as soon as i burn it to 16f877a i can not change the value of TMR0 at all(initTmr0() ). when i debug with pickit 3, TMR0 randomly changes on every step; On the simulator it increments 2 at a time.
 Did i miss something? code is below.
 
Also, when i look at this on the logic analyzer, the pulse is 200 nanoseconds (on pin RB4) and repeats ever 50uSeconds no matter what i change the TMR0 value to.
 
===================================================================
timers.h
 
//header files for the timersAll main c files
//created by Trent Herring 04/26/15 124a
//set config fuses
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#define Nop() {_asm nop _endasm}

int counter;
//include define to use delay functions __delay
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000
#endif
//define function to handle interrupts
void interrupt isrRoutine(void);
//define function to initilizte timer 0
void initTmr0(void);
//define function to initilizte timer 1
//define function to initilizte timer 2

//enable timer0 interrupts
void tmr0IntEnable(void);
//setup some IO pins
void configIO();
 ==================================================================
TimersAll.c
 
 
//timers.c
// function defintions for all timer C All fumctions
#include "timers.h"

void interrupt isrRoutine(void){

if(TMR0IE && TMR0IF){
RB4 = 1;
RB4 = 0;

TMR0IF = 0;

}
 
GIE = 1; //RE EMABLE GLOBAL INTERRUPT

counter++;
//TMR0 = 0x00;
}

void initTmr0(void){

//
//Sequence below sets up the timer to a 1:1 configuration; this code below heeds the special method to avoid RESET
//
T0CS = 0; //Enable timer mode (increments on ever intruction clock cycle (Tcyc))
PS2 = 0; //prescaler of 1:1
PS1 = 0; //prescaler of 1:1
PS0 = 1; //prescaler of 1:1
TMR0 = 0; //Initialize timer counter value
PSA = 1; // This assigns the pre Scaler to the WDT
CLRWDT(); // RESET THE WATCH DOG TIMER
PSA = 1; // This assigns the pre Scaler to the WDT
PS2 = 0; //prescaler of 1:1
PS1 = 0; //prescaler of 1:1
PS0 = 0; //prescaler of 1:1
//END SEQUENCE
TMR0 = 240;
tmr0IntEnable();
}
void tmr0IntEnable(){
TMR0IE = 1; //enable timer0 interrupts
TMR0IF = 0; //reset the timer 0 interrupt flag
GIE = 1; // if not enabled. enable global interrupts
}
void configIO(){
//INIT THE RB4 PIN
TRISB4 = 0;

}
 
 
========================================================================
Main.c
 
#include "timers.h" //includes the xc.h header file which defines all the pins and such
 
void main(){
counter = 0;
initTmr0();
configIO();
while(1){
 
}
}
 
 
 
 
 
post edited by kingargon - 2015/05/19 21:17:11
#1

11 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 19:10:41 (permalink)
    +1 (1)
    kingargon
    ...when i debug with pickit 3, TMR0 randomly changes on every step;

    I think you are missing the point that the timer keeps running in the real hardware, so of course you see a different value every time you read it.
     

    Also, when i look at this on the logic analyzer, the pulse is 200 nanoseconds (on pin RB4) and repeats ever 50uSeconds no matter what i change the TMR0 value to.
     

    That's another common mistake when starting with timers.
    Writing a value to the timer only sets it once.
    It then keeps incrementing, and generates an interrupt when it wraps back to zero.
    It does NOT reload the previous value you wrote. If you don't write a new value in the ISR, it will just count up from zero again.
    If you want a regular interrupt at a specified rate, Timer-2 is much more useful, as the PR2 register lets you set a reload value that happens automatically.
    It is possible to get a similar effect using Timer-1 and one of the CCP modules in "trigger special event" mode.
     
    Note: Do NOT set the GIE bit inside your ISR. That happens automatically when the RETFIE instruction is executed when the function exits. Setting it early is a bug which will come back to bite you later.




    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    kingargon
    Starting Member
    • Total Posts : 41
    • Reward points : 0
    • Joined: 2012/06/11 16:40:44
    • Location: 0
    • Status: offline
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 19:31:29 (permalink)
    0
    thanks for the response; ill be sure to not include GIE in the isr anymore. As for the TMR0 register, it doesnt matter where i set it, it never changes to the correct value when running on PICKIT3. i did have it in the ISR but i moved it, while trying to debug the vector. I think my chip is toast, maybe? TMR0 just increments to random values no matter what i do.
    #3
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 19:46:37 (permalink)
    +1 (1)
    kingargon
    ... As for the TMR0 register, it doesnt matter where i set it, it never changes to the correct value when running on PICKIT3. i did have it in the ISR but i moved it, while trying to debug the vector. I think my chip is toast, maybe? TMR0 just increments to random values no matter what i do.

    I suspect the problem is on how you are checking it, not in your PIC.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    kingargon
    Starting Member
    • Total Posts : 41
    • Reward points : 0
    • Joined: 2012/06/11 16:40:44
    • Location: 0
    • Status: offline
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 19:55:16 (permalink)
    0
    Im using a watch variable on TMR0 with a break point during and after setting TMR0 to 0xFF and the value always changes to something random on hardware debugger. But in simulator it runs as expected. :( I'm gonna try on 18f458 and see if it works any better.
    #5
    kingargon
    Starting Member
    • Total Posts : 41
    • Reward points : 0
    • Joined: 2012/06/11 16:40:44
    • Location: 0
    • Status: offline
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 20:09:38 (permalink)
    0
    i coded another copy in a new project and it works now. The pulse is still 200 ns but repeats at 7.5 microseconds instead of every 50. i guess its something about how im linking all these files in the project. 


    /*
    * File: main.c
    * Author: Trent
    *
    * Created on May 19, 2015, 6:47 PM
    */
     
    #include <xc.h>
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
     

    void interrupt isrRoutine(void);
    void interrupt isrRoutine(void){

    if(TMR0IE && TMR0IF){
    RB4 = 1;
    RB4 = 0;

    TMR0IF = 0;

    }
     
    //GIE = 1; //RE EMABLE GLOBAL INTERRUPT
    TMR0 = 0xFF;
    //counter++;
    }
    #include <xc.h>
    void main(void) {

    T0CS = 0;
    PSA = 1; // This assigns the pre Scaler to the WDT
    PS2 = 0; //prescaler of 1:1
    PS1 = 0; //prescaler of 1:1
    PS0 = 0; //prescaler of 1:1
    //END SEQUENCE
    TMR0 = 240;
    TMR0IE = 1; //enable timer0 interrupts
    TMR0IF = 0; //reset the timer 0 interrupt flag
    GIE = 1; // if not enabled. enable global interrupts
    TRISB4 = 0;
    while(1){





    }
    }
    post edited by kingargon - 2015/05/19 20:11:06
    #6
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 20:35:51 (permalink)
    +1 (1)
    kingargon
    Im using a watch variable on TMR0 with a break point during and after setting TMR0 to 0xFF and the value always changes to something random on hardware debugger.
    ...

    I don't think you understood me the first time.
    On real hardware, the timer keeps running all the time. You are assuming it stops the moment the breakpoint is hit.
    You are also loading the wrong value into the timer. It counts UP from your value until it wraps to zero.
    If you want it to interrupt in 50 microseconds (assuming a 20MHz crystal), then you need it to count 250 times.
    That means you need to load it with 256 - 250 = 6, not 240.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    kingargon
    Starting Member
    • Total Posts : 41
    • Reward points : 0
    • Joined: 2012/06/11 16:40:44
    • Location: 0
    • Status: offline
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/05/19 21:16:31 (permalink)
    0
    I was confused on the timer operation during debugging but i think i see whats happening now. Thankyou for being patient and explaining!grin: grin
    #8
    GlennP
    Super Member
    • Total Posts : 710
    • Reward points : 0
    • Joined: 2009/03/29 15:04:55
    • Location: El Paso County, CO, USA
    • Status: offline
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/08/05 11:13:42 (permalink)
    0 (1)
    ric
    ..
    If you want it to interrupt in 50 microseconds (assuming a 20MHz crystal), then you need it to count 250 times.
    That means you need to load it with 256 - 250 = 6, not 240.

     
    For many of the PICs (such as the p16lf1823 I'm using now), there is a two instruction delay after loading TMR0.  So ric's calculation would be changed to: 256 - 250 + 2 = 8.
     
    PIC16F1823-Datasheet-RevE-20.1.1
    When TMR0 is written, the increment is inhibited for two instruction cycles immediately following the write.

     
    I can verify that is indeed the case for the '16LF1823.  The datasheet, in a 'Note', mentions compensating for the inhibition.
     
    I seem to remember this note in many other datasheets.  Check the datasheet for the part you are using.
     
    GlennP
    #9
    NKurzman
    A Guy on the Net
    • Total Posts : 17846
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2015/08/05 12:58:09 (permalink)
    0
    Some newer PICs have the option in the debugger to freeze peripherals on break.
    I do not think the PIC16F877A has that option.
    #10
    hersham1980
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/06/26 07:19:32
    • Location: 0
    • Status: offline
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2019/07/15 07:35:28 (permalink)
    -1 (1)
    Dear All
     
    could someone help me please show me the necessary steps for programming the TIMER-0 module of PIC16F877A (using XC8_MPLAB compiler)
    I really do not know how to program Peripheral/External interrupts in conjunction with overflow timer.
    Let us assume for example that the oscillator is 8.0MHz external crystal oscillator, with 32 prescaler.  to generate a time-delay of 1ms. 
     
    Thanks very much in Advance
    Adam
    #11
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Cannot write TMR0 register on hardware; works fine in simulator. 2019/07/15 13:25:54 (permalink)
    +1 (1)
    Please start your own topic, don't hijack someone else's.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5