STM32F4_LCD12864并行驱动
生活随笔
收集整理的這篇文章主要介紹了
STM32F4_LCD12864并行驱动
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
頭文件:
#ifndef __12864LCD_H__
#define __12864LCD_H__#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "sys.h"
#include "delay.h"
#include <stdlib.h> /***控制端口GPIO定義***/
#define LCD_RST GPIO_Pin_0
#define LCD_PSB GPIO_Pin_1
#define LCD_EN GPIO_Pin_8
#define LCD_RW GPIO_Pin_9
#define LCD_RS GPIO_Pin_10//定義控制端口 置1
#define Set_LCD_RS GPIO_SetBits(GPIOD,LCD_RS)
#define Set_LCD_RW GPIO_SetBits(GPIOD,LCD_RW)
#define Set_LCD_EN GPIO_SetBits(GPIOD,LCD_EN)
#define Set_LCD_PSB GPIO_SetBits(GPIOD,LCD_PSB)
#define Set_LCD_RST GPIO_SetBits(GPIOD,LCD_RST)//定義控制端口 清0
#define Clear_LCD_RS GPIO_ResetBits(GPIOD,LCD_RS)
#define Clear_LCD_RW GPIO_ResetBits(GPIOD,LCD_RW)
#define Clear_LCD_EN GPIO_ResetBits(GPIOD,LCD_EN)
#define Clear_LCD_PSB GPIO_ResetBits(GPIOD,LCD_PSB)
#define Clear_LCD_RST GPIO_ResetBits(GPIOD,LCD_RST)/********數據端口GPIO定義*********/
#define LCD_DB0 GPIO_Pin_0
#define LCD_DB1 GPIO_Pin_1
#define LCD_DB2 GPIO_Pin_2
#define LCD_DB3 GPIO_Pin_3
#define LCD_DB4 GPIO_Pin_4
#define LCD_DB5 GPIO_Pin_5
#define LCD_DB6 GPIO_Pin_6
#define LCD_DB7 GPIO_Pin_7#define LCD_CONTROL_PORT GPIOD
#define LCD_DATA_PORT GPIOE//GPIO時鐘
#define LCD_CONTROL_PORT_CLOCK RCC_AHB1Periph_GPIOD
#define LCD_DATA_PORT_CLOCK RCC_AHB1Periph_GPIOE#define LCD_CONTROL_PIN LCD_RST | LCD_PSB| LCD_EN | LCD_RW | LCD_RS
#define LCD_DATA_PIN LCD_DB0 | LCD_DB1|LCD_DB2 | LCD_DB3 | LCD_DB4 |LCD_DB5 | LCD_DB6 | LCD_DB7void _NOP_(void);
void LCD12864_PORT_Init(void);
void LCD12864_WriteCmd(u8 cmd); //寫命令函數
void LCD12864_WriteData(u8 data); //寫數據函數
void LCD12864_Init(void); //初始化 12864
void Locate16(u8 X, u8 Y);
void DisPlayString(u8 X,u8 Y,u8 *str); //顯示字符串的函數
void LCD12864_Clear(void);
void LCD_ShiftLeft(void);
void LCD_ShiftRight(void);#endif
以下是驅動.c文件:
#include "12864LCD.h"void LCD12864_PORT_Init(void)
{GPIO_InitTypeDef GPIO_PortInit;/************配置控制IO端口**********/RCC_AHB1PeriphClockCmd(LCD_CONTROL_PORT_CLOCK, ENABLE);//使能GPIOD時鐘GPIO_PortInit.GPIO_Pin = LCD_CONTROL_PIN;//控制IO端口GPIO_PortInit.GPIO_Mode = GPIO_Mode_OUT;//普通輸出模式GPIO_PortInit.GPIO_OType = GPIO_OType_PP;//推挽輸出GPIO_PortInit.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_PortInit.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(LCD_CONTROL_PORT, &GPIO_PortInit);//初始化控制端口GPIO/************配置數據端口*************/RCC_AHB1PeriphClockCmd(LCD_DATA_PORT_CLOCK, ENABLE);//使能GPIOE時鐘GPIO_PortInit.GPIO_Pin = LCD_DATA_PIN;//GPIO_PortInit.GPIO_Mode = GPIO_Mode_OUT;//普通輸出模式GPIO_PortInit.GPIO_OType = GPIO_OType_PP;//推挽輸出GPIO_PortInit.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_PortInit.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(LCD_DATA_PORT, &GPIO_PortInit);//始化數據端口GPIO
}//稍作延時
void _NOP_(void)
{u8 i = 200;while(i > 0){i--;}
}/*******************************************************************/
/*寫指令數據到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/*******************************************************************/
void LCD12864_WriteCmd(u8 cmd) //寫命令函數
{Clear_LCD_RS; //RS = 0.Clear_LCD_RW; //RW = 0. Set_LCD_EN; //EN = 1._NOP_();_NOP_();LCD_DATA_PORT->ODR=((LCD_DATA_PORT->ODR & 0xff00)|cmd); //此處,// 只有直接操作寄存器才能//達到,只改變輸出數據寄存器ODR的低8位,其它位//不變的目的。因為,只有低8位是數據引腳,//其它位可能是控制引腳,不能改變。_NOP_();_NOP_();Clear_LCD_EN; //EN = 0;_NOP_(); _NOP_();
}
/*******************************************************************/
/*寫顯示數據到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數據。 */
/*******************************************************************/
void LCD12864_WriteData(u8 data) //寫數據函數
{Set_LCD_RS; //RS = 1. Clear_LCD_RW; //RW = 0. Set_LCD_EN; //EN = 1._NOP_();_NOP_();LCD_DATA_PORT->ODR=((LCD_DATA_PORT->ODR & 0xff00)|data); _NOP_();_NOP_();Clear_LCD_EN; //EN = 0;_NOP_(); _NOP_();
}
/*******************************************************************/
/* LCD12864初始化 */
/*******************************************************************/
void LCD12864_Init(void) //初始化 12864
{ Clear_LCD_RST;delay_ms(2);Set_LCD_RST;delay_ms(2);Set_LCD_PSB; //令PSB=1,設置為并行數據模式delay_ms(2); LCD12864_WriteCmd(0x30); //選擇基本指令集,和,8位數據模式delay_ms(2);LCD12864_WriteCmd(0x0c); //開顯示,無游標,不反白.delay_ms(2); LCD12864_WriteCmd(0x01); //清除顯示,并將 DDRAM 的地址計數器 AC 設為 00H.delay_ms(2);LCD12864_WriteCmd(0x06); //設置,外部讀寫數據后,地址記數器 AC 會自動加 1delay_ms(2);LCD12864_WriteCmd(0x80); //將 DDRAM 地址計數器 AC 設為 0.delay_ms(2);
}
/********************************************************************************/
/* 設定顯示位置,以下是光標定位函數,第一個參數為行坐標, */
/* 第二個為列坐標,起始坐標是1行1列。只能以16個點的寬度為單位移動 */
/********************************************************************************/
void Position(u8 X, u8 Y)
{switch(X){case 1: LCD12864_WriteCmd(0x80+Y); break;case 2: LCD12864_WriteCmd(0x90+Y); break;case 3: LCD12864_WriteCmd(0x88+Y); break;case 4: LCD12864_WriteCmd(0x98+Y); break;}
}
/*******************************************************************/
/* LCD12864顯示字符串 */
/*******************************************************************/
void DisPlayString(u8 X,u8 Y,u8 *str) //顯示字符串的函數
{if(X>4){X = 1;}if(Y>4){Y = 0;}Position(X,Y) ; while(*str != '\0'){LCD12864_WriteData(*str);str++;delay_ms(5);}
}//清屏,清除LCD12864顯示
void LCD12864_Clear(void)
{LCD12864_WriteCmd(0x01);
}//屏幕整體左移一格,用于LCD12864來做滾動顯示
void LCD_ShiftLeft(void)
{LCD12864_WriteCmd(0x18);
}//屏幕整體右移一格,用于LCD12864來做滾動顯示
void LCD_ShiftRight(void)
{LCD12864_WriteCmd(0x1C);
}
總結
以上是生活随笔為你收集整理的STM32F4_LCD12864并行驱动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Java模拟网卡、声卡继承PIC接口,
- 下一篇: IAR J-Link下载程序出现错误提