pongfactory

Lab 4-3 PIC

Jan 27th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.03 KB | None | 0 0
  1. #include <plib.h>
  2. #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
  3. #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2
  4. #pragma config UPLLEN = ON, UPLLIDIV = DIV_2
  5. #pragma config DEBUG = OFF, ICESEL = ICS_PGx2, CP = OFF, BWP = OFF, PWP = OFF
  6. #pragma FWDTEN = OFF, FCKSM = CSDCMD
  7. /*Definitions ************************************************/
  8. #define SYS_FREQ                (80000000UL)  //  Clock Frequency 80 MHz
  9. #define SYSCLK_UART             (SYS_FREQ/2)  //  Peripheral Bus Clock: 40 MHz
  10. #define Baud2BRG(desired_baud)  ((SYSCLK_UART/(16*desired_baud))-1) // set baud rate
  11. #define FOSC           80E6
  12. #define PB_DIV         2
  13. #define PRESCALE       256
  14. #define MSEC           10E-3
  15. #define T1_TICK       (50 * MSEC * FOSC)/(PB_DIV * PRESCALE) // 1/20kHz = 50us
  16. typedef int bool;
  17. #define true 1
  18. #define false 0
  19. /*function ***************************************************/
  20. int UART2Configure( unsigned int desired_baud );
  21. int SerialTransmit(char *buffer);
  22. void sentButton( UINT32 data );
  23. /*Global Variables *******************************************/
  24. UINT8 buf[1024];
  25. /*Main Function **********************************************/
  26. int main(void)
  27. {
  28.     SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
  29.     mJTAGPortEnable(DEBUG_JTAGPORT_OFF); //Disable JTAG Port
  30.     /*variable *********************************/
  31.     unsigned int CaptureTime;
  32.     bool start    = false;
  33.     int count     = 0;
  34.     UINT32 data   = 0x0;
  35.     int state     = 0;
  36.     /*Setup port I/O ***************************/
  37.     TRISDbits.TRISD1 = 0;   // making pin RD1 as output...pin76
  38.     //Clear interrupt flag
  39.     mIC1ClearIntFlag();
  40.     UART2Configure(250000); // Confugure UART
  41.     // Setup Timer 3
  42.     OpenTimer3(T3_ON | T1_PS_1_256, T1_TICK);
  43.     // Enable Input Capture Module 1
  44.     // - Capture Every Talling edge
  45.     // - Enable capture interrupts
  46.     // - Use Timer 3 source
  47.     // - Capture rising edge first
  48.     OpenCapture1( IC_EVERY_FALL_EDGE | IC_INT_1CAPTURE | IC_TIMER3_SRC | IC_FEDGE_FALL | IC_ON );
  49.     while(1){
  50.         // Wait for Capture events
  51.         while( !mIC1CaptureReady() ) ;
  52.         //Now Read the captured timer value
  53.         while( mIC1CaptureReady() )
  54.         {
  55.             CaptureTime = mIC1ReadCapture();
  56.             WriteTimer3(0);
  57.             //sprintf(buf, "%d\r\n", CaptureTime);
  58.             //SerialTransmit(buf);
  59.             switch( state ){
  60.                 case 0:
  61.                     if( CaptureTime >= 283000000 && CaptureTime <= 300000000 ){
  62.                         //SerialTransmit("start\r\n");
  63.                         state = 1;
  64.                     }
  65.                     break;
  66.                 case 1:
  67.                     if( CaptureTime >= 360000000 && CaptureTime <= 400000000 ){
  68.                         count++;
  69.                         data = data << 1;
  70.                         //sprintf(buf, "bit:%d\r\n", count);
  71.                         //SerialTransmit(buf);
  72.                     }
  73.                     if( CaptureTime >= 710000000 && CaptureTime <= 750000000 ){
  74.                         count++;
  75.                         data++;
  76.                         data = data << 1;
  77.                         //sprintf(buf, "bit:%d\r\n", count);
  78.                         //SerialTransmit(buf);
  79.                     }
  80.                     if( count == 31 ){
  81.                         state = 0;
  82.                         //sprintf(buf, "data:%d\r\n", data);
  83.                         //SerialTransmit(buf);
  84.                         sentButton( data );
  85.                         data = 0;
  86.                         count = 0;
  87.                     }
  88.                     break;
  89.             } // end - switch case
  90.         } // end - while mIC1CaptureReady()
  91.     } // end - while loop
  92. } // end - main
  93. void sentButton( UINT32 data ){
  94.     UINT32 input = data;
  95.     switch( input ){
  96.         case 0xFF629C:
  97.             sprintf(buf, "Button: UP\r\n");
  98.             SerialTransmit(buf);
  99.             break;
  100.         case 0xFFA856:
  101.             sprintf(buf, "Button: DOWN\r\n");
  102.             SerialTransmit(buf);
  103.             break;
  104.         case 0xFF22DC:
  105.             sprintf(buf, "Button: LEFT\r\n");
  106.             SerialTransmit(buf);
  107.             break;
  108.         case 0xFFC23C:
  109.             sprintf(buf, "Button: RIGHT\r\n");
  110.             SerialTransmit(buf);
  111.             break;
  112.         case 0xFF02FC:
  113.             sprintf(buf, "Button: OK\r\n");
  114.             SerialTransmit(buf);
  115.             break;
  116.         case 0xFF6896:
  117.             sprintf(buf, "Button: %d\r\n", 1);
  118.             SerialTransmit(buf);
  119.             break;
  120.         case 0xFF9866:
  121.             sprintf(buf, "Button: %d\r\n", 2);
  122.             SerialTransmit(buf);
  123.             break;
  124.         case 0xFFB04E:
  125.             sprintf(buf, "Button: %d\r\n", 3);
  126.             SerialTransmit(buf);
  127.             break;
  128.         case 0xFF30CE:
  129.             sprintf(buf, "Button: %d\r\n", 4);
  130.             SerialTransmit(buf);
  131.             break;
  132.         case 0xFF18E6:
  133.             sprintf(buf, "Button: %d\r\n", 5);
  134.             SerialTransmit(buf);
  135.             break;
  136.         case 0xFF7A84:
  137.             sprintf(buf, "Button: %d\r\n", 6);
  138.             SerialTransmit(buf);
  139.             break;
  140.         case 0xFF10EE:
  141.             sprintf(buf, "Button: %d\r\n", 7);
  142.             SerialTransmit(buf);
  143.             break;
  144.         case 0xFF38C6:
  145.             sprintf(buf, "Button: %d\r\n", 8);
  146.             SerialTransmit(buf);
  147.             break;
  148.         case 0xFF5AA4:
  149.             sprintf(buf, "Button: %d\r\n", 9);
  150.             SerialTransmit(buf);
  151.             break;
  152.         case 0xFF4AB4:
  153.             sprintf(buf, "Button: %d\r\n", 0);
  154.             SerialTransmit(buf);
  155.             break;
  156.         case 0xFF42BC:
  157.             sprintf(buf, "Button: *\r\n");
  158.             SerialTransmit(buf);
  159.             break;
  160.         case 0xFF52AC:
  161.             sprintf(buf, "Button: #\r\n");
  162.             SerialTransmit(buf);
  163.             break;
  164.     }
  165. } // end-sentButton
  166. // UART2 CONFIGURE
  167. int UART2Configure( unsigned int desired_baud ){
  168.     U2MODE     = 0;                      // disable autobaud, TX and RX enabled only, 8N1, idle=HIGH
  169.     U2MODESET  = 0x8000;                 // Enable Uart for 8-bit data
  170.                                          // no parity bit, 1 STOP bit
  171.     U2STA      = 0;
  172.     U2STASET   = 0x1400;                 // Enable Transmit and Receive
  173.     U2BRG      = Baud2BRG(desired_baud); // Set Data Rate
  174.                                          // This Project set Data Rate is 250k
  175.     return 0;
  176. } //  END! UART2Configur()
  177. int SerialTransmit(char *buffer){
  178.     unsigned int size = strlen(buffer);
  179.     while( size ){
  180.         while( U2STAbits.UTXBF ); // wait while TX buffer full
  181.         U2TXREG = *buffer;        // send single character to transmit buffer
  182.         buffer++;                 // transmit next character on following loop
  183.         size--;                   // loop until all characters sent (when size = 0)
  184.     }
  185.     while( !U2STAbits.TRMT );     // wait for last transmission to finish
  186.     return 0;
  187. } //  END! SerialTransmit()
Advertisement
Add Comment
Please, Sign In to add comment