0% found this document useful (0 votes)
8 views

STM

The document contains code for an STM32 microcontroller to control a 4-bit LCD display, specifically to scroll the text 'Happy Diwali'. It includes functions for initializing the LCD, sending commands and data, and displaying strings. The main loop continuously scrolls the text across the display with a delay for visibility.

Uploaded by

Maathu
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)
8 views

STM

The document contains code for an STM32 microcontroller to control a 4-bit LCD display, specifically to scroll the text 'Happy Diwali'. It includes functions for initializing the LCD, sending commands and data, and displaying strings. The main loop continuously scrolls the text across the display with a delay for visibility.

Uploaded by

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

STM-32 (SCROLLING led 4BIT)

#include "stm32f10x.h"

#include "delayy_header.h" // Custom delay header

// Define LCD control pins (RS, E) and data pins (D4, D5, D6, D7)

#define LCD_RS_PIN GPIO_Pin_0 // RS Pin (Pin 0 for RS)

#define LCD_E_PIN GPIO_Pin_1 // E Pin (Pin 1 for E)

#define LCD_D4_PIN GPIO_Pin_2 // D4 Pin (Pin 2 for D4)

#define LCD_D5_PIN GPIO_Pin_3 // D5 Pin (Pin 3 for D5)

#define LCD_D6_PIN GPIO_Pin_4 // D6 Pin (Pin 4 for D6)

#define LCD_D7_PIN GPIO_Pin_5 // D7 Pin (Pin 5 for D7)

#define LCD_GPIO_PORT GPIOA // GPIO Port for controlling LCD (GPIOA)

void LCD_INIT(void);

void LCD_COMMAND(unsigned char cmd);

void LCD_DATA(unsigned char data);

void LCD_STR(unsigned char *str);

int main(void)

unsigned char *p = "Happy Diwali"; // String to display

int len = strlen(p); // Length of string

LCD_INIT();

while (1)

for (int start_pos = 0; start_pos < len + 16; start_pos++) // Loop to scroll text

LCD_COMMAND(0x80); // Set cursor to the first position (start of the screen)

// Print the string starting from the current position


LCD_STR(p + start_pos);

delay_millisec(500); // Small delay for scrolling effect

if (start_pos == len) // If we reach the end of the string, reset to the beginning

start_pos = -1; // This will make it start scrolling from the beginning

LCD_COMMAND(0x01); // Clear the display to start fresh for next scroll

void LCD_INIT(void)

// Enable GPIOA Clock (for STM32F103, GPIOA is used)

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

// Configure GPIO pins for LCD (RS, E, D4 to D7)

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN | LCD_E_PIN | LCD_D4_PIN | LCD_D5_PIN | LCD_D6_PIN | LCD_D7_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push-pull mode

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(LCD_GPIO_PORT, &GPIO_InitStructure);

// Initialize LCD in 4-bit mode

LCD_COMMAND(0x02); // Set 4-bit mode

LCD_COMMAND(0x28); // 4-bit mode, 2 lines, 5x7 font

LCD_COMMAND(0x0C); // Display ON, Cursor OFF

LCD_COMMAND(0x06); // Increment cursor, no shift

LCD_COMMAND(0x01); // Clear display

delay_millisec(2); // Wait for clear command to finish

}
void LCD_COMMAND(unsigned char cmd)

GPIO_ResetBits(LCD_GPIO_PORT, LCD_RS_PIN); // RS = 0 for command mode

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // E = 0 (Start with E low)

// Send high nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (cmd >> 4) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (cmd >> 5) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (cmd >> 6) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (cmd >> 7) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

// Send low nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (cmd & 0x01));

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (cmd >> 1) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (cmd >> 2) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (cmd >> 3) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

delay_millisec(2); // Delay after sending command

void LCD_DATA(unsigned char data)

GPIO_SetBits(LCD_GPIO_PORT, LCD_RS_PIN); // RS = 1 for data mode

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // E = 0 (Start with E low)

// Send high nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (data >> 4) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (data >> 5) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (data >> 6) & 0x01);


GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (data >> 7) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

// Send low nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (data & 0x01));

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (data >> 1) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (data >> 2) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (data >> 3) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

delay_millisec(2); // Delay after sending data

void LCD_STR(unsigned char *str)

while (*str)

LCD_DATA(*str); // Send each character to the LCD

str++;

FOR DISPLAY PROGRAM (4 BIT)


#include "stm32f10x.h"

#include "delayy_header.h" // Custom delay header

// Define LCD control pins (RS, E) and data pins (D4, D5, D6, D7)

#define LCD_RS_PIN GPIO_Pin_0 // RS Pin (Pin 0 for RS)

#define LCD_E_PIN GPIO_Pin_1 // E Pin (Pin 1 for E)

#define LCD_D4_PIN GPIO_Pin_2 // D4 Pin (Pin 2 for D4)


#define LCD_D5_PIN GPIO_Pin_3 // D5 Pin (Pin 3 for D5)

#define LCD_D6_PIN GPIO_Pin_4 // D6 Pin (Pin 4 for D6)

#define LCD_D7_PIN GPIO_Pin_5 // D7 Pin (Pin 5 for D7)

#define LCD_GPIO_PORT GPIOA // GPIO Port for controlling LCD (GPIOA)

void LCD_INIT(void);

void LCD_COMMAND(unsigned char cmd);

void LCD_DATA(unsigned char data);

void LCD_STR(unsigned char *str);

int main(void)

unsigned char *p = "Happy Diwali"; // String to display

int len = strlen(p); // Length of string

int count, len1;

LCD_INIT();

len1 = len;

while (1)

count = 0;

for (unsigned char cmd = 0x80; cmd <= 0x8F; cmd++) // Loop through LCD positions

LCD_COMMAND(cmd);

LCD_STR(p); // Print the string

delay_millisec(500); // Small delay for visibility

LCD_COMMAND(0x01); // Clear LCD

count++;

if (count > (16 - len1)) // If the string length is larger than 16 characters, shift

LCD_COMMAND(0x80); // Move cursor back to the start

len1--;
LCD_STR(p + len1); // Shift the string

void LCD_INIT(void)

// Enable GPIOA Clock (for STM32F103, GPIOA is used)

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

// Configure GPIO pins for LCD (RS, E, D4 to D7)

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN | LCD_E_PIN | LCD_D4_PIN | LCD_D5_PIN | LCD_D6_PIN | LCD_D7_PIN;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Push-pull mode

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(LCD_GPIO_PORT, &GPIO_InitStructure);

// Initialize LCD in 4-bit mode

LCD_COMMAND(0x02); // Set 4-bit mode

LCD_COMMAND(0x28); // 4-bit mode, 2 lines, 5x7 font

LCD_COMMAND(0x0C); // Display ON, Cursor OFF

LCD_COMMAND(0x06); // Increment cursor, no shift

LCD_COMMAND(0x01); // Clear display

delay_millisec(2); // Wait for clear command to finish

void LCD_COMMAND(unsigned char cmd)

GPIO_ResetBits(LCD_GPIO_PORT, LCD_RS_PIN); // RS = 0 for command mode

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // E = 0 (Start with E low)

// Send high nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (cmd >> 4) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (cmd >> 5) & 0x01);


GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (cmd >> 6) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (cmd >> 7) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

// Send low nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (cmd & 0x01));

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (cmd >> 1) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (cmd >> 2) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (cmd >> 3) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

delay_millisec(2); // Delay after sending command

void LCD_DATA(unsigned char data)

GPIO_SetBits(LCD_GPIO_PORT, LCD_RS_PIN); // RS = 1 for data mode

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // E = 0 (Start with E low)

// Send high nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (data >> 4) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (data >> 5) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (data >> 6) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (data >> 7) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

// Send low nibble

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D4_PIN, (data & 0x01));

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D5_PIN, (data >> 1) & 0x01);


GPIO_WriteBit(LCD_GPIO_PORT, LCD_D6_PIN, (data >> 2) & 0x01);

GPIO_WriteBit(LCD_GPIO_PORT, LCD_D7_PIN, (data >> 3) & 0x01);

GPIO_SetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E high

GPIO_ResetBits(LCD_GPIO_PORT, LCD_E_PIN); // Pulse E low

delay_millisec(2); // Delay after sending data

void LCD_STR(unsigned char *str)

while (*str)

LCD_DATA(*str); // Send each character to the LCD

str++;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy