#pragma config FOSC = INTOSCIO
T1CONbits.TMR1CS=1;//internal instruction cycle clock
T1CONbits.T1OSCEN=1;//LP oscillator is enabled for Timer1 clock
T1CONbits.nT1SYNC=1;//Timer1 set up in Asynchronous mode
T1CONbits.TMR1ON = 1;
void interrupt ISR (void){
TMR1H = 0xFF; //Clear timer1 Registers H&L before enabling timer1
TMR1L = 0x00; //
T1CONbits.TMR1ON = 1;
if (PIR1bits.TMR1IF == 1){//check flag
PIR1bits.TMR1IF = 0;// clear
counter1++;
if (counter1==100){
LED1 =~LED1;
LED2 =~LED2;
counter1=1;
}
}
if (counter1==100){
LED1 =~LED1;
LED2 =~LED2;
counter1=1;
}
#include <xc.h>
#include <stdlib.h>
#include <pic12f683.h>
#define _XTAL_FREQ 8000000
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Detect (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#define LED1 GPIObits.GP4 // Pin No. 3
#define LED2 GPIObits.GP5 // Pin No. 2
#define LED3 GPIObits.GP0 // Pin No. 7
#define LED4 GPIObits.GP1 // Pin No. 6
volatile char counter1=0;
char T1_InterruptTriggered = 0;
char SW_InterruptTriggered = 0;
void T1_ISR(void);
void SW_ISR(void);
void interrupt chk_isr(void)
{
if(PIR1bits.TMR1IF==1)
{
T1_InterruptTriggered = 1;
PIR1bits.TMR1IF = 0;
}
if(INTCONbits.INTF == 1)
{
SW_InterruptTriggered = 1;
INTCONbits.INTF = 0;
}
}
void main (void){
ANSEL = 0x00; // turn off analog inputs
ADCON0 = 0x00; //Turn off ADC
CMCON0 = 0x07; //Turn off Comparators
VRCON = 0x00; //Turn off Voltage Reference
//OSCCON=0b01110111;
PIE1bits.TMR1IE=1;// enable timer1 interrupt
PIR1bits.TMR1IF=0;// clear timer1 overflow flag
INTCONbits.GIE=1; //Enable all global interrupts
INTCONbits.PEIE=1; //Enable all periferal interrupts
T1CONbits.TMR1CS=1;//internal instruction cycle clock
T1CONbits.T1OSCEN=1;//LP oscillator is enabled for Timer1 clock
T1CONbits.nT1SYNC=1;//Timer1 set up in Asynchronous mode
PIE1bits.TMR1IE=1;// enable timer1 interrupt
PIR1bits.TMR1IF=0;// clear timer1 overflow flag
T1CONbits.TMR1ON = 1;
T1CONbits.T1CKPS0=1;//Prescalar assigned to timer1
T1CONbits.T1CKPS1=1;//Prescalar assigned to timer1
INTCONbits.INTE = 1;//enable External interrupt
INTCONbits.INTF = 0;//clear external interrupt flag
IOCbits.IOC2 = 1;//make GP2 as External Interrupt
OPTION_REGbits.nGPPU= 1; //enable internal weak pull up resistors
WPUbits.WPU2 = 1; //pull up enabled on GP2 INPUT
TRISIObits.TRISIO0=0;
TRISIObits.TRISIO1=0;
TRISIObits.TRISIO4=0;
TRISIObits.TRISIO5=0;
LED1=0; // set initial state of pin
LED2=0;
LED3=0;
LED4=0;
if(T1_InterruptTriggered) T1_ISR();
if(SW_InterruptTriggered) SW_ISR();
SLEEP();
while(1){
if(counter1 == 10){
LED1=1;
counter1=1;
}
}
}
void T1_ISR(void)
{
T1_InterruptTriggered = 0;
counter1++;
LED2 = ~LED2;
TMR1H= 0x00;
TMR1L= 0x00;
}
void SW_ISR(void)
{
SW_InterruptTriggered = 0;
LED4 = 1;
}
Go back and look at the code you showed in Post #11True, putting the SLEEP() before checking the variables makes more sense because one of them must be responsible for waking it up but all of that is in the main() loop anyway.
if(T1_InterruptTriggered) T1_ISR();
if(SW_InterruptTriggered) SW_ISR();
SLEEP();
while(1){
if(counter1 == 10){
LED1=1;
counter1=1;
}
}
while(1)
{
SLEEP();
if(T1_InterruptTriggered) T1_ISR();
if(SW_InterruptTriggered) SW_ISR();
if(counter1 == 10)
{
LED1=1;
counter1=1;
}
}
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?