Embedded Lab Work Book
Embedded Lab Work Book
Embedded Lab Work Book
Systems Engineering
Practical :- 1
AIM :- Introduction to MSP430 Kit and Programming Environment with program to work with ports.
MSP430 Launchpad Overview :-
MSP430 launchpad is a development board which can be used to design all Arduino based
applications since both have similar capabilities and features. Similar to Arduino being developed
onAVR controllers, the MSP430 launchpad is developed on TI MSP430 microcontrollers. This
device canbe used to develop low end applications and not high end applications, as it does not
have high processing power like Raspberry pi.
MSP430 Features :-
Port configuration :-
There are few ports present on the board as shown in the MSP430 pin-out and the functions of these
ports are described below.
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Operating Modes :-
The MSP430 has one active mode and five 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.
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
● Low-power mode 2 (LPM2)
CPU is disabled
MCLK and SMCLK are disabled
DCO's dc generator remains
enabledACLK remains active
1. PxDIR: This is the GPIO direction control register. Setting any bit to 0 in this register will
configurethe corresponding Pin[0 to 7] to be used as an Input while setting it to 1 will configure it
as Output.
2. PxIN (Read only): Used to Read values of the Digital I/O pins configured as Input. 0 = Input is
LOW, 1 = Input is HIGH.
3. PxOUT: Used to directly write values to pins when pullup/pulldown resistors are disabled. 0
= Output is LOW, 1 = Output is HIGH. When pullup/pulldown resistors are enabled: 0 = Pin is
pulled down, 1 = Pin is pulled up.
4. RxREN: For pins configured as input, PxREN is used to enable pullup/down resistor for a given
pin and PxOUT in conjunction with PxREN is used to select either Pullup or pulldown resistor.
Setting a bitto 1 will enable pullup/down resistor for the corresponding pin while setting it to 0 will
disable the same.
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
0 1 1 Input with Internal PullUp enabled
1 x x Output – PxREN has no effect
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
5. PxSEL & PxSEL2: Since most of the port pins are multiplexed with upto 4 different
functions, we need a mechanism to select these functions. This is achived using PxSEL and
PxSEL2 registers. Thebit combinations of these registers for a particular pin will select a particular
pin function. The bit combination is as given below:
PxSEL2(nth bit) PxSEL(nth Pin
bit) Function
0 0 GPIO (Digital I/O) Function
0 1 Primary Peripheral Function
1 0 Reserved. Consult device specific
datasheet.
1 1 Secondary Peripheral Function
Applications :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 2
AIM :- Getting started with CCS(code composer studio) for MSP430. (Blinking an LED).
CODE :-
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //Stop watchdog timer
while(1)
{
P1OUT ^=(BIT0|BIT6); //toggle the bits
delay_cycles(1000000); //1s Delay
}
}
OUTPUT :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
TASK 1 :- Toggle 4 LED connected at P1.0-P1.3 Port.
CODE :-
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //Stop watchdog timer
while(1)
{
P1OUT ^=(BIT0|BIT1|BIT2|BIT3); //toggle the bits
delay_cycles(1000000); //1s Delay
}
OUTPUT :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
TASK 2 :- Turn on led one by one from left to right and vice versa.
CODE :-
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //Stop watchdog timer
int i;
while(1)
{
for(i=0;i<=3;i++)
{
P1OUT ^= 1<<i; //toggle the bits
delay_cycles(1000000); //1s Delay
}
for(i=3;i>=0;i--)
{
P1OUT ^= 1<<i; //toggle the bits
delay_cycles(1000000); //1s Delay
if(i==0)
break;
}
}
}
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 3
AIM :- Switch interfacing with MSP430
A. LED on When Switch is Pressed
B. LED on/off using single Switch
CODE (A) :-
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //stop watchdog timer
while(1)
{
if(P1IN & BIT3) //if switch press
P1OUT |= BIT0; //LED ON
else
P1OUT &= ~BIT0; //LED OFF
}
}
OUTPUT (A) :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems
CODE (B) :-
Engineering
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; //stop watchdog timer
P1DIR|=BIT0; //p1.0 as output
P1OUT&= ~BIT0; //p1.0 bit is clear
P1DIR&=~BIT3; // p1.3 as input
P1REN|=BIT3; // enable pullup reg
P1OUT|=BIT3; // to enable pullup high
unsigned int x=0;
while(1)
{
if(((P1IN & BIT3)==0) && x==0)//if switch press
{
delay_cycles(200000); //20ms Delay
P1OUT |= BIT0;
while((P1IN & BIT3)==0);x=1;
}
if(((P1IN & BIT3)==0) && x==1)//if switch press
{
delay_cycles(200000); //20ms Delay
P1OUT &= ~BIT0;
while((P1IN & BIT3)==0);x=0;
}
}
}
OUTPUT (B) :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 4
AIM :- Interrupt Driven I/O concept in MSP430. Toggle Two LED 1st LED Continues Toggling and
2nd LED using switch with Interrupt.
CODE :-
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT6 + BIT0; // Configure P1.0 and P1.6 as Output
P1REN = BIT3; // p1.3 as input
P1OUT = BIT3; // enable pullup reg
P1IE = BIT3; // to enable pullup high
while(1)
{
enable_interrupt();
P1OUT |= (1<<0); //LED ON
delay_cycles(500000); //0.5s delay
P1OUT &= ~(1<<0); //LED OFF
delay_cycles(500000); //0.5s delay P1IFG
= ~BIT3; //clear Flag
}
}
#pragma vector=PORT1_VECTOR
interrupt void Port_1(void)
{
P1OUT ^= BIT6;
P1IFG =~BIT3;
delay_cycles(1000);
}
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 5
AIM :- Seven Segment Interfacing with MSP430. Write a Program to display 0 to F on LCD.
CODE :-
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
P2SEL &= ~(BIT6+BIT7); // Set P2.6 & P2.7 as GPIO (Default is XIN/XOUT)
while(1)
{
volatile unsigned int i;
for(i = 0; i < 16; i++)
{
P2OUT = (P2OUT & DMASK) + digits[i]; // Display current digitP2OUT
^= SEG_DP; // Toggle decimal point
delay_cycles(1000000); // Delay 1s
}
}
}
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Task :- WAP to display 00 to 99 on two seven segment with multiplexing concept.
Countincremented every one second.
CODE :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
for(i=0;i<10;i++)
Systems Engineering
{
P1OUT |= BIT2; //WRITE tens on the second segment
P2OUT = (P2OUT & DMASK) + digits[tens];
delay_cycles(50000); // Delay 1s
P1OUT &= ~BIT2;
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
SystemsP1OUT |= BIT1; Engineering
//WRITE unit on the first segment
P2OUT = (P2OUT & DMASK) + digits[unit];
delay_cycles(50000); // Delay 1s
P1OUT &= ~BIT1;
}
}
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int xx;
while(1)
{
for(xx=0;xx<100;xx++)
{
display(xx); // count from 1 to 99;
}
}
}
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 6
AIM :- Using Timer of MSP430. Configure timer block to generate 1 Hz signal.
CODE :-
void initTimer_A(void);void
delayMS(int msecs);
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR |= BIT0; //Configure P1.0 as Output
initTimer_A();
_enable_interrupt();
while(1)
{
P1OUT |= BIT0; //Drive P1.0 HIGH - LED1 ON
delayMS(500); //Wait 0.5 Secs
void initTimer_A(void)
{
//Timer0_A3 Configuration
TACCR0 = 0; //Initially, Stop the Timer
TACCTL0 |= CCIE; //Enable interrupt for CCR0.
TACTL = TASSEL_2 + ID_0 + MC_1; //Select SMCLK, SMCLK/1, Up Mode
}
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
while(OFCount<=msecs);
//Timer ISR
#pragma vector = TIMER0_A0_VECTOR
interrupt void Timer_A_CCR0_ISR(void)
{
OFCount++; //Increment Over-Flow Counter
}
OUTPUT :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Task 1 :- Using interrupt Configure timer block to generate 1 Hz signal.
CODE :-
#include <msp430.h> //header file that depends upon your board
#define BLINKY_DELAY_MS 500 //Change this as per your needs
void initTimer_A(void);void
delayMS(int msecs);
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR |= BIT0; //Configure P1.0 as Output
initTimer_A();
_enable_interrupt();
OFCount = 0;
TACCR0 = 1000-1; //Start Timer, Compare value for Up Mode to get 1msdelay
per loop
/*Total count = TACCR0 + 1. Hence we need to subtract 1.1000
ticks @ 1MHz will yield a delay of 1ms.*/
while(1);
}
void initTimer_A(void)
{
//Timer Configuration
TACCR0 = 0; //Initially, Stop the Timer
TACCTL0 |= CCIE; //Enable interrupt for CCR0.
TACTL = TASSEL_2 + ID_0 + MC_1; //Select SMCLK, SMCLK/1 , Up Mode
}
//Timer ISR
#pragma vector = TIMER0_A0_VECTOR
interrupt void Timer_A_CCR0_ISR(void)
{
OFCount++;
if(OFCount >= BLINKY_DELAY_MS)
{
P1OUT ^= BIT0;
OFCount = 0;
}
}
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
OUTPUT :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Task 2 :- Using interrupt in MSP430. Configure timer block to generate 1 Hz signal using continuous
mode.
CODE :-
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 7
AIM :- Using LCD with MSP430. Print your name on 1 st row of LCD and an incrementing count
(at1sec) on 2nd row.
CODE :-
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
char count[16];
int i;
while(1)
{
lcd_write(0x01, CMD); // Clear screen
lcd_setCursor(0,0); // set cursor
lcd_print("Manendra");
delay(50); // Wait for power up (50 ms)
lcd_setCursor(1,0); // set cursor
lcd_print("Count = ");
for(i=0;i<=10;i++)
{
lcd_setCursor(1,8); // set cursor
sprintf(count,"%d",i);
lcd_print(count);
delay(50); // Wait for power up (50 ms)
}
}
}
lcd.h :-
#ifndef LCD_H_
#define LCD_H_
#include <msp430.h>
#include <inttypes.h>
#define CMD 0
#define DATA 1
#define LCD_OUT P2OUT
#define LCD_DIR P2DIR
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibblefirst
pulseEN();
delay(1); // Wait ( 1 ms )
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0) // Write low nibblenext
pulseEN();
delay(1); // Wait ( 1 ms )
}
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
}
// Initialize LCD
void lcd_init()
{
P2SEL &= ~(BIT6+BIT7);
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
#endif /* LCD_H_ */
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 8
AIM :- Using UART Protocol. Do the communication between MSP430 and PC. Send
Temperature of IC Serially to PC. When temperature of IC exceed above 30C it sends
message “IC Temp High” andalso turn on LED.
CODE :-
void tempInit()
{
ADC10CTL0=SREF_1 + REFON + ADC10ON + ADC10SHT_3 ;
//1.5V ref,Ref on,64 clocks for sample
ADC10CTL1=INCH_10+ ADC10DIV_3; //temp sensor is at 10 and clock/4
}
int tempOut()
{
int t=0;`
delay_cycles(1000); //wait 4 ref to settle
initialise
while(1)
{
delay_cycles(500); //wait and set break point
temp=tempOut(); //read tempif
serial_print(temp);
if(temp>30)
{
P1OUT |= BIT0;
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
serial_println(" IC Temp High ");
}
else
P1OUT &= ~BIT0;
serial_println("\r\n");
delay_cycles(100000); //wait and set breakpoint
}
}
uart.h :-
#ifndef UART_H_
#define UART_H_
#include<msp430.h>
*str = '\0';
do
{
unsigned long m = num;
num /= 10;
char c = (m - 10 * num) + '0';
*--str = c;
} while(num);
serial_println(str);
}
void uart_Init()
{
if (CALBC1_1MHZ==0xFF) // Check if calibration constant erased
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
{
while(1); // do not load program
}
DCOCTL = 0; // Select lowest DCO settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz
DCOCTL = CALDCO_1MHZ;
#endif /* UART_H_ */
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 9
AIM :- Using ADC of MSP430. Make use of ADC for input and observe its effect in LED. LED must
onwhen input is greater than 0.2V.
Block Diagram :-
CODE :-
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog
char adc[16],vol[16];
int adcVal,ans,v;
while(1)
{
P1OUT &= ~(BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7);
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
SystemsADC10CTL0 |= ENC + ADC10SC; Engineering
// Sampling and conversion start
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end
//lcd
lcd_setCursor(0,0); // set cursor
sprintf(adc,"ADC=%d",adcVal); // Print ADC value on LCD
lcd_print(adc);
lcd_setCursor(1,0); // set cursor
sprintf(vol,"Voltage=%d",v); // Print Voltage value on LCD
lcd_print(vol);
//serial
serial_println("ADC=");
serial_print(adcVal); // Print ADC value on UART
serial_println(" Voltage=");
serial_print(v); // Print Voltage value on LCD
serial_println("\r\n"); // Print newline
delay_cycles(100000);
}
}
lcd.h :-
#ifndef LCD_H_
#define LCD_H_
#include <msp430.h>
#include <inttypes.h>
#define CMD 0
#define DATA 1
#define LCD_OUT P2OUT
#define LCD_DIR P2DIR
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
// Delay function for producing delay in 1 ms increments
void delay(uint8_t t)
{
uint8_t i;
for(i=t; i > 0; i--)
delay_cycles(1000);
}
LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibblefirst
pulseEN();
delay(1); // Wait ( 1 ms )
LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0) // Write low nibblenext
pulseEN();
delay(1); // Wait ( 1 ms )
}
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
delay(1); // Wait ( 1 ms )
}
// Initialize LCD
void lcd_init()
{
P2SEL &= ~(BIT6+BIT7);
LCD_DIR |= (D4+D5+D6+D7+RS+EN);
LCD_OUT &= ~(D4+D5+D6+D7+RS+EN);
#endif /* LCD_H_ */
uart.h :-
#ifndef UART_H_
#define UART_H_
#include<msp430.h>
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
*str = '\0';
do
{
unsigned long m = num;
num /= 10;
char c = (m - 10 * num) + '0';
*--str = c;
} while(num);
serial_println(str);
}
void uart_Init()
{
if (CALBC1_1MHZ==0xFF) // Check if calibration constant erased
{
while(1); // do not load program
}
DCOCTL = 0; // Select lowest DCO settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz
DCOCTL = CALDCO_1MHZ;
#endif /* UART_H_ */
OUTPUT :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 10
AIM :- Using Watchdog Timer of MSP430. Observe the effect of watchdog timer when processor is busy
blinking LED.
A. Watchdog Mode
B. Interval Timer Mode
CODE(A) :-
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop the WDT to prevent
reset int i; // Count value for delay P1DIR
|= BIT0 + BIT6; // P1.0(Red) and P1.6(Green)output
P1DIR&=~BIT3; // p1.3 as input
P1REN|=BIT3; // enable pullup reg
P1OUT|=BIT3; // to enable pullup high
CODE(B) :-
#include <msp430.h>
void main(void)
{
WDTCTL = WDT_MDLY_32; // 32ms interval (default)P1DIR
|= BIT0; // P1.0 output
IE1 |= WDTIE; // Enable the WDTIE bit
_BIS_SR(LPM0_bits + GIE); // Go to LPM0 with interrupts enabled
}
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
interrupt void IntervalTimer(void)
{
static int i = 0;i++;
if (i == 32) { // 31.25 * 32 ms = 1s
P1OUT ^= BIT0; // Toggle P1.0 using exclusive-OR
// 1s on, 1s off; period = 2s, f = 1/2s = 0.5Hz
i = 0;
}
}
OUTPUT :-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
Practical :- 11
AIM :- Using Comparator of MSP430. Compare the signal threshold level with input signal.
CODE :-
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop the WDT to prevent reset
P1DIR = GREEN + RED + BIT7; // P1.0 and P1.6 and P1.7 are outputs
CACTL2 = P2CA4; // P1.1 = +comp
CACTL1 = CARSEL + CAREF_2 + CAON; // -comp = 0.5*Vcc; Comparator on
P1SEL = BIT7; // P1.7 updates based on
CAOUT
while(1)
{
if (CACTL2 & 0x01)
{
P1OUT &= ~GREEN; // green LED off
P1OUT |= RED; // red LED on
}
else
{
P1OUT &= ~RED; // red LED off
P1OUT |= GREEN; // green LED on
}
}
}
OUTPUT:-
Grade :- Signature :-
MANENDRA
YADAV ,210760111003
SSASIT,SURAT
Embedded Electronics & Communication
Systems Engineering
MANENDRA
YADAV ,210760111003
SSASIT,SURAT