Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f84a timer0 delay!

Status
Not open for further replies.

meche

Junior Member level 2
Joined
Apr 12, 2017
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
Please I want a C code (XC8 if possible) for timer0 delay of 1 hour for 8 bit mcu such as pic16f84a. I have tried all I can but cant make headway
 

Hi,

show your code, show what and why you have done so far, show your calculations.

Klaus
 

My attempted code is given below: This is supposed to create a delay as shown below:
Code:
 * File:   newmain.c
 * Author: meche1
 *
 * Created on September 13, 2017, 9:55 AM
 */

#define _XTAL_FREQ 8000000
#include "terrawood_alarm_2.h"

void Timer_ISR();
void Initialize_Timer()
void Reset_Timer_Variables();

void main(void) 
{
    int milisecond=0, second=0, minute=0, hour=0;
    Initialize_Timer();
    TRISB0=1;
    TRISB4=0;
    TRISB5=0;
    while(RB0==0)
    {
        RB5=0;
        RB4=1;
        while(minute<=2)
            Reset_Timer_Variables();
        RB5=0;
        RB4=0;
        while(minute<=15)
            Reset_Timer_Variables();
    }
    if(RB0==1)
    {
        RB5=1;
        RB4=1;
        while(minute<=2)
            Reset_Timer_Variables();
        RB5=0;
        RB4=0;
        while(hour<=2);
        Reset_Timer_Variables();
    }
}

void Initialize_Timer()
{
 OPTION_REG=(1<<2);
 TMR0=100;
 TMR0IE=1;
 GIE=1;
}

void Reset_Timer_Variables()
{
 milisecond=0;
 second=0;
 minute=0;
 hour=0;
}

void Timer_ISR()
{
    milisecond++;
    if(milisecond==1000)
    {
        second++;
        milisecond=0;
    }
    if(second==60)
    {
        minute++;
        second=0;
    }
    if(minute==60)
    {
        hour++;
        minute=0;
    }
}
 

hello,


if you enter in timer0 interrupt
you must RAZ TMR0IF bit ....
and reinit the counter value

// MikroC code .. to adapt for XC8 rule

Code:
// 1ms at 8Mhz
void InitTimer0(){
  OPTION_REG	 = 0x82;
  TMR0		 = 6;
  INTCON	 = 0xA0;
}



void Interrupt(){
  if (TMR0IF_bit){ 

    milisecond++;
    if(milisecond==1000)
    {
        second++;
        milisecond=0;
    }
    if(second==60)
    {
        minute++;
        second=0;
    }
    if(minute==60)
    {
        hour++;
        minute=0;
    }
  } 
   TMR0		 = 6;
  TMR0IF_bit	 = 0;
}
 

...for timer0 delay of 1 hour for 8 bit mcu such as pic16f84a

The PIC16 Timer0 is not suited to trigger interrupts for long term counting, once it is has only 8bits lenght, unless you can select a crystal which yields a precise timming, instead you can rather consider using Timer1 or even an external source.

- - - Updated - - -

At this website you can make experiments with several settings on Timer1, and the one which gave a precise fraction of seconds ( actually 0.25s ) were:


Code C - [expand]
1
2
3
4
5
6
7
8
9
//Timer1 Registers Prescaler= 8 - TMR1 Preset = 3036 - Freq = 4.00 Hz - Period = 0.250000 seconds
T1CON.T1CKPS1 = 1;   // bits 5-4  Prescaler Rate Select bits
T1CON.T1CKPS0 = 1;   // bit 4
T1CON.T1OSCEN = 1;   // bit 3 Timer1 Oscillator Enable Control bit 1 = on
T1CON.T1SYNC = 1;    // bit 2 Timer1 External Clock Input Synchronization Control bit...1 = Do not synchronize external clock input
T1CON.TMR1CS = 0;    // bit 1 Timer1 Clock Source Select bit...0 = Internal clock (FOSC/4)
T1CON.TMR1ON = 1;    // bit 0 enables timer
TMR1H = 11;             // preset for timer1 MSB register
TMR1L = 220;             // preset for timer1 LSB register

 

Thanks for the code on timer1 interrupt delay. I actually changed my compiler on the MPLAB X IDE to Hitech C and the delay loops actually worked but not very accurate.

Please I need the timer1 based interrupt delay inserted into the code below to take the place of the delay loop so as to ease the load on the processor.
the code is given below:

Code:
#define _XTAL_FREQ 8000000
#include<htc.h>

void main(void) 
{
    TRISB0=1;
    TRISB4=0;
    TRISB5=0;
    while(1)
    {
        while(RB0==1)
        {
        RB5=1;
        RB4=1;
        for(int x=0; x<=47;x++)//delay for 2 minutes
        __delay_ms(2000);
        RB5=0;
        RB4=0;
        for(int x=0; x<=2760; x++)//delay for 2 hours
        __delay_ms(2000);
        
        if(RB0==0)
        {
            RB5=0;
            RB4=1;
            for(int x=0; x<=47; x++)//delay for 2 mins
            __delay_ms(2000);
            RB5=0;
            RB4=0;
            for(int x=0; x<=120; x++)//delay for approx. 5 mins
            __delay_ms(2000);
        }
    
    }   
 }
}
 

After 10 days...

Have you ever seen the link above?
Or rather, did you read the above reply?
 

PIC 16F84A does not have timer1 peripherical, just timer0. In order to get one hour is better to implement delays decrementing a variable and then, multiply these short delay with nested loops. Maybe you will run out of memory with the 16F84A
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top