LordSputnik

PIC Remote - 2013-03-15

Mar 15th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include <xc.h>
  2. #include <pic12f617.h>
  3.  
  4. #pragma config WRT=OFF //Disable write protection
  5. #pragma config FOSC=INTOSCIO //Use internal clock, with I/O on clock pins.
  6. #pragma config MCLRE=OFF //Use MCLRE as an input pin.
  7. #pragma config WDTE=OFF //Disable Watchdog Timer
  8. #pragma config IOSCFS=4MHZ //Run at 4MHz
  9. #pragma config CP=OFF //No code protection
  10. #pragma config PWRTE=OFF //Enable Power-Up-Timer
  11. #pragma config BOREN=OFF //Disable Brownout reset
  12.  
  13. char transmit_codes[5] = {0b1000, 0b1001, 0b1010, 0b1011, 0b1100};
  14.  
  15. void transmit(char code)
  16. {
  17.     char value = transmit_codes[code]; //Copy the code
  18.  
  19.     //Output bit 4
  20.     if((value & 0b1000) == 0)
  21.         GPIO = 0x00;
  22.     else
  23.         GPIO = 0x01;
  24.    
  25.     _delay(50000);
  26.  
  27.     //Output bit 3
  28.     if((value & 0b100) == 0)
  29.         GPIO = 0x00;
  30.     else
  31.         GPIO = 0x01;
  32.  
  33.     _delay(50000);
  34.  
  35.     //Output bit 2
  36.     if((value & 0b10) == 0)
  37.         GPIO = 0x00;
  38.     else
  39.         GPIO = 0x01;
  40.  
  41.     _delay(50000);
  42.  
  43.     //Output bit 1
  44.     if((value & 0b1) == 0)
  45.         GPIO = 0x00;
  46.     else
  47.         GPIO = 0x01;
  48.  
  49.     _delay(50000);
  50.  
  51.     GPIO = 0;
  52. }
  53.  
  54. void interrupt isr(void)
  55. {
  56.     INTCONbits.GPIF = 0; //Clear flag.
  57.     INTCONbits.GPIE = 0; //Disable interrupts to avoid triggering twice.
  58.     _delay(10000); //Can probably scrap this now.
  59.     return;
  60. }
  61.  
  62. void main()
  63. {
  64.     WPU = 0xFF; //Enable all pull-up resistors.
  65.     GPIO = 0; //Clear all pins.
  66.     ANSEL = 0; //All pins digital inputs, rather than analogue.
  67.     TRISIO = 0b11111110; //RB0 output, others inputs.
  68.     OPTION_REG = 0; //Enable pull-ups globally.
  69.  
  70.     INTCON = 0b10001000; //Enable global and GPIO change interrupts.
  71.     IOC = 0b11111110; //Enable interrupt-on-change for all pins.
  72.  
  73.     while(1)
  74.     {
  75.         if(GPIObits.GP1 == 0)
  76.             transmit(0);
  77.         else if(GPIObits.GP2 == 0)
  78.             transmit(1);
  79.         else if(GPIObits.GP3 == 0)
  80.             transmit(2);
  81.         else if(GPIObits.GP4 == 0)
  82.             transmit(3);
  83.         else if(GPIObits.GP5 == 0)
  84.             transmit(4);
  85.  
  86.         while((GPIO &= 0b00111110) != 0b00111110); //Wait for button to be released.
  87.  
  88.         INTCONbits.GPIE = 1; //Re-enable interrupts before sleeping.
  89.         SLEEP();
  90.     }
  91.  
  92.     return;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment