0% found this document useful (0 votes)
68 views55 pages

IoT Lab Manual

This document outlines three experiments using the MSP430 LaunchPad, focusing on embedded systems programming. The first experiment introduces the platform and involves writing a program to blink an LED, while the second experiment teaches how to read input from a switch to control LED blinking rates. The third experiment demonstrates using interrupts to toggle an LED based on a pushbutton press, highlighting the advantages of interrupts over polling in embedded applications.

Uploaded by

hrcapgemini72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views55 pages

IoT Lab Manual

This document outlines three experiments using the MSP430 LaunchPad, focusing on embedded systems programming. The first experiment introduces the platform and involves writing a program to blink an LED, while the second experiment teaches how to read input from a switch to control LED blinking rates. The third experiment demonstrates using interrupts to toggle an LED based on a pushbutton press, highlighting the advantages of interrupts over polling in embedded applications.

Uploaded by

hrcapgemini72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Experiment 1: Introduction to MSP430 LaunchPad and Programming Environment

1. Aim
The aim of this experiment is to introduce the MSP430 LaunchPad, its architecture, and the
programming environment used to develop embedded systems applications. By the end of
this experiment, you will write, compile, and execute a basic program that controls an LED on
the MSP430 LaunchPad using either Code Composer Studio (CCS) or Energia.
2. Description
The MSP430 LaunchPad is a low-power microcontroller platform from Texas Instruments
designed for embedded applications. This experiment focuses on familiarizing students with
the MSP430G2 development board, its features, and its programming environment.
Key Features of the MSP430 LaunchPad:
• MSP430 Microcontroller: Low-power, 16-bit microcontroller.
• On-Board LEDs: Typically two LEDs (red and green) for easy visual feedback.
• Pushbuttons: Pre-configured pushbuttons connected to GPIO pins for input.
• USB Interface: Provides power and communication with the PC.
In this experiment, you will learn to write a simple program to blink an LED using the GPIO
functionality of the MSP430 microcontroller. The program will be implemented using CCS or
Energia.
3. Circuit Diagram
In this experiment, we will use the on-board components of the MSP430 LaunchPad (LED and
buttons), so no external circuitry is required. The onboard components are connected as
follows:
• P1.0 (Pin 1.0): Connected to the red LED.
• P1.6 (Pin 1.6): Connected to the green LED (depending on the version of the
MSP430).
4. Implementation
Step 1: Install the IDE
You can choose between two IDEs:
• Code Composer Studio (CCS): A powerful IDE provided by Texas Instruments for
MSP430 programming.
• Energia: An easier, Arduino-like IDE for simple embedded system programming.
Step 2: Connect the MSP430 LaunchPad
• Use a USB cable to connect the MSP430 LaunchPad to your computer.
• Ensure that the board is powered and recognized by the system.
Step 3: Write the Code
Option 1: Code Composer Studio
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "BlinkLED").
4. Select the Empty Project with Main.c template.
• Write the Blink LED Program:
#include <msp430.h>

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 (Red LED) as output

while (1)
{
P1OUT ^= 0x01; // Toggle P1.0 (Red LED)
__delay_cycles(100000); // Delay loop
}
}
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Blink LED Program:
void setup() {
pinMode(RED_LED, OUTPUT); // Set Red LED pin as output
}

void loop() {
digitalWrite(RED_LED, HIGH); // Turn the Red LED on
delay(1000); // Wait for 1 second
digitalWrite(RED_LED, LOW); // Turn the Red LED off
delay(1000); // Wait for 1 second
}
Step 4: Compile and Upload the Code
• In CCS: Click on the Build icon (hammer) to compile the code and Debug to upload
the code to the MSP430.
• In Energia: Click on the Upload button to compile and upload the code to the MSP430.
Step 5: Run the Program
Once the program is successfully uploaded, observe the red LED on the MSP430 LaunchPad.
It should start blinking at a 1-second interval.
5. Results and Discussion
Expected Results:
• Upon successfully uploading the code, the red LED (connected to P1.0) will toggle its
state (on/off) every 1 second.
Discussion:
• The main purpose of this experiment is to get familiar with the MSP430 microcontroller
and its programming environment.
• The GPIO pin P1.0 is configured as an output, and the program continuously toggles
its state using the P1OUT ^= 0x01; instruction. The __delay_cycles(100000);
introduces a delay, controlling the blink speed.
• Different versions of the MSP430 LaunchPad may have additional peripherals, but the
basic principles remain the same across all boards.
• This basic experiment introduces core concepts like configuring GPIO pins, controlling
onboard peripherals, and writing embedded C programs.
6. Conclusion
This experiment successfully introduced the MSP430 LaunchPad and demonstrated the
programming process using both Code Composer Studio (CCS) and Energia IDE. By
controlling an LED, the experiment illustrated how to configure GPIO pins on the MSP430
microcontroller and write a simple embedded program. Understanding this basic setup is
crucial for future experiments involving sensors, actuators, and more complex embedded
systems applications.
Experiment 2: Read Input from Switch and Automatic Control/Flash LED (Software
Delay)
1. Aim
The aim of this experiment is to learn how to read input from a pushbutton switch and control
the flashing of an LED using software delay in an MSP430 microcontroller. The LED will blink
at different rates depending on the state of the switch.
2. Description
In this experiment, you will learn how to interface a pushbutton switch with the MSP430
LaunchPad and use the input from the switch to control the behavior of an LED. A software
delay will be used to adjust the blinking rate of the LED.
Key Concepts:
• Switch as Input: The pushbutton switch is connected to one of the GPIO pins and
configured as an input.
• LED as Output: The LED is connected to another GPIO pin and controlled based on
the state of the switch.
• Software Delay: A simple delay loop will be used to control the time between LED
toggles.
• Polling the Switch: The microcontroller continuously checks (polls) the state of the
switch to control the LED behavior.
3. Circuit Diagram
The MSP430 LaunchPad includes on-board switches and LEDs, which we can use for this
experiment.
• P1.0 (Pin 1.0): Connected to the red LED.
• P1.3 (Pin 1.3): Connected to the pushbutton switch (S2).
4. Implementation
Step 1: Set Up the IDE
You can choose between two IDEs:
• Code Composer Studio (CCS)
• Energia IDE
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "SwitchControlLED").
4. Select the Empty Project with Main.c template.
• Write the Code:
#include <msp430.h>

void delay(unsigned int time); // Function prototype for delay

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop the watchdog timer
P1DIR |= 0x01; // Set P1.0 (Red LED) as output
P1DIR &= ~0x08; // Set P1.3 (Switch S2) as input
P1REN |= 0x08; // Enable pull-up resistor for P1.3
P1OUT |= 0x08; // Set pull-up resistor for P1.3

while (1)
{
if (!(P1IN & 0x08)) // Check if switch S2 is pressed (active low)
{
P1OUT ^= 0x01; // Toggle P1.0 (Red LED)
delay(100000); // Delay for fast blinking
}
else
{
P1OUT ^= 0x01; // Toggle P1.0 (Red LED)
delay(500000); // Delay for slow blinking
}
}
}

// Delay function using simple loop


void delay(unsigned int time)
{
while (time--); // Loop until time reaches 0
}
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code:
void setup() {
pinMode(RED_LED, OUTPUT); // Set the Red LED pin as output
pinMode(PUSH2, INPUT_PULLUP); // Set the Pushbutton (S2) as input with pull-up
}

void loop() {
if (digitalRead(PUSH2) == LOW) { // Check if the button is pressed
digitalWrite(RED_LED, HIGH); // Turn the Red LED on
delay(100); // Short delay for fast blinking
digitalWrite(RED_LED, LOW); // Turn the Red LED off
delay(100); // Short delay for fast blinking
} else {
digitalWrite(RED_LED, HIGH); // Turn the Red LED on
delay(500); // Long delay for slow blinking
digitalWrite(RED_LED, LOW); // Turn the Red LED off
delay(500); // Long delay for slow blinking
}
}
Step 3: Compile and Upload the Code
• In CCS: Click on the Build icon (hammer) to compile the code, then click the Debug
icon to upload it to the MSP430.
• In Energia: Click on the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• After uploading the code, observe the red LED on the MSP430 LaunchPad. The LED
will blink at different rates depending on whether the pushbutton switch (S2) is pressed
or not.
5. Results and Discussion
Expected Results:
• When the switch (S2) is pressed, the LED should blink rapidly (fast toggle).
• When the switch (S2) is not pressed, the LED should blink slowly (slow toggle).
Discussion:
• This experiment demonstrates how to read an input (the switch) and control an output
(the LED) using software. The pull-up resistor ensures that the input pin (P1.3) reads
HIGH when the switch is not pressed.
• The delay function (delay(unsigned int time)) is a simple software loop used to create
time intervals between the LED toggles. The delay time changes based on the state of
the switch.
• This simple implementation illustrates how input polling works: the microcontroller
continuously checks the switch’s state in the while loop. This is known as "polling" and
is a common method used in embedded systems to monitor digital inputs.
6. Conclusion
In this experiment, you successfully interfaced a pushbutton switch and controlled the behavior
of an LED using software delay in the MSP430 microcontroller. The state of the switch
determines the blinking rate of the LED, with different delays being introduced depending on
whether the switch is pressed or not. This experiment enhances your understanding of basic
GPIO input/output configurations and introduces the concept of software delays and input
polling in embedded systems.
Experiment 3: Interrupts Programming Example Using GPIO
1. Aim
The aim of this experiment is to demonstrate how to use interrupts in the MSP430
microcontroller, specifically by configuring an external GPIO interrupt triggered by a
pushbutton switch. The LED will be toggled on and off whenever the switch is pressed using
the interrupt service routine (ISR).
2. Description
In embedded systems, interrupts allow a microcontroller to respond to asynchronous events.
This experiment focuses on configuring GPIO pins to trigger interrupts, thus allowing the
MSP430 microcontroller to handle the input from a pushbutton switch without the need for
continuous polling. The microcontroller will toggle an LED when the switch is pressed, with
the LED control being handled by an interrupt service routine (ISR).
Key Concepts:
• GPIO Interrupts: Configuring GPIO pins to generate an interrupt when a state change
(e.g., button press) occurs.
• Edge Detection: The interrupt will be triggered based on the edge of the signal (falling
or rising edge).
• Interrupt Service Routine (ISR): A function that is executed when an interrupt is
triggered.
• Low-Power Operation: The microcontroller can enter low-power mode and "wake up"
when an interrupt occurs.
3. Circuit Diagram
For this experiment, the built-in pushbutton switch (S2) and red LED on the MSP430
LaunchPad will be used.
• P1.0 (Pin 1.0): Connected to the red LED.
• P1.3 (Pin 1.3): Connected to the pushbutton switch (S2).
4. Implementation
Step 1: Set Up the IDE
You can choose either:
• Code Composer Studio (CCS)
• Energia IDE
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "GPIOInterruptExample").
4. Select the Empty Project with Main.c template.
• Write the Code:
#include <msp430.h>

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1DIR |= 0x01; // Set P1.0 as output (Red LED)


P1DIR &= ~0x08; // Set P1.3 as input (Switch S2)
P1REN |= 0x08; // Enable pull-up resistor on P1.3
P1OUT |= 0x08; // Set pull-up resistor (Switch active low)

P1IE |= 0x08; // Enable interrupt for P1.3


P1IES |= 0x08; // Trigger on falling edge (button press)
P1IFG &= ~0x08; // Clear interrupt flag for P1.3

__bis_SR_register(GIE); // Enable global interrupts

while(1)
{
// Enter low-power mode and wait for interrupt
__bis_SR_register(LPM4_bits | GIE);
}
}

// Interrupt Service Routine (ISR) for Port 1


#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= 0x01; // Toggle P1.0 (Red LED)
P1IFG &= ~0x08; // Clear interrupt flag for P1.3
__bic_SR_register_on_exit(LPM4_bits); // Exit low-power mode on return
}
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code:
void setup() {
pinMode(RED_LED, OUTPUT); // Set the Red LED as output
pinMode(PUSH2, INPUT_PULLUP); // Set the pushbutton (S2) as input with pull-up
attachInterrupt(PUSH2, toggleLED, FALLING); // Attach interrupt on button press (falling
edge)
}

void loop() {
// The main loop remains empty because the interrupt handles the toggling
}

void toggleLED() {
digitalWrite(RED_LED, !digitalRead(RED_LED)); // Toggle the state of the Red LED
}
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• Once the program is uploaded, the microcontroller will toggle the red LED each time
the pushbutton switch (S2) is pressed.
• The microcontroller will remain in a low-power mode until the interrupt (from the button
press) wakes it up.
5. Results and Discussion
Expected Results:
• The red LED (connected to P1.0) will toggle (turn on or off) each time the switch (S2)
is pressed.
• The system should use interrupts, meaning the LED state changes only when an
external event (button press) occurs, without the need for continuous polling.
Discussion:
• Interrupts vs. Polling: This experiment demonstrates how interrupts make the
microcontroller more efficient. Rather than continuously polling the state of the switch,
the microcontroller can enter a low-power mode (LPM4) and "wake up" only when the
switch is pressed. This saves power and processing resources.
• Edge-Triggered Interrupts: In the code, the interrupt is configured to trigger on the
falling edge of the signal (button press). When the button is pressed (causing the input
signal to go from HIGH to LOW), the ISR is executed, and the LED is toggled.
• Low-Power Mode: The microcontroller enters low-power mode using
__bis_SR_register(LPM4_bits | GIE);, which reduces power consumption while waiting
for the interrupt. Upon triggering the interrupt, it exits low-power mode using
__bic_SR_register_on_exit(LPM4_bits);.
• Debouncing: A mechanical pushbutton may introduce noise (bouncing), which could
cause multiple toggles of the LED for a single press. A software or hardware debounce
solution may be required in more advanced implementations.
6. Conclusion
This experiment successfully demonstrates how to configure and use GPIO interrupts on the
MSP430 microcontroller. The LED toggles in response to the pushbutton press, with the
microcontroller efficiently managing the input event using an interrupt service routine (ISR)
rather than continuously polling the input pin. This approach enhances the efficiency of the
microcontroller by allowing it to enter low-power modes when no events are occurring. The
use of interrupts is critical in designing responsive and low-power embedded systems.
Experiment 4: Configure Watchdog Timer in Watchdog & Interval Mode
1. Aim
The aim of this experiment is to configure the Watchdog Timer (WDT) of the MSP430
microcontroller in both watchdog mode and interval timer mode. In watchdog mode, the
WDT resets the system if it is not cleared within a specified time interval. In interval mode, the
WDT generates periodic interrupts, allowing the microcontroller to perform time-based
operations.
2. Description
The Watchdog Timer (WDT) is a hardware timer used to reset the microcontroller if the
software gets stuck in an unintended loop or fails to execute a critical operation in a timely
manner. It helps prevent the system from locking up.
The MSP430 Watchdog Timer can be configured to operate in two modes:
1. Watchdog Mode: The WDT resets the system after a timeout unless it is cleared
periodically.
2. Interval Timer Mode: The WDT generates periodic interrupts, allowing it to be used
as a simple timekeeping device.
In this experiment, you will:
• Configure the Watchdog Timer in watchdog mode to demonstrate its reset functionality.
• Configure the Watchdog Timer in interval mode to generate periodic interrupts and
toggle an LED.
3. Circuit Diagram
For this experiment, we will use the onboard red LED connected to P1.0 on the MSP430
LaunchPad. No additional components are required.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "WDT_Configuration").
4. Select the Empty Project with Main.c template.
Watchdog Mode Example:
In watchdog mode, the WDT will reset the system if it is not cleared within a specific time
period.
• Write the Code for Watchdog Mode:
#include <msp430.h>

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 (Red LED) as output

WDTCTL = WDTPW | WDTNMI | WDTSSEL | WDTIS1; // Set WDT in watchdog mode,


ACLK, interval ~1s

