Tuesday, June 2, 2020

MM32 microcontroller with LCD 16x2 display

#include "MM32F103.h"
#include<stdio.h>
#include "sys.h"
#include "delay.h"
#include "uart.h"


/* LCD Commands */
#define LCD_DD_RAM_PTR 0x80 // Address Display Data RAM pointer
#define LCD_CG_RAM_PTR 0x40 // Address Character Generator RAM pointer
#define LCD_CLEAR_DISPLAY   0x01 // Clear entire display and set Display Data Address to 0
#define LCD_RETRN_HOME 0x02 // sets DDRAM address 0 and returns display from being shifted to original position.
#define LCD_DISP_INIT 0x28 // function set is 4 bit data length and 2 lines
#define LCD_INC_MODE   0x06 // Entry mode is display Data RAM pointer incremented after write

#define LCD_DISP_OFF   0x08    // Sets entire display off, cursor off
#define LCD_CURSOR_ON   0x04 // turn on cursor
#define LCD_CURSOR_OFF     0x00    // turn off cursor
#define LCD_CUR_MOV_LEFT   0x10 // Cursor move and shift to left
#define LCD_CUR_MOV_RIGHT   0x14 // Cursor move and shift to right
#define LCD_BUSY              0x80    // LCD is busy
#define LCD_CURSOR_BLINKING   0x0D //LCD cursor blinking
#define LCD_DISP_ON   0x0E   //lcd cursor ON 


/*************************LCD Control pins***********************************************/

#define Lcd_En_High()     GPIO_SetBits(GPIOB, GPIO_Pin_15)
#define Lcd_En_Low()      GPIO_ResetBits(GPIOB, GPIO_Pin_15) 

#define Lcd_Rs_High()    GPIO_SetBits(GPIOB, GPIO_Pin_14) 
#define Lcd_Rs_Low()     GPIO_ResetBits(GPIOB, GPIO_Pin_14) 

/************************** LCD Data pins *************************************************/

#define Lcd_D7_High()     GPIO_SetBits(GPIOB, GPIO_Pin_5)   
#define Lcd_D7_Low()     GPIO_ResetBits(GPIOB, GPIO_Pin_5)    

#define Lcd_D6_High()     GPIO_SetBits(GPIOB, GPIO_Pin_4)     
#define Lcd_D6_Low()     GPIO_ResetBits(GPIOB, GPIO_Pin_4)   

#define Lcd_D5_High()     GPIO_SetBits(GPIOB, GPIO_Pin_11)        
#define Lcd_D5_Low()     GPIO_ResetBits(GPIOB, GPIO_Pin_11)         

#define Lcd_D4_High()     GPIO_SetBits(GPIOB, GPIO_Pin_10)                 
#define Lcd_D4_Low()      GPIO_ResetBits(GPIOB, GPIO_Pin_10)  
/****************************************************************************************/


void Delay_stm(u32);
void LCD_Control_Write(unsigned char cmd);


void GPIO_Configuration_LCD(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); 
    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_5 | GPIO_Pin_4 | GPIO_Pin_11 | GPIO_Pin_10| GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void Lcd_Delay(u32 nCount)
 
 for(; nCount != 0; nCount--);

}

void LCD_initialization(void)
{
/* Set 4-bits interface */
printf("\r\n LCD init done");
LCD_Control_Write(0x33);
Delay_stm(1);
LCD_Control_Write(0x32);
Delay_stm(1);

LCD_Control_Write(LCD_DISP_INIT);
Delay_stm(1); 
LCD_Control_Write(LCD_CLEAR_DISPLAY); 
Delay_stm(1);
LCD_Control_Write(LCD_INC_MODE); 
Delay_stm(1);
LCD_Control_Write(LCD_DISP_ON); 
Delay_stm(1);
LCD_Control_Write(LCD_CURSOR_BLINKING); 
Delay_stm(1);
LCD_Control_Write(LCD_CUR_MOV_RIGHT);
Delay_stm(1);
LCD_Control_Write(LCD_RETRN_HOME);  
Delay_stm(1);
}

void LCD_Clear(void)
{
printf("\r\n LCD clr fun");
LCD_Control_Write(LCD_CLEAR_DISPLAY);
  Delay_stm(5);
}

