PIC16F877a: mental LED blink timing - Page 1
Forum section: electronics / general

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 24, 2020, 04:22:46 pm
content: I have the very simplest circuit imaginable. A PIC16F877a with a 12MHz crystal and one LED connecting port RB0 via a 330Ohm resistor to ground.I run the following programme:Code:#define _XTAL_FREQ 12000000#include <xc.h>// BEGIN CONFIG#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)#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)//END CONFIGint main(){TRISB0 = 0; //RB0 as Output PINwhile(1){RB0 = 1; // LED ON__delay_ms(500); // 1 Second DelayRB0 = 0; // LED OFF__delay_ms(500); // 1 Second Delay}return 0;}and get the LED flash as per trace "500_500" attached. It looked to me that each alternate "on" was longer than the one before. So, I change the timings to:500 on, 1000 offand got trace "500_1000" attached. Though the "off" time has roughly doubled, as expected, there is now a completely mental pattern!Then, for good measure, I made it:2000 on, 500 offand got trace "2000_500". I would have expected the "on" time to quadruple (it did) and the "off" time to be the same. However, the "off" time is less than half what I expected.What on earth is happening? I realise that these delay times are not super accurate and probably not hugely consistent but this is outrageous!Could you please point me in the right direction understanding.Cheers.

author: NorthGuy
author_rank: Super Contributor
timestamp: August 24, 2020, 04:43:46 pm
content: Disable watchdog timer. Or it will reset your PIC periodically and your pattern will restart every time it resets.

author: Ian.M
author_rank: Super Contributor
timestamp: August 24, 2020, 04:56:10 pm
content: Congratulations, Your watchdog timer is running 7% faster than nominal, though it is within spec. (see datasheet Table 17-5, param. 31 & Fig. 18-15). Your lab *may* be a bit cooler than 25 deg C, though I am making too much soup from not enough data.Try warming and cooling the chip, and you should see the period in trace 2000_500 vary.Code:#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)If you don't want a wachdog reset, either don't enable it,or service it regularly withCLRWDT();before it times out!N.B. its period is determined by a crappy internal RC oscillator (see specs in datasheet I noted above) that is divided down by a prescaler shared with timer 0. As you haven't configured Timer 0, the power-on defaults for the OPTION register apply, and the prescaler is assigned to the WDT and the WDT rate is 1:128 of the raw WDT oscillator period.

author: Wilksey
author_rank: Super Contributor
timestamp: August 24, 2020, 05:59:42 pm
content: That watchdog timer is a real bitch, don't forget to kick the pooch!

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 24, 2020, 07:28:59 pm
content: I am such a numpty! I could not for the life of me think what was wrong with such a simple programme!Thank you all very much.I feel like a Mega-noob.

author: Ian.M
author_rank: Super Contributor
timestamp: August 24, 2020, 08:07:04 pm
content: BTDTGTTS

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 24, 2020, 09:08:09 pm
content: I’m gonna get that on a t-shirt.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 25, 2020, 10:08:39 am
content: I decided to investigate the watchdog timer problem.I have fallen at the first hurdle!I've diligently followed a C course (having been advised by many to use C not asembly) and so I get that. However, it wasn't specific to microcontrollers and didn't say how registers are addressed.In assembly this is easy but I can't find out how to do it in C. I looked in the XC8 manual but couldn't even see what section to use! All I want to do is check the value of the TO bit in the STATUS register.Where do I look to find the correct for for this and other registers please?

author: Ian.M
author_rank: Super Contributor
timestamp: August 25, 2020, 11:13:55 am
content: I'm betting that will be something likeSTATUSbits.TOand to confirm that you'll have to dig in the device specific header "pic16f877a.h" for your PIC. I've just looked and the datasheet has the bit as /TO ('overbar capital t capital o'), so the header declares it asnTOand the complete bit reference isSTATUSbits.nTO.Other bits and bitfields that may prove useful to you are:OPTION_REGbits.PSA- the prescaler assignment bitOPTION_REGbits.PS- the group of three prescaler selection bits PS2:PS0HTHIan.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 25, 2020, 04:53:10 pm
content: Brilliant, cheers. It worked a treat.

author: Wilksey
author_rank: Super Contributor
timestamp: August 26, 2020, 05:43:58 pm
content: IIRC you can use XC8's ClrWdt() command to clear the WDT, I usually stick it in the while loop.It is in the XC8 manual somewhere.

author: Ian.M
author_rank: Super Contributor
timestamp: August 26, 2020, 06:24:37 pm
content: Quote from: Wilksey on August 26, 2020, 05:43:58 pmIts C so identifier case matters.If you don't 'recall correctly' and cant be arsed to check the manual before posting, at least review the whole topic,Quote from: Ian.M on August 24, 2020, 04:56:10 pmas I posted that, correctly capitalised (and checked against the user guide) back in reply #2!

author: Wilksey
author_rank: Super Contributor
timestamp: August 27, 2020, 08:30:28 am
content: Quote from: Ian.M on August 26, 2020, 06:24:37 pmChrist, who pissed on your Christmas dinner, calm down sunshine! CLRWDT() for the anal retentives.