Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CTS_Layer.h"
- #include "RTC.h"
- #include "state.h"
- // Define User Configuration values //
- //----------------------------------//
- // Sensor settings
- #define KEY_lvl 0xfffe//375 // Defines the min count for a "key press"
- // Set to ~ half the max delta expected
- #define PRESSED 1
- // Global variables for sensing
- unsigned int base_cnt[2];
- unsigned int meas_cnt[2];
- unsigned int delta_cnt;
- uint8_t touch_press = 0;
- uint8_t CCR0_interrupted = 0;
- extern volatile uint16_t minutes; // Minutes (0 to 12*60-1)
- extern volatile uint8_t count_time; // 4th of seconds counter
- extern State state; // State of the device
- volatile uint8_t fourth = 0; // 4th of seconds spent (0 to 4*60-1)
- // Main Function
- void main(void)
- {
- WDTCTL = WDT_ADLY_1_9; // WDT 250ms interval timer
- IE1 |= WDTIE; // Enable WDT interrupt
- BCSCTL1 = CALBC1_8MHZ; // Set DCO to 8MHz
- DCOCTL = CALDCO_8MHZ;
- BCSCTL1 |= DIVA_0; // ACLK = 32.768kHz/1
- BCSCTL2 = 0b00000000; // MCLK = DCOCLK/1, SMCLK=DCOCLK/1
- BCSCTL3 = LFXT1S_0 + XCAP_3; // LFXT1 = 32768-Hz crystal on LFXT1 + 12.5pF caps
- P1OUT = 0x00;
- P1DIR = 0xFF & ~BIT2;//~(BIT2+BIT6); // All outputs except P1.2 & P1.6 (touch sensors)
- P2OUT = 0x00; // Drive all Port 2 pins low
- P2DIR = 0xFF; // Configure all Port 2 pins outputs
- TA0CTL &= ~(BIT4 + BIT5); // Stop TimerA
- sleep(50);
- // Initialize Baseline measurement
- TI_CAPT_Raw(&middle_button,base_cnt);
- // Main loop starts here
- while (1)
- {
- updateInputs();
- updateStateMachine();
- updateOutputs();
- }
- } // End Main
- void updateTouchSensor(void) {
- // Take raw delta measurement
- TI_CAPT_Raw(&middle_button,meas_cnt);
- if(base_cnt[0] < meas_cnt[0]) // Handle baseline measurement for a base C decrease
- { // beyond baseline, i.e. cap decreased
- base_cnt[0] = (base_cnt[0]+meas_cnt[0]) >> 1; // Re-average baseline up quickly
- delta_cnt = 0; // Zero out delta for position determination
- }
- else
- {
- delta_cnt = base_cnt[0] - meas_cnt[0]; // Calculate delta: c_change
- }
- if (delta_cnt > KEY_lvl) // Determine if each key is pressed per a preset threshold
- {
- // Button is pressed
- //base_cnt = base_cnt - (base_cnt >> 6); // Adjust baseline down, should be VERY slow
- touch_press = PRESSED;
- }
- else
- {
- // Button is not pressed
- base_cnt[0] = base_cnt[0] - (base_cnt[0] >> 3); // Adjust baseline down, should be slow
- touch_press = !PRESSED;
- }
- }
- /******************************************************************************/
- // Timer0_A0 Capture Interrupt Service Routine: Exists LPM0
- /******************************************************************************/
- #pragma vector=TIMER0_A0_VECTOR
- __interrupt void timerA0_capture_event(void) {
- _BIC_SR_IRQ(LPM0_bits); // Exits LPM0
- P1OUT |= BIT6;
- CCR0_interrupted = 1;
- }
- //#ifdef WDT_GATE
- // Watchdog Timer interrupt service routine
- #pragma vector=WDT_VECTOR
- __interrupt void watchdog_timer(void)
- {
- _BIC_SR_IRQ(LPM3_bits); // Exits LPM3
- P1OUT |= BIT0;
- fourth++;
- if(fourth >= 240) {
- fourth = 0;
- minutes++;
- if (minutes >= MIN_IN_HALF_DAY) {
- minutes = 0;
- }
- }
- count_time++;
- }
- //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement