LCD1602

–LCD1602.h

#ifndef __LCD1602_h
#define __LCD1602_h

#define uchar  unsigned char

void Lcd1602_Init(void);
void LCD1602_WriteData(uchar);
void LCD1602_WriteCmd(uchar);
void LCD1602_delay(int);
void LCD_ShowString(uchar *str , uchar num ,uchar pos);
int LCD1602_ReadCmd(uchar Cmd);
uchar LCD1602_ReadStatus(void);
#endif

–LCD1602..c

#include <stdio.h>
#include "stm32f10x.h"
#include "LCD1602.h"

void Lcd1602_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD , ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
                  | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC ,&GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
    GPIO_Init(GPIOD ,&GPIO_InitStructure);

    LCD1602_ReadStatus();
    LCD1602_WriteCmd(0x38);
    LCD1602_WriteCmd(0x02);
    LCD1602_WriteCmd(0x06);
    LCD1602_WriteCmd(0x0C);
    LCD1602_WriteCmd(0x1C);
    LCD1602_WriteCmd(0x01);

}

void LCD1602_WriteCmd(uchar Cmd)
{
    GPIO_ResetBits(GPIOD,GPIO_Pin_5);// RS = 0
    GPIO_ResetBits(GPIOD,GPIO_Pin_6); //RW  = 0
    GPIO_SetBits(GPIOD,GPIO_Pin_7);
    GPIO_Write(GPIOC,(GPIO_ReadOutputData(GPIOC) & 0xFF00) | Cmd); //   (GPIO_ReadOutputData(GPIOC) & 0xFF00 | Cmd)
    LCD1602_delay(100);
    GPIO_ResetBits(GPIOD,GPIO_Pin_7);
}

void LCD1602_WriteData(uchar Data)
{
    GPIO_SetBits(GPIOD,GPIO_Pin_5); // RS = 1
    GPIO_ResetBits(GPIOD,GPIO_Pin_6); //RW  = 0
    GPIO_Write(GPIOC,(GPIO_ReadOutputData(GPIOC) & 0xFF00) | Data); 
    GPIO_SetBits(GPIOD,GPIO_Pin_7); //EN = 1
    LCD1602_delay(100);
    GPIO_ResetBits(GPIOD,GPIO_Pin_7); //E = 0
}

uchar LCD1602_ReadStatus(void)
{
    uchar StatusValue = 0;
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
                                                              | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOC ,&GPIO_InitStructure);

    GPIO_ResetBits(GPIOD,GPIO_Pin_5);       // RS = 0
    GPIO_SetBits(GPIOD,GPIO_Pin_6);         // R/W = 1
    GPIO_ResetBits(GPIOD,GPIO_Pin_7);       // E = 0
    GPIO_SetBits(GPIOD,GPIO_Pin_7);         // E = 1
    LCD1602_delay(100);
    StatusValue = GPIO_ReadInputData(GPIOC) & 0x00FF ;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
                                                              | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC ,&GPIO_InitStructure);

    return StatusValue;
}
void LCD1602_delay(int ms)
{
        int i = 0;
        int j = 0;
        for( i = 0 ; i < 10000 ; i++)
        for(j = 0 ; j < ms ; j++);
}

void LCD_ShowString(uchar *str , uchar num ,uchar pos)
{
    if(num == 1)
    {
            LCD1602_WriteCmd((0x80 + 0x00 + pos));
    }else if(num == 2)
    {
            LCD1602_WriteCmd((0x80 + 0x40 + pos));
    }

    while(*str != '\0')
    {
        LCD1602_WriteData(*str++);
    }
}

int LCD1602_ReadCmd(uchar Cmd)
{
    int int_FB;
  uint8_t FB = 0;
    FB = (GPIO_ReadOutputData(GPIOC) & 0x7F) | Cmd; //   (GPIO_ReadOutputData(GPIOC) & 0xFF00 | Cmd)
    LCD1602_delay(100);
    int_FB = FB;
    return int_FB;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值