#define LED_DIR P1DIR #define LED_OUT P1OUT
#define BUTTON BIT3 #define BUTTON_OUT P1OUT #define BUTTON_DIR P1DIR #define BUTTON_IN P1IN #define BUTTON_IE P1IE #define BUTTON_IES P1IES #define BUTTON_IFG P1IFG #define BUTTON_REN P1REN
#define TXD BIT1 #define RXD BIT2
#define APP_STANDBY_MODE 0 #define APP_APPLICATION_MODE 1
#define TIMER_PWM_MODE 0 #define TIMER_UART_MODE 1 #define TIMER_PWM_PERIOD 2000 #define TIMER_PWM_OFFSET 20
#define TEMP_SAME 0 #define TEMP_HOT 1 #define TEMP_COLD 2
#define TEMP_THRESHOLD 5
// Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz
#define Bitime_5 0x05*4 + small adjustment
#define Bitime 13*4//0x0D
#define UART_UPDATE_INTERVAL 1000
// TXD on P1.1 // RXD on P1.2 // ~ 0.5 bit length
unsigned char BitCnt;
unsigned char applicationMode = APP_STANDBY_MODE; unsigned char timerMode = TIMER_PWM_MODE;
unsigned char tempMode; unsigned char calibrateUpdate = 0;
unsigned char tempPolarity = TEMP_SAME; unsigned int TXByte;
/* Using an 8-value moving average filter on sampled ADC values */ long tempMeasured[8];
unsigned char tempMeasuredPosition=0; long tempAverage;
long tempCalibrated, tempDifference;
void InitializeLeds(void); void InitializeButton(void);
void PreApplicationMode(void); // Blinks LED, waits for button press
void ConfigureAdcTempSensor(void); void ConfigureTimerPwm(void); void ConfigureTimerUart(void); void Transmit(void); void InitializeClocks(void);
void main(void) {
unsigned int uartUpdateTimer = UART_UPDATE_INTERVAL; unsigned char i;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
InitializeClocks(); InitializeButton(); InitializeLeds();
PreApplicationMode(); // Blinks LEDs, waits for button press
/* Application Mode begins */
applicationMode = APP_APPLICATION_MODE; ConfigureAdcTempSensor(); ConfigureTimerPwm();
__enable_interrupt(); // Enable interrupts.
/* Main Application Loop */ while(1) {
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled
/* Moving average filter out of 8 values to somewhat stabilize sampled ADC */ tempMeasured[tempMeasuredPosition++] = ADC10MEM; if (tempMeasuredPosition == 8) tempMeasuredPosition = 0; tempAverage = 0; for (i = 0; i < 8; i++)
tempAverage += tempMeasured[i];
tempAverage >>= 3; // Divide by 8 to get average
if ((--uartUpdateTimer == 0) || calibrateUpdate ) {
ConfigureTimerUart(); if (calibrateUpdate) {
TXByte = 248; // A character with high value, outside of temp range
Transmit(); calibrateUpdate = 0; }
TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 ); Transmit();
uartUpdateTimer = UART_UPDATE_INTERVAL; ConfigureTimerPwm(); }
tempDifference = tempAverage - tempCalibrated; if (tempDifference < -TEMP_THRESHOLD) {
tempDifference = -tempDifference; tempPolarity = TEMP_COLD; LED_OUT &= ~ LED1; } else
if (tempDifference > TEMP_THRESHOLD) {
tempPolarity = TEMP_HOT; LED_OUT &= ~ LED0; } else {
tempPolarity = TEMP_SAME; TACCTL0 &= ~CCIE; TACCTL1 &= ~CCIE;
LED_OUT &= ~(LED0 + LED1); }
if (tempPolarity != TEMP_SAME) {
tempDifference <<= 3;
tempDifference += TIMER_PWM_OFFSET;
TACCR1 = ( (tempDifference) < (TIMER_PWM_PERIOD-1) ? (tempDifference) : (TIMER_PWM_PERIOD-1) );
TACCTL0 |= CCIE; TACCTL1 |= CCIE; } } }
void PreApplicationMode(void) {
LED_DIR |= LED0 + LED1;
LED_OUT |= LED0; // To enable the LED toggling effect LED_OUT &= ~LED1;
BCSCTL1 |= DIVA_1; // ACLK/2 BCSCTL3 |= LFXT1S_2; // ACLK = VLO
TACCR0 = 1200; //
TACTL = TASSEL_1 | MC_1; // TACLK = SMCLK, Up mode. TACCTL1 = CCIE + OUTMOD_3; // TACCTL1 Capture Compare TACCR1 = 600;
__bis_SR_register(LPM3_bits + GIE); // LPM0 with interrupts enabled }
void ConfigureAdcTempSensor(void) {
unsigned char i;
/* Configure ADC Temp Sensor Channel */
ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; __delay_cycles(1000); // Wait for ADC Ref to settle ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled tempCalibrated = ADC10MEM; for (i=0; i < 8; i++)
tempMeasured[i] = tempCalibrated; tempAverage = tempCalibrated; }