6f Programs
6f Programs
1. Blinking LED
2. LED control using switch
3. GPIO interrupt
4. ADC & PWM application – speed control of dc motor
5. Serial communication using SPI protocol
6. Serial communication using UART
7. Wi-Fi application using CC3100
8. Remote control of Air conditioner using MSP430
1. Blinking LED
Program to blink the on-board red LED (connected to P1.0) using GPIO :
The MSP430G2553 has two general-purpose digital I/O pins connected to green LED (P1.6) and
red LED (P1.0) as shown in figure.
The program code first disables the Watch Dog Timer (WDT) to prevent a processor restart
on expiry of the WDT count.
The port pin P1.0 connected to the red LED is configured as output. A HIGH on the pin
turns the red LED on, while a LOW turns the LED off.
The P1.0 output is toggled using bit XOR function at regular intervals determined by the
software delay set in the program using variable ‘i’.
Thus, the red LED on the MSP-EXP430G2 Launch Pad blinks at regular intervals.
PROGRAM :
#include<msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction
while(1)
{
volatile unsigned long i; // Volatile to prevent optimization
P1OUT ^= 0x01; // Toggle P1.0 using XOR
i = 50000; // SW Delay
do
{
i--;
} while(i != 0);
}
} // end main
Program to control the on-board green LED of MSP430 by an input from the on-board switch
The MSP-EXP430G2 Launch Pad has two general-purpose digital I/O pins connected to
green LED (P1.6) and red LED (P1.0) for visual feedback.
It also has a push button (p1.3) for user feedback
In this experiment, the input from the left push button switch (S2) connected to Port P1.3 is
read by the processor for LED control.
If the switch is pressed, the green LED is turned OFF by output at port P1.6 reset to ’0’.
Else, the output at port P1.6 is set HIGH and the green LED is turned ON.
Thus the green LED is therefore controlled by the switch S2.
PROGRAM :
#include<msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x40; // Set P1.6 to output direction
P1REN |= 0x08;
P1OUT |= 0X08;
while(1)
{
if ((P1IN & BIT3)) // If button is open(P1.3 HIGH)
{
P1OUT = P1OUT | BIT6; // ... turn on LED
}
else
{
P1OUT = P1OUT & ~BIT6; // ... else turn it off.
}
} // end while
} // end main
3. GPIO Interrupt :
The MSP430G2553 has one active mode and six software selectable low-power modes of
operation. An interrupt event can wake up the device from any of the low-power modes, service
the request, and restore back to the low power mode on return from the interrupt program. And
these Interrupts may be generated from the GPIO of MSP430G2553.
Some GPIOs in the MSP430G2553 have the capability to generate an interrupt and inform
the CPU when a transition has occurred. The MSP430G2553 allows flexibility in configuring which
GPIO will generate the interrupt, and on what edge (rising or falling). MSP430G2553 devices
typically have interrupt capability on Ports 1 and 2.
The program code first disables the WDT to prevent a restart on expiry of the WDT count. The
port pin P1.6 connected to the green LED is configured as output.
The port pin P1.3 connected to switch S2 is configured as falling edge interrupt.
The global interrupt is enabled with low power mode 4 enabled during interrupt.
On each interrupt, the green LED connected to P1.6 is toggled.
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= BIT6; // Set P1.6 to output direction
P1REN |= BIT3; // Enable P1.3 internal resistance
P1OUT |= BIT3; // Set P1.3 as pull up resistance
P1IES |= BIT3; // P1.3 High/Low Edge
P1IFG &= ~BIT3; // P1.3 IFG Cleared
P1IE |= BIT3; // P1.3 Interrupt Enabled
#pragma vector=PORT1_VECTOR
__interrupt void Port_1 (void)
{
P1OUT ^= BIT6; // Toggle P1.6
P1IFG &= ~BIT3; // P1.3 IFG Cleared
}
The idea behind PWM is very simple: The load is switched on and off periodically so that
the average voltage has the desired value.
The fraction of the time while the load is active is called the duty cycle D.
The duty cycle is almost always varied by keeping the period constant and changing the
width of the pulses, hence the name of PWM.
The period of Timer_A is set by TACCR0 in the Up mode.
The main parameters that must be chosen before suitable values can be selected for PWM:
(a) The time period of the output PWM waveform = (TACCR0 +1) counts
The above Figure shows that, the Reset/Set output mode (7) is used for active high loads,
called as positive PWM and the Set/Reset mode (3) is used for active low loads, called as
negative PWM.
Note that by changing the value in TACCR1, we can change the duty cycle.
#include <msp430.h>
int pwmDirection = 1;
void main(void)
{
WDTCTL = WDTPW|WDTHOLD; // Stop WDT
ADC10CTL0 = SREF_0 + ADC10SHT_2 + ADC10ON;
ADC10CTL1 = INCH_3; // input A3
ADC10AE0 |= 0x08; // PA.3 ADC option select
P1DIR |= BIT6; // Green LED for output
P1SEL |= BIT6; // Green LED Pulse width modulation
TA0CCR0 = 1024; // PWM period
TA0CCR1 = 1; // PWM duty cycle,on 1/1000 initially
TA0CCTL1 = OUTMOD_7; // TA0CCR1 reset/set mode (7)
TA0CTL = TASSEL_2 + MC_1 + TAIE +ID_3;
_BIS_SR(LPM4_bits + GIE);
while (1); // Enter Low power mode 0
}
Serial Peripheral Interface (SPI) mode is selected when the UCSYNC bit is set and SPI mode (3-
pin or 4-pin) is selected with the UCMODEx bits.
FLOW CHART :
The flowchart for the code of the master is shown in above figure.
The program code first disables WDT to prevent a processor restart on expiry of the WDT
count.
The port pin P1.1 is set as slave reset and port pin P1.0 is configured for the red LED.
The port pins P3.3, P3.4 and P2.7 are configured for SPI communication. The USCI logic is held
in reset state and the 3-pin, 8- bit synchronous SPI master is enabled.
The SMCLK is selected as clock source and the USCI logic initiated. The USCI_RX interrupt is
enabled in low power mode and the slave is reset via output pin P1.1 and allowed to initialize.
The data values to be transmitted to the slave is also initialized and transmitted via the
transmit buffer UCA0TXBUF.
On receiving data from the slave, the USCI_RX interrupt occurs. If the data received is same as
the data transmitted, the red LED is turned on and the data transmission is continued.
PROGRAM :
#include <msp430.h>
unsigned char MST_Data,SLV_Data;
unsigned char temp;
int main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
P1OUT |= 0x02; // Set P1.0 for LED
// Set P1.1 for slavereset
P1DIR |= 0x03; // Set P1.0-2 to output direction
P3SEL |= BIT3+BIT4; // P3.3,4 option select
P2SEL |= BIT7; // P2.7 option select
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB;
// 3-pin,8-bit SPI synchronous master, Clock polarity high,MSB
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x02; // division /2
UCA0BR1 = 0;
UCA0MCTL = 0; // No modulation
The block diagram for the experiment is as shown in Figure 9-1. The UART baud rate is set to
9600. The UART specifies two pins, a TX pin (located at P1.2) and an RX pin (P1.1) for
communication. The USCI-UART module is configured to transmit a preset message on an
interrupt. An interrupt provided within the program code transmits the message to be displayed
on the UART terminal of the computer.
PROGRAM :
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
if (CALBC1_1MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; //Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // CLK = SMCLK
UCA0BR0 = 104; // 1MHz/9600 = 104.166
UCA0BR1 = 0x00;
UCA0MCTL = UCBRS0; // Modulation UCBRx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ int
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF;
}
The MSP430FR4133 is a FRAM-based ultra-low power mixed signal MCU. With the
following features, the MSP430FR4133 is highly suitable for portable device applications.
Circuit Design
A 4x28 segment LCD is directly connected to the MSP430FR4133 LCD driver pins.
A 4x4 matrix is used to detect 15 buttons. The matrix columns are connected to interrupt-
enabled GPIOs
MCU internal pull up/pull down resistors are used as button scan matrix pull up resistors.
No external resistor is needed for button detection, and no external circuit is needed for
battery voltage detection.
The function is also realized by the MCU ADC module without any external component.
A 32.768 KHz watch crystal serves as the MCU FLL and RTC clock source.
Two chip capacitors, C4 and C6, are used as the crystal loading capacitor. The C4 and C6
values are selected according to crystal specification.