Advertisement
Guest User

main.c

a guest
May 18th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include "CTS_Layer.h"
  2.  
  3. #include "RTC.h"
  4. #include "state.h"
  5.  
  6. // Define User Configuration values //
  7. //----------------------------------//
  8. // Sensor settings
  9. #define KEY_lvl     0xfffe//375         // Defines the min count for a "key press"
  10.                                         // Set to ~ half the max delta expected
  11. #define PRESSED     1
  12.  
  13. // Global variables for sensing
  14. unsigned int base_cnt[2];
  15. unsigned int meas_cnt[2];
  16. unsigned int delta_cnt;
  17. uint8_t touch_press = 0;
  18. uint8_t CCR0_interrupted = 0;
  19.  
  20. extern volatile uint16_t minutes;           // Minutes (0 to 12*60-1)
  21. extern volatile uint8_t count_time;         // 4th of seconds counter
  22. extern State state;                         // State of the device
  23.  
  24. volatile uint8_t fourth = 0;                // 4th of seconds spent (0 to 4*60-1)
  25.  
  26. // Main Function
  27. void main(void)
  28. {
  29.   WDTCTL = WDT_ADLY_1_9;                // WDT 250ms interval timer
  30.   IE1 |= WDTIE;                         // Enable WDT interrupt
  31.  
  32.   BCSCTL1 = CALBC1_8MHZ;                // Set DCO to 8MHz
  33.   DCOCTL = CALDCO_8MHZ;
  34.  
  35.   BCSCTL1 |= DIVA_0;                    // ACLK = 32.768kHz/1
  36.   BCSCTL2 = 0b00000000;                 // MCLK = DCOCLK/1, SMCLK=DCOCLK/1
  37.   BCSCTL3 = LFXT1S_0 + XCAP_3;          // LFXT1 = 32768-Hz crystal on LFXT1 + 12.5pF caps
  38.  
  39.   P1OUT = 0x00;
  40.   P1DIR = 0xFF & ~BIT2;//~(BIT2+BIT6);          // All outputs except P1.2 & P1.6 (touch sensors)
  41.  
  42.   P2OUT = 0x00;                         // Drive all Port 2 pins low
  43.   P2DIR = 0xFF;                         // Configure all Port 2 pins outputs  
  44.  
  45.   TA0CTL &= ~(BIT4 + BIT5);             // Stop TimerA
  46.  
  47.   sleep(50);
  48.  
  49.   // Initialize Baseline measurement
  50.   TI_CAPT_Raw(&middle_button,base_cnt);
  51.  
  52.   // Main loop starts here
  53.     while (1)
  54.     {
  55.         updateInputs();
  56.         updateStateMachine();
  57.         updateOutputs();
  58.     }
  59. } // End Main
  60.  
  61. void updateTouchSensor(void) {
  62.     // Take raw delta measurement
  63.     TI_CAPT_Raw(&middle_button,meas_cnt);
  64.  
  65.     if(base_cnt[0] < meas_cnt[0])   // Handle baseline measurement for a base C decrease
  66.     {                       // beyond baseline, i.e. cap decreased
  67.         base_cnt[0] = (base_cnt[0]+meas_cnt[0]) >> 1;   // Re-average baseline up quickly
  68.         delta_cnt = 0;                          // Zero out delta for position determination
  69.     }
  70.     else
  71.     {
  72.         delta_cnt = base_cnt[0] - meas_cnt[0];  // Calculate delta: c_change
  73.     }
  74.  
  75.     if (delta_cnt > KEY_lvl)    // Determine if each key is pressed per a preset threshold
  76.     {
  77.         // Button is pressed
  78.         //base_cnt = base_cnt - (base_cnt >> 6);    // Adjust baseline down, should be VERY slow
  79.         touch_press = PRESSED;
  80.     }
  81.     else
  82.     {
  83.         // Button is not pressed
  84.         base_cnt[0] = base_cnt[0] - (base_cnt[0] >> 3); // Adjust baseline down, should be slow
  85.         touch_press = !PRESSED;
  86.     }
  87. }
  88.  
  89. /******************************************************************************/
  90. // Timer0_A0 Capture Interrupt Service Routine: Exists LPM0
  91. /******************************************************************************/
  92. #pragma vector=TIMER0_A0_VECTOR
  93. __interrupt void timerA0_capture_event(void) {
  94.     _BIC_SR_IRQ(LPM0_bits);                 // Exits LPM0
  95.     P1OUT |= BIT6;
  96.     CCR0_interrupted = 1;
  97. }
  98.  
  99.  
  100. //#ifdef WDT_GATE
  101. // Watchdog Timer interrupt service routine
  102. #pragma vector=WDT_VECTOR
  103. __interrupt void watchdog_timer(void)
  104. {
  105.     _BIC_SR_IRQ(LPM3_bits);                 // Exits LPM3
  106.     P1OUT |= BIT0;
  107.  
  108.     fourth++;
  109.     if(fourth >= 240) {
  110.         fourth = 0;
  111.         minutes++;
  112.         if (minutes >= MIN_IN_HALF_DAY) {
  113.             minutes = 0;
  114.         }
  115.     }
  116.  
  117.     count_time++;
  118. }
  119. //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement