kevin2458

Kevin Lara - 201110510072 - Microcontroladores - 25/06/2016

Jun 24th, 2016
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define LED_1 LATA.F0
  2. #define LED_2 LATA.F1
  3.  
  4. #define TOUCH_KEY_CLICK_INPUT_0 PORTB.F2
  5. #define TOUCH_KEY_CLICK_INPUT_1 PORTA.F2
  6. #define TOUCH_KEY_CLICK_INPUT_2 PORTC.F6
  7. #define TOUCH_KEY_CLICK_INPUT_3 PORTD.F4
  8.  
  9. #define EVERLOOP while (1)
  10.  
  11. #define STATE_LOW  0
  12. #define STATE_HIGH 1
  13.  
  14. #define PUSH_BUTTON_1_2_PRESSED 0
  15. #define PUSH_BUTTON_1_PRESSED   4
  16. #define PUSH_BUTTON_2_PRESSED   8
  17.  
  18. #define TOUCH_KEY_CLICK_C_PRESSED   4
  19. #define TOUCH_KEY_CLICK_D_PRESSED   8
  20. #define TOUCH_KEY_CLICK_C_D_PRESSED 12
  21.  
  22. #define DEFAULT_DELAY_MS 100
  23.  
  24. void initMCU() {
  25.   ADCON0 = 0;
  26.   ADCON1 = 0;
  27.  
  28.   LATA = 0;
  29.   LATB = 0;
  30.   LATC = 0;
  31.   LATD = 0;
  32.  
  33.   PORTA = 0;
  34.   PORTB = 0;
  35.   PORTC = 0;
  36.   PORTD = 0;
  37.  
  38.   TRISA.F0 = 0;
  39.   TRISA.F1 = 0;
  40.   TRISD.F3 = 1;
  41.   TRISD.F2 = 1;
  42.  
  43.   TOUCH_KEY_CLICK_INPUT_0 = 1;
  44.   TOUCH_KEY_CLICK_INPUT_1 = 1;
  45.   TOUCH_KEY_CLICK_INPUT_2 = 1;
  46.   TOUCH_KEY_CLICK_INPUT_3 = 1;
  47. }
  48.  
  49. char getTouchKeyClickReading() {
  50.   char reading;
  51.  
  52.   reading.B0 = TOUCH_KEY_CLICK_INPUT_0;
  53.   reading.B1 = TOUCH_KEY_CLICK_INPUT_1;
  54.   reading.B2 = TOUCH_KEY_CLICK_INPUT_2;
  55.   reading.B3 = TOUCH_KEY_CLICK_INPUT_3;
  56.  
  57.   return reading;
  58. }
  59.  
  60. void main() {
  61.   initMCU();
  62.  
  63.   EVERLOOP {
  64.     switch (getTouchKeyClickReading()) {
  65.       case TOUCH_KEY_CLICK_C_PRESSED:
  66.         LED_1 = LED_1 ? 0 : 1;
  67.  
  68.         delay_ms(DEFAULT_DELAY_MS);
  69.         break;
  70.  
  71.       case TOUCH_KEY_CLICK_D_PRESSED:
  72.         LED_2 = LED_2 ? 0 : 1;
  73.  
  74.         delay_ms(DEFAULT_DELAY_MS);
  75.         break;
  76.  
  77.       case TOUCH_KEY_CLICK_C_D_PRESSED:
  78.         if (LED_1 == STATE_LOW) {
  79.           LED_1 = STATE_HIGH;
  80.         }
  81.  
  82.         if (LED_2 == STATE_HIGH) {
  83.           LED_2 = STATE_LOW;
  84.         }
  85.  
  86.         delay_ms(DEFAULT_DELAY_MS / 2);
  87.  
  88.         if (LED_1 == STATE_HIGH) {
  89.           LED_1 = STATE_LOW;
  90.         }
  91.  
  92.         if (LED_2 == STATE_LOW) {
  93.           LED_2 = STATE_HIGH;
  94.         }
  95.  
  96.         delay_ms(DEFAULT_DELAY_MS / 2);
  97.         break;
  98.  
  99.       default:
  100.         if (LED_1 == STATE_HIGH && PORTD != PUSH_BUTTON_1_PRESSED) {
  101.           LED_1 = STATE_LOW;
  102.         }
  103.  
  104.         if (LED_2 == STATE_HIGH && PORTD != PUSH_BUTTON_2_PRESSED) {
  105.           LED_2 = STATE_LOW;
  106.         }
  107.         break;
  108.     }
  109.  
  110.     switch (PORTD) {
  111.       case PUSH_BUTTON_1_2_PRESSED:
  112.         if (getTouchKeyClickReading() != TOUCH_KEY_CLICK_C_D_PRESSED) {
  113.           if (LED_1 == STATE_LOW) {
  114.             LED_1 = STATE_HIGH;
  115.           }
  116.  
  117.           if (LED_2 == STATE_LOW) {
  118.             LED_2 = STATE_HIGH;
  119.           }
  120.         }
  121.         break;
  122.  
  123.       case PUSH_BUTTON_1_PRESSED:
  124.         if (getTouchKeyClickReading() != TOUCH_KEY_CLICK_C_PRESSED) {
  125.           if (LED_1 == STATE_LOW) {
  126.             LED_1 = STATE_HIGH;
  127.           }
  128.         }
  129.         break;
  130.  
  131.       case PUSH_BUTTON_2_PRESSED:
  132.         if (getTouchKeyClickReading() != TOUCH_KEY_CLICK_D_PRESSED) {
  133.           if (LED_2 == STATE_LOW) {
  134.             LED_2 = STATE_HIGH;
  135.           }
  136.         }
  137.         break;
  138.  
  139.       default:
  140.         if (LED_1 == STATE_HIGH &&
  141.             getTouchKeyClickReading() != TOUCH_KEY_CLICK_C_PRESSED) {
  142.           LED_1 = STATE_LOW;
  143.         }
  144.  
  145.         if (LED_2 == STATE_HIGH &&
  146.             getTouchKeyClickReading() != TOUCH_KEY_CLICK_D_PRESSED) {
  147.           LED_2 = STATE_LOW;
  148.         }
  149.         break;
  150.     }
  151.   }
  152. }
RAW Paste Data