新手上路
- 积分
- 32
- 金钱
- 32
- 注册时间
- 2020-3-15
- 在线时间
- 11 小时
|
4金钱
按照网上的例子自己改了连接的管脚,但是却不能显示。下面是代码。
这是LCD.h的头文件
#ifndef _BSP_LCD1602_H
#define _BSP_LCD1602_H
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "bsp_SysTick.h"
#define LCD1602_CLK RCC_APB2Periph_GPIOA
#define LCD1602_GPIO_PORT GPIOA
#define LCD1602_E GPIO_Pin_11 //定义使能引脚
#define LCD1602_RW GPIO_Pin_12 //定义读写引脚
#define LCD1602_RS GPIO_Pin_15 //定义数据、命名引脚
#define EO(X) X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_E)) GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_E))
#define RWO(X) X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_RW)) GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_RW))
#define RSO(X) X? (GPIO_SetBits(LCD1602_GPIO_PORT,LCD1602_RS)) GPIO_ResetBits(LCD1602_GPIO_PORT,LCD1602_RS))
//只能是某个GPIO口的低八位
#define DB0 GPIO_Pin_3
#define DB1 GPIO_Pin_4
#define DB2 GPIO_Pin_5
#define DB3 GPIO_Pin_6
#define DB4 GPIO_Pin_7
#define DB5 GPIO_Pin_9
#define DB6 GPIO_Pin_9
#define DB7 GPIO_Pin_10
void LCD1602_Init(void); //初始化LCD602;
void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str,uint8_t len);
void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num);
void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat);
#endif //_BSP_LCD1602_H
这是led.c的文件
#include "bsp-lcd1602.h"
void LCD1602_GPIO_Config(void)
{
GPIO_InitTypeDef LCD1602_GPIOStruct;
RCC_APB2PeriphClockCmd(LCD1602_CLK, ENABLE);
//定义gpio口
LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_PP;
LCD1602_GPIOStruct.GPIO_Speed = GPIO_Speed_10MHz;
LCD1602_GPIOStruct.GPIO_Pin = LCD1602_E | LCD1602_RS | LCD1602_RW ;
GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
LCD1602_GPIOStruct.GPIO_Mode = GPIO_Mode_Out_OD;
LCD1602_GPIOStruct.GPIO_Pin = DB0 | DB1 | DB2 |DB3 | DB4 | DB5|
DB6 | DB7 ; //设置为开漏输出
GPIO_Init(LCD1602_GPIO_PORT,&LCD1602_GPIOStruct);
}
void LCD1602_WaitReady(void) //检测忙状态
{
uint8_t sta;
GPIOA->ODR =0x00FF;
RSO(0); //低电平,选择命令
RWO(1); //高电平,从lcd读取
EO(1); //使能
SysTick_Delay_Us(1);
do{
sta=GPIO_ReadInputDataBit(LCD1602_GPIO_PORT,GPIO_Pin_10); //读取状态字
EO(0);
}while(sta);
}
/* 向LCD1602液晶写入一字节命令,cmd-待写入命令值 */
void LCD1602_WriteCmd(uint8_t cmd) //写指令
{
LCD1602_WaitReady();
RSO(0);
RWO(0);
EO(0);
SysTick_Delay_Us(1);
EO(1);
LCD1602_GPIO_PORT->ODR &= (cmd|0xFF00);
EO(0);
SysTick_Delay_Us(400);
}
/* 向LCD1602液晶写入一字节数据,dat-待写入数据值 */
void LCD1602_WriteDat(uint8_t dat) //写数据
{
LCD1602_WaitReady();
RSO(1);
RWO(0);
SysTick_Delay_Us(30);
EO(1);
LCD1602_GPIO_PORT->ODR &=(dat|0xFF00);
EO(0);
SysTick_Delay_Us(400);
}
/* 设置显示RAM起始地址,亦即光标位置,(x,y)-对应屏幕上的字符坐标 */
void LCD1602_SetCursor(uint8_t x, uint8_t y)
{
uint8_t addr;
if (y == 0) //由输入的屏幕坐标计算显示RAM的地址
addr = 0x00 + x; //第一行字符地址从0x00起始
else
addr = 0x40 + x; //第二行字符地址从0x40起始一个字符等于64个字节
LCD1602_WriteCmd(addr|0x80); //设置RAM地址
}
/* 在液晶上显示字符串,(x,y)-对应屏幕上的起始坐标,str-字符串指针 */
void LCD1602_ShowStr(uint8_t x, uint8_t y, uint8_t *str, uint8_t len)
{
LCD1602_SetCursor(x, y); //设置起始地址
while (len--) //连续写入len个字符数据
{
LCD1602_WriteDat(*str++);
}
}
//??1???
//x,y :????
//num:??(0~99)
//-----------------------------*/
void LCD_ShowNum(uint8_t x, uint8_t y,uint8_t num)
{
LCD1602_SetCursor(x, y); //设置起始地址
LCD_ShowChar(x,y,num+'0');
}
void LCD_ShowChar(uint8_t x, uint8_t y,uint8_t dat)
{
LCD1602_SetCursor(x, y); //设置起始地址
LCD1602_WriteDat(dat);
}
void LCD1602_Init(void)
{
LCD1602_GPIO_Config(); //开启GPIO口
LCD1602_WriteCmd(0X38); //16*2显示,5*7点阵,8位数据接口
LCD1602_WriteCmd(0x0C); //显示器开,光标关闭
LCD1602_WriteCmd(0x06); //文字不动,地址自动+1
LCD1602_WriteCmd(0x01); //清屏
}
|
|