while (1)
{
P1OUT ^= 0x01; // Toggle P1.0 (Red LED)

// Simulate the system getting stuck by commenting out the WDT reset
// __delay_cycles(500000); // Introduce delay
// WDTCTL = WDTPW | WDTCNTCL; // Clear WDT (if enabled, prevents reset)

// Without clearing the watchdog, the microcontroller will reset after ~1s
}
}
In this code:
• The watchdog is configured to time out after approximately 1 second.
• If WDTCTL = WDTPW | WDTCNTCL; is not called within this time, the system will
reset.
Interval Mode Example:
In interval mode, the WDT generates periodic interrupts instead of resetting the system.
• Write the Code for Interval Mode:
#include <msp430.h>

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 (Red LED) as output

WDTCTL = WDTPW | WDTTMSEL | WDTSSEL | WDTIS1; // Interval mode, ACLK, ~1s


interval
IE1 |= WDTIE; // Enable WDT interrupt

__bis_SR_register(LPM3_bits + GIE); // Enter low-power mode 3 with interrupts enabled


}

// Watchdog Timer interrupt service routine


#pragma vector = WDT_VECTOR
__interrupt void watchdog_timer(void)
{
P1OUT ^= 0x01; // Toggle P1.0 (Red LED)
}
In this code:
• The WDT is set to interval mode, generating an interrupt every 1 second.
• The LED will toggle on and off in the interrupt service routine (ISR).
Option 2: Energia IDE
• Watchdog Mode Example:
void setup() {
pinMode(RED_LED, OUTPUT); // Set the Red LED pin as output

// Set WDT to watchdog mode with ~1 second timeout


WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
WDTCTL = WDTPW | WDTSSEL | WDTNMI | WDTIS1; // Watchdog mode, ACLK, interval
~1s
}

void loop() {
digitalWrite(RED_LED, HIGH); // Turn the Red LED on
delay(500); // Short delay

// Without resetting the WDT, the MCU will reset after ~1s
// Watchdog reset function commented out
// WDTCTL = WDTPW | WDTCNTCL; // Clear WDT (if enabled, prevents reset)
}
• Interval Mode Example:
void setup() {
pinMode(RED_LED, OUTPUT); // Set Red LED pin as output

// Set WDT to interval mode with ~1 second interval


WDTCTL = WDTPW | WDTTMSEL | WDTSSEL | WDTIS1; // Interval mode, ACLK, ~1s
interval
IE1 |= WDTIE; // Enable WDT interrupt
}

void loop() {
// Main loop remains empty, WDT ISR will handle the LED toggling
}

#pragma vector = WDT_VECTOR


__interrupt void watchdog_timer() {
digitalWrite(RED_LED, !digitalRead(RED_LED)); // Toggle Red LED in ISR
}
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• In watchdog mode, if the WDT reset function (WDTCTL = WDTPW | WDTCNTCL;) is
not called within the defined interval, the microcontroller will reset.
• In interval mode, the WDT will generate periodic interrupts, toggling the LED every
second.
5. Results and Discussion
Expected Results:
• Watchdog Mode: The system will reset if the WDT is not cleared within the set time
interval (approximately 1 second). The red LED may toggle briefly before the reset.
• Interval Mode: The red LED will toggle on and off at 1-second intervals, controlled by
the WDT interrupt.
Discussion:
• Watchdog Mode: This mode is critical for ensuring the microcontroller does not get
stuck in an infinite loop. If the program fails to clear the WDT, it forces a system reset,
ensuring the system can recover from software failures.
• Interval Mode: In this mode, the WDT serves as a periodic timer, generating interrupts
at fixed intervals. This is useful for time-based tasks, such as regularly toggling an LED
or performing sensor readings.
• Low-Power Operation: In interval mode, the MSP430 can enter a low-power mode
(LPM3) between interrupts, reducing power consumption while maintaining periodic
tasks.
6. Conclusion
This experiment demonstrates how to configure and use the Watchdog Timer (WDT) in both
watchdog mode and interval mode on the MSP430 microcontroller. In watchdog mode, the
WDT helps protect the system from lockups by resetting the microcontroller if the timer is not
cleared. In interval mode, the WDT generates periodic interrupts, which can be used for timing-
based tasks. Understanding how to configure and use the WDT is essential for developing
reliable and efficient embedded systems.
Experiment 5: Configure Timer Block for Signal Generation (with Given Frequency)
1. Aim
The aim of this experiment is to configure the Timer block of the MSP430 microcontroller to
generate a square wave signal with a specified frequency using the Timer_A module. The
square wave will be output on a GPIO pin and can be used to drive external components,
such as LEDs or other devices.
2. Description
The MSP430 microcontroller has built-in timer modules (e.g., Timer_A) that can be configured
to generate PWM signals or simple square waves. By setting the timer period and controlling
the output mode, it is possible to generate a signal with a specific frequency.
In this experiment, the Timer_A module will be configured to output a square wave signal at a
predefined frequency (e.g., 1 kHz) on a GPIO pin. The duty cycle of the signal can also be
adjusted if needed, but in this case, we focus on generating a signal with a 50% duty cycle.
Key Concepts:
• Timer Block: MSP430 timers can operate in various modes, including up mode,
continuous mode, and up/down mode.
• Frequency Control: The timer's period and the clock source determine the frequency
of the output signal.
• Output Pin: The signal is output on a GPIO pin configured as a timer output.
3. Circuit Diagram
The signal will be output on P1.6, which is connected to Timer_A (TA0.1) on the
MSP430G2553. For testing purposes, an LED can be connected to this pin to visualize the
signal.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "Timer_Signal_Generation").
4. Select the Empty Project with Main.c template.
Signal Generation Example (1 kHz Square Wave):
In this example, the Timer_A module is configured to generate a square wave on P1.6 at a
frequency of 1 kHz.
• Write the Code:
#include <msp430.h>

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1DIR |= BIT6; // Set P1.6 as output


P1SEL |= BIT6; // Select P1.6 as Timer_A output

// Configure Timer_A
TA0CCR0 = 1048; // Set the period for 1 kHz signal (assuming 1.048 MHz SMCLK)
TA0CCTL1 = OUTMOD_7; // Set output mode to Reset/Set (PWM)
TA0CCR1 = 524; // Set the duty cycle to 50%
TA0CTL = TASSEL_2 + MC_1; // SMCLK, Up mode

while (1) {
// Main loop, Timer_A handles signal generation
}
}
In this code:
• P1.6 is configured as the output pin for the Timer_A signal.
• The Timer_A period is set to 1048 clock cycles, which corresponds to a 1 kHz
frequency with a 1.048 MHz clock (SMCLK).
• The duty cycle is set to 50%, which creates a square wave output.
• TA0CCTL1 = OUTMOD_7 configures the timer to operate in Reset/Set mode, which
generates a PWM-like signal with a fixed frequency.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code for Signal Generation:
void setup() {
pinMode(GREEN_LED, OUTPUT); // Set P1.6 (Green LED) as output
analogWriteFrequency(1000); // Set the frequency to 1 kHz
}

void loop() {
analogWrite(GREEN_LED, 128); // Set 50% duty cycle (128/255)
// Main loop remains empty as the timer handles signal generation
}
In this Energia code:
• The analogWriteFrequency(1000) function is used to set the PWM frequency to 1
kHz.
• The analogWrite(GREEN_LED, 128) function sets the duty cycle to 50%, which
generates a square wave signal.
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• Once the program is uploaded, the Timer_A module will generate a square wave signal
at 1 kHz on P1.6.
• You can measure the signal using an oscilloscope or visualize it by connecting an LED
to P1.6.
5. Results and Discussion
Expected Results:
• A square wave signal with a frequency of 1 kHz will be generated on P1.6.
• If an LED is connected to P1.6, it will blink at a rate corresponding to the 1 kHz signal.
Discussion:
• Frequency Calculation: The frequency of the signal is determined by the timer period
set in TA0CCR0. For example, if the clock frequency is 1.048 MHz, the period for a 1
kHz signal is calculated.
• Output Mode: The timer is configured to use Reset/Set mode (OUTMOD_7), which
produces a PWM signal. In this mode, the timer output is set high when the timer
counts to the value in CCR1 and reset to low when the timer counts to the value in
CCR0. With CCR1 = CCR0 / 2, the output is a 50% duty cycle square wave.
• Timer Modes: The Up Mode (MC_1) is used, where the timer counts from 0 to the
value in CCR0 and then resets, continuously cycling to generate the signal.
• Signal Measurement: An oscilloscope or logic analyzer can be used to verify the
frequency and duty cycle of the output signal. Alternatively, an LED can be used to
visualize the frequency.
6. Conclusion
This experiment successfully demonstrates how to configure the Timer_A module in the
MSP430 microcontroller to generate a square wave signal at a specified frequency (e.g., 1
kHz) on a GPIO pin. By adjusting the timer period and output mode, it is possible to generate
signals with precise frequency and duty cycle characteristics. This technique is useful in
applications requiring signal generation, such as driving LEDs, motors, or communication
interfaces.
Experiment 6: Read Temperature of MSP430 with the Help of ADC
1. Aim
The aim of this experiment is to read the internal temperature of the MSP430 microcontroller
using its onboard Analog-to-Digital Converter (ADC). The internal temperature sensor will
be utilized to measure the temperature, and the digital result from the ADC will be converted
into degrees Celsius.
2. Description
The MSP430 microcontroller has an onboard Analog-to-Digital Converter (ADC) that can
read analog signals and convert them into digital values. The microcontroller also includes an
internal temperature sensor that provides an analog signal corresponding to the temperature
of the chip. By using the ADC, this signal can be read and converted into a temperature value.
In this experiment, the internal temperature sensor's output will be connected to the ADC
channel. The ADC will sample the temperature signal, and the digital output will be processed
to calculate the temperature in degrees Celsius.
Key Concepts:
• ADC (Analog-to-Digital Converter): Converts the analog signal from the temperature
sensor into a digital value.
• Internal Temperature Sensor: Measures the temperature of the MSP430 die.
• Temperature Calculation: The ADC result can be converted to temperature using the
sensor's calibration data.
3. Circuit Diagram
Since the temperature sensor is internal to the MSP430, no external components are required.
The internal temperature sensor is connected to the ADC.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "Temperature_Reading").
4. Select the Empty Project with Main.c template.
Code Example:
This example demonstrates how to read the internal temperature sensor using the ADC10
module on the MSP430G2553. The ADC value is converted into a temperature value in
degrees Celsius.
• Write the Code:
#include <msp430.h>

long tempRaw; // Raw ADC value


long temperatureC; // Temperature in Celsius

void configureADC(void);
void startADC(void);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

// Configure ADC to read internal temperature sensor


configureADC();

while (1)
{
startADC(); // Start ADC conversion

// Calculate temperature in Celsius


temperatureC = ((tempRaw - 673) * 423) / 1024;

__delay_cycles(500000); // Delay between measurements


}
}

// Function to configure ADC


void configureADC(void)
{
ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; // Vref+, 64
clocks, ADC On, Enable ADC interrupt
__delay_cycles(1000); // Allow reference to settle
ADC10CTL0 |= ENC + ADC10SC; // Enable and start conversion
}

// Function to start ADC conversion


void startADC(void)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while (ADC10CTL1 & ADC10BUSY); // Wait until ADC conversion is complete
tempRaw = ADC10MEM; // Store the ADC value
}
In this code:
• The ADC10 module is configured to read from the internal temperature sensor channel
(INCH_10).
• The temperature is calculated using the formula.
• The startADC() function starts the ADC conversion and waits for the result to be
available.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code for Temperature Reading:
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
analogReference(INTERNAL1V5); // Set reference to 1.5V for temperature sensor
}

void loop() {
int tempRaw = analogRead(TEMPSENSOR); // Read the internal temperature sensor
float temperatureC = (tempRaw - 673) * 423.0 / 1024.0; // Convert to Celsius

// Print temperature to serial monitor


Serial.print("Temperature (C): ");
Serial.println(temperatureC);
delay(1000); // Delay 1 second between readings
}
In this Energia code:
• The internal temperature sensor is read using analogRead(TEMPSENSOR).
• The temperature is calculated and printed to the serial monitor.
• The analogReference(INTERNAL1V5) function sets the ADC reference voltage to
1.5V, which is typically used with the internal temperature sensor.
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• Open a serial monitor (in Energia) or observe the output in a debug session (in CCS)
to view the temperature readings in degrees Celsius.
• The temperature values will be printed continuously with a delay between
measurements.
5. Results and Discussion
Expected Results:
• The internal temperature sensor's readings will be displayed, providing the
temperature of the MSP430 chip in degrees Celsius.
• In the serial monitor or debugger, you will see the temperature values updating
approximately every second.
Discussion:
• ADC Configuration: The ADC10 module is configured to read the temperature sensor
connected to channel INCH_10. The ADC converts the analog signal from the sensor
into a 10-bit digital value, which is used to calculate the temperature.
• Temperature Calculation: The calibration constants used in the formula for
calculating temperature are specific to the MSP430G2553. The constants 673 and 423
are used to scale the ADC result to temperature in degrees Celsius. These values may
vary slightly for different MSP430 models.
• Temperature Fluctuations: The measured temperature reflects the die temperature
of the microcontroller. It may fluctuate depending on ambient conditions and the current
operating state of the microcontroller.
6. Conclusion
This experiment successfully demonstrates how to read the internal temperature sensor of the
MSP430 microcontroller using the ADC10 module. The analog signal from the temperature
sensor is converted into a digital value by the ADC, and this value is then processed to obtain
the temperature in degrees Celsius. This technique is useful for monitoring the temperature of
the microcontroller during operation, which is important for applications that require thermal
management.
Experiment 7: Test Various Power Down Modes in MSP430
1. Aim
The aim of this experiment is to explore the different power-down modes of the MSP430
microcontroller, specifically Low-Power Modes (LPM0, LPM1, LPM2, LPM3, and LPM4),
and to measure the impact of these modes on power consumption. The MSP430
microcontroller offers several low-power modes to reduce energy consumption during idle
periods while maintaining the ability to wake up for processing when required.
2. Description
The MSP430 microcontroller is known for its ultra-low-power capabilities. To conserve power,
especially in battery-operated systems, the MSP430 offers multiple Low-Power Modes
(LPMs) that selectively disable various system components like the CPU, clocks, and
peripherals. By entering these power-down modes, the system can dramatically reduce power
consumption while still being able to wake up via interrupts or external events.
Each low-power mode disables specific components:
• LPM0: CPU off, MCLK off, SMCLK and ACLK active.
• LPM1: CPU off, MCLK off, DCO inactive, SMCLK and ACLK active.
• LPM2: CPU off, MCLK off, SMCLK off, ACLK active.
• LPM3: CPU off, MCLK off, SMCLK off, ACLK active, DCO off.
• LPM4: CPU and clocks off, only an external interrupt can wake up the system.
In this experiment, we will configure the MSP430 to enter different low-power modes and
measure the effect on power consumption. An LED will be used to indicate activity, and the
system will wake up using an external button interrupt.
3. Circuit Diagram
The circuit is simple and consists of:
• A push button connected to P1.3 for waking the system from low-power modes using
an interrupt.
• An LED connected to P1.0 to indicate activity.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "Low_Power_Modes").
4. Select the Empty Project with Main.c template.
Code Example:
This example demonstrates how to enter different low-power modes (LPM0 through LPM4)
and wake up using an external interrupt (button on P1.3).
• Write the Code:
#include <msp430.h>

void configureLED(void);
void configureButton(void);
void enterLowPowerMode(unsigned int mode);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

configureLED(); // Configure LED on P1.0


configureButton(); // Configure Button on P1.3

while (1)
{
P1OUT ^= BIT0; // Toggle LED to indicate activity
__delay_cycles(500000); // Delay to observe the LED

enterLowPowerMode(LPM0_bits); // Enter Low-Power Mode 0 (LPM0)


}
}

// Function to configure LED on P1.0


