0% found this document useful (0 votes)
41 views53 pages

EMSLabManual DevanshRamdurgekar 0801EC221025

The document is a laboratory journal for the Embedded Systems course at Shri Govindram Seksaria Institute of Technology & Science, detailing experiments conducted by a student named Devansh Ramdurgekar. It includes a list of experiments with dates, aims, components used, and code snippets for various projects involving AVR ATmega32 and STM32 microcontrollers. The journal serves as a record of practical coursework completed during the Jan-June 2025 session.
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)
41 views53 pages

EMSLabManual DevanshRamdurgekar 0801EC221025

The document is a laboratory journal for the Embedded Systems course at Shri Govindram Seksaria Institute of Technology & Science, detailing experiments conducted by a student named Devansh Ramdurgekar. It includes a list of experiments with dates, aims, components used, and code snippets for various projects involving AVR ATmega32 and STM32 microcontrollers. The journal serves as a record of practical coursework completed during the Jan-June 2025 session.
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

SHRI GOVINDRAM SEKSARIA INSTITUTE OF

TECHNOLOGY & SCIENCE, INDORE 452003


(AN AUTONOMOUS INSTITUTE, ESTABLISHED IN 1952)

LABORATORY JOURNAL

Embedded System (EC35513)


Session – Jan-June 2025
B Tech. III year
Department of Electronics & Telecommunication Engineering

NAME : DEVANSH RAMDURGEKAR


ROLL NO : 0801EC221025
CLASS : B.TECH
BRANCH: ELECTRONICS AND TELECOMMUNICATION
SUBJECT: EMBEDDED SYSTEMS EC35513
SEMESTER: VI

Certificate
This is to certify that Ms./Mr.
Roll No. studying in
Year of this institute has completed a practical course based
on the syllabus and given a satisfactory account of it in the notebook containing a
record of the laboratory work.

Date: Signature
Grade: Professor in charge

2
INDEX

SNo Date of Title Date of

Experiment submission
To blink multiple LEDs on a single port using AVR
1. 8/01/25 ATmega32 Microcontroller. 13/01/25

Blink multiple LEDs with corresponding Switches


2 13/01/25 using AVR ATmega32 Microcontroller using default 15/01/25
port.

To blink multiple LEDs with corresponding Switches


3 15/01/25 using AVR ATmega32 Microcontroller. 20/01/25

To control two Stepper Motors using ULN2003A


4 21/01/25 motor drivers and AVR ATmega32 Microcontroller 22/01/25

To control single Stepper Motor using ULN2003A


5 27/01/25 motor driver and AVR ATmega32 Microcontroller 29/01/25
To display data(string) on LCD Screen using AVR
6 03/02/25 ATmega32 Microcontroller 04/02/25

To design counters on Seven Segment Display


7 04/02/25 using AVR ATmega32 Microcontroller 05/02/25
To generate square waveform with 50% duty cycle
8 05/02/25 using AVR ATmega32 microcontroller 10/02/25
To design AC Relay Interfacing using AVR
9 10/02/25 ATmega32 microcontroller 11/02/25
To Blink LED using STM32 microcontrollert
10 11/02/25 12/02/25
To Blink Multiple LEDs at different ports using
11 17/02/25 STM32 microcontroller. 18/02/25
To Display Strings on LCD using STM32
12 18/02/25 microcontroller. 19/02/25
To Blink LEDs ON & OFF and display it on LCD
13 19/02/25 using STM32 microcontroller 24/02/25
To Read Temperature from LM35 and Display on
14 24/02/25 LCD using STM32 microcontroller. 26/02/25
To Blink LED with the help of Switch using STM32
15 26/02/25 microcontroller. 5/03/25

16 5/03/25 To Interface DC Motor using STM32 12/03/25


microcontroller
To Interface Two DC Motors with Two Switches
17 12/03/25 using STM32 microcontroller. 19/03/25

To blink LED using MSP430 microcontroller.


18 19/03/25 26/03/25
To blink LED on Positive Edge of Clock using
19 26/03/25 MSP430 microcontroller. 02/04/25
To Simulate Timers using MSP430 microcontroller.
20 02/04/25 09/04/25
EXPERIMENT - 1

AIM : To blink multiple LEDs on a single port using AVR ATmega32 Microcontroller.

Components Used : LEDs, AVR ATmega32 Microcontroller IC and connecting wires.

Code :

start:
ldi r16, 0xff
out ddrd, r16
ldi r17, 0xff
hi : out portd,r17
call delay
ldi r20, 0xaa
out portd, r20
call delay
ldi r21, 0x55
out portd,r21
call delay
ldi r18,0x00
out portd,r18
call delay
rjmp hi

delay:
ldi r19,240
l3 : ldi r20,240
l2 : dec r20
brne l2
dec r19
brne l3
ret

4
OUTPUT :

5
EXPERIMENT - 2

AIM : To blink multiple LEDs with corresponding Switches using AVR ATmega32
Microcontroller

Components Used : LEDs, Switches, AVR ATmega32 Microcontroller IC and connecting wires.

Code :

start:
ldi r16, 0xff
ldi r17, 0x00
out ddrd, r16
out ddrb, r15
hi: in r20, 0x16
out portd, r20
call delay
rjmp hi

delay:
ldi r19,240
l3 : ldi r20,240
l2 : dec r20
brne l2
dec r19
brne l3
ret

6
OUTPUT :

7
EXPERIMENT - 3

AIM : To blink LEDs in particular Patterns using AVR ATmega32 Microcontroller

Components Used : LEDs, Switches, AVR ATmega32 Microcontroller IC and connecting wires.

Code :

; --- Initialization ---


start:
ldi r16, 0x55 ; Initial pattern: 01010101
ldi r18, 0xFF ;
out DDRD, r18 ; Set PORTD as OUTPUT
out PORTD, r16 ; Output initial pattern

ldi r20, 10 ; Initialize outer loop counter (r20 = 10)

; --- Main Loop ---


main_loop:
ldi r21, 70 ; Inner loop counter (r21 = 70)
toggle_loop:
com r16 ; Invert bits in r16 (e.g., 0x55 → 0xAA)
out PORTD, r16 ; Update PORTD
call delay ; Add a visible delay (e.g., ~100ms)
dec r21 ; Decrement inner counter
brne toggle_loop ; Repeat inner loop if r21 != 0
dec r20 ; Decrement outer counter
brne main_loop ; Repeat outer loop if r20 != 0
rjmp start ; Restart the program (optional)

; --- Delay Subroutine (~100ms at 16MHz) ---


delay:
ldi r22, 255 ; Delay counter 1
delay1:
ldi r23, 255 ; Delay counter 2
delay2:
dec r23
brne delay2
dec r22
brne delay1
ret

8
OUTPUT :

9
EXPERIMENT - 4

AIM : To control two Stepper Motors using ULN2003A motor drivers and AVR ATmega32
Microcontroller

Components Used : Stepper Motors, Motor Drivers, Switches, AVR ATmega32 Microcontroller
IC and connecting wires.

Code :

#include <xc.h>
#include <avr/io.h>
#include <util/delay.h>
#define step_angle 1.8
#define step_delay 5000
#define F_CPU 80000000
int main(void)
{
DDRC = 0xff;
DDRA = 0x00;
int steps = 90/ step_angle;

void clock_anticlock(){
for (int i =0 ; i < steps/4;i++){
PORTC = 0x18; _delay_ms(step_delay);
PORTC = 0x24; _delay_ms(step_delay);
PORTC = 0x42; _delay_ms(step_delay);
PORTC = 0x81; _delay_ms(step_delay);
}
}

void anticlock_clock(){
for (int i =0 ; i < steps/4;i++){
PORTC = 0x81; _delay_ms(step_delay);
PORTC = 0x42; _delay_ms(step_delay);
PORTC = 0x24; _delay_ms(step_delay);
PORTC = 0x18; _delay_ms(step_delay);
}
}

void clock_clock(){
for (int i =0 ; i < steps/4;i++){
PORTC = 0x88; _delay_ms(step_delay);
PORTC = 0x44; _delay_ms(step_delay);

10
PORTC = 0x22; _delay_ms(step_delay);
PORTC = 0x11; _delay_ms(step_delay);
}
}

void anticlock_anticlock(){
for (int i =0 ; i < steps/4;i++){
PORTC = 0x11; _delay_ms(step_delay);
PORTC = 0x22; _delay_ms(step_delay);
PORTC = 0x44; _delay_ms(step_delay);
PORTC = 0x88; _delay_ms(step_delay);
}
}

if(PINA==0x00) { clock_clock(); }
if(PINA==0x01) { clock_anticlock(); }
if(PINA==0x10) { anticlock_clock(); }
if(PINA==0x11) { anticlock_anticlock(); }

while(1);
}

OUTPUT :

11
EXPERIMENT - 5

AIM : To control single Stepper Motor using ULN2003A motor driver and AVR ATmega32
Microcontroller

Components Used : Stepper Motor, Motor Driver, AVR ATmega32 Microcontroller IC and
connecting wires.

Code :

#define STEP_ANGLE 1.8


#define STEP_DELAY 5000
#define F_CPU 80000000

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
DDRC = 0x0F; // Configure only lower 4 bits of PORTC as output (for one stepper motor)

int steps = 90 / STEP_ANGLE;


for (int i = 0;i<steps/4;i++){
PORTC = 0x08; _delay_ms(STEP_DELAY);
PORTC = 0x04;_delay_ms(STEP_DELAY);
PORTC = 0x02; _delay_ms(STEP_DELAY);
PORTC = 0x01;_delay_ms(STEP_DELAY);
}
while (1);
}

12
OUTPUT :

13
EXPERIMENT - 6

AIM : To display data(string) on LCD Screen using AVR ATmega32 Microcontroller

Components Used : A 16x2 LCD Screen, Resistor, AVR ATmega32 Microcontroller IC and
connecting wires.

Code :

#ifndef F_CPU
#define F_CPU 16000000UL // Clock speed is 16 MHz
#endif

#include <avr/io.h>
#include <util/delay.h>

// LCD Control Pins (RS, RW, EN)


#define LCD_RS PB0
#define LCD_RW PB1
#define LCD_EN PB2

// LCD Data Pins (D0 to D7)


#define LCD_DATA_PORT PORTC
#define LCD_DATA_DDR DDRC

// Function to send a command to the LCD


void LCD_Command(unsigned char cmd) {
LCD_DATA_PORT = cmd; // Send command to data pins
PORTB &= ~(1 << LCD_RS); // RS = 0 (Command mode)
PORTB &= ~(1 << LCD_RW); // RW = 0 (Write mode)
PORTB |= (1 << LCD_EN); // EN = 1 (Enable pulse)
_delay_ms(1); // Small delay
PORTB &= ~(1 << LCD_EN); // EN = 0
_delay_ms(2); // Allow command execution
}

// Function to send a character to the LCD


void LCD_Char(unsigned char data) {
LCD_DATA_PORT = data; // Send data to data pins
PORTB |= (1 << LCD_RS); // RS = 1 (Data mode)
PORTB &= ~(1 << LCD_RW); // RW = 0 (Write mode)
PORTB |= (1 << LCD_EN); // EN = 1 (Enable pulse)
_delay_ms(1);

14
PORTB &= ~(1 << LCD_EN); // EN = 0
_delay_ms(2);
}

// Function to initialize the LCD


