Hi all
I want to be able to create a 1ms delay using timer0 and have tried the below code.
I have used the formula I got from https://proteustutorials.blogspot.com/2018/07/pic-timer-0-programming.html :
TMR0 = 256-(Delay * Fosc)/(Prescalar*4))
so for a 1ms delay, the values:
Delay = 1ms
Fosc = 16MHz
Prescaler = 32
gives a TMR0 value of 131
Looking at the oscilloscope waveform I am getting a pulse width of 2.8us.
Can anyone tell me why this is the case, how do i fix it or has anyone got any code that gives a correct delay please?
I want to be able to create a 1ms delay using timer0 and have tried the below code.
Code:
#include <stdio.h>
#include <pic16f1716.h>
#include <xc.h>
#include <stdlib.h>
#pragma config FOSC = INTOSC
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config CLKOUTEN = ON
void delay();
int main(int argc, char** argv)
{
PORTB = LATB = ANSELB = TRISB = 0;
OSCCONbits.SCS = 0b10;
OSCCONbits.IRCF = 0b1111;
OPTION_REGbits.PSA = 0;
OPTION_REGbits.PS = 0b100;
OPTION_REGbits.nWPUEN = 1;
OPTION_REGbits.TMR0CS = 0;
OPTION_REGbits.TMR0SE = 0;
TMR0 = 131;
INTCONbits.TMR0IE = 1;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
while(1)
{
PORTB = ~PORTB;
delay();
}
}
void delay()
{
if(INTCONbits.TMR0IF == 1)
{
TMR0 = 132;
INTCONbits.TMR0IF = 0;
}
}
I have used the formula I got from https://proteustutorials.blogspot.com/2018/07/pic-timer-0-programming.html :
TMR0 = 256-(Delay * Fosc)/(Prescalar*4))
so for a 1ms delay, the values:
Delay = 1ms
Fosc = 16MHz
Prescaler = 32
gives a TMR0 value of 131
Looking at the oscilloscope waveform I am getting a pulse width of 2.8us.
Can anyone tell me why this is the case, how do i fix it or has anyone got any code that gives a correct delay please?