So I'm working on controller servos with TLC5940 LED Driver and this is my first time using interrupts. The interrupt is on Timer 2 match with PR2. I know the interrupt is working because of some testing blinking an LED but it appears that the interrupt is called over and over again.
All my while loop does at the end of main is blink an LED. The LED turns on but never off, I assume this is when the first interrupt happens but it seems like every time the interrupt returns to the main it seems to be called again.
All my while loop does at the end of main is blink an LED. The LED turns on but never off, I assume this is when the first interrupt happens but it seems like every time the interrupt returns to the main it seems to be called again.
Rich (BB code):
//Microcontroller: PIC18F4520
//8MHz internal clock
//Complier: xc8 v1.20
#include <xc.h>
#include "tlc5940.h"
#define _XTAL_FREQ 8000000
#define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
#define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
#define OSCCON = 0b01110000
#define ANSEL = 0b00000000
#pragma config LVP = OFF
#pragma config OSC = HS, WDT=OFF, PWRT=OFF, BOREN=OFF, CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF, MCLRE=ON
volatile int count;
void delay_sec() //actually, half a second
{
for(int i = 0; i < 50; i++)
{
__delay_ms(10);
}
}
void interrupt blank_pulse(void)
{
if(PIE1bits.TMR2IE && PIR1bits.TMR2IF)
{
if(count >= 520) //572
{
tlc_blank = 1;
__delay_us(1);
tlc_blank = 0;
count = 0; //Reset count
}
count++;
PIR1bits.TMR2IF = 0;
// if(PIR1bits.TMR2IF == 0)//Testing if there is a loop happening
// {
// LATDbits.LD3 = 1;
// delay_sec();
// LATDbits.LD3 = 0;
// delay_sec();
// }
}
}
void spi_init()
{
SSPSTATbits.SMP = 0; //Input data sampled at middle of data output time
SSPSTATbits.CKE = 1; //Transmit on transition from active to Idle clock state
SSPCON1bits.CKP = 0; //Idle state for clock is a low level
SSPCON1bits.SSPM0 = 0;//Sets SPI in master mode and sets clock to FOSC/4
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM3 = 0;
SSPCON1bits.SSPEN = 1; //Enables the serial port
TRISCbits.RC5 = 0; //Set SDO to ouput
TRISCbits.RC3 = 0; //Set SCK to output
}
int main()
{
init_TMR2();
count = 0;
PIE1bits.TMR2IE = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
T2CONbits.TMR2ON = 0; //Turn Timer on
TRISD = 0b00000000;
PORTD = 0b00000000;
TRISB = 0b00100000;
PORTB = 0b00000000;
TRISA = 0b00001111;
PORTA = 0b00001111;
TRISE = 0b00001000;
PORTE = 0b00000000;
SSPSTATbits.SMP = 0; //Input data sampled at middle of data output time
SSPSTATbits.CKE = 1; //Transmit on transition from active to Idle clock state
SSPCON1bits.CKP = 0; //Idle state for clock is a low level
SSPCON1bits.SSPM0 = 0;//Sets SPI in master mode and sets clock to FOSC/4
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM3 = 0;
SSPCON1bits.SSPEN = 1; //Enables the serial port
T2CONbits.TMR2ON = 0; //Turn Timer on
//dot correction
//delay_sec();
init_dot_correction();
//delay_sec();
//delay_sec();
init_grayscale();
//delay_sec();
update_grayscale();
T2CONbits.TMR2ON = 1; //Turn Timer on
// while(1) //Old function before new interrupt
// {
// while(counter < 572) //Wait until ~40 ticks have passed //was 5 888
// {
// if(TMR2 == PR2)
// {
// counter = counter + 1;
// }
// }
// tlc_blank = 1;
// __delay_us(1);
// tlc_blank = 0;
// counter = 0; //Reset counter
// }
while(1)
{
LATDbits.LD3 = 1;
delay_sec();
LATDbits.LD3 = 0;
delay_sec();
}
}