#include _CONFIG1 ( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) // Select FRC oscillator with PLL _CONFIG2 ( POSCMOD_NONE & FCKSM_CSDCMD & FNOSC_FRCPLL & OSCIOFNC_OFF) int t1count; int menuselection_new; int menuselection_prev; void e() { } void dly() { int x; for (x = 0; x != 75; ++x) e(); } void dly5us() { int x; for (x=0; x!=160; ++x) e(); } void dly3ms() { int x; for (x=0; x!=600; ++x) dly5us(); } void dly205us() { int x; for (x=0; x!=41; ++x) dly5us(); } /*void setupTransceiver() { dly3ms(); LATB = LATB | 0x0008; //Set CS high to enter config mode dly5us(); SPI2BUF = 0x21; dly(); SPI2BUF = 0x4F; dly(); SPI2BUF = 0x84; dly(); LATB = LATB & 0xFFF7; //Set CS low to exit config mode and save changes dly(); }*/ void setupTransceiver() { LATB = LATB & 0xFFEF; // Set CE low dly3ms(); LATB = LATB | 0x0008; //Set CS high to enter config mode dly5us(); SPI2BUF = 0x1000; dly5us(); SPI2BUF = 0x0000; dly5us(); SPI2BUF = 0x0000; dly5us(); SPI2BUF = 0x0000; dly5us(); SPI2BUF = 0x0070; dly5us(); SPI2BUF = 0x8043; dly5us(); SPI2BUF = 0x4F02; dly5us(); LATB = LATB & 0xFFF7; //Set CS low to exit config mode and save changes dly(); LATB = LATB | 0x0010; //Set CE high to enter transmit mode dly205us(); } /*void sendData() { dly(); LATB = LATB | 0x0010; // Set CE to 1 dly5us(); SPI2BUF = 0xE7; dly(); SPI2BUF = 0xAA; dly(); SPI2BUF = 0xAA; dly(); SPI2BUF = 0x00; dly(); SPI2BUF = 0x00; dly(); LATB = LATB & 0xFFEF; // Set CE to 0 }*/ void sendData(int value1) { dly(); LATB = LATB | 0x0010; // Set CE to 1 dly5us(); SPI2BUF = 0x7080; dly5us(); SPI2BUF = value1; dly5us(); LATB = LATB & 0xFFEF; // Set CE to 0 } void preparetransmission() { int temp2 = 0; int temp3 = 0; int sendValue = 0; if ((menuselection_new > 16) && (menuselection_new < 53)) { temp2 = menuselection_new - 17 + 12 + 11; if (temp2 > 35) { temp2 = temp2 - 35; } temp2 = 35 - temp2; temp2 <<= 4; sendData(temp2); } else { temp3 = menuselection_new & 0x000F; if (temp3) { temp3 = temp3 - 1; temp3 <<= 10; sendValue = temp3 + 0x4000; } else { sendValue = 0x8000; } //switch(menuselection_new) //{ // case 0b0000: sendValue = 0x8000; break; // case 0b0001: sendValue = 0x4800; break; // case 0b0010: sendValue = 0x4000; break; // case 0b0011: sendValue = 0x4400; break; // case 0b0100: sendValue = 0x4C00; break; // default: break; //} sendData(sendValue); } } void getARMdata() { menuselection_new = PORTBbits.RB6 + 2*(PORTBbits.RB7 + 2*(PORTBbits.RB8 + 2*(PORTBbits.RB9 + 2*(PORTAbits.RA1+ 2*PORTAbits.RA0)))); if (menuselection_new != menuselection_prev) { preparetransmission(); } menuselection_prev = menuselection_new; } void toDebugLEDs() { int temp = 0; LATBbits.LATB15 = (menuselection_new & 0x1); temp = (menuselection_new & 0x2); temp >>= 1; LATBbits.LATB14 = temp; temp = (menuselection_new & 0x4); temp >>= 2; LATBbits.LATB13 = temp; temp = (menuselection_new & 0x8); temp >>= 3; LATBbits.LATB12 = temp; } // Timer 1 interrupt void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void) { IFS0bits.T1IF = 0; //clear flag t1count++; } int main(void) { CLKDIV = 0x0000; AD1PCFG = 0x1FFF; //set up as digital i/o pins //set up port pins LATB = 0; TRISB = 0b0000111111100100; LATA = 0; TRISA = 0b11; //SPI 2 setup (for sending data to other transceiver) RPOR0bits.RP0R = 10; // SPI2 data out RPOR0bits.RP1R = 11; // SPI2 clock out SPI2CON1 = 0x0521; SPI2CON2 = 0x0000; SPI2STATbits.SPIROV = 0; SPI2STATbits.SPIEN = 1; //enable SPI //TIM1 setup (fires every 4ms) t1count = 0; T1CON = 0x00; //Stops the Timer1 and reset control reg. TMR1 = 0x00; //Clear contents of the timer register PR1 = 0xFFFF; //Load the Period register with the value 0xFFFF IPC0bits.T1IP = 0x03; //Setup Timer1 interrupt for desired priority level IFS0bits.T1IF = 0; //Clear the Timer1 interrupt status flag IEC0bits.T1IE = 1; //Enable Timer1 interrupts T1CONbits.TON = 1; //Start Timer1 with prescaler settings at 1:1 and //clock source set to the internal instruction cycle menuselection_new = 0; menuselection_prev = 0; setupTransceiver(); int flag = 0; while (1) { if ((t1count >= 25) && (t1count < 50) && (flag == 0)) { //sendData(0xFF00, 0x5555); flag = 1; } if ((t1count >= 50) && (flag == 1)) { //sendData(0x00FF, 0xAAAA); getARMdata(); toDebugLEDs(); t1count = 0; TMR1 = 0x00; flag = 0; } } return 0; }