Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Created May 5, 2014 00:47
Show Gist options
  • Select an option

  • Save ramirez7/2dfacfd12c619f5c6dea to your computer and use it in GitHub Desktop.

Select an option

Save ramirez7/2dfacfd12c619f5c6dea to your computer and use it in GitHub Desktop.
The code on our PIC24F microcontroller in BoilerReversi (ECE 477 Senior Design)
#include "p24F08KL302.h"
//_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
//_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)
_FGS(GSS0_OFF & GWRP_OFF)
_FICD(ICS_PGx2)
_FWDT(FWDTEN_OFF)
_FOSC(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMD_XT)
_FOSCSEL(FNOSC_PRI & SOSCSRC_DIG)
// generic helpers
void uart2Init(int BAUDRATEREG) {
U2BRG = BAUDRATEREG;
//interrupt config
IFS1bits.U2TXIF = 0;
IEC1bits.U2TXIE = 1;
IFS1bits.U2RXIF = 0;
IEC1bits.U2RXIE = 1;
_U2RXIP0 = 1;
_U2RXIP1 = 0;
_U2RXIP2 = 1;
//TODO: u2rx interrupt priority
//configure and enable
U2STA = 0b0000010000000000;
U2MODE = 0b1000100000000000;
}
unsigned char uart2GetChar() {
char Temp;
//_LATB9 = ~_LATB9;
//wait for buffer to fill up, wait for interrupt
//while(IFS0bits.U1RXIF == 0);
while(U2STAbits.URXDA == 0);
Temp = U2RXREG;
//reset interrupt
//IFS0bits.U1RXIF = 0;
//return my received byte
return Temp;
}
void __attribute__((__interrupt__)) _U2TXInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _U2TXInterrupt(void) {
IFS1bits.U2TXIF = 0;
}
void uart2PutChar(unsigned char c) {
//hang until room in buffer
while(U2STAbits.UTXBF == 1);
U2TXREG = c;
U2STAbits.UTXEN = 1;
while(U2STAbits.TRMT == 0);
}
void spi1Init(){
//SSP1CON1
//Bit 7 WCOL = 1
//Bit 6 SSPOV = 0
//Bit 5 SSPEN = 1
//bit 4 CKP = 0
//bits 3-0 SSPM = 0b0000
SSP1CON1 = 0b10100000;
//SSP1CON2
//Bit 4 BOEN = 0 //doesn't matter tho since we aren't receiving atm
SSP1CON2 = 0;
// no prescale option available in SPI
}
void spi1PutChar(unsigned char c) {
_SPI1BUF = c;
while(!SSP1STATbits.BF);//IDE yells at us but it compiles so (t'-'t)
}
void TimerInit(void) {
PR1 = 0xB0F;//set the time somehow
_T1IP0 = 1; //set interrupt priority
_T1IP1 = 1;
_T1IP2 = 1;
T1CON = 0b1000000000000000; // turn on timer
IFS0bits.T1IF = 0; // reset interrupt flag
IEC0bits.T1IE = 1;// turn on timer1 interrupt
}
void gpioInit() {
// All digital
ANSA = 0x0000;
ANSB = 0x0000;
// OUTPUTS
// RA0 -> power cycling
// RA1 -> power cycling
// RA4 -> column scan A2
// INPUTS
// RA6 -> push button
TRISA = 0b11101100;//RA0 and RA1 output
// OUTPUTS
// RB0 -> U2TX
// RB2 -> power cycling
// RB3 -> column scan A1
// RB4 -> column scan A2
// RB15 -> general debug
// INPUTS
// RB1 -> U2RX
// RB7 -> row return GS
// RB8 -> row return A0
// RB9 -> row return A1
// RB10 -> row return A2
// RB14 -> push button
TRISB = 0b0111111111100010;
}
void ExternalIntInit(void) {
// INT1 (RB14)
_INT1EP = 1; // interrupt on INT1 on falling edge
_INT1IE = 1; // Enable INT1 external ISR
_INT1IF = 0; // reset INT1 interrupt flag
// priority
_INT1IP1 = 1;
_INT1IP2 = 1;
_INT1IP0 = 1;
// INT2 (RA6)
_INT2EP = 1; // interrupt on INT0 on rising edge
_INT2IE = 1; // Enable INT0 external ISR
_INT2IF = 0; // reset INT0 interrupt flag
// priority
_INT2IP1 = 1;
_INT2IP2 = 1;
_INT2IP0 = 1;
}
void delay(void) {
long i = 65535;
while(i--)
;
}
void delay2(void) {
long i = 32000;
while(i--)
;
}
//
void setPowerDemux(char select) {
// Select should between 0-7
_LATA0 = 0b00000001 & select;//PIC2 (RA0) to A0
_LATA1 = (0b00000010 & select) >> 1;//PIC3 (RA1) to A1
_LATB2 = (0b00000100 & select) >> 2;//PIC6 (RB2) to A2
}
//
void setColumnScan (char select) {
_LATB3 = 0b00000001 & select;//RB3 (PIC7)
_LATB4 = (0b00000010 & select) >> 1;//RB4 (PIC11)
_LATA4 = (0b00000100 & select) >> 2;//RA4 (PIC12)
}
char getGridMark (char select) {
return (select << 3) | (_RB8 | (_RB9 << 1) | (_RB10 << 2));
}
//MAIN CODE
//static globals
static volatile unsigned char grid[8][3] = {
{0b00000000, 0b00000000, 0b00000000},//1
{0b00000000, 0b00000000, 0b00000000},
{0b00000000, 0b00000000, 0b00000000},
{0b00000000, 0b00000000, 0b00000000},
{0b00000000, 0b00000000, 0b00000000},
{0b00000000, 0b00000000, 0b00000000},
{0b00000000, 0b00000000, 0b00000000},//3
{0b00000000, 0b00000000, 0b00000000}};//2
static volatile unsigned int uartX = 0;
static volatile unsigned int uartY = 0;
static volatile unsigned char outputRow = 0;
static void updateUartX () {
uartX = (uartY == 2) ? (uartX + 1) % 8 : uartX;
}
static void updateUartY () {
uartY = (uartY + 1) % 3;
}
static void updateGrid (unsigned char c) {
int i;
grid[uartX][uartY] = c;
updateUartX();
for (i = 0; i < 5; i++);
updateUartY();
}
void __attribute__((__interrupt__)) _U2RXInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _U2RXInterrupt(void) {
volatile unsigned char c;
c = U2RXREG;
if (c != 0b11111111) {
grid[uartX][uartY] = c;
uartX = (uartY == 2) ? (uartX + 1) % 8 : uartX;
uartY = (uartY + 1) % 3;
}
IFS1bits.U2RXIF = 0;
}
void cyclePower() {
int i;
//shift out next row
//spi1PutChar(0);
spi1PutChar(grid[outputRow][0]);
for (i = 0; i < 20; i++);
spi1PutChar(grid[outputRow][1]);
//spi1PutChar(0);
for (i = 0; i < 20; i++);
spi1PutChar(grid[outputRow][2]);
//spi1PutChar(0);
//for (i = 0; i < 500; i++);
//change power to next row
setPowerDemux(outputRow);
//next row
outputRow = (outputRow == 7) ? 0 : outputRow + 1;
}
void __attribute__((__interrupt__)) _T1Interrupt(void);
void __attribute__((__interrupt__, auto_psv)) _T1Interrupt(void)
{
cyclePower();
IFS0bits.T1IF = 0;
}
int main(int argc, char** argv) {
int i;
int j;
char prev = 1;
char curr;
char prevPB1 = 1;
char currPB1;
char prevPB2 = 1;
char currPB2;
char columnSelect = 0;
unsigned char prevScan = 255;
unsigned char currScan;
OSCCON = 0x2200;
CLKDIV = 0x0000;
gpioInit();
spi1Init();
TimerInit();
uart2Init(25);//make 25 derived from Fcy
//startup LED flash
_LATB15 = 0;
delay();
_LATB15 = 1;
delay();
_LATB15 = 0;
uartX = 0;
uartY = 0;
while(1) {
setColumnScan(columnSelect);
prevScan = 255;
for (i = 0; i < 100; i++) {
curr = 1;
currPB1 = 1;
currPB2 = 1;
for (j = 0; j < 50; j++) {
curr = _RB7;
currPB1 = _RB14;
currPB2 = _RA6;
if ((curr == 0) || (currPB1 == 0) || (currPB2 == 0)) {
break;
}
}
if ((prev == 1) && (curr == 0)) {
currScan = getGridMark(columnSelect);
if (currScan != prevScan) {
uart2PutChar(currScan);
}
prevScan = currScan;
}
if ((prevPB1 == 1) && (currPB1 == 0)) {
uart2PutChar(75);
}
if ((prevPB2 == 1) && (currPB2 == 0)) {
uart2PutChar(95);
}
prev = curr;
prevPB1 = currPB1;
prevPB2 = currPB2;
}
columnSelect = (columnSelect + 1) % 8;
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment