int main() { int driverPointer = 0; // Stop the watchdog WDTCTL = WDTPW | WDTHOLD; // enable output for pins connected to LEDs on Launchpad // init the peripherals initClocks(); initTimer(); P1DIR |= BIT0 | BIT1 | BIT2 | BIT4; P1OUT |= BIT0 | BIT1 | BIT2 | BIT4; P1REN &= ~(BIT0|BIT1|BIT2|BIT4); // enable interrupts __bis_status_register(GIE); while(1) { __nop(); __bis_status_register(CPUOFF); if (P1IN & BIT3){ driverPointer +=1; TACCR0 = SIDEREAL_RATE; } else { driverPointer -= 1; TACCR0 = 300; } driverPointer = driverPointer & ((sizeof DRIVER_TABLE / sizeof *DRIVER_TABLE)-1); setDriver(DRIVER_TABLE[driverPointer]); } }
void vBSP430platformInitialize_ni (void) { int crystal_ok = 0; (void)crystal_ok; #if BSP430_PLATFORM_BOOT_DISABLE_WATCHDOG - 0 /* Hold off watchdog */ WDTCTL = WDTPW | WDTHOLD; #endif /* configBSP430_CORE_SUPPORT_WATCHDOG */ #if (BSP430_PLATFORM_BOOT_CONFIGURE_LEDS - 0) && (BSP430_LED - 0) vBSP430ledInitialize_ni(); #endif /* BSP430_PLATFORM_BOOT_CONFIGURE_LEDS */ #if BSP430_PLATFORM_BOOT_CONFIGURE_LFXT1 - 0 /* Enable XT1 functions and clock */ crystal_ok = iBSP430clockConfigureLFXT1_ni(1, (BSP430_PLATFORM_BOOT_LFXT1_DELAY_SEC * BSP430_CLOCK_PUC_MCLK_HZ) / BSP430_CLOCK_LFXT1_STABILIZATION_DELAY_CYCLES); #endif /* BSP430_PLATFORM_BOOT_CONFIGURE_LFXT1 */ #if BSP430_PLATFORM_BOOT_CONFIGURE_CLOCKS - 0 iBSP430clockConfigureACLK_ni(BSP430_PLATFORM_BOOT_ACLKSRC); ulBSP430clockConfigureMCLK_ni(BSP430_CLOCK_NOMINAL_MCLK_HZ); iBSP430clockConfigureSMCLKDividingShift_ni(BSP430_CLOCK_NOMINAL_SMCLK_DIVIDING_SHIFT); #if configBSP430_CORE_DISABLE_FLL - 0 __bis_status_register(SCG0); #endif /* configBSP430_CORE_DISABLE_FLL */ #endif /* BSP430_PLATFORM_BOOT_CONFIGURE_CLOCKS */ #if BSP430_UPTIME - 0 vBSP430uptimeStart_ni(); #endif /* BSP430_UPTIME */ }
void configure_ports(const uint8_t *port_mapping, uint8_t *PxMAPy, uint8_t num_of_ports, uint8_t port_map_reconfig) { uint16_t i; // Store current interrupt state, then disable all interrupts uint16_t globalInterruptState = __read_status_register() & GIE; __dint(); // Get write-access to port mapping registers: PMAPPWD = PMAPPW; if (port_map_reconfig){ // Allow reconfiguration during runtime: PMAPCTL = PMAPRECFG; } // Configure Port Mapping: for (i = 0; i < num_of_ports * 8; i++){ PxMAPy[i] = port_mapping[i]; } // Disable write-access to port mapping registers: PMAPPWD = 0; // Restore previous interrupt state __bis_status_register(globalInterruptState); }
int main(void){ MSP430_Init(); MSP430_Ports(); MSP430_Timer(); TLC5940_Init(); TLC5940_SetAllDC(63); TLC5940_ClockInDC(); // Default all channels to off TLC5940_SetAllGS(0); channel_t ch = 0; uint16_t bright = 0; // GLOBAL INTERRUPTS ENABLED __bis_status_register(GIE); for(;;){ while(uigsUpdateFlag); // wait until we can modify gsData //TLC5940_SetAllGS(0); TLC5940_SetGS(ch, bright); TLC5940_SetGSUpdateFlag(); __delay_cycles(10000); ch = (ch + 1) % NUM_CHANNELS; bright++; if (bright > 4095){ bright = 0; } } return 0; }
/* * Block in low power mode for ms milliseconds */ void timer_sleep_ms(uint16_t ms, uint32_t mode) { #if SC_USE_SLEEP == 1 timer_repeats = 0; timer_set(ms); __bis_status_register(mode + GIE); #else busysleep_ms(ms); #endif }
/* * Start sending (unless already sending) */ void uart_send_next_msg(void) { // Disable interrupts to make sure UartTxBuffer state isn't modified in the middle __bic_status_register(GIE); if (UartTxBufferLength > 0 && uart_state != UART_STATE_TX) { uart_state = UART_STATE_TX; UCA0TXBUF = UartTxBuffer[UartTxBuffer_i]; // Send first byte } // Enable interrupts __bis_status_register(GIE); }
void oscillatorFault(){ /*blinks in case of osc fault*/ BCSCTL3 = LFXT1S_2; // DEFAULT : XTS = 0 -> uses VLO for LFXT1 IFG1 &= ~OFIFG; // clear interrupt flg BCSCTL2 |= SELM_3|SELS; // MCLK = SMCLK = LFXT1 (see -2lines = VLO) __bis_status_register(SCG1+SCG0); // stop MCLCK & SMCLK (settingthem vlo => cut power consumption) while(1){ P1OUT ^= BIT0; __delay_cycles(500); // blink Red for problem } }
/* Change the current power-saving mode. */ enum lpm_mode lpm_set(enum lpm_mode target) { enum lpm_mode last_mode = lpm_get(); switch (target) { case LPM_ON: // fully running MCU __bic_status_register(CPUOFF | OSCOFF | SCG0 | SCG1); break; case LPM_IDLE: // lightest mode => LPM0 mode of MSP430 __bic_status_register(OSCOFF | SCG0 | SCG1); // only stops CPU block __bis_status_register(CPUOFF); break; case LPM_SLEEP: // mid-level mode => LPM1 mode of MSP430 __bic_status_register(OSCOFF | SCG1); // stops CPU and master clock blocks __bis_status_register(CPUOFF | SCG0); break; case LPM_POWERDOWN: // deep-level mode => LPM3 mode of MSP430 __bic_status_register(OSCOFF); // stops all blocks except auxiliary clock (timers) __bis_status_register(CPUOFF | SCG0 | SCG1); break; case LPM_OFF: // MCU totally down (LPM4), only RESET or NMI can resume execution __bis_status_register(CPUOFF | OSCOFF | SCG0 | SCG1); // all blocks off break; default: printf("ERROR: trying to set an invalid low-power mode!\n"); printf(" Operation aborted.\n\n"); } return last_mode; }
/* * Block in low poewr mode for min minutes */ void timer_sleep_min(uint16_t min, uint32_t mode) { #if SC_USE_SLEEP == 1 // Repeat 10sec sleep 6*min times. timer_repeats = 6 * min; timer_set(10000); // sleep 10 sec timer_repeats times __bis_status_register(mode + GIE); #else int i; for (i = 0; i < 6 * min; ++i) { busysleep_ms(10000); } #endif }
/* * Append new message to transmit buffer */ uint8_t uart_tx_append_msg(unsigned char *buf, unsigned char len) { int i; // Disable interrupts to make sure UartTxBuffer state isn't modified in the middle __bic_status_register(GIE); // Check that there's enough space if (UartTxBufferLength + len >= UART_BUF_LEN) { // Enable interrupts __bis_status_register(GIE); return 0; } for (i = 0; i < len; ++i) { UartTxBuffer[UartTxBufferLength + i] = buf[i]; } UartTxBufferLength += i; // Enable interrupts __bis_status_register(GIE); return i; }
int main(void) { int prevPwm = 0; int prevDegC = 0; int degC; int pwm; WDTCTL = WDTPW + WDTSSEL + WDTCNTCL; // ACLK / 32768: 3,2s // config clk BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; BCSCTL3 = LFXT1S_2; // Use VLO // Unused pins as inputs with pull-down. P1DIR = 0; P1REN = ~(MOSFET_GATE_PIN | BIT1 | BIT2 | ZERO_DETECT_PIN); P2DIR = 0; P2REN = 0xff; initConsole(); printf("Start\r\n"); ADC10AE0 |= (1 << 4); // PA.1 ADC option select // Configure MOSFET gate pin P1OUT &= ~MOSFET_GATE_PIN; P1DIR |= MOSFET_GATE_PIN; // P1.6 output // Configure voltage zero level detect pin P1IE |= ZERO_DETECT_PIN; P1IES |= ZERO_DETECT_PIN; // Timer A0 for PWM TA0CCR0 = (ACLK_HZ / PWM_HZ) - 1; TA0CCR1 = 0; TA0CCTL1 = OUTMOD_7 | CCIE; // Positive PWM TA0CCTL0 = CCIE; TA0CTL = TASSEL_1 + MC_1 + TACLR + TAIE; // ACLK, upmode __eint(); while (1) { WDTCTL = WDTPW + WDTSSEL + WDTCNTCL; // ACLK / 32768: 3,2s __bis_status_register(CPUOFF); degC = adcTemp - *tempCal30; degC = 30 + ((float)degC) / 1024 * 1500 / 3.55; // Filter temperature changes, // during cooling react only if change // is more than 2 degrees. if (degC > prevDegC) prevDegC = degC; else if (prevDegC - degC >= 2) prevDegC = degC; printf ("adc %d degc %d filtdegc %d ", adcPot, degC, prevDegC); if (adcPot < 20) adcPot = 0; else if (adcPot > 1000) adcPot = 1000; pwm = adcPot / 10; // PWM % if (pwm > 10 && prevDegC > 40) { // check overheat pwm = 10; printf ("overheat "); } printf("pwm %d %%\r\n", pwm); if (abs(prevPwm - pwm) > 2) { prevPwm = pwm; TA0CCR1 = ((float)pwm) / 100.0 * (ACLK_HZ / PWM_HZ); } } }