Raady Here
Full Member level 5
- Joined
- Jun 8, 2013
- Messages
- 242
- Helped
- 26
- Reputation
- 52
- Reaction score
- 26
- Trophy points
- 28
- Location
- India
- Activity points
- 1,571
void INIT_TIMER(void)
{
RCONbits.IPEN = 1; // Enable priority levels on interrupts
INTCONbits.GIEH = 1; // Enables all high priority interrupts
INTCONbits.GIEL = 0; // Enables all Low priority interrupts
PIR1bits.TMR1IF = 0; // TMR1 register overflow is cleared
PIE1bits.TMR1IE = 1; // Enables overflow interrupt
IPR1bits.TMR1IP = 1; // High priority
T1CONbits.RD16 = 1; // Enables register Read/Write of Timer1 in one 16-bit operation
T1CONbits.T1RUN = 0; // Device clock is derived from other oscillator
T1CONbits.T1CKPS1 = 0;
T1CONbits.T1CKPS0 = 0; // Prescalar 1:1
T1CONbits.T1OSCEN = 1; // Timer 1 osc on.
T1CONbits.TMR1CS = 0; // Internal Clock Fosc /4.
T1CONbits.TMR1ON = 1; // Enables Timer1
TMR1L = 0xD0;
TMR1H = 0x07;
}
#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)
{
_asm
goto high_isr
_endasm
}
#pragma code // return to the default code section
#pragma interrupt high_isr
void high_isr(void)
{
if(PIR1bits.TMR1IF) // TMR1 register overflowed
{
PIR1bits.TMR1IF = 0; //clear interrupt flag
count++;
if(count > 29) // 29 approximated by trail and error
{
count = 0;
flag_clock = 1; // flags for every sec
Clock();
}
}
}
TMR1L = 0xD0;
TMR1H = 0x07;
#include<P18F4520.h>
#include<delays.h>
#include"timer.h"
unsigned char TimerDelay_flag_200us = 0, flag_clock = 0;
unsigned int count = 0;
Time SystemTime;
// High priority interrupt vector
#pragma code high_vector = 0x08
void interrupt_at_high_vector(void)
{
_asm
goto high_isr
_endasm
}
#pragma code // return to the default code section
#pragma interrupt high_isr
void high_isr(void)
{
if(PIR1bits.TMR1IF)
{
TMR1H = 0x07;
TMR1L = 0xD0;
PIR1bits.TMR1IF = 0;
count++;
if(count > 29)
{
count = 0;
flag_clock = 1;
Clock();
}
TimerDelay_flag_200us = 1;
}
}
void INIT_TIMER(void)
{
RCONbits.IPEN = 1; // Enable priority levels on interrupts
INTCONbits.GIEH = 1; // Enables all high priority interrupts
INTCONbits.GIEL = 0; // Enables all Low priority interrupts
PIR1bits.TMR1IF = 0; // TMR1 register overflow is cleared
PIE1bits.TMR1IE = 1; // Enables overflow interrupt
IPR1bits.TMR1IP = 1; // High priority
T1CONbits.RD16 = 1; // Enables register Read/Write of Timer1 in one 16-bit operation
T1CONbits.T1RUN = 0; // Device clock is derived from other oscillator
T1CONbits.T1CKPS1 = 0;
T1CONbits.T1CKPS0 = 0; // Prescalar 1:1
T1CONbits.T1OSCEN = 1; // Timer 1 osc on.
T1CONbits.TMR1CS = 0; // Internal Clock Fosc /4.
T1CONbits.TMR1ON = 1; // Enables Timer1
TMR1H = 0x07;
TMR1L = 0xD0;
}
//----------------------------------------------------------------------------
void TimerDelay_200us(unsigned int delay)
{
TimerDelay_flag_200us = 0;
while(delay)
{
if(TimerDelay_flag_200us == 1)
{
TimerDelay_flag_200us = 0;
delay--;
}
}
}
void Clock(void)
{
if ( SystemTime.Seconds < 59 ) // is cummulative seconds < 59?
{
SystemTime.Seconds++; // yes, so increment seconds
}
else // else seconds => 59
{
SystemTime.Seconds = 0x00; // reset seconds
if ( SystemTime.Minutes < 59 ) // is cummulative minutes < 59?
{
SystemTime.Minutes++; // yes, so updates minutes
}
else // else minutes => 59
{
SystemTime.Minutes = 0x00; // reset minutes
if ( SystemTime.Hours < 23 ) // is cummulative hours < 23
{
SystemTime.Hours++;
}
else
{
SystemTime.Hours = 0x00; // Reset time
}
}
}
}
typedef union _Time
{
unsigned long int Value;
struct
{
unsigned char Seconds;
unsigned char Minutes;
unsigned char Hours;
unsigned char Res; // Reserved
};
} Time;
void INIT_TIMER(void);
void high_isr(void);
void TimerDelay_200us(unsigned int delay);
void Clock(void);
#include<P18F4520.h>
#include<stdio.h>
#include"uart.h"
#include"timer.h"
extern Time SystemTime;
extern unsigned char flag_clock;
void main(void)
{
ADCON0bits.ADON = 0;
INTCON2bits.RBPU = 0;
ADCON1 = 0b00001111;
OSCCON = 0x72;
OSCTUNEbits.PLLEN = 0; // PLL disabled
TRISAbits.TRISA0 = 0;
init_uart();
INIT_TIMER();
SystemTime.Value = 0;
while(1)
{
if(flag_clock == 1)
{
LATAbits.LATA0 = ! LATAbits.LATA0;
printf((const rom far char *) "\n\r%d.%d.%d", SystemTime.Hours, SystemTime.Minutes, SystemTime.Seconds);
flag_clock = 0;
}
}
}
#define _1ms
#ifndef _1ms
#define _count 998
#define _tmrh 0xF8
#define _tmrl 0x2F
#endif
void high_isr(void)
{
if(PIR1bits.TMR1IF)
{
TMR1H = _tmrh;
TMR1L = _tmrl;
PIR1bits.TMR1IF = 0;
count++;
if(count > _count)
{
count = 0;
flag_clock = 1;
Clock();
}
TimerDelay_flag_200us = 1;
}
}
I didnt understand the reason but some others in threads used the value by subtracting from FFFF
so instead of TMR1H = 0x07; and TMR1L = 0xD0;
I have loaded TMR1H = 0xF8 and TMR1L = 0x2F; (FFFF - 07D0 = F82F) // calculations for 1ms timer
and its working with some error, with respect to my project need not be considered. But...
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?