0% found this document useful (0 votes)
35 views48 pages

ES Manual MSP430 PDF

Uploaded by

Sneha Golakiya
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)
35 views48 pages

ES Manual MSP430 PDF

Uploaded by

Sneha Golakiya
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/ 48

Embedded Systems Electronics & Communication 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 on
AVR controllers, the MSP430 launchpad is developed on TI MSP430 microcontrollers. This device can
be used to develop low end applications and not high end applications, as it does not have high
processing power like Raspberry pi.
MSP430 Features :-

● Low Supply-Voltage Range: 1.8 V to 3.6 V


● Ultra-Low Power Consumption

⮚ Active Mode: 230 µA at 1 MHz, 2.2 V


⮚ Standby Mode: 0.5 µA
⮚ Off Mode (RAM Retention): 0.1 µA

● Five Power-Saving Modes


● Ultra-Fast Wake-Up From Standby Mode in Less Than 1 µs
● 16-Bit RISC Architecture, 62.5-ns Instruction Cycle Time
● Basic Clock Module Configurations

⮚ Internal Frequencies up to 16 MHz With Four Calibrated Frequency


⮚ Internal Very-Low-Power Low-Frequency (LF) Oscillator
⮚ 32-kHz Crystal
⮚ External Digital Clock Source

● Two 16-Bit Timer_A With Three Capture/Compare Registers


● Up to 24 Capacitive-Touch Enabled I/O Pins

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
● Universal Serial Communication Interface (USCI)

⮚ Enhanced UART Supporting Auto Baudrate Detection (LIN)


⮚ IrDA Encoder and Decoder
⮚ Synchronous SPI
⮚ I2C™

● On-Chip Comparator for Analog Signal Compare Function or Slope Analog-to-Digital (A/D)
Conversion
● 10-Bit 200-ksps Analog-to-Digital (A/D) Converter With Internal Reference, Sample -and-Hold, and
Autoscan
● Brownout Detector
● Serial Onboard Programming,No External Programming Voltage Needed,Programmable Code
Protection by Security Fuse
● On-Chip Emulation Logic With Spy-Bi-Wire Interface
● Family Members are Summarized
● Package Options

⮚ TSSOP: 20 Pin, 28 Pin


⮚ PDIP: 20 Pin
⮚ QFN: 32 Pin

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.

Name Pin Function


GPIO pins P1.0 to P1.7 & P2.0 to P2.5 These are the input/output pins of the
microcontroller (placed in the socket) given out
of the board for interfacing.
Button P1.3 Can be used for general purpose interfacing
during operation.
LEDs P1.0 & P1.6 Can be used for general purpose indication
during operation.
Reset Connected to RESET pin of If pressed the microcontroller will reset.
microcontroller
Power Connected Three pin jack at the bottom Can be used to connect eternal power sources
after programming.
Crystal pads Solder joints adjacent to A crystal oscillator can be soldered here for
controller providing more accurate clock source.
eZ430 connector Six pin connector Provides a bridge between Emulator board and
Microcontroller breakout board. Can be
disconnected to isolate both sections of the PCB.
Programming USB connector at the top The device will be connected to PC (For
port programming and power) using this port.

MSP430G2x13 and MSP430G2x53 Device Pinout, 28-Pin Devices, TSSOP :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

NOTE: ADC10 is available on MSP430G2x53 devices only.

Functional Block Diagram :-

NOTE :- Port P3 is available on 28-pin and 32-pin devices only.

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

CPU Register of MSP430

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.

The following six operating modes can be configured by software:

● Active mode (AM)


⮚ All clocks are active

● Low-power mode 0 (LPM0)


⮚ CPU is disabled
⮚ ACLK and SMCLK remain active, MCLK is disabled

● Low-power mode 1 (LPM1)


⮚ CPU is disabled
⮚ ACLK and SMCLK remain active, MCLK is disabled
⮚ DCO's dc generator is disabled if DCO not used in active mode

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

● Low-power mode 2 (LPM2)


⮚ CPU is disabled
⮚ MCLK and SMCLK are disabled
⮚ DCO's dc generator remains enabled
⮚ ACLK remains active

● Low-power mode 3 (LPM3)


⮚ CPU is disabled
⮚ MCLK and SMCLK are disabled
⮚ DCO's dc generator is disabled
⮚ ACLK remains active

● Low-power mode 4 (LPM4)


⮚ CPU is disabled
⮚ ACLK is disabled
⮚ MCLK and SMCLK are disabled
⮚ DCO's dc generator is disabled
⮚ Crystal oscillator is stopped

Input/output Pin Control Register :-

1. PxDIR: This is the GPIO direction control register. Setting any bit to 0 in this register will configure
the 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 bit
to 1 will enable pullup/down resistor for the corresponding pin while setting it to 0 will disable the
same.
PxDIR PxREN PxOUT I/O Config
0 0 x Input with resistors disabled
0 1 0 Input with Internal Pulldown enabled
0 1 1 Input with Internal PullUp enabled
1 x x Output – PxREN has no effect

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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. The
bit 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 bit) Pin 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 :-

● TI microcontroller learning tool


● Prototyping
● Robotics
● Planes and Drones
● Building automation
● Access control and security
● Security systems and electronic locks
● Electronic and Electrical measuring instruments
● Health monitoring devices

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 2
AIM :- Getting started with CCS(code composer studio) for MSP430. (Blinking an LED).

CODE :-

#include <msp430.h> //header file that depends upon your board

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

P1DIR |= BIT0|BIT6; //Declare PIN0 and PIN6 AS OUTPUT

P1OUT|=BIT0; //P1.0=1 set bit


P1OUT&=~(BIT6); //P1.6=0 clear bit

while(1)
{
P1OUT ^=(BIT0|BIT6); //toggle the bits
__delay_cycles(1000000); //1s Delay
}
}

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
TASK 1 :- Toggle 4 LED connected at P1.0-P1.3 Port.

CODE :-

#include <msp430.h> //header file that depends upon your board

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

P1DIR |= BIT0|BIT1|BIT2|BIT3; //Declare p1.0-p1.3 as output

while(1)
{
P1OUT ^=(BIT0|BIT1|BIT2|BIT3); //toggle the bits
__delay_cycles(1000000); //1s Delay
}

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
TASK 2 :- Turn on led one by one from left to right and vice versa.

CODE :-

#include <msp430.h> //header file that depends upon your board

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

P1DIR |= BIT0|BIT1|BIT2|BIT3; //Declare p1.0-p1.3 as output


P1OUT &= ~(BIT0|BIT1|BIT2|BIT3); //clear p1.0-p1.3 bits

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 :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 3
AIM :- Switch interfacing with MSP430
A. LED on When Switch is Pressed
B. LED on/off using single Switch

CODE (A) :-

#include <msp430.h> //header file that depends upon your board

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

P1DIR|=BIT0; //p1.0 as output

P1DIR&=~BIT3; // p1.3 as input


P1REN|=BIT3; // enable pullup reg
P1OUT|=BIT3; // to enable pullup high

while(1)
{
if(P1IN & BIT3) //if switch press
P1OUT |= BIT0; //LED ON
else
P1OUT &= ~BIT0; //LED OFF
}
}

OUTPUT (A) :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
CODE (B) :-

#include <msp430.h> //header file that depends upon your board

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) :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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 :-

#include <msp430.h> //header file that depends upon your board

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);
}

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 5
AIM :- Seven Segment Interfacing with MSP430. Write a Program to display 0 to F on LCD.

CODE :-

#include <msp430.h> //header file that depends upon your board

// Segments are connected to P2.0 - P2.7