void configureLED(void)
{
P1DIR |= BIT0; // Set P1.0 as output
P1OUT &= ~BIT0; // Turn off LED initially
}
// Function to configure Button on P1.3
void configureButton(void)
{
P1DIR &= ~BIT3; // Set P1.3 as input (Button)
P1REN |= BIT3; // Enable pull-up resistor on P1.3
P1OUT |= BIT3; // Pull-up resistor to VCC
P1IE |= BIT3; // Enable interrupt on P1.3
P1IES |= BIT3; // Trigger interrupt on falling edge
P1IFG &= ~BIT3; // Clear interrupt flag
}

// Function to enter different low-power modes


void enterLowPowerMode(unsigned int mode)
{
__bis_SR_register(mode + GIE); // Enter LPMx with interrupts enabled
}

// Interrupt service routine for the button (P1.3)


#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1IFG &= ~BIT3; // Clear interrupt flag
P1OUT |= BIT0; // Turn on LED when button is pressed
__delay_cycles(500000); // Delay to simulate work after wake-up
P1OUT &= ~BIT0; // Turn off LED
}
In this code:
• P1.0 is configured as an output for controlling the LED.
• P1.3 is configured as an input for the button, with an interrupt enabled for waking up
the system from a low-power mode.
• The enterLowPowerMode() function enters a specified low-power mode.
• The Port_1 ISR handles the button press interrupt, waking up the system and toggling
the LED.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code for Low-Power Modes:
void setup() {
pinMode(GREEN_LED, OUTPUT); // Set P1.0 as output for LED
pinMode(PUSH2, INPUT_PULLUP); // Set P1.3 as input with pull-up for Button
attachInterrupt(PUSH2, wakeUp, FALLING); // Interrupt on falling edge
}

void loop() {
digitalWrite(GREEN_LED, HIGH); // Turn on LED
delay(500); // Wait for 500ms
digitalWrite(GREEN_LED, LOW); // Turn off LED
delay(500); // Wait for 500ms

// Enter Low-Power Mode 3


__bis_SR_register(LPM3_bits + GIE);
}

void wakeUp() {
// ISR to wake up the MCU
}
In this Energia code:
• The button on P1.3 triggers an interrupt to wake the microcontroller from Low-Power
Mode 3 (LPM3).
• The LED on P1.0 indicates activity before the system enters a low-power state.
• attachInterrupt() sets up the button press to wake the system.
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• The system will alternate between active mode (where the LED blinks) and low-power
modes.
• Press the button connected to P1.3 to wake the system from a low-power mode.
5. Results and Discussion
Expected Results:
• The LED will blink for a short period, indicating that the microcontroller is in active
mode.
• After the LED toggles, the MSP430 will enter a low-power mode (e.g., LPM0, LPM1,
LPM3, etc.), reducing power consumption.
• Pressing the button will wake up the microcontroller, and the LED will turn on to indicate
activity.
Discussion:
• Low-Power Modes: Each low-power mode (LPM0-LPM4) progressively disables
more components of the microcontroller, reducing power consumption at the cost of
reduced functionality.
• Interrupts for Wake-Up: The button press triggers an external interrupt, which wakes
the MSP430 from a low-power state. The LED indicates the system’s return to active
mode.
• Power Consumption: To measure power consumption, a multimeter or an energy
measurement tool like the MSP430 EnergyTrace™ feature in Code Composer Studio
can be used to monitor the power draw in each mode.
6. Conclusion
This experiment demonstrates the different low-power modes available in the MSP430
microcontroller. By entering and exiting these modes using external interrupts (e.g., button
presses), we can achieve significant power savings. The experiment also highlights the
balance between power consumption and functionality, as progressively lower power modes
disable more system components. Understanding and using these low-power modes is
essential in designing energy-efficient embedded systems, particularly for battery-operated
devices.
Experiment 8: PWM Generator using MSP430
1. Aim
The aim of this experiment is to configure the Timer module in the MSP430 microcontroller
to generate a Pulse Width Modulated (PWM) signal on a specific output pin. The experiment
will demonstrate how varying the duty cycle of the PWM signal affects the average voltage
output and can be used to control devices like LEDs or motors.
2. Description
Pulse Width Modulation (PWM) is a technique used to create a digital signal that can control
analog devices. By rapidly switching between high and low states, PWM can simulate varying
levels of voltage. The duty cycle, which is the percentage of time the signal is high during one
period, determines the average output voltage.
In this experiment, the Timer_A module of the MSP430 will be configured to generate a PWM
signal with a specific frequency. The duty cycle of the PWM signal will be varied to observe its
effect on the brightness of an LED connected to the output pin.
Key Concepts:
• PWM Signal: A digital signal that alternates between high and low states. The duty
cycle determines the percentage of time the signal remains high during a period.
• Duty Cycle: The proportion of time the signal is high within a single PWM period.
• Timer_A Module: The timer will be configured in Up mode to control the period of the
PWM signal and generate the output.
3. Circuit Diagram
The circuit includes:
• An LED connected to the PWM output pin P1.6 (TA0.1).
• The PWM signal generated by the MSP430 will control the brightness of the LED.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "PWM_Generator").
4. Select the Empty Project with Main.c template.
Code Example:
This example demonstrates how to generate a PWM signal on P1.6 (TA0.1) with a
configurable duty cycle.
• Write the Code:
#include <msp430.h>

void configurePWM(void);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

configurePWM(); // Configure Timer_A for PWM

while (1)
{
// Main loop does nothing, PWM is controlled by Timer_A
}
}

void configurePWM(void)
{
P1DIR |= BIT6; // Set P1.6 as output (TA0.1)
P1SEL |= BIT6; // Select TA0.1 function for P1.6

// Configure Timer_A
TA0CCR0 = 1000 - 1; // Set PWM period (1000 cycles)
TA0CCTL1 = OUTMOD_7; // Set TA0.1 to reset/set mode
TA0CCR1 = 500; // Set duty cycle to 50% (initial)
TA0CTL = TASSEL_2 + MC_1; // Set Timer_A to use SMCLK, up mode
}
In this code:
• The P1.6 pin is configured for PWM output from Timer_A's channel 1 (TA0.1).
• Timer_A is configured in up mode using SMCLK as the clock source.
• TA0CCR0 sets the PWM period, and TA0CCR1 sets the duty cycle.
o A value of 1000 in TA0CCR0 results in a frequency of approximately 1 kHz
(depending on the clock configuration).
o TA0CCR1 = 500 sets the duty cycle to 50%.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code for PWM Generation:
int ledPin = GREEN_LED; // Define PWM output pin (P1.6)

void setup() {
pinMode(ledPin, OUTPUT); // Set P1.6 as output
}

