Last active
April 10, 2016 20:37
-
-
Save lucafaggianelli/f40c92e65321267314a43f2e01800e5e to your computer and use it in GitHub Desktop.
PIC18 firmware for RGB LED driver
This file contains hidden or 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
#define ST_CHAR 0xFE | |
#define ESC_CHAR 0xFD | |
#define MSG_LEN 6 | |
unsigned char rxCounter = 0, escaped = 0, in_msg = 0, rxBuffer[MSG_LEN]; | |
void parseSerialMsg(void) { | |
static unsigned char byte, status; | |
if (DataRdyUSART()) { | |
byte = getcUSART(); | |
if ( (!in_msg) && (byte != ST_CHAR) ) { | |
status = 'n'; | |
in_msg = 0; | |
} else if ( byte == ST_CHAR ) { | |
if (escaped) { | |
status = 'd'; | |
rxBuffer[rxCounter++] = byte; | |
escaped = 0; | |
} else { | |
status = 's'; | |
rxCounter = 0; | |
in_msg = 1; | |
escaped = 0; | |
} | |
} else if ( byte == ESC_CHAR ) { | |
if (escaped) { | |
status = 'd'; | |
rxBuffer[rxCounter++] = byte; | |
escaped = 0; | |
} else { | |
status = 'e'; | |
escaped = 1; | |
} | |
} else { | |
status = 'd'; | |
rxBuffer[rxCounter++] = byte; | |
escaped = 0; | |
} | |
if ( rxCounter == MSG_LEN ) { | |
status = 'k'; | |
rxCounter = 0; | |
escaped = 0; | |
in_msg = 0; | |
paintLED(rxBuffer); | |
} | |
while(BusyUSART()); | |
putcUSART(status); | |
} | |
} | |
void paintLED(char* colors) { | |
static unsigned char i,j; | |
// Set colors of LED0 | |
for (i=0; i<6; i++) { | |
ledDriverMsg[27-i] = colors[i]; | |
} | |
// Stream data | |
PORTBbits.RB1 = 1; | |
for(i=0; i<28; i++) { | |
for(j=8; j>0; j--){ | |
PORTBbits.RB1 = 0; | |
PORTBbits.RB0 = ( ledDriverMsg[i]>>(j-1) ) & 0x01; | |
PORTBbits.RB1 = 1; | |
} | |
} | |
PORTBbits.RB0 = 0; | |
PORTBbits.RB1 = 0; | |
} | |
void highISR(void); | |
#pragma code HIGH_INTERRUPT_VECTOR = 0x8 | |
/* High-priority service */ | |
void high_ISR(void) { | |
_asm | |
goto highISR | |
_endasm | |
} | |
#pragma code | |
#pragma interrupt highISR | |
/* High-priority service */ | |
void highISR(void) { | |
if(PIR1bits.RCIF) { | |
PIE1bits.RCIE = 0; | |
// Real function to execute | |
parseSerialMsg(); | |
// Clear Interrupt Flag | |
PIR1bits.RCIF = 0; | |
PIE1bits.RCIE = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment