Hi,
I have been working on this issue for a week and cannot seem to get anything to work as expected. I assume I am missing a step. I want to get two LEDs flashing at different rates using two timers. I found this tutorial which was very helpful to get one LED to work. However when I tried to modify it to work with two timers both LEDs flash at the same rate and that should not be the cases since I played around with the speeds and the rates were still the same. Could anyone point out what I am doing wrong for some reason I just cannot understand timers in PIC compared to the other microcontrollers I have used.
Also if there is a more efficient way of doing this that requires less code that would be great if someone could show me that way.
Thanks
I am using the 18F4550 in MPLABX with XC8
config.h
main.c
I have been working on this issue for a week and cannot seem to get anything to work as expected. I assume I am missing a step. I want to get two LEDs flashing at different rates using two timers. I found this tutorial which was very helpful to get one LED to work. However when I tried to modify it to work with two timers both LEDs flash at the same rate and that should not be the cases since I played around with the speeds and the rates were still the same. Could anyone point out what I am doing wrong for some reason I just cannot understand timers in PIC compared to the other microcontrollers I have used.
Also if there is a more efficient way of doing this that requires less code that would be great if someone could show me that way.
Thanks
I am using the 18F4550 in MPLABX with XC8
config.h
Rich (BB code):
#ifndef CONFIG_H
#define CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_H */
#include <xc.h>
//#pragma config CONFIG1L = 0x0
__CONFIG(1, PLLDIV_1 & CPUDIV_OSC1_PLL2 & USBDIV_1);
//#pragma config CONFIG1H = 0x8
__CONFIG(2, FOSC_INTOSCIO_EC & FCMEN_OFF & IESO_OFF);
//#pragma config CONFIG2L = 0x19
__CONFIG(3, PWRT_OFF & BOR_OFF & BORV_3 & VREGEN_OFF);
//#pragma config CONFIG2H = 0x1E
__CONFIG(4, WDT_OFF & WDTPS_32768);
//#pragma config CONFIG3H = 0x83
__CONFIG(5, CCP2MX_ON & PBADEN_ON & LPT1OSC_OFF & MCLRE_ON);
//#pragma config CONFIG4L = 0x85
__CONFIG(6, STVREN_ON & LVP_ON & ICPRT_OFF & XINST_OFF);
//#pragma config CONFIG5L = 0xF
__CONFIG(7, CP0_OFF & CP1_OFF & CP2_OFF & CP3_OFF);
//#pragma config CONFIG5H = 0xC0
__CONFIG(8, CPB_OFF & CPD_OFF);
//#pragma config CONFIG6L = 0xF
__CONFIG(9, WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF);
//#pragma config CONFIG6H = 0xE0
__CONFIG(10, WRTC_OFF & WRTB_OFF & WRTD_OFF);
//#pragma config CONFIG7L = 0xF
__CONFIG(11, EBTR0_OFF & EBTR1_OFF & EBTR2_OFF & EBTR3_OFF);
//#pragma config CONFIG7H = 0x40
__CONFIG(12, EBTRB_OFF);
Rich (BB code):
#include <xc.h>
#include <plib/timers.h>
#include "config.h"
#define _XTAL_FREQ 800000
#define USE_AND_MASKS
int i =0;
unsigned char Timer0Config;
unsigned char Timer1Config;
void SetupClock(void);
void main(int argc,char** argv)
{
SetupClock();
TRISBbits.RB4=0;
LATB4=1;
TRISBbits.RB3=0;
LATB3=1;
Timer0Config = TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_256 ;
T1CON=0b11110101;
Timer1Config = T1CON;
OpenTimer0(Timer0Config);
WriteTimer0(0xE17B);
OpenTimer1(Timer1Config);
WriteTimer1(0xE17B);
PIR1bits.TMR1IF = 0;
INTCONbits.TMR0IF = 0;
ei();
while(1)
{
}
}
void SetupClock()
{
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
}
void interrupt TImerOverflow()
{
if(INTCONbits.TMR0IF ==1)
{
LATB4 =~LATB4;
INTCONbits.TMR0IF = 0;
WriteTimer0(0xE17B);
}
if(PIR1bits.TMR1IF ==1)
{
LATB3 =~LATB3;
PIR1bits.TMR1IF = 0;
WriteTimer1(0xE17B);
}
}