void loop() {
analogWrite(ledPin, 128); // Set 50% duty cycle (128 out of 255)
delay(1000); // Delay for 1 second

analogWrite(ledPin, 64); // Set 25% duty cycle


delay(1000); // Delay for 1 second

analogWrite(ledPin, 192); // Set 75% duty cycle


delay(1000); // Delay for 1 second
}
In this Energia code:
• P1.6 (defined as GREEN_LED) is used as the PWM output pin.
• The analogWrite() function sets the PWM duty cycle. The range is from 0 to 255,
where 128 represents a 50% duty cycle.
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• The LED connected to P1.6 will glow at varying brightness levels, depending on the
duty cycle of the PWM signal.
• By observing the LED, you will see how the duty cycle affects the LED's brightness
(higher duty cycle means brighter LED).
5. Results and Discussion
Expected Results:
• The PWM signal on P1.6 controls the brightness of the LED. When the duty cycle is
50%, the LED will glow at half brightness. A duty cycle of 25% will result in dimmer
light, while a 75% duty cycle will make the LED glow brighter.
• The frequency of the PWM signal remains constant, while the duty cycle varies to
control the brightness.
Discussion:
• PWM Signal Generation: The Timer_A module in the MSP430 is configured to
generate the PWM signal. The timer's CCR0 register defines the PWM period
(frequency), while the CCR1 register controls the duty cycle.
• Duty Cycle Adjustment: Adjusting TA0CCR1 changes the duty cycle. The duty cycle
affects the proportion of time the signal is high, which controls the average voltage
delivered to the LED.
• Real-World Applications: PWM is commonly used in embedded systems to control
devices such as LEDs, motors, and audio signals. By varying the duty cycle, we can
efficiently control the power delivered to these devices without the need for complex
analog circuitry.
6. Conclusion
This experiment demonstrates the use of the MSP430 microcontroller's Timer_A module to
generate a PWM signal. By configuring the timer and adjusting the duty cycle, we can control
the brightness of an LED. The PWM technique is a versatile and energy-efficient method for
controlling devices in embedded systems. This experiment highlights the importance of
understanding timers and PWM for applications requiring fine control over output signals.
Experiment 9: Use Comparator to Compare the Signal Threshold Level Using MSP430
1. Aim
The aim of this experiment is to use the Comparator_A+ module in the MSP430
microcontroller to compare an analog input signal against a set threshold level. The
comparator will output a signal indicating whether the input voltage is above or below the
threshold. This can be useful in applications such as monitoring battery voltage levels or
threshold detection for sensors.
2. Description
The Comparator_A+ module in the MSP430 allows for comparison between two analog
voltages. It generates a digital output based on whether the input voltage on a comparator pin
is higher or lower than a reference voltage. The module has flexible options for choosing the
reference voltage, which can either be an external reference or an internally generated
voltage.
In this experiment, we will configure the Comparator_A+ to compare an external analog
signal on P1.1 (CA1) against an internal reference voltage. An LED connected to P1.0 will
indicate whether the input signal is above or below the threshold level.
Key Concepts:
• Comparator: Compares two analog signals and generates a digital output.
• Threshold Detection: The comparator detects when an input signal crosses a
predefined voltage level (the threshold).
• Comparator_A+ Module: A peripheral in the MSP430 that handles analog signal
comparison.
3. Circuit Diagram
The circuit consists of:
• Analog input signal connected to the comparator input P1.1 (CA1).
• An LED connected to P1.0 to display the comparator output (ON if the input signal is
above the threshold, OFF if it is below).
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "Comparator_Threshold").
4. Select the Empty Project with Main.c template.
Code Example:
This example demonstrates how to configure the Comparator_A+ to compare the input signal
on P1.1 (CA1) with an internal reference voltage and toggle an LED based on the result.
• Write the Code:
#include <msp430.h>

void configureComparator(void);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1DIR |= BIT0; // Set P1.0 as output (LED)


P1OUT &= ~BIT0; // Initially turn off LED

configureComparator(); // Configure Comparator_A+

while (1)
{
if (CACTL2 & CAOUT) // Check if input signal > threshold
{
P1OUT |= BIT0; // Turn on LED (input > threshold)
}
else
{
P1OUT &= ~BIT0; // Turn off LED (input < threshold)
}
}
}

void configureComparator(void)
{
CACTL1 = CARSEL + CAREF_2 + CAON; // Comparator ON, V+ = CA1 (P1.1), Reference
= 0.5 * Vcc
CACTL2 = P2CA0; // Input pin CA1 (P1.1)
}
In this code:
• CACTL1: Configures the comparator settings. The CAON bit enables the comparator,
and CARSEL + CAREF_2 selects the reference voltage as 0.5 * Vcc.
• CACTL2: Selects CA1 (P1.1) as the input signal for comparison.
• The comparator output CAOUT is checked in the main loop to determine if the input
signal is greater than the reference threshold. The LED connected to P1.0 will turn ON
if the input signal exceeds the threshold, and OFF otherwise.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code for Comparator Threshold Detection:
void setup() {
pinMode(GREEN_LED, OUTPUT); // Set P1.0 as output (LED)
digitalWrite(GREEN_LED, LOW); // Initially turn off LED

// Configure Comparator_A+
CACTL1 = CARSEL + CAREF_2 + CAON; // Enable comparator, ref = 0.5 * Vcc
CACTL2 = P2CA0; // Input pin CA1 (P1.1)
}

void loop() {
if (CACTL2 & CAOUT) {
digitalWrite(GREEN_LED, HIGH); // Turn on LED if input > threshold
} else {
digitalWrite(GREEN_LED, LOW); // Turn off LED if input < threshold
}
}
In this Energia code:
• The Comparator_A+ is configured similarly to the CCS code.
• The CAOUT bit indicates the result of the comparison. The LED on P1.0 reflects
whether the input signal is above or below the threshold.
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• Apply an external analog signal to P1.1 (CA1).
• The LED on P1.0 will turn ON when the input signal exceeds the threshold voltage (0.5
* Vcc) and OFF when the input signal is below the threshold.
5. Results and Discussion
Expected Results:
• When the input voltage applied to P1.1 (CA1) is higher than 0.5 * Vcc, the LED on
P1.0 will turn ON.
• When the input voltage is below the threshold, the LED will remain OFF.
Discussion:
• Comparator Operation: The Comparator_A+ successfully compares the input signal
on P1.1 (CA1) with the internal reference voltage of 0.5 * Vcc.
• Digital Output (CAOUT): The CAOUT bit of the Comparator_A+ module reflects the
comparison result, which is used to control the LED. When the input signal exceeds
the threshold, CAOUT goes high, turning on the LED.
• Applications: This comparator-based threshold detection can be useful in various
applications such as battery monitoring, sensor threshold detection, or signal
conditioning.
6. Conclusion
This experiment demonstrates how to use the Comparator_A+ module in the MSP430
microcontroller to compare an analog input signal against a predefined threshold voltage. The
comparator generates a digital output based on whether the input voltage is above or below
the threshold. This functionality is critical in many real-world applications where voltage
thresholds need to be monitored and acted upon. Understanding and using comparators in
embedded systems is key to developing efficient analog-to-digital interfacing solutions.
Experiment 10: Speed Control of DC Motor Using MSP430
1. Aim
The aim of this experiment is to control the speed of a DC motor using Pulse Width
Modulation (PWM) generated by the MSP430 microcontroller. The experiment will
demonstrate how varying the PWM duty cycle can adjust the motor speed, illustrating an
essential technique in motor control applications.
2. Description
DC motors are widely used in various applications due to their simplicity and ease of control.
The speed of a DC motor can be regulated by adjusting the voltage supplied to it, which can
be efficiently done using PWM. By changing the duty cycle of the PWM signal, we can
effectively control the average voltage delivered to the motor and thus its speed.
In this experiment, the MSP430 microcontroller will generate a PWM signal that drives a DC
motor via a motor driver circuit. The motor's speed will be adjusted by varying the PWM duty
cycle.
Key Concepts:
• Pulse Width Modulation (PWM): A technique to control the power delivered to a load
by varying the duty cycle of a digital signal.
• DC Motor Control: The average voltage across a DC motor can be controlled by
adjusting the duty cycle of the PWM signal.
• Motor Driver: A circuit that allows the microcontroller to control larger currents needed
to drive a motor.
3. Circuit Diagram
The circuit consists of:
• An MSP430 microcontroller generating the PWM signal.
• A DC motor connected through a motor driver (e.g., L293D).
• Power supply for the motor and the microcontroller.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create a New Project:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device.
3. Name the project (e.g., "DC_Motor_Speed_Control").
4. Select the Empty Project with Main.c template.
Code Example:
This example demonstrates how to generate a PWM signal on P1.6 to control the speed of a
DC motor connected through a motor driver.
• Write the Code:
#include <msp430.h>

void configurePWM(int dutyCycle);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1DIR |= BIT6; // Set P1.6 as output (PWM output)


P1SEL |= BIT6; // Select TA0.1 function for P1.6

TA0CCR0 = 1000 - 1; // Set PWM period (1000 cycles)


TA0CCTL1 = OUTMOD_7; // Set TA0.1 to reset/set mode

for (int i = 0; i <= 1000; i += 100) // Vary the duty cycle


{
configurePWM(i); // Set duty cycle from 0% to 100%
__delay_cycles(1000000); // Delay to observe motor speed
}

while (1)
{
// Loop indefinitely
}
}

void configurePWM(int dutyCycle)


{
TA0CCR1 = dutyCycle; // Set duty cycle
TA0CTL = TASSEL_2 + MC_1; // Set Timer_A to use SMCLK, up mode
}
In this code:
• P1.6 is configured for PWM output from Timer_A.
• TA0CCR0 sets the PWM period, and TA0CCR1 sets the duty cycle.
• The duty cycle is varied in a loop from 0% to 100% with increments, allowing the motor
speed to change gradually.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Write the Code for DC Motor Speed Control:
int motorPin = 6; // P1.6 for PWM output

void setup() {
pinMode(motorPin, OUTPUT); // Set P1.6 as output
}

void loop() {
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle += 25) // Vary duty cycle
{
analogWrite(motorPin, dutyCycle); // Set duty cycle (0-255)
delay(1000); // Delay to observe motor speed
}

for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle -= 25) // Decrease duty cycle
{
analogWrite(motorPin, dutyCycle); // Set duty cycle (0-255)
delay(1000); // Delay to observe motor speed
}
}
In this Energia code:
• The motor pin is set for PWM output.
• The analogWrite() function is used to set the duty cycle for the motor speed. The loop
gradually increases and then decreases the duty cycle, allowing the motor speed to
change.
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code, and then click the Debug
icon to upload it to the MSP430.
• In Energia: Click the Upload button to compile and upload the code to the MSP430.
Step 4: Run the Program
• Connect the DC motor to the motor driver circuit and power supply.
• Run the program. The DC motor should gradually speed up and slow down as the
PWM duty cycle changes.
5. Results and Discussion
Expected Results:
• The DC motor's speed should vary with the duty cycle of the PWM signal.
• At 0% duty cycle, the motor will stop. At 100% duty cycle, the motor will run at its
maximum speed. Intermediate duty cycles will correspond to intermediate speeds.
Discussion:
• PWM Signal Generation: The Timer_A module generates a PWM signal based on
the duty cycle set in TA0CCR1. The average voltage across the motor is controlled by
varying the duty cycle, which effectively controls its speed.
• Motor Driver Role: The motor driver circuit (like L293D) allows the MSP430 to control
the larger currents needed by the motor while isolating it from the microcontroller.
• Applications: This method of speed control is widely used in robotics, conveyor
systems, and other applications requiring precise motor control.
6. Conclusion
This experiment demonstrates the use of the MSP430 microcontroller to control the speed of
a DC motor using PWM. By varying the duty cycle of the PWM signal, we can adjust the motor
speed efficiently. This technique is essential in many applications, including robotics and
automation, where precise control of motor speed is required. Understanding PWM and its
application in motor control is fundamental for anyone working with embedded systems.
Experiment 11: Master-Slave Communication Between MSP430 Microcontrollers Using
SPI
1. Aim
The aim of this experiment is to demonstrate Serial Peripheral Interface (SPI)
communication between two MSP430 microcontrollers, where one acts as the master and the
other as the slave. This will illustrate how data can be transmitted and received between the
two devices using SPI protocol.
2. Description
SPI is a synchronous serial communication protocol used for short-distance communication.
It operates in full-duplex mode, allowing data to be transmitted and received simultaneously.
In this experiment, one MSP430 will be configured as the master device, which initiates the
communication and controls the clock line, while the other MSP430 will function as the slave
device, responding to the master's requests.
The master sends data to the slave, and the slave can also send data back to the master. This
experiment will demonstrate how to set up both devices and implement simple data transfer.
Key Concepts:
• SPI Protocol: A synchronous communication protocol using multiple lines for data and
control signals.
• Master and Slave: The master device controls the clock and initiates communication,
while the slave responds to commands from the master.
• Full-Duplex Communication: Both devices can send and receive data at the same
time.
3. Circuit Diagram
The circuit consists of two MSP430 microcontrollers connected via the SPI interface.
• P1.5 (SCLK): Serial Clock
• P1.6 (MOSI): Master Out, Slave In
• P1.7 (MISO): Master In, Slave Out
• Connect a common ground between the two MSP430 microcontrollers.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create Two New Projects:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device for both projects.
3. Name the first project (e.g., "SPI_Master") and the second (e.g., "SPI_Slave").
4. Select the Empty Project with Main.c template for both.
Master Code Example:
This code demonstrates how to configure the MSP430 as the master device, sending data to
the slave.
#include <msp430.h>

void SPI_Init(void);
void SPI_Write(unsigned char data);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
SPI_Init(); // Initialize SPI

while (1)
{
SPI_Write(0xAA); // Send data (0xAA) to the slave
__delay_cycles(100000); // Delay between transmissions
}
}

void SPI_Init(void)
{
P1SEL |= BIT5 | BIT6 | BIT7; // P1.5, P1.6, P1.7 as SPI function
P1DIR |= BIT5 | BIT6; // Set SCLK and MOSI as output
P1DIR &= ~BIT7; // Set MISO as input

UCB0CTL1 |= UCSWRST; // Put USCI in reset


UCB0CTL0 |= UCMST + UCSYNC + UCCKPL; // 3-pin, 8-bit SPI master
UCB0BR0 = 0x02; // Set clock divider (f_clk/2)
UCB0CTL1 &= ~UCSWRST; // Release USCI for operation
}
void SPI_Write(unsigned char data)
{
while (!(UCB0IFG & UCTXIFG)); // Wait for TX buffer to be empty
UCB0TXBUF = data; // Transmit data
}
Slave Code Example:
This code configures the MSP430 as the slave device, receiving data from the master.
#include <msp430.h>

void SPI_Init(void);
unsigned char SPI_Read(void);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
SPI_Init(); // Initialize SPI

while (1)
{
unsigned char received_data = SPI_Read(); // Read data from master
// You can process the received data here
__delay_cycles(100000); // Delay for demonstration
}
}

void SPI_Init(void)
{
P1SEL |= BIT5 | BIT6 | BIT7; // P1.5, P1.6, P1.7 as SPI function
P1DIR &= ~BIT5; // Set SCLK as input
P1DIR |= BIT6 | BIT7; // Set MOSI and MISO as output

UCB0CTL1 |= UCSWRST; // Put USCI in reset


UCB0CTL0 |= UCSLAVE + UCSYNC; // 3-pin, 8-bit SPI slave
UCB0I2COA = 0x01; // Set own address (optional)
UCB0CTL1 &= ~UCSWRST; // Release USCI for operation
}

unsigned char SPI_Read(void)


{
while (!(UCB0IFG & UCRXIFG)); // Wait for RX buffer to be full
return UCB0RXBUF; // Return received data
}
In the master code:
• The SPI interface is initialized, and data (0xAA) is sent to the slave in a continuous
loop.
• The UCB0 registers are configured for SPI master mode.
In the slave code:
• The SPI interface is initialized in slave mode.
• Data is read from the master in a continuous loop, which can then be processed as
needed.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Master Code for SPI:
#include <SPI.h>

void setup() {
pinMode(5, OUTPUT); // Set SCLK as output
pinMode(6, OUTPUT); // Set MOSI as output
pinMode(7, INPUT); // Set MISO as input
SPI.begin(); // Initialize SPI
}

void loop() {
SPI.transfer(0xAA); // Send data (0xAA) to the slave
delay(1000); // Delay between transmissions
}
Slave Code for SPI:
#include <SPI.h>

void setup() {
pinMode(5, INPUT); // Set SCLK as input
pinMode(6, INPUT); // Set MOSI as input
pinMode(7, OUTPUT); // Set MISO as output
SPI.begin(); // Initialize SPI
}

void loop() {
byte receivedData = SPI.transfer(0); // Read data from master
// Process received data as needed
delay(1000); // Delay for demonstration
}
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code for both master and slave
projects, then upload them separately.
• In Energia: Click the Upload button for both the master and slave codes to compile
and upload them to their respective MSP430 boards.
Step 4: Run the Program
• Connect the two MSP430 microcontrollers as per the circuit diagram.
• Power on both devices. The master should begin sending data (0xAA) to the slave,
which can process the data as programmed.
5. Results and Discussion
Expected Results:
• The master device sends the byte 0xAA to the slave continuously.
• The slave device receives the data and can process or display it as needed (this can
be extended to show the received data via an LED or serial output).
Discussion:
• SPI Operation: The master initiates communication by sending a clock signal on
SCLK, and the slave responds on the MISO line. Data transfer occurs on the MOSI
line, which carries data from the master to the slave.
• Full-Duplex Communication: SPI allows simultaneous sending and receiving of data,
making it efficient for applications requiring fast data transfer.
• Applications: SPI is commonly used in applications such as sensor interfacing, SD
card communication
Experiment 12: Networking MSP430 Microcontrollers Using Wi-Fi
1. Aim
The aim of this experiment is to demonstrate the capability of MSP430 microcontrollers to
communicate over a Wi-Fi network using a Wi-Fi module (e.g., ESP8266). This experiment
will illustrate how to set up a wireless network between MSP430 devices and exchange data.
2. Description
In this experiment, we will use the ESP8266 Wi-Fi module to connect MSP430
microcontrollers to a Wi-Fi network. One MSP430 will act as the server and the other as the
client. The client will send data to the server, which can process it and respond accordingly.
This experiment highlights how IoT (Internet of Things) devices can communicate wirelessly,
paving the way for remote control and monitoring applications.
Key Concepts:
• Wi-Fi Networking: Connecting devices to a wireless network to facilitate data
exchange.
• Server-Client Architecture: The server listens for requests from clients and
processes these requests accordingly.
• ESP8266 Module: A low-cost Wi-Fi module that enables microcontrollers to connect
to the internet.
3. Circuit Diagram
The circuit consists of an MSP430 microcontroller connected to an ESP8266 module.
• P1.0 (TX): Serial transmit pin to ESP8266 RX
• P1.1 (RX): Serial receive pin from ESP8266 TX
• Connect a common ground between the MSP430 and the ESP8266 module.
4. Implementation
Step 1: Set Up the IDE
• Use Code Composer Studio (CCS) or Energia as the development environment.
Step 2: Write the Code
Option 1: Code Composer Studio (CCS)
• Create Two New Projects:
1. Open CCS and go to File -> New -> CCS Project.
2. Select MSP430G2553 as the target device for both projects.
3. Name the first project (e.g., "WiFi_Server") and the second (e.g.,
"WiFi_Client").
4. Select the Empty Project with Main.c template for both.
Server Code Example:
This code demonstrates how to set up the MSP430 as a Wi-Fi server that listens for incoming
connections.
#include <msp430.h>
#include <string.h>

void UART_Init(void);
void sendATCommand(char *cmd, char *expectedResponse);
void setupWiFi(void);
void startServer(void);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
UART_Init(); // Initialize UART for ESP8266 communication
setupWiFi(); // Set up Wi-Fi connection
startServer(); // Start the server

while (1)
{
// Loop indefinitely
}
}

void UART_Init(void)
{
P1SEL |= BIT1 | BIT2; // Set P1.1 and P1.2 for UART TX/RX
P1SEL2 |= BIT1 | BIT2; // Select UART function
UCA0CTL1 |= UCSWRST; // Put USCI in reset
UCA0CTL1 |= UCSSEL_2; // Select SMCLK
UCA0BR0 = 104; // 9600 baud rate for 1 MHz
UCA0BR1 = 0; // Set upper byte
UCA0MCTL = UCBRS0; // Modulation
UCA0CTL1 &= ~UCSWRST; // Initialize USCI
}

void sendATCommand(char *cmd, char *expectedResponse)


{
// Send AT command to ESP8266 and wait for response
// Implement send and receive logic (serial communication)
}

void setupWiFi(void)
{
sendATCommand("AT+CWJAP=\"your_SSID\",\"your_PASSWORD\"", "OK");
}

void startServer(void)
{
sendATCommand("AT+CIPSERVER=1,80", "OK");
}
Client Code Example:
This code configures the MSP430 as a Wi-Fi client that connects to the server and sends data.
#include <msp430.h>
#include <string.h>

void UART_Init(void);
void sendATCommand(char *cmd, char *expectedResponse);
void connectToWiFi(void);
void sendData(void);

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
UART_Init(); // Initialize UART for ESP8266 communication
connectToWiFi(); // Connect to Wi-Fi
sendData(); // Send data to the server

while (1)
{
// Loop indefinitely
}
}

void UART_Init(void)
{
P1SEL |= BIT1 | BIT2; // Set P1.1 and P1.2 for UART TX/RX
P1SEL2 |= BIT1 | BIT2; // Select UART function
UCA0CTL1 |= UCSWRST; // Put USCI in reset
UCA0CTL1 |= UCSSEL_2; // Select SMCLK
UCA0BR0 = 104; // 9600 baud rate for 1 MHz
UCA0BR1 = 0; // Set upper byte
UCA0MCTL = UCBRS0; // Modulation
UCA0CTL1 &= ~UCSWRST; // Initialize USCI
}

void sendATCommand(char *cmd, char *expectedResponse)


{
// Send AT command to ESP8266 and wait for response
// Implement send and receive logic (serial communication)
}

void connectToWiFi(void)
{
sendATCommand("AT+CWJAP=\"your_SSID\",\"your_PASSWORD\"", "OK");
}
void sendData(void)
{
sendATCommand("AT+CIPSEND", ">");
sendATCommand("Hello from Client!", "SEND OK");
}
In the server code:
• The ESP8266 is initialized and set up to connect to the specified Wi-Fi network. It
listens for incoming connections on port 80.
In the client code:
• The client connects to the same Wi-Fi network and sends a simple message to the
server.
Option 2: Energia IDE
• Select the MSP430 Board:
o In Energia, go to Tools -> Board and select MSP430G2553.
• Server Code for Wi-Fi:
#include <SoftwareSerial.h>

SoftwareSerial espSerial(2, 3); // RX, TX

void setup() {
Serial.begin(9600);
espSerial.begin(115200);
connectToWiFi();
startServer();
}

void loop() {
// Handle incoming data
}

void connectToWiFi() {
espSerial.println("AT+CWJAP=\"your_SSID\",\"your_PASSWORD\"");
delay(2000);
}

void startServer() {
espSerial.println("AT+CIPSERVER=1,80");
delay(1000);
}
Client Code for Wi-Fi:
#include <SoftwareSerial.h>

SoftwareSerial espSerial(2, 3); // RX, TX

void setup() {
Serial.begin(9600);
espSerial.begin(115200);
connectToWiFi();
}

void loop() {
sendData();
delay(5000); // Send data every 5 seconds
}

void connectToWiFi() {
espSerial.println("AT+CWJAP=\"your_SSID\",\"your_PASSWORD\"");
delay(2000);
}

void sendData() {
espSerial.println("AT+CIPSEND");
delay(2000);
espSerial.println("Hello from Client!");
}
Step 3: Compile and Upload the Code
• In CCS: Click the Build icon (hammer) to compile the code for both server and client
projects, then upload them separately.
• In Energia: Click the Upload button for both the server and client codes to compile
and upload them to their respective MSP430 boards.
Step 4: Run the Program
• Connect the MSP430 microcontrollers to the ESP8266 modules as per the circuit
diagram.
• Power on both devices. The server should start listening for incoming connections, and
the client should connect to the Wi-Fi network and send data to the server.
5. Results and Discussion
Expected Results:
• The server MSP430 should successfully connect to the specified Wi-Fi network and
wait for incoming messages.
• The client MSP430 should connect to the same Wi-Fi network and send the message
"Hello from Client!" to the server.
Discussion:
• Wi-Fi Communication: This experiment demonstrates how MSP430 microcontrollers
can connect to a Wi-Fi network, enabling wireless communication between devices.
The use of the ESP8266 module facilitates this connectivity.
• Server-Client Interaction: The server waits for data from the client, showcasing the
basics of server-client architecture in networking. Further enhancements can include
handling multiple clients or processing data received from the clients.
• Applications: This setup can be used in various IoT applications, including remote
monitoring, home automation, and data logging.
6. Conclusion
In this experiment, we successfully established Wi-Fi networking between two MSP430
microcontrollers using an ESP8266 module. The master microcontroller acted as a server,
receiving data from the client microcontroller. This experiment highlights the potential of
MSP430 microcontrollers in IoT applications, allowing for wireless communication and control
in various scenarios. Future experiments can build on this foundation by implementing more
complex interactions and functionalities.

You might also like