#define SEG_A BIT0
#define SEG_B BIT1
#define SEG_C BIT2
#define SEG_D BIT3
#define SEG_E BIT4
#define SEG_F BIT5
#define SEG_G BIT6
#define SEG_DP BIT7

// Define each digit according to truth table


#define D0 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_DP)
#define D1 ~(SEG_B + SEG_C + SEG_DP)
#define D2 ~(SEG_A + SEG_B + SEG_D + SEG_E + SEG_G + SEG_DP)
#define D3 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_G + SEG_DP)
#define D4 ~(SEG_B + SEG_C + SEG_F + SEG_G + SEG_DP)
#define D5 ~(SEG_A + SEG_C + SEG_D + SEG_F + SEG_G + SEG_DP)
#define D6 ~(SEG_A + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G + SEG_DP)
#define D7 ~(SEG_A + SEG_B + SEG_C + SEG_DP)
#define D8 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G + SEG_DP)
#define D9 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_F + SEG_G + SEG_DP)
#define D10 ~(SEG_A + SEG_B + SEG_C + SEG_E + SEG_F + SEG_G + SEG_DP)
#define D11 ~(SEG_C + SEG_D + SEG_E + SEG_F + SEG_G + SEG_DP)
#define D12 ~(SEG_A + SEG_D + SEG_E + SEG_F + SEG_DP)
#define D13 ~(SEG_B + SEG_C + SEG_D + SEG_E + SEG_G + SEG_DP)
#define D14 ~(SEG_A + SEG_D + SEG_E + SEG_F + SEG_G + SEG_DP)
#define D15 ~(SEG_A + SEG_E + SEG_F + SEG_G + SEG_DP)

// Define mask value for all digit segments except DP


#define DMASK ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)

// Store digits in array for display


const unsigned int digits[16] = {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10,
D11, D12, D13, D14, D15};

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

// Initialize 7-segment pins as Output


P2DIR |= (SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G + SEG_DP);

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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 digit
P2OUT ^= SEG_DP; // Toggle decimal point
__delay_cycles(1000000); // Delay 1s
}
}
}

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
Task :- WAP to display 00 to 99 on two seven segment with multiplexing concept. Count
incremented every one second.

CODE :-

#include <msp430.h> //header file that depends upon your board

// Define Pin Mapping of 7-segment Display


// Segments are connected to P2.0 - P2.7
#define SEG_A BIT0
#define SEG_B BIT1
#define SEG_C BIT2
#define SEG_D BIT3
#define SEG_E BIT4
#define SEG_F BIT5
#define SEG_G BIT6
#define SEG_DP BIT7

// Define each digit according to truth table


#define D0 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F)
#define D1 ~(SEG_B + SEG_C)
#define D2 ~(SEG_A + SEG_B + SEG_D + SEG_E + SEG_G)
#define D3 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_G)
#define D4 ~(SEG_B + SEG_C + SEG_F + SEG_G)
#define D5 ~(SEG_A + SEG_C + SEG_D + SEG_F + SEG_G)
#define D6 ~(SEG_A + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)
#define D7 ~(SEG_A + SEG_B + SEG_C)
#define D8 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)
#define D9 ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_F + SEG_G)

// Define mask value for all digit segments except DP


#define DMASK ~(SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G)

// Store digits in array for display


const unsigned int digits[10] = {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9};

void display(int num)


