jiangqingqiao的个人空间 https://bbs.21ic.com/?1074616 [收藏] [复制] [RSS]

日志

stm32+lcd12864串行驱动

已有 333 次阅读2017-8-15 13:49 |个人分类:STM32|系统分类:单片机

网上并行驱动的资料居多,但有时受IO口的影响需要用到串行驱动,下面公开一段简单STM32的串行驱动程序,已经测试成功




/**********************************************************************************************



  称:  LCD12864-15B串行显示程序



  本:  V1.0



  能:  在显示屏任意位置显示内容



软件协议:



硬件环境: 主控STM32F103R8T6



                   PB0->EN



                    PB1->RW



                    VCC->RS



编写日期:20170812



编写人员:NO WAY



***************************************************************************************************



更改日:        



更内容:        



更改人员:



其它说明:



*************************************************************************************************/



 



#include "stm32f10x.h"



#include "string.h"



#include "delay.h"



#include "lcd12864.h"



int main(void)



{       



         Delay_int();



         delay_ms(200);



         GPIO_Config2();



         Lcdint();



         while(1)



        {



                   LCD_Display(0,0,"什么问题导致!");



                   LCD_Display(1,0,"时序不对!");



                   LCD_Display(2,0,"少与了低四位");



                   LCD_Display(3,0,"问题解决了!哈哈");



                   //highlight(1,8,16,60);



         }



}



 



#include "lcd12864.h"



char b;



/*****LCD屏显示地址*****/



uchar 
addr_tab[]={



0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,//第一行



0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,//第二行



0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,//第三行



0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,//第四行



};



/*******************************************************************************



* 函数名 :GPIO_Config



* 描述   : IO配置



* 输入   :



* 输出   :



* 返回   :



* 其它   :



*******************************************************************************/



void GPIO_Config2(void)



{



         GPIO_InitTypeDef
GPIO_InitStructure;



         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);



         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;



         GPIO_InitStructure.GPIO_Pin=RS|RW|EN;



         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;



         GPIO_Init(GPIOB,&GPIO_InitStructure);



}



/*******************************************************************************



* 函数名 :Writebyte



* 描述   :写一个字节函数



* 输入   :



* 输出   :



* 返回   :



* 其它   :



*******************************************************************************/



void Writebyte(u8 byte)



{



         int
i;



                   EN_L;



         for(i=0;i<8;i++)



         {



                   if(byte&0x80)



                   {



                            RW_H;



                   }



                   else



                   {



                            RW_L;



                   }



                 byte=byte<<1;



                   EN_L;



                   EN_H;



                   delay_us(700);



         }



}



/*******************************************************************************



* 函数名 :Writebyte



* 描述   :写一个字节函数



* 输入   :



* 输出   :



* 返回   :



* 其它   :



*******************************************************************************/



void Writ(u8 cmd ,u8 data)



{



        



         char
start_cmd=0,Hdata=0,Ldata=0;



         if(cmd==0)



         {



                   start_cmd=0xf8;



         }



         else



         {



                   start_cmd=0xfa;     



         }



         Hdata=data&0xf0;



         Ldata=(data<<4)&0xf0;



         Writebyte(start_cmd);



         Writebyte(Hdata);



         Writebyte(Ldata);



}



/*******************************************************************************



* 函数名 :Lcdint



* 描述   :12864初始化程序



* 输入   :



* 输出   :



* 返回   :



* 其它   :



*******************************************************************************/



void Lcdint(void)



{



         delay_ms(2);



         Writ(0,0x30);



         Writ(0,0x0c);



         Writ(0,0x01);



 
Writ(0,0x06);



         Writ(0,0x80);



}



/*******************************************************************************



* 函数名 :LCD_Display



* 描述   :12864(带字库)显示函数



* 输入   : x,y,*s



* 输出   :



* 返回   :



* 其它   :



*******************************************************************************/



void LCD_Display(uchar x,uchar y,uchar *s)



{



Writ(0,addr_tab[8*x+y]);



while(*s>0)



    {



     
Writ(1,*s);



     
s++;    



    }



}



 



void highlight(char x,char y,char width,char
mode )



{



         char
i,j,flag=0x00;



         if(y>1)



         {



                   flag=0x08;



                   y=y-2;



         }



         Writ(0,0x34);



         for(i=0;i<16;i++)



         {



                   Writ(0,0x80+(y<<4)+i);



                   Writ(0,0x80+flag+x);



                   for(j=0;j<width;j++)



                   {



                            Writ(1,mode);



                   }



         }



         Writ(0,0x36);



                   Writ(0,0x30);



}       



 



#ifndef _LCD12864_H



#define _LCD12864_H



#include   "stm32f10x.h"



#include "delay.h"



#include "string.h"



#define uint unsigned int



#define uchar unsigned char



#define RS GPIO_Pin_11



#define RW GPIO_Pin_1



#define     EN
GPIO_Pin_0



#define RS_H GPIO_SetBits(GPIOB,RS)



#define RS_L GPIO_ResetBits(GPIOB,RS)



#define RW_H GPIO_SetBits(GPIOB,RW)



#define RW_L GPIO_ResetBits(GPIOB,RW)



#define EN_H GPIO_SetBits(GPIOB,EN)



#define EN_L GPIO_ResetBits(GPIOB,EN)



void GPIO_Config2(void);



void Writebyte(u8 cmd);



void Writ(u8 cmd ,u8 data);



void Lcdint(void);



void LCD_Display(uchar x,uchar y,uchar *s);



void highlight(char x,char y,char
width,char mode);



#endif


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)