最近课设要用到LCD1602,就借鉴了一下前辈的代码:
http://www.openedv.com/posts/list/46874.htm
这个我用战舰的ZE没有问题,但改成C8的最小开发板就不行了,只能显示白框。我也看了兼容5V电平的IO口。
这么连的:VSS |VDD| V0 | RS | RW| E |D0 | D1 | D2 | D3| D4| D5 | D6 | D7 | A | K
GND |5V |GND | B15| B14| B13| B10| B11 | B12| A8 | A9 |A10| A11 |A12|3.3V| GND
[mw_shl_code=c,true]#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "led.h"
#include "lcd1602.h"
int main(void)
{
u8 str[] = "ATOM@ALIENTEK";
delay_init();
NVIC_Configuration();
uart_init(9600);
GPIO_Configuration();
LED_Init();
LCD1602_Init();
LCD1602_Show_Str(1, 0, str);
LCD1602_Show_Str(2, 1, "I love STM32");
/*while(1)
{
LED0 = 0;
delay_ms(200);
LED0 = 1;
delay_ms(200);
}*/
}
[/mw_shl_code][mw_shl_code=c,true]#include "lcd1602.h"
#include "delay.h"
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOA,ENABLE);//ê1ÄüPB,PD¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //3õê¼»ˉGPIOD0~7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15|GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //3õê¼»ˉGPIB15,14,13
}
/* μè′yòo¾§×¼±¸oà */
void LCD1602_Wait_Ready(void)
{
u8 sta;
DATAOUT(0xff);
LCD_RS_Clr();
LCD_RW_Set();
do
{
LCD_EN_Set();
delay_ms(5); //Ñóê±5ms£¬·Ç3£ÖØòa
sta = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12);//¶áè¡×′ì¬×Ö
LCD_EN_Clr();
}while(sta & 0x80);//bit7μèóú1±íê¾òo¾§ÕyÃ|£¬Öظ′¼ì2aÖ±μ½Æäμèóú0ÎaÖ1
}
/* ÏòLCD1602òo¾§D′èëò»×Ö½úÃüácmd-′yD′èëÃüáîÖμ */
void LCD1602_Write_Cmd(u8 cmd)
{
LCD1602_Wait_Ready();
LCD_RS_Clr();
LCD_RW_Clr();
DATAOUT(cmd);
LCD_EN_Set();
LCD_EN_Clr();
}
/* ÏòLCD1602òo¾§D′èëò»×Ö½úêy¾Y£¬dat-′yD′èëêy¾YÖμ */
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);
}
/* éèÖÃÏÔê¾RAMÆeê¼μØÖ·£¬òà¼′1a±êλÖã¬(x,y)-¶Ôó|ÆáÄ»éÏμÄ×Ö·û×ø±ê */
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);
}
/* Ôúòo¾§éÏÏÔê¾×Ö·û′®£¬(x,y)-¶Ôó|ÆáÄ»éÏμÄÆeê¼×ø±ê£¬str-×Ö·û′®Ö¸Õë */
void LCD1602_Show_Str(u8 x, u8 y, u8 *str)
{
LCD1602_Set_Cursor(x, y);
while(*str != ' ')
{
LCD1602_Write_Dat(*str++);
}
}
/* 3õê¼»ˉ1602òo¾§ */
void LCD1602_Init(void)
{
LCD1602_Write_Cmd(0x38); //16*2ÏÔê¾£¬5*7μãÕó£¬8λêy¾Y¿ú
LCD1602_Write_Cmd(0x0c); //¿aÏÔê¾£¬1a±ê1رÕ
LCD1602_Write_Cmd(0x06); //ÎÄ×Ö2»¶ˉ£¬μØÖ·×Ô¶ˉ+1
LCD1602_Write_Cmd(0x01); //ÇåÆá
}
[/mw_shl_code][mw_shl_code=c,true]#include "led.h"
//3õê¼»ˉPB5oíPE5Îaêä3ö¿ú.2¢ê1ÄüÕaὸö¿úμÄê±Öó
//LED IO3õê¼»ˉ
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE); //ê1ÄüPB,PE¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //LED0-->PB.5 ¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿úËù¶èÎa50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //¸ù¾Yé趨2Îêy3õê¼»ˉGPIOB.5
GPIO_SetBits(GPIOB,GPIO_Pin_6); //PB.5 êä3ö¸ß
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //LED1-->PE.5 ¶Ë¿úÅäÖÃ, íÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure); //íÆíìêä3ö £¬IO¿úËù¶èÎa50MHz
GPIO_SetBits(GPIOB,GPIO_Pin_9); //PE.5 êä3ö¸ß
}
[/mw_shl_code]
一周热门 更多>