Part Number:MSP430FR6989
Tool/software: Code Composer Studio
I have 2 devices MSP430FR6989. I am a beginner in this domain and trying to figure things out :)
I am sending 2 bytes data from master to slave. One byte increses by 1, the otherone decreses by 1.
I am trying to add an operational mode using timer A and button states(LONG_PRESS, SHORT_PRESS and TRIPLE_PRESS).
I want after 5 seconds of sending correct data to enter in the operational mode and the buttons to function.
I do not know exactly how to change the code in the USCI interrupt and Timer interrupt so that the buttons trigger the modes I need.
I attach the code:
/* --COPYRIGHT--,BSD_EX
* Copyright (c) 2014, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************
*
* MSP430 CODE EXAMPLE DISCLAIMER
*
* MSP430 code examples are self-contained low-level programs that typically
* demonstrate a single peripheral function or device feature in a highly
* concise manner. For this the code may rely on the device's power-on default
* register values and settings such as the clock configuration and care must
* be taken when combining code from several examples to avoid potential side
* effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
* for an API functional library-approach to peripheral configuration.
*
* --/COPYRIGHT--*/
//******************************************************************************
// MSP430F59xx Demo - eUSCI_A0, SPI 3-Wire Master Incremented Data
//
// Description: SPI master talks to SPI slave using 3-wire mode. Incrementing
// data is sent by the master starting at 0x01. Received data is expected to
// be same as the previous transmission TXData = RXData-1.
// USCI RX ISR is used to handle communication with the CPU, normally in LPM0.
// ACLK = 32.768kHz, MCLK = SMCLK = DCO ~1MHz. BRCLK = ACLK/2
//
//
// MSP430FR6989
// -----------------
// /|\ | XIN|-
// | | | 32KHz Crystal
// ---|RST XOUT|-
// | |
// | P2.0|-> Data Out (UCA0SIMO)
// | |
// | P2.1|<- Data In (UCA0SOMI)
// | |
// | P1.5|-> Serial Clock Out (UCA0CLK)
//
// William Goh
// Texas Instruments Inc.
// April 2014
// Built with IAR Embedded Workbench V5.60 & Code Composer Studio V6.0
//******************************************************************************
#include <msp430.h>
#include "lcd.h"
volatile unsigned char RXData;
volatile unsigned char TXData;
volatile unsigned char TXData1;
void transmitData(char *rxData);
void Init_GPIO(void);
// Code for button states in the SPI
void Timer_A0_Init(void);
void Init_Btn_GPIO(void);
void btn_states(void);
unsigned char state = 0;
unsigned char last_state = 0;
unsigned char counter_5ms = 0;
volatile unsigned char button_pressed = 0;
static unsigned char counter_triple_press = 0;
enum btn_states
{
INIT, READY, LONG_PRESS, SHORT_PRESS, TRIPLE_PRESS
};
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
Init_Btn_GPIO();
// initialize the GPIO for the SPI
Init_GPIO();
// Initialize the SPI for displaying transmit/received data
Init_LCD();
Timer_A0_Init();
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// XT1 Setup
CSCTL0_H = CSKEY >> 8; // Unlock CS registers
CSCTL1 = DCOFSEL_0; // Set DCO to 1MHz
CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1; // set all dividers
CSCTL4 &= ~LFXTOFF;
do
{
CSCTL5 &= ~LFXTOFFG; // Clear XT1 fault flag
SFRIFG1 &= ~OFIFG;
}
while (SFRIFG1 & OFIFG); // Test oscillator fault flag
CSCTL0_H = 0; // Lock CS registers
// Configure USCI_A0 for SPI operation
UCA0CTLW0 = UCSWRST; // **Put state machine in reset**
UCA0CTLW0 |= UCMST | UCSYNC | UCCKPL | UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCA0CTLW0 |= UCSSEL__ACLK; // ACLK
UCA0BR0 = 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTLW = 0; // No modulation
UCA0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
TXData = 0x00; // Holds TX data
TXData1 = 0xff;
while (1)
{
UCA0IE |= UCTXIE;
//__enable_interrupt();
__bis_SR_register(LPM0_bits | GIE); // CPU off, enable interrupts
__delay_cycles(1000000); // Delay before next transmission
TXData++; // Increment transmit data
TXData1--;
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch (__even_in_range(UCA0IV, USCI_SPI_UCTXIFG))
{
case USCI_NONE:
break;
case USCI_SPI_UCRXIFG:
RXData = UCA0RXBUF;
clearLCD();
displayNumber(UCA0RXBUF);
UCA0IFG &= ~UCRXIFG;
__bic_SR_register_on_exit(LPM0_bits); // Wake up to setup next TX
break;
case USCI_SPI_UCTXIFG:
UCA0TXBUF = TXData; // Transmit characters
while (!(UCA0IFG & UCRXIFG))
; //wait for register to empty
UCA0TXBUF = TXData1; //2nd byte
// clearLCD();
// displayNumber(UCA0TXBUF);
while ((UCA0STATW & BIT0))
; //wait for the the busy signal to clear
UCA0IE &= ~UCTXIE;
break;
default:
break;
}
}
// Timer0_A0 interrupt service routine
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer0_A0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) Timer0_A0_ISR (void)
#else
#error Compiler not supported!
#endif
{
btn_states();
// add offset to TA0CCR0
//TA0CCR0 += 50000;
counter_5ms++;
}
void Init_GPIO(void)
{
P1DIR |= BIT0;
P1OUT |= BIT0;
P9DIR |= BIT7;
P9OUT |= BIT7;
P1OUT ^= BIT0;
P9OUT ^= BIT7;
// Configure GPIO
P1SEL1 |= BIT5; // USCI_A0 operation
P2SEL0 |= BIT0 | BIT1; // USCI_A0 operation
PJSEL0 |= BIT4 | BIT5; // For XT1
}
void Init_Btn_GPIO(void)
{
// Configure GPIO
P1DIR |= BIT0;
P1OUT |= BIT0;
P1OUT = BIT1; // Pull-up resistor on P1.1
P1REN = BIT1; // Select pull-up mode for P1.1
P1DIR = 0xFF ^ BIT1; // Set all but P1.1 to output direction
P1IES = BIT1; // P1.1 Hi/Lo edge
}
void Timer_A0_Init(void)
{
// TACCR0 interrupt enabled
TA0CCTL0 = CCIE;
TA0CCR0 = 5000;
// SMCLK continous mode
TA0CTL = TASSEL__SMCLK | MC__CONTINOUS;
}
void btn_states(void)
{
switch (state)
{
case INIT:
{
displayNumber(1);
if (state != INIT)
{
last_state = INIT;
}
state = READY;
break;
}
case READY:
{
if ((P1IN & BIT1) == 0)
{
counter_5ms++;
button_pressed = 1;
}
else
{
if (button_pressed == 1)
{
button_pressed = 0;
if (counter_5ms < 50)
{
state = SHORT_PRESS;
}
else if (counter_5ms >= 100)
{
state = LONG_PRESS;
}
counter_5ms = 0;
}
}
break;
}
case LONG_PRESS:
{
last_state = LONG_PRESS;
displayNumber(111);
counter_triple_press++;
if (counter_triple_press == 3)
{
state = TRIPLE_PRESS;
}
else
{
state = READY;
}
break;
}
case SHORT_PRESS:
{
last_state = SHORT_PRESS;
displayNumber(222);
// counter_triple_press = 0;
state = READY;
break;
}
case TRIPLE_PRESS:
{
last_state = TRIPLE_PRESS;
displayNumber(333);
counter_triple_press = 0;
state = READY;
break;
}
default:
break;
}
}