void LCD_Init(void) {
DDRB |= (1 << LCD_RS) | (1 << LCD_RW) | (1 << LCD_EN); // Set control pins as output
LCD_DATA_DDR = 0xFF; // Set data port as output

_delay_ms(15); // Wait for LCD to power up


LCD_Command(0x38); // 8-bit mode, 2 lines, 5x7 font
LCD_Command(0x0C); // Display ON, Cursor OFF
LCD_Command(0x06); // Increment cursor
LCD_Command(0x01); // Clear display
_delay_ms(2); // Wait for command execution
}

// Function to display a string on the LCD


void LCD_String(const char *str) { // Use const char * to avoid address space issues
while (*str) {
LCD_Char(*str++); // Display each character
}
}

int main(void) {
LCD_Init(); // Initialize the LCD

while (1) {
LCD_Command(0x80); // Move cursor to first line
LCD_String("Hello, World!");

LCD_Command(0xC0); // Move cursor to second line


LCD_String("ATmega32");

_delay_ms(1000); // Wait for a second


LCD_Command(0x01); // Clear display
_delay_ms(2);
}

return 0;
}

15
OUTPUT :

16
EXPERIMENT - 7

AIM : To design counters on Seven Segment Display using AVR ATmega32 Microcontroller

Components Used : 3 seven segment displays, AVR ATmega32 Microcontroller IC and


connecting wires.

Code 1 :

#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL

unsigned char seg_digit[] = {


0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};

void init_ports() {
DDRC = 0xFF; // Set PORTC as output
PORTC = 0x00; // Initialize PORTC to 0
DDRB = 0xFF; // Set PORTC as output
PORTB = 0x00; // Initialize PORTC to 0
DDRD = 0xFF; // Set PORTC as output
PORTD = 0x00; // Initialize PORTC to 0
}

void display_digit(unsigned char digit) {

int main(void) {
unsigned char i,j,k;

init_ports();
while (1) {

17
PORTB = 0X00;
PORTC = 0X00;
PORTD = 0X00;
for (i=0;i<=9;i++)
{
PORTC=seg_digit[i];
for (j=0;j<=9;j++)
{
PORTB=seg_digit[j];
for (k=0;k<=9;k++)
{
PORTD=seg_digit[k];
_delay_ms(200);
}
_delay_ms(200);
}
}
}
return 0;
}

OUTPUT 1 :

18
Code 2 :

#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL

unsigned char seg_digit[] = {


0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};

void init_ports() {
DDRC = 0xFF; // Set PORTC as output
PORTC = 0x00; // Initialize PORTC to 0
DDRB = 0xFF; // Set PORTB as output
PORTB = 0x00; // Initialize PORTB to 0
}

int main(void) {
unsigned char i, j;

init_ports();
while (1) {
PORTB = 0X00;
PORTC = 0X00;
for (i = 0; i <= 9; i++) {
PORTC = seg_digit[i];
for (j = 0; j <= 9; j++) {
PORTB = seg_digit[j];
_delay_ms(200);
}
}
}
return 0;
}

19
Output 2 :

Code 3 :

#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL

unsigned char seg_digit[] = {


0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};

void init_ports() {
DDRC = 0xFF; // Set PORTC as output

20
PORTC = 0x00; // Initialize PORTC to 0
}

int main(void) {
unsigned char i;

init_ports();
while (1) {
PORTC = 0X00;
for (i = 0; i <= 9; i++) {
PORTC = seg_digit[i];
_delay_ms(200);
}
}
return 0;
}

Output 3 :

21
EXPERIMENT - 8

AIM : To generate square waveform with 50% dutycycle using AVR ATmega32 microcontroller.

Components Used : Oscilloscope, AVR ATmega32 Microcontroller IC and connecting wires.

Code 1(using manual delay) :

start:
ldi r16, 0xff
ldi r17, 0x00
out ddrd, r16
out ddrb, r15
hi: in r20, 0x16
out portd, r20
call delay
rjmp hi

delay:
ldi r19,240
l3 : ldi r20,240
l2 : dec r20
brne l2
dec r19
brne l3
ret

Code 2(using timer in compare mode) :

; Define constants
.equ F_CPU = 1000000 ; 1 MHz clock
.equ PRESCALER = 1024 ; Prescaler value
.equ OCR_VALUE = (F_CPU / PRESCALER / 2) - 1 ; 50% Duty Cycle Calculation

.org 0x00
rjmp main ; Reset vector

.org 0x20 ; Start of program memory


main:
; Set PB0 as output
sbi DDRB, PB0 ; Set PB0 as output

; Configure Timer0
ldi r16, (1 << WGM01) ; Set CTC mode (Clear Timer on Compare Match)

22
out TCCR0, r16 ; Apply setting to TCCR0

; Set compare value for 50% duty cycle


ldi r16, OCR_VALUE
out OCR0, r16 ; Load OCR0 with the calculated value

; Set prescaler to 1024 (Start Timer)


ldi r16, (1 << CS02) | (1 << CS00)
out TCCR0, r16 ; Apply prescaler setting

loop:
sbis TIFR, OCF0 ; Check if Timer0 Compare Match flag is set
rjmp loop ; If not, keep checking

sbi PINB, PB0 ; Toggle PB0 (LED)


sbi TIFR, OCF0 ; Clear Timer0 Compare Match flag
rjmp loop ; Repeat

.end

Code 3(using timer in normal mode) :

.equ F_CPU = 1000000 ; 1 MHz clock


.equ PRESCALER = 1024 ; Timer prescaler value
.equ TIMER_COUNT = 256 - (F_CPU / PRESCALER / 2) ; Preload value for 50% duty cycle

.org 0x00
rjmp main ; Reset vector

.org 0x20 ; Start of program memory


main:
; Set PB0 as output
sbi DDRB, PB0 ; Configure PB0 as output

; Configure Timer0 in Normal Mode


ldi r16, (1 << CS02) | (1 << CS00) ; Prescaler = 1024
out TCCR0, r16 ; Apply to Timer Control Register

ldi r16, TIMER_COUNT ; Load timer preload value


out TCNT0, r16 ; Set Timer0 initial value

loop:
sbis TIFR, TOV0 ; Check Timer Overflow flag
rjmp loop ; Wait for overflow

23
sbi PINB, PB0 ; Toggle PB0 (LED)
sbi TIFR, TOV0 ; Clear Overflow flag
ldi r16, TIMER_COUNT ; Reload timer value
out TCNT0, r16

rjmp loop ; Repeat

.end

Output :

24
25
EXPERIMENT - 9

AIM : To design AC Relay Interfacing using AVR ATmega32 microcontroller.

Components Used : AC Relay, Driver, AC Bulb, AVR ATmega32 Microcontroller IC and


connecting wires.

Code :

#include <xc.h>
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRC = 0xff;
while(1)
{
PORTC=0xff;
_delay_ms(2000);
PORTC=0x00;
_delay_ms(2000);

}
}

Output :

26
STM CIRCUITS
EXPERIMENT - 10

AIM : To Blink LED using STM32 microcontroller.

Components Used : LED, Resistor, STM32F401RE Microcontroller IC and connecting wires.

Code :

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,1);
HAL_Delay(500); // 500ms delay
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,0);
HAL_Delay(500); //

Pinout :

27
Output :

28
EXPERIMENT - 11

AIM : To Blink Multiple LEDs at different ports using STM32 microcontroller.

Components Used : LED, Resistor, STM32F401RE Microcontroller IC and connecting wires.

Code :

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8,1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9,1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6,1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7,1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6,1); // PC9,PB8,,PA5,PA6,pA7,pb6,pc7,pa9
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7,1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9,1);
HAL_Delay(500); // 500ms delay

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8,0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9,0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5,0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6,0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7,0);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6,0);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7,0);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9,0);
HAL_Delay(500); //

Pinout :

29
Output :

30
EXPERIMENT - 12

AIM : To Display Strings on LCD using STM32 microcontroller.

Components Used : LCD, STM32F401RE Microcontroller IC and connecting wires.

Code :

#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"

#define LCD_RS GPIO_PIN_5 // Register Select


#define LCD_EN GPIO_PIN_6 // Enable
#define LCD_D4 GPIO_PIN_7 // Data bit 4
#define LCD_D5 GPIO_PIN_8 // Data bit 5
#define LCD_D6 GPIO_PIN_9 // Data bit 6
#define LCD_D7 GPIO_PIN_10 // Data bit 7

#define LCD_PORT GPIOA // Define LCD GPIO Port

void LCD_Command(uint8_t cmd);


void LCD_Data(uint8_t data);
void LCD_Init(void);
void LCD_String(char *str);
void LCD_Send4Bit(uint8_t data);
void LCD_Enable(void);
void LCD_SetCursor(uint8_t row, uint8_t col);
void delay_ms(uint16_t ms);

HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable GPIOA clock

GPIO_InitTypeDef GPIO_InitStruct = {0};


GPIO_InitStruct.Pin = LCD_RS | LCD_EN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LCD_PORT, &GPIO_InitStruct);

LCD_Init();
LCD_SetCursor(0, 0);
LCD_String("STM32 LC.D");
LCD_SetCursor(1, 0);
LCD_String("Code with Eddie");

31
ER CODE END WHILE */
LCD_SetCursor(0, 0);
LCD_String("Welcome To SGSITS");
LCD_SetCursor(1, 0);
LCD_String("College Indore");
HAL_Delay(4000);
LCD_Clear();
LCD_SetCursor(0, 0);
LCD_String("Department of ");
LCD_SetCursor(1, 0);
LCD_String("E & TC");

HAL_Delay(4000);
LCD_Clear();

/* Function to send command to LCD */


void LCD_Command(uint8_t cmd) {
HAL_GPIO_WritePin(LCD_PORT, LCD_RS, GPIO_PIN_RESET); // RS = 0 (Command
mode)
LCD_Send4Bit(cmd >> 4); // Send upper nibble
LCD_Send4Bit(cmd); // Send lower nibble
}

/* Function to send data to LCD */


void LCD_Data(uint8_t data) {
HAL_GPIO_WritePin(LCD_PORT, LCD_RS, GPIO_PIN_SET); // RS = 1 (Data mode)
LCD_Send4Bit(data >> 4); // Send upper nibble
LCD_Send4Bit(data); // Send lower nibble
}

/* Function to send 4-bit data */


void LCD_Send4Bit(uint8_t data) {
if (data & 0x01)
HAL_GPIO_WritePin(LCD_PORT, LCD_D4, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(LCD_PORT, LCD_D4, GPIO_PIN_RESET);

if (data & 0x02)


HAL_GPIO_WritePin(LCD_PORT, LCD_D5, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(LCD_PORT, LCD_D5, GPIO_PIN_RESET);

if (data & 0x04)

32
HAL_GPIO_WritePin(LCD_PORT, LCD_D6, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(LCD_PORT, LCD_D6, GPIO_PIN_RESET);

if (data & 0x08)


HAL_GPIO_WritePin(LCD_PORT, LCD_D7, GPIO_PIN_SET);
else
HAL_GPIO_WritePin(LCD_PORT, LCD_D7, GPIO_PIN_RESET);

// Enable pulse to latch the data


HAL_GPIO_WritePin(LCD_PORT, LCD_EN, GPIO_PIN_SET);
HAL_Delay(1);
HAL_GPIO_WritePin(LCD_PORT, LCD_EN, GPIO_PIN_RESET);
HAL_Delay(1);
}

/* Enable signal pulse */


void LCD_Enable(void) {
HAL_GPIO_WritePin(LCD_PORT, LCD_EN, GPIO_PIN_SET);
HAL_Delay(1); // Short delay
HAL_GPIO_WritePin(LCD_PORT, LCD_EN, GPIO_PIN_RESET);
HAL_Delay(1);
}

/* LCD Initialization */
void LCD_Init(void) {
HAL_Delay(20); // Wait for LCD to power up
LCD_Command(0x02); // 4-bit mode
LCD_Command(0x28); // 2-line, 5x7 font
LCD_Command(0x0C); // Display ON, Cursor OFF
LCD_Command(0x06); // Auto increment cursor
LCD_Command(0x01); // Clear display
HAL_Delay(2);
}

/* Function to print a string on LCD */


void LCD_String(char *str) {
while (*str) {
LCD_Data(*str++);
}
}

/* Function to set cursor position */


void LCD_SetCursor(uint8_t row, uint8_t col)

33
{
uint8_t pos[] = {0x80, 0xC0}; // First row: 0x80, Second row: 0xC0
LCD_Command(pos[row] + col);
}
/* Function to Clear the Lcd */
void LCD_Clear(void) {
LCD_Command(0x01); // Send command to clear display
HAL_Delay(2); // LCD needs time to process the command
}

Pinout :

34
Output :

35
EXPERIMENT - 13

AIM : To Blink LEDs ON & OFF and display it on LCD using STM32 microcontroller.

Components Used : LEDs, Resistors, LCD, STM32F401RE Microcontroller IC and wires.

Code :

while (1)
{
LCD_SetCursor(0, 0);
LCD_String("LEDs ON");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,1);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,1);
HAL_Delay(1000);
LCD_Clear();

LCD_SetCursor(0, 0);
LCD_String("LEDs OFF");
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0,0);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1,0);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2,0);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3,0);
HAL_Delay(1000);
LCD_Clear();

36
Pinout :

Output :

37
EXPERIMENT - 14

AIM : To Read Temperature from LM35 and Display on LCD using STM32 microcontroller.

Components Used : LM35 IC, LCD, STM32F401RE Microcontroller IC and connecting wires.

Code :

while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
adc_value = HAL_ADC_GetValue(&hadc1)
HAL_ADC_Stop(&hadc1);
voltage = (adc_value*3.3)/4096;
temp = voltage*100;
sprintf(temp_str,"%d\n", temp);
LCD_SetCursor(0, 0);
LCD_String("Temperature: ");
LCD_SetCursor(1, 0);
LCD_String(temp);
LCD_SetCursor(1, 2);
LCD_Command(0xDF);
LCD_SetCursor(1, 3);
LCD_Command('C');
HAL_Delay(1000);
LCD_Command(0x01);
}

// *Read Temperature from LM35 Sensor*


uint16_t Read_Temperature(void) {
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t raw_value = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);

float voltage = (raw_value * 3.3) / 4095.0; // Convert ADC value to voltage


return (uint16_t)(voltage * 100.0); // Convert to °C
}

38
Pinout :

Output :

39
EXPERIMENT - 15

AIM : To Blink LED with the help of Switch using STM32 microcontroller.

Components Used : LED, Resistors, Switch, STM32F401RE Microcontroller IC and connecting


wires.

Code :

while(1){

if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 1){ // if Button pressed


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Turn on LED
}
else{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn off LED
}
}

Pinout :

40
Output :

EXPERIMENT - 16

41
AIM : To Interface DC Motor using STM32 microcontroller.

Components Used : DC Motor, L293D Motor Driver, STM32F401RE Microcontroller IC and


connecting wires.

Code :

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4

while (1)
{
/* USER CODE END WHILE */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET); // IN4
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4
HAL_Delay(1000);

/* USER CODE BEGIN 3 */


}

Pinout :

42
Output :

43
EXPERIMENT - 17

AIM : To Interface Two DC Motors with Two Switches using STM32 microcontroller.

Components Used : DC Motors, L293D Motor Drivers, Switches, STM32F401RE


Microcontroller IC and connecting wires.

Code :

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4

while (1){
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_11) == 1) // if Button pressed
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4
HAL_Delay(100);
}

else{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4
}

if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_12) == 1){


HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET); // IN4
HAL_Delay(100);
}

else{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // IN1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // IN2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET); // IN3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); // IN4}}

44
Pinout :

Output :

45
MSP430 CIRCUITS
EXPERIMENT - 18

AIM : To blink LED using MSP430 microcontroller.

Components Used : LED, Resistor, MSP430 Microcontroller IC and connecting wires.

Code :

void delay(long int x)


{
volatile long int i=0;
for(i=0;i<x;i++)
{
}
return;
}

void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P1DIR=0x00;
while(1)
{
P1OUT=0x00;
delay(25000);
P1OUT=0x01;
delay(25000);
}
}

46
Output :

47
EXPERIMENT - 19

AIM : To blink LED on Positive Edge of Clock using MSP430 microcontroller.

Components Used : LED, Resistor, Oscilloscope, Switch, MSP430 Microcontroller IC and


connecting wires.

Code :

#include <msp430.h>

#define sw BIT3
#define LED BIT7
void main(void)
{
int i;
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P1DIR |= LED;
P1DIR &= ~sw;

while(1)
{
if(!(P1IN & sw))
{
while(!(P1IN & sw));
for(i=0;i<100;i++);
P1OUT ^= LED;
}
}
}

48
Output :

49
EXPERIMENT - 20

AIM : To Simulate Timers using MSP430 microcontroller.

Components Used : LED, Resistor, Oscilloscope, Switch, MSP430 Microcontroller IC and


connecting wires.

Code :

#include <msp430.h>

#define LED BIT7 // Led Pin

#define SW1 BIT3 // 1.5Khz


#define SW2 BIT4 // 3Khz
#define SW3 BIT5 // 12Khz

volatile unsigned int i; // Volatile to prevent removal

void switch_input()
{

if(!(P1IN & SW1)) // If SW1 is Pressed


{
__delay_cycles(2000); // Wait 20ms to debounce
while(!(P1IN & SW1)); // Wait till SW Released
__delay_cycles(2000); // Wait 20ms to debounce

BCSCTL2 &=~ (BIT5 + BIT4); //Reset VLO divider


BCSCTL2 |= (BIT5 + BIT4); //VLO = 12kHz/8 = 1.5kHz
}

if(!(P1IN & SW2)) // If SW is Pressed


{
__delay_cycles(2000); // Wait 20ms to debounce
while(!(P1IN & SW2)); // Wait till SW Released
__delay_cycles(2000); // Wait 20ms to debounce

BCSCTL2 &=~ (BIT5 + BIT4); //Reset VLO divider


BCSCTL2 |= (BIT5); //VLO = 12kHz/4 = 3kHz

50
}

if(!(P1IN & SW3)) // If SW is Pressed


{
__delay_cycles(2000); // Wait 20ms to debounce
while(!(P1IN & SW3)); // Wait till SW Released
__delay_cycles(2000); // Wait 20ms to debounce

BCSCTL2 &=~ (BIT5 + BIT4); //VLO = 12kHz/1 = 12kHz


}

void register_settings_for_GPIO()
{
P1DIR |= LED; //P1.7 is set as Output
P1DIR &=~ (SW1 + SW2 + SW3); //P1.3, P1.4, P1.5 are set as Input
}

void register_settings_for_VLO()
{
BCSCTL3 |= LFXT1S_2; // LFXT1 = VLO
do{
IFG1 &= ~OFIFG; // Clear oscillator fault flag
for (i = 50000; i; i--); // Delay
} while (IFG1 & OFIFG); // Test osc fault flag

BCSCTL2 |= SELM_3; // MCLK = VLO


}

int main(void)
{

WDTCTL = WDTPW | WDTHOLD; //! Stop Watchdog


register_settings_for_VLO(); //Register settings for VLO
register_settings_for_GPIO(); //Register settings for GPIO

while(1)
{
switch_input(); //Switch Input

P1OUT ^= LED; //LED Toggle


for (i = 100; i > 0; i--);

51
}
}
Output :

52

You might also like