void LCD_Control_Write(unsigned char cmd)
{
unsigned char temp;
temp = cmd;
temp = temp & 0xf0;
if (temp & 0x80)
  Lcd_D7_High();        
  else
  Lcd_D7_Low();         
temp = temp << 1;

if (temp & 0x80)
  Lcd_D6_High();         
  else
  Lcd_D6_Low();           
temp = temp << 1;

if (temp & 0x80)
  Lcd_D5_High();       
  else
  Lcd_D5_Low();         
    temp = temp << 1;

if (temp & 0x80)
  Lcd_D4_High();       
  else
  Lcd_D4_Low();
temp = temp << 1;

Lcd_Rs_Low();  
Lcd_En_High();
  delay_ms(1);
Lcd_En_Low();
  Delay_stm(200);


cmd = cmd << 4;
cmd = cmd & 0xF0;
if (cmd & 0x80)
Lcd_D7_High();         
  else
Lcd_D7_Low();         
cmd = cmd << 1;

if (cmd & 0x80)
  Lcd_D6_High();       
  else
   Lcd_D6_Low();         
cmd = cmd << 1;

if (cmd & 0x80)
  Lcd_D5_High();       
  else
  Lcd_D5_Low();         
cmd = cmd << 1;

if (cmd & 0x80)
  Lcd_D4_High();         
  else
  Lcd_D4_Low();         
  cmd = cmd << 1;

Lcd_Rs_Low();  
Lcd_En_High();
delay_ms(1);
Lcd_En_Low();
  //Delay_stm(1);
}

void LCD_Data_Write(unsigned char data)
{
  
  unsigned char temp;
printf("\r\n IN LCD data write fun");
temp = data;
temp = temp & 0xf0;
//printf("hai_in _write");
if (temp & 0x80)
Lcd_D7_High();         
  else
Lcd_D7_Low();         
temp = temp << 1;

if (temp & 0x80)
  Lcd_D6_High();       
  else
   Lcd_D6_Low();         
temp = temp << 1;

if (temp & 0x80)
  Lcd_D5_High();       
  else
  Lcd_D5_Low();         
temp = temp << 1;

if (temp & 0x80)
  Lcd_D4_High();         
  else
  Lcd_D4_Low();         
temp = temp << 1;

Lcd_Rs_High();  
Lcd_En_High();
delay_ms(1);
Lcd_En_Low();
  Lcd_Delay(2000);


data = data << 4;
data = data & 0xF0;

if (data & 0x80)
Lcd_D7_High();         
  else
Lcd_D7_Low();         
data = data << 1;

if (data & 0x80)
  Lcd_D6_High();       
  else
  Lcd_D6_Low();         
data = data << 1;

if (data & 0x80)
  Lcd_D5_High();       
  else
  Lcd_D5_Low();         
data = data << 1;

if (data & 0x80)
  Lcd_D4_High();        
  else
  Lcd_D4_Low();         
data = data << 1;
Lcd_Rs_High();  
Lcd_En_High();
delay_ms(1);
Lcd_En_Low();
}

void RowDisplay(const char *Ptr,char line1)
{
   int i = 0;
printf("\r\n IN Row display fun");
   if(line1==1)LCD_Control_Write(0x80);  //Address of first line first digit
   if(line1==2)LCD_Control_Write(0xC0);  //Address of secondline first digit
   Lcd_Delay(50000);
   for(i=0;i<16;i++)                  //for 16x2 lcd
   {
      LCD_Data_Write(Ptr[i]);
      Lcd_Delay(2000);    
   } 
}

void Delay_stm(u32 num)
{
delay_ms(num);
}

int main(void)
{
 
GPIO_Configuration_LCD();
uart_initwBaudRate(115200);
delay_init();
 
LCD_initialization();
   LCD_Clear();
   
RowDisplay("M",1);
delay_ms(20);
RowDisplay("raju",2);
delay_ms(20);

while(1)
{
LCD_Clear();
delay_ms(1000);
RowDisplay("HELLO  WORLD",2);
delay_ms(1000);
}
}

python class topic video