Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <plib.h>
- #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
- #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2
- #pragma config UPLLEN = ON, UPLLIDIV = DIV_2
- #pragma config DEBUG = OFF, ICESEL = ICS_PGx2, CP = OFF, BWP = OFF, PWP = OFF
- #pragma FWDTEN = OFF, FCKSM = CSDCMD
- /*Definitions ************************************************/
- #define SYS_FREQ (80000000UL) // Clock Frequency 80 MHz
- #define SYSCLK_UART (SYS_FREQ/2) // Peripheral Bus Clock: 40 MHz
- #define Baud2BRG(desired_baud) ((SYSCLK_UART/(16*desired_baud))-1) // set baud rate
- #define FOSC 80E6
- #define PB_DIV 2
- #define PRESCALE 256
- #define MSEC 10E-3
- #define T1_TICK (50 * MSEC * FOSC)/(PB_DIV * PRESCALE) // 1/20kHz = 50us
- typedef int bool;
- #define true 1
- #define false 0
- /*function ***************************************************/
- int UART2Configure( unsigned int desired_baud );
- int SerialTransmit(char *buffer);
- void sentButton( UINT32 data );
- /*Global Variables *******************************************/
- UINT8 buf[1024];
- /*Main Function **********************************************/
- int main(void)
- {
- SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
- mJTAGPortEnable(DEBUG_JTAGPORT_OFF); //Disable JTAG Port
- /*variable *********************************/
- unsigned int CaptureTime;
- bool start = false;
- int count = 0;
- UINT32 data = 0x0;
- int state = 0;
- /*Setup port I/O ***************************/
- TRISDbits.TRISD1 = 0; // making pin RD1 as output...pin76
- //Clear interrupt flag
- mIC1ClearIntFlag();
- UART2Configure(250000); // Confugure UART
- // Setup Timer 3
- OpenTimer3(T3_ON | T1_PS_1_256, T1_TICK);
- // Enable Input Capture Module 1
- // - Capture Every Talling edge
- // - Enable capture interrupts
- // - Use Timer 3 source
- // - Capture rising edge first
- OpenCapture1( IC_EVERY_FALL_EDGE | IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_FALL | IC_ON );
- while(1){
- // Wait for Capture events
- while( !mIC1CaptureReady() ) ;
- //Now Read the captured timer value
- while( mIC1CaptureReady() )
- {
- CaptureTime = mIC1ReadCapture();
- WriteTimer3(0);
- //sprintf(buf, "%d\r\n", CaptureTime);
- //SerialTransmit(buf);
- switch( state ){
- case 0:
- if( CaptureTime >= 283000000 && CaptureTime <= 300000000 ){
- //SerialTransmit("start\r\n");
- state = 1;
- }
- break;
- case 1:
- if( CaptureTime >= 360000000 && CaptureTime <= 400000000 ){
- count++;
- data = data << 1;
- //sprintf(buf, "bit:%d\r\n", count);
- //SerialTransmit(buf);
- }
- if( CaptureTime >= 710000000 && CaptureTime <= 750000000 ){
- count++;
- data++;
- data = data << 1;
- //sprintf(buf, "bit:%d\r\n", count);
- //SerialTransmit(buf);
- }
- if( count == 31 ){
- state = 0;
- //sprintf(buf, "data:%d\r\n", data);
- //SerialTransmit(buf);
- sentButton( data );
- data = 0;
- count = 0;
- }
- break;
- } // end - switch case
- } // end - while mIC1CaptureReady()
- } // end - while loop
- } // end - main
- void sentButton( UINT32 data ){
- UINT32 input = data;
- switch( input ){
- case 0xFF629C:
- sprintf(buf, "Button: UP\r\n");
- SerialTransmit(buf);
- break;
- case 0xFFA856:
- sprintf(buf, "Button: DOWN\r\n");
- SerialTransmit(buf);
- break;
- case 0xFF22DC:
- sprintf(buf, "Button: LEFT\r\n");
- SerialTransmit(buf);
- break;
- case 0xFFC23C:
- sprintf(buf, "Button: RIGHT\r\n");
- SerialTransmit(buf);
- break;
- case 0xFF02FC:
- sprintf(buf, "Button: OK\r\n");
- SerialTransmit(buf);
- break;
- case 0xFF6896:
- sprintf(buf, "Button: %d\r\n", 1);
- SerialTransmit(buf);
- break;
- case 0xFF9866:
- sprintf(buf, "Button: %d\r\n", 2);
- SerialTransmit(buf);
- break;
- case 0xFFB04E:
- sprintf(buf, "Button: %d\r\n", 3);
- SerialTransmit(buf);
- break;
- case 0xFF30CE:
- sprintf(buf, "Button: %d\r\n", 4);
- SerialTransmit(buf);
- break;
- case 0xFF18E6:
- sprintf(buf, "Button: %d\r\n", 5);
- SerialTransmit(buf);
- break;
- case 0xFF7A84:
- sprintf(buf, "Button: %d\r\n", 6);
- SerialTransmit(buf);
- break;
- case 0xFF10EE:
- sprintf(buf, "Button: %d\r\n", 7);
- SerialTransmit(buf);
- break;
- case 0xFF38C6:
- sprintf(buf, "Button: %d\r\n", 8);
- SerialTransmit(buf);
- break;
- case 0xFF5AA4:
- sprintf(buf, "Button: %d\r\n", 9);
- SerialTransmit(buf);
- break;
- case 0xFF4AB4:
- sprintf(buf, "Button: %d\r\n", 0);
- SerialTransmit(buf);
- break;
- case 0xFF42BC:
- sprintf(buf, "Button: *\r\n");
- SerialTransmit(buf);
- break;
- case 0xFF52AC:
- sprintf(buf, "Button: #\r\n");
- SerialTransmit(buf);
- break;
- }
- } // end-sentButton
- // UART2 CONFIGURE
- int UART2Configure( unsigned int desired_baud ){
- U2MODE = 0; // disable autobaud, TX and RX enabled only, 8N1, idle=HIGH
- U2MODESET = 0x8000; // Enable Uart for 8-bit data
- // no parity bit, 1 STOP bit
- U2STA = 0;
- U2STASET = 0x1400; // Enable Transmit and Receive
- U2BRG = Baud2BRG(desired_baud); // Set Data Rate
- // This Project set Data Rate is 250k
- return 0;
- } // END! UART2Configur()
- int SerialTransmit(char *buffer){
- unsigned int size = strlen(buffer);
- while( size ){
- while( U2STAbits.UTXBF ); // wait while TX buffer full
- U2TXREG = *buffer; // send single character to transmit buffer
- buffer++; // transmit next character on following loop
- size--; // loop until all characters sent (when size = 0)
- }
- while( !U2STAbits.TRMT ); // wait for last transmission to finish
- return 0;
- } // END! SerialTransmit()
Advertisement
Add Comment
Please, Sign In to add comment