PIC16F18446 - TImer 0 not counting - Page 1
Forum section: electronics / general

author: injb
author_rank: Regular Contributor
timestamp: November 27, 2018, 02:17:50 am
content: Hi, I'm trying to get a really simple example of Timer 0 running in the MPLAB simulator. I've read the timer section of the datasheet thoroughly, and I've looked at samples from around the web. I didn't find any code examples for my exact device (16F18446) but I think I have the gist of how it should be set up, I can't figure out what I'm doing wrong. The TMR0L and TMR0H registers just will not change. I'm watching them with the debugger and the low byte is 0x00, the high one 0xFF. They just stay there forever.All I've got is this (I had some interrupt stuff in there, but apparently I was being too ambitious, so I commented all that out in case it was somehow interfering):Code:void main(void) {//TRISA = 0x00;//TRISB = 0x00;//TRISC = 0x00;//PORTA = 0x00;//PORTB = 0x00;//PORTC = 0x00;//ANSELA = 0x00;//INTCONbits.GIE = 1; //global interrupt enable//INTCONbits.PEIE = 1; //peripheral interrupt enable//PIE0bits.TMR0IE = 1; //timer0 interrupt enableT0CON0bits.T016BIT = 1; //timer0 8/16 bit modeT0CON1bits.T0CS = 2;T0CON1bits.T0ASYNC = 0;T0CON1bits.T0CKPS = 0;T0CON0bits.T0OUTPS = 1;T0CON0bits.T0EN = 1; //timer0 enabledwhile (1) {__nop();}return;}The above settings are listed in that exact order in the datasheet under "Timer0 Example":QuoteI've spent hours on this and I can't figure it out. In desperation I created a new project and tried using the code configurator. Their code didn't seem to be doing anything special, and the registers didn't change when I ran that code either, but I didn't spend much time on that.Any ideas? Thanks!

author: JPortici
author_rank: Super Contributor
timestamp: November 27, 2018, 06:26:50 am
content: In the future, check which features are implemented with the simulator!http://ww1.microchip.com/downloads/en/DeviceDoc/SimPeripheralSupport.pdfin your case TMR0 is supported so it should work...have you by any chance forgot to set the appropriate OSC source and frequency in the simulator settings?

author: injb
author_rank: Regular Contributor
timestamp: November 27, 2018, 01:41:40 pm
content: Quote from: JPortici on November 27, 2018, 06:26:50 amThanks. I wasn't aware of that spreadsheet. I would hope that if the simulator doesn't support something, it would tell me instead of failing silently. It's a good thing you gave me that link anyway because it looks like it only supports timers 0 and 1.I had a look at the oscillator section of the simulator settings, but I didn't change anything. I don't remember what was selected now but nothing jumped out as being obviously wrong. I'll take another look later.

author: FireBird
author_rank: Regular Contributor
timestamp: November 27, 2018, 02:41:25 pm
content: Did you try async mode (T0CON1bits.T0ASYNC = 1;)? I'm not sure if the timer is working if the clock source is the internal clock and clock synchronization is active.

author: injb
author_rank: Regular Contributor
timestamp: November 27, 2018, 02:55:25 pm
content: Quote from: FireBird on November 27, 2018, 02:41:25 pmYes, I tried that. I also tried 8 and 16 bit modes, and all 7 clock source options. None of them made any difference. Also, the current combination of options is straight from the datasheet.Once I realized that my interrupts weren't happening, I put a breakpoint in the while loop (that's why I put __nop() in there) and started monitoring the low/high timer0 bytes in the variable watch window. They never change, whether I step through one line at a time, or let it run for a while. For a sanity check, I put in a counter variable and I can see that changing, so I know the variable watch feature works the way I expect.

author: injb
author_rank: Regular Contributor
timestamp: November 28, 2018, 12:11:59 am
content: Update...someone on the Microchip forum confirmed that they had the same issue. I finally got around to trying it on the real device, and the timer seems to be working fine. So it looks like the simulator just doesn't work. Thanks for the input everyone!

author: NorthGuy
author_rank: Super Contributor
timestamp: November 28, 2018, 01:46:51 am
content: It is always better to work with real hardware. By not using a simulator you're eliminating another source of bugs.