Created
December 27, 2015 02:01
-
-
Save hayview/3884cfba1cb1634f53bd to your computer and use it in GitHub Desktop.
timer0 interrupt setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <p18f4553.h> | |
#include "task_led.h" | |
#include "timer0.h" | |
//****************************************************************************** | |
// CONFIG | |
//****************************************************************************** | |
#pragma config PLLDIV = 5 // (20 MHz crystal on PICDEM FS USB board) | |
#pragma config CPUDIV = OSC1_PLL2 | |
#pragma config USBDIV = 2 // Clock source from 96MHz PLL/2 | |
#pragma config FOSC = HSPLL_HS | |
#pragma config FCMEN = OFF | |
#pragma config IESO = OFF | |
#pragma config PWRT = OFF | |
#pragma config BOR = ON | |
#pragma config BORV = 3 | |
#pragma config VREGEN = ON //USB Voltage Regulator | |
#pragma config WDT = OFF | |
#pragma config WDTPS = 32768 | |
#pragma config MCLRE = ON | |
#pragma config LPT1OSC = OFF | |
#pragma config PBADEN = OFF | |
// #pragma config CCP2MX = ON | |
#pragma config STVREN = ON | |
#pragma config LVP = OFF | |
// #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming | |
#pragma config XINST = OFF // Extended Instruction Set | |
#pragma config CP0 = OFF | |
#pragma config CP1 = OFF | |
// #pragma config CP2 = OFF | |
// #pragma config CP3 = OFF | |
#pragma config CPB = OFF | |
// #pragma config CPD = OFF | |
#pragma config WRT0 = OFF | |
#pragma config WRT1 = OFF | |
// #pragma config WRT2 = OFF | |
// #pragma config WRT3 = OFF | |
#pragma config WRTB = OFF // Boot Block Write Protection | |
#pragma config WRTC = OFF | |
// #pragma config WRTD = OFF | |
#pragma config EBTR0 = OFF | |
#pragma config EBTR1 = OFF | |
// #pragma config EBTR2 = OFF | |
// #pragma config EBTR3 = OFF | |
#pragma config EBTRB = OFF | |
#define GIEL() INTCONbits.GIEL=1; | |
unsigned int counter=0; | |
unsigned char flag_1sec=0; | |
unsigned char flag_2sec=0; | |
unsigned char flag_3sec=0; | |
unsigned char flag_led1=0; | |
unsigned char flag_led2=0; | |
void main() | |
{ | |
//------------------------------------------------------------------------------ | |
// Init PIC periphery | |
//------------------------------------------------------------------------------ | |
RCONbits.IPEN=1; | |
mInitLEDAll(); | |
Timer0_Init(); | |
Timer0_Write(63661); | |
Timer0_Start(); | |
INTCONbits.GIEH=1; | |
while(1) | |
{ | |
if(flag_led1==1) | |
{ | |
flag_led1=0; | |
taskLED1(); | |
} | |
if(flag_led2==1) | |
{ | |
flag_led2=0; | |
taskLED2(); | |
} | |
} | |
} | |
//---------------------------------------------------------------------------- | |
// High priority interrupt vector | |
void InterruptHandlerHigh() ; | |
#pragma code InterruptVectorHigh = 0x08 | |
void InterruptVectorHigh(void) { | |
_asm | |
goto InterruptHandlerHigh //jump to interrupt routine | |
_endasm | |
} | |
//---------------------------------------------------------------------------- | |
// Low priority interrupt routine | |
#pragma code | |
#pragma interrupt InterruptHandlerHigh | |
// | |
// Timer0 overflow | |
void InterruptHandlerHigh() | |
{ | |
if (INTCONbits.TMR0IF) { | |
INTCONbits.TMR0IF = 0; | |
//set timer int on overflow | |
Timer0_Write(63661); | |
counter++; | |
if(counter>=100) | |
{ | |
counter=0; | |
flag_1sec=1; | |
} | |
if(flag_1sec==1) | |
{ | |
flag_1sec=0; | |
flag_2sec++; | |
flag_led1=1; | |
} | |
if(flag_2sec==2) | |
{ | |
flag_2sec=0; | |
flag_led2=1; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "task_led.h" | |
void taskLED1(void) | |
{ | |
mLED_1^=1; | |
} | |
void taskLED2(void) | |
{ | |
mLED_2^=1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TASK_LED_H | |
#define TASK_LED_H | |
#include <p18cxxx.h> | |
#define mInitLED1() {TRISBbits.TRISB6=0;} | |
#define mInitLED2() {TRISBbits.TRISB7=0;} | |
#define mInitLEDAll() {mInitLED1(); mInitLED2();} | |
#define mLED_1 LATBbits.LATB6 | |
#define mLED_2 LATBbits.LATB7 | |
void taskLED1(void); | |
void taskLED2(void); | |
#endif /* TASK_DISPLAY_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "timer0.h" | |
void Timer0_Init(void) | |
{ | |
TMR0H = 0; // Reset Timer0 to 0x0000 | |
TMR0L = 0; | |
INTCONbits.TMR0IF = 0; // Clear Timer0 overflow flag | |
INTCONbits.TMR0IE = 0; | |
/* T0CON values | |
bit 7 TMR0ON: Timer0 On/Off Control bit | |
1 = Enables Timer0 | |
0 = Stops Timer0 | |
bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit | |
1 = Timer0 is configured as an 8-bit timer/counter | |
0 = Timer0 is configured as a 16-bit timer/counter | |
bit 5 T0CS: Timer0 Clock Source Select bit | |
1 = Transition on T0CKI pin | |
0 = Internal instruction cycle clock (CLKO) | |
bit 4 T0SE: Timer0 Source Edge Select bit | |
1 = Increment on high-to-low transition on T0CKI pin | |
0 = Increment on low-to-high transition on T0CKI pin | |
bit 3 PSA: Timer0 Prescaler Assignment bit | |
1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler. | |
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output. | |
bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits | |
111 = 1:256 Prescale value | |
110 = 1:128 Prescale value | |
101 = 1:64 Prescale value | |
100 = 1:32 Prescale value | |
011 = 1:16 Prescale value | |
010 = 1:8 Prescale value | |
001 = 1:4 Prescale value | |
000 = 1:2 Prescale value | |
*/ | |
//set up timer0 see abive | |
T0CONbits.TMR0ON=0; | |
T0CONbits.T08BIT=0; | |
T0CONbits.T0CS=0; | |
T0CONbits.PSA=0; | |
T0CONbits.T0PS=5; | |
INTCON2bits.TMR0IP=1; | |
} | |
void Timer0_Start(void) | |
{ | |
T0CONbits.TMR0ON = 1; // Enable Timer0 | |
INTCONbits.TMR0IE = 1; // Enable Timer0 overflow interrupt | |
} | |
void Timer0_Stop(void) | |
{ | |
T0CONbits.TMR0ON = 0; // Disable Timer0 | |
INTCONbits.TMR0IE = 0; // Disable Timer0 overflow interrupts | |
} | |
void Timer0_Write(unsigned int timer0) | |
{ | |
union Timers timer; | |
timer.lt = timer0; // Copy timer value into union | |
TMR0H = timer.bt[1]; // Write high byte to Timer0 | |
TMR0L = timer.bt[0]; // Write low byte to Timer0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __TIMER_0_H | |
#define __TIMER_0_H | |
#include <p18cxxx.h> | |
/* used to hold 16-bit timer value */ | |
union Timers | |
{ | |
unsigned int lt; | |
char bt[2]; | |
}; | |
void Timer0_Init(void); | |
void Timer0_Start(void); | |
void Timer0_Stop(void); | |
void Timer0_Write(unsigned int timer0); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment