1、液晶买3.3V电源的,液晶的第三脚对比度要用103的电位器,这样可以调节对比度,不然看不到的
RS:PB5 RW:PB6; EN:PB7 DB0~DB7接PA0~PA7; VO拉电位器,1脚VSS接GND,2脚VDD接3.3V,15脚接3.3V,16脚接GND.
2、 "sys.h"文件
#ifndef __SYS_H
#define __SYS_H
#include "stm32f10x.h"
#define SYSTEM_SUPPORT_UCOS 0
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n)
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n)
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n)
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n)
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n)
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n)
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n)
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n)
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n)
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n)
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n)
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n)
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n)
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n)
void NVIC_Configuration(void);
2、 "sys.c"文件
#include "sys.h"
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
}
3、1602.h文件
#ifndef __LCD1602_H
#define __LCD1602_H
#include "sys.h"
#define LCD_RS_Set() GPIO_SetBits(GPIOB, GPIO_Pin_5)
#define LCD_RS_Clr() GPIO_ResetBits(GPIOB, GPIO_Pin_5)
#define LCD_RW_Set() GPIO_SetBits(GPIOB, GPIO_Pin_6)
#define LCD_RW_Clr() GPIO_ResetBits(GPIOB, GPIO_Pin_6)
#define LCD_EN_Set() GPIO_SetBits(GPIOB, GPIO_Pin_7)
#define LCD_EN_Clr() GPIO_ResetBits(GPIOB, GPIO_Pin_7)
#define DATAOUT(x) GPIO_Write(GPIOA, x)
void GPIO_Configuration(void);
void LCD1602_Wait_Ready(void);
void LCD1602_Write_Cmd(u8 cmd);
void LCD1602_Write_Dat(u8 dat);
void LCD1602_ClearScreen(void);
void LCD1602_Set_Cursor(u8 x, u8 y);
void LCD1602_Show_Str(u8 x, u8 y, u8 *str);
void LCD1602_Init(void);
#endif
4、1602.c文件
#include"1602.h"
#include"sys.h"
#include "delay.h"
#include "stdio.h"
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = 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(GPIOB, &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_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void LCD1602_Wait_Ready(void)
{
u8 sta;
DATAOUT(0xff);
LCD_RS_Clr();
LCD_RW_Set();
do
{
LCD_EN_Set();
delay_ms(5);
sta = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7);
LCD_EN_Clr();
}while(sta & 0x80);
}
void LCD1602_Write_Cmd(u8 cmd)
{
LCD1602_Wait_Ready();
LCD_RS_Clr();
LCD_RW_Clr();
DATAOUT(cmd);
LCD_EN_Set();
LCD_EN_Clr();
//printf("%d",cmd);
}
void LCD1602_Write_Dat(u8 dat)
{
LCD1602_Wait_Ready();
LCD_RS_Set();
LCD_RW_Clr();
DATAOUT(dat);
LCD_EN_Set();
LCD_EN_Clr();
}
void LCD1602_ClearScreen(void)
{
LCD1602_Write_Cmd(0x01);
}
void LCD1602_Set_Cursor(u8 x, u8 y)
{
u8 addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LCD1602_Write_Cmd(addr | 0x80);
}
void LCD1602_Show_Str(u8 x, u8 y, u8 *str)
{
LCD1602_Set_Cursor(x, y);
while(*str != '\0')
{
LCD1602_Write_Dat(*str++);
}
}
void LCD1602_Init(void)
{
GPIO_Configuration();
LCD1602_Write_Cmd(0x38); //16*2ÏÔʾ£¬5*7µãÕó£¬8λÊý¾Ý¿Ú
LCD1602_Write_Cmd(0x0c); //¿ªÏÔʾ£¬¹â±ê¹Ø±Õ
LCD1602_Write_Cmd(0x06); //ÎÄ×Ö²»¶¯£¬µØÖ·×Ô¶¯+1
LCD1602_Write_Cmd(0x01); //ÇåÆÁ
}
void delay_us(u16 us)
{
u16 i;
do
{
i = 6;
while(i--)__nop();
} while (--us);
}
void delay_ms(u16 ms)
{
do
{
delay_us(250);
delay_us(250);
delay_us(250);
delay_us(250);
} while (--ms);
}