{
int i,unit,tens;

unit = num%10; // compute the unit number


tens = num/10; //compute the tens number

for(i=0;i<10;i++)
{
P1OUT |= BIT2; //WRITE tens on the second segment
P2OUT = (P2OUT & DMASK) + digits[tens];
__delay_cycles(50000); // Delay 1s
P1OUT &= ~BIT2;

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
P1OUT |= BIT1; //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

// Initialize 7-segment pins as Output


P2DIR |= (SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G);
P1DIR|=(BIT1 + BIT2); // Initialize p1.3 AND p1.4 as Output

int xx;
while(1)
{
for(xx=0;xx<100;xx++)
{
display(xx); // count from 1 to 99;
}
}
}

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 6
AIM :- Using Timer of MSP430. Configure timer block to generate 1 Hz signal.

CODE :-

#include <msp430.h> //header file that depends upon your board

void initTimer_A(void);
void delayMS(int msecs);

unsigned int OFCount;

int main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR |= BIT0; //Configure P1.0 as Output

//Set MCLK = SMCLK = 1MHz


BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

initTimer_A();
_enable_interrupt();

while(1)
{
P1OUT |= BIT0; //Drive P1.0 HIGH - LED1 ON
delayMS(500); //Wait 0.5 Secs

P1OUT &= ~BIT0; //Drive P1.0 LOW - LED1 OFF


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
}

void delayMS(int msecs)


{
OFCount = 0; //Reset Over-Flow counter
TACCR0 = 1000-1; //Start Timer, Compare value for Up Mode to get 1ms
delay per loop
//Total count = TACCR0 + 1. Hence we need to subtract 1.

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
while(OFCount<=msecs);

TACCR0 = 0; //Stop Timer


}

//Timer ISR
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A_CCR0_ISR(void)
{
OFCount++; //Increment Over-Flow Counter
}

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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);

unsigned int OFCount;

int main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR |= BIT0; //Configure P1.0 as Output

//Set MCLK = SMCLK = 1MHz


BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;

initTimer_A();
_enable_interrupt();

OFCount = 0;
TACCR0 = 1000-1; //Start Timer, Compare value for Up Mode to get 1ms
delay 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;
}

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
}

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
Task 2 :- Using interrupt in MSP430. Configure timer block to generate 1 Hz signal using continuous
..mode.

CODE :-

#include <msp430.h> //header file that depends upon your board


int c=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR |= BIT0 | BIT6; // P1.0 output
TACCTL0 = CCIE; // CCR0 interrupt enabled
TACCR0 = 0; //load tccr0 value
TACTL = TASSEL_2 + MC_2 + ID_3 + TAIE; // SMCLK, Continuous mode, divider
8
TAR = 53036; //load TAR value
_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
while(1);
}

#pragma vector = TIMER0_A0_VECTOR //Timer0,TAIFG interrupt vector


__interrupt void Timer_A(void)
{
switch(TAIV)
{
case 0x000A: c++;
if(c==5)
{
P1OUT ^= BIT0; //TAR overflow interrupt
c=0;
}TAR = 53036;
break;
}
}

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 7
AIM :- Using LCD with MSP430. Print your name on 1 st row of LCD and an incrementing count (at
1sec) on 2nd row.

CODE :-

#include <msp430.h> //header file that depends upon your board


#include <lcd.h> //header file for LCD
#include <stdio.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
char count[16];
int i;

lcd_init(); // initializing LCD

while(1)
{
lcd_write(0x01, CMD); // Clear screen
lcd_setCursor(0,0); // set cursor
lcd_print("PRITI");
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

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
#define D4 BIT4
#define D5 BIT5
#define D6 BIT6
#define D7 BIT7
#define RS BIT2
#define EN BIT3

// 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);
}

// Function to pulse EN pin after data is written


void pulseEN(void)
{
LCD_OUT |= EN;
delay(1); // Wait ( 1 ms )
LCD_OUT &= ~EN;
delay(1); // Wait ( 1 ms )
}

//Function to write data/command to LCD


void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode

LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble
first
pulseEN();
delay(1); // Wait ( 1 ms )

LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0) // Write low nibble
next
pulseEN();
delay(1); // Wait ( 1 ms )
}

// Function to print a string on LCD


void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
}

// Function to move cursor to desired position on LCD


void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);
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);

delay(10); // Wait for power up ( 10 ms )


lcd_write(0x33, CMD); // Initialization Sequence 1
delay(1); // Wait ( 1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 1 ms )
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor ON, Blink ON
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(1);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}

#endif /* LCD_H_ */

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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” and
also turn on LED.

CODE :-

#include<msp430.h> //header file that depends upon your board


#include<uart.h> //header file for serial communication

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

ADC10CTL0 |= ENC + ADC10SC; //enable conversion and start conversion


while(ADC10CTL1 & BUSY); //wait..i am converting..pum..pum..
t=ADC10MEM; //store val in t
ADC10CTL0&=~ENC; //disable adc conv
return(int) ((t - 673) * 423) / 1024;
// C = (
(t/1024)*1500mV)-986mV)*1/3.55mV
//return(int) ((t * 27069L - 18169625L) >> 16); //convert and pass
}
void main(void)
{
volatile int temp; //initialise
P1DIR |= BIT0;

WDTCTL = WDTPW + WDTHOLD; // stop watchdog


temp=0;
tempInit(); //initialise adc

uart_Init(); //uart initialise

while(1)
{
__delay_cycles(500); //wait and set break point
temp=tempOut(); //read tempif
serial_print(temp);
if(temp>30)
{
P1OUT |= BIT0;

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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>

void serial_println(char *text)


{
unsigned int i = 0;
while(text[i] != '\0')
{
while (!(IFG2&UCA0TXIFG)); // Check if TX is ongoing
UCA0TXBUF = text[i]; // TX -> Received Char + 1
i++;
}
}

void serial_print(unsigned int num)


{
char buf[6];
char *str = &buf[5];

*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

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
{
while(1); // do not load program
}
DCOCTL = 0; // Select lowest DCO settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1 MHz
DCOCTL = CALDCO_1MHZ;

P1SEL = BIT1 + BIT2 ; // Select UART RX/TX function on P1.1,P1.2


P1SEL2 = BIT1 + BIT2;

UCA0CTL1 |= UCSSEL_2; // UART Clock -> SMCLK


UCA0BR0 = 104; // Baud Rate Setting for 1MHz 9600
UCA0BR1 = 0; // Baud Rate Setting for 1MHz 9600
UCA0MCTL = UCBRS_1; // Modulation Setting for 1MHz 9600
UCA0CTL1 &= ~UCSWRST; // Initialize UART Module
}

#endif /* UART_H_ */

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 9
AIM :- Using ADC of MSP430. Make use of ADC for input and observe its effect in LED. LED must on
.when input is greater than 0.2V.

Block Diagram :-

CODE :-

#include <msp430.h> //header file that depends upon your board


#include <stdio.h>
#include <lcd.h>
#include <uart.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog

char adc[16],vol[16];
int adcVal,ans,v;

P2SEL &= ~(BIT6+BIT7);


P1DIR |= (BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7);
lcd_init(); //LCD initialise
uart_Init(); //uart initialise
ADC10AE0 |= BIT0; // P1.0
ADC10CTL1 = INCH_0; // ADC Channel -> 1 (P1.0)
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON; // Ref -> Vcc, 64 CLK S&H

while(1)
{
P1OUT &= ~(BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7);

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while(ADC10CTL1 & ADC10BUSY); // Wait for conversion to end

adcVal = ADC10MEM; // Read ADC Value


v= (adcVal * 3.3)/1023;

ans = adcVal/113; //divide output in 9 parts for 8 LEDs + no LED


for(;ans!=0;ans--) //start from value of ans turn on all LEDs 1 by 1
{
P1OUT |=(1<<(ans-1));
}
__delay_cycles(100000);

//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

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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);
}

// Function to pulse EN pin after data is written


void pulseEN(void)
{
LCD_OUT |= EN;
delay(1); // Wait ( 1 ms )
LCD_OUT &= ~EN;
delay(1); // Wait ( 1 ms )
}

//Function to write data/command to LCD


void lcd_write(uint8_t value, uint8_t mode)
{
if(mode == CMD)
LCD_OUT &= ~RS; // Set RS -> LOW for Command mode
else
LCD_OUT |= RS; // Set RS -> HIGH for Data mode

LCD_OUT = ((LCD_OUT & 0x0F) | (value & 0xF0)); // Write high nibble
first
pulseEN();
delay(1); // Wait ( 1 ms )

LCD_OUT = ((LCD_OUT & 0x0F) | ((value << 4) & 0xF0) // Write low nibble
next
pulseEN();
delay(1); // Wait ( 1 ms )
}

// Function to print a string on LCD


void lcd_print(char *s)
{
while(*s)
{
lcd_write(*s, DATA);
s++;
}
}

// Function to move cursor to desired position on LCD


void lcd_setCursor(uint8_t row, uint8_t col)
{
const uint8_t row_offsets[] = { 0x00, 0x40};
lcd_write(0x80 | (col + row_offsets[row]), CMD);

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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);

delay(10); // Wait for power up ( 10 ms )


lcd_write(0x33, CMD); // Initialization Sequence 1
delay(1); // Wait ( 1 ms )
lcd_write(0x32, CMD); // Initialization Sequence 2
delay(1); // Wait ( 1 ms )
lcd_write(0x28, CMD); // 4 bit mode, 2 line
delay(1);
lcd_write(0x0C, CMD); // Display ON, Cursor ON, Blink ON
delay(1);
lcd_write(0x01, CMD); // Clear screen
delay(1);
lcd_write(0x06, CMD); // Auto Increment Cursor
delay(1);
lcd_setCursor(0,0); // Goto Row 1 Column 1
}

#endif /* LCD_H_ */

uart.h :-

#ifndef UART_H_
#define UART_H_

#include<msp430.h>

void serial_println(char *text)


{
unsigned int i = 0;
while(text[i] != '\0')
{
while (!(IFG2&UCA0TXIFG)); // Check if TX is ongoing
UCA0TXBUF = text[i]; // TX -> Received Char + 1
i++;
}
}

void serial_print(unsigned int num)


{
char buf[6];
char *str = &buf[5];

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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;

P1SEL = BIT1 + BIT2 ; // Select UART RX/TX function on P1.1,P1.2


P1SEL2 = BIT1 + BIT2;

UCA0CTL1 |= UCSSEL_2; // UART Clock -> SMCLK


UCA0BR0 = 104; // Baud Rate Setting for 1MHz 9600
UCA0BR1 = 0; // Baud Rate Setting for 1MHz 9600
UCA0MCTL = UCBRS_1; // Modulation Setting for 1MHz 9600
UCA0CTL1 &= ~UCSWRST; // Initialize UART Module
}

#endif /* UART_H_ */

OUTPUT :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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) :-

#include <msp430.h> //header file that depends upon your board

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

P1OUT &= ~BIT0; // P1.0 = 0


for(i =0;i<0xffff;i++); // A Small delay

WDTCTL = WDT_MRST_32; // Put WDT+ in Watch Dog Mode

while(P1IN & BIT3); // used to simulate glitch


P1OUT |= BIT0; // P1.0 = 1(Red LED On)
WDTCTL = WDT_MRST_32; // Reset WDT+ for another 1 second,
while(1)
{
P1OUT^= BIT6; // Switch on green LED
__delay_cycles(1000); // 1ms Delay
}
}

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
}

#pragma vector = WDT_VECTOR // Interval timer vector location

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication 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 :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Practical :- 11
AIM :- Using Comparator of MSP430. Compare the signal threshold level with input signal.

CODE :-

#include <msp430.h> //header file that depends upon your board


#define GREEN BIT0
#define RED BIT6

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 :-

SSASIT, SURAT

180760111014
Embedded Systems Electronics & Communication Engineering

Grade :- ____________________________ Signature :- ______________________________________

SSASIT, SURAT

180760111014

You might also like