i'm trying to control four separate lighting arrays using a timer overflow interrupt to control four pulse width modulations. at the same time i want to be able to control the pwm frequencies using serial communication. i'm using a version of pic lite for my compiling and can compile my code with either interrupt but not with both of them together. below is the full code (simplified) that i'm trying to use.
/*-----------------------------------------------------
contol 4 leds through pwm and serial
-------------------------------------------------------*/
#include "mxapi.h"
__CONFIG(0X7E61); //Sets Interrupt Configuration for PIC
char counter;
char led_1;
char led_2;
char led_3;
char led_4;
char id;
/*---------------------------------------------------------------\
| The following section defines a special "interrupt function" |
| called "lights()" that will execute every time an interrupt is |
| triggered. (In this case, that's every time Timer0 overflows.) |
================================================================*/
void interrupt lights(void) {
if (T0IF == 1) //check if Timer0 "Interrupt Flag" is set -when it
{ //times out ("overflows" at 255) it's set to 1.)
T0IF = 0; //reset Timer0 flag
counter ++; //increase counter by 1
if (counter==38) //check if interrupt has triggered 39 times
{
RB3=1; //start pulse for led 1
RB4=1; //start pulse for led 2
RB5=1; //start pulse for led 3
RB6=1; //start pulse for led 4
counter=0; //reset counter
}
if (counter==led_1) //check if time to end pulse for led 1
RB3=0;
if (counter==led_2) //check if time to end pulse for led 2
RB4=0;
if (counter==led_3) //check if time to end pulse for led 3
RB5=0;
if (counter==led_4) //check if time to end pulse for led 4
RB6=0;
} //end if(T0IF==1)
} //end servo() definition
//====================================================\\
//-----------------------START PROGRAM-------------------------\\
//====================================================\\
void main() {
GIE=1; //enable global interrupts
PEIE = 1; // enable peripheral interrups
RCIE = 1; // enable receive interrupt pin
usart_init(30000);
T0IE=1; //enable Timer0 interrupts
T0CS=0; //set Timer0 to use internal instruction clock
PSA=0; //assign prescaler to Timer0
//---- The next three lines set a "pre-scale value" for frequency of interrupts on the
//---- PIC. "000" sets a pre-scale value of 2, meaning interrupts happen 1/2 as often.
PS2=0; //set first digit of pre-scaler value
PS1=0; //set second digit of pre-scaler value
PS0=0; //set third digit of pre-scaler value
TRISB3=0; //set B3 as output (for led 1)
TRISB4=0; //set B4 as output (for led 2)
TRISB5=0; //set B5 as output (for led 2)
TRISB6=0; //set B6 as output (for led 2)
while (1==1) {
led_1=id; //set pulse rates to leds
led_2=10; //set pulse rates to leds
led_3=20; //set pulse rates to leds
led_4=30; //set pulse rates to leds
}
}
void interrupt usart_test(){ //interrupt that loads new serial_in from the serial port
if(RCIF){ // if data is ready on USART
id = RCREG;
RCIF = 0;
}
}