|
【GD32F350开发分享五】GPIO模拟SPI控制液晶显示屏
[复制链接]
大家都应该知道,用GPIO模拟SPI的话,涉及GPIO的输出高低电平、读取电平,先来看GPIO的寄存器
(1)GPIOx_CTL寄存器控制GPIO的模式(Pin0~Pin15)
00:输入模式(复位值) 01:GPIO输出模式 10:备用功能模式 11:模拟模式
(2)GPIOx_OMODE寄存器控制GPIO的输出模式(Pin0~Pin15)
该位由软件置位和清除。 0:输出推挽模式(复位值) 1:输出开漏模式
(3)GPIOx_OSPD0寄存器控制GPIO的输出速度(Pin0~Pin15)
该位由软件置位和清除。 x0:输出最大速度2M(复位值) 01:输出最大速度10M 11:输出最大速度50M
(4)GPIOx_PUD寄存器控制GPIO的上下拉
该位由软件置位和清除。 00:悬空模式,无上拉和下拉(复位值) 01:端口上拉模式 10:端口下拉模式 11:保留
(5)GPIOx_ISTAT寄存器为端口输入状态寄存器,低16位有效
这些位由软件置位和清除。 0:引脚输入信号为低电平 1:引脚输入信号为高电平
(6)GPIOx_OCTL寄存器为端口输出控制寄存器,低16位有效
该位由软件置位和清除。 0:引脚输出低电平 1:引脚输出高电平
下面为液晶显示屏的驱动程序
- #include "gd32f3x0.h"
- #include <stdio.h>
- #include "gd32f3x0_eval.h"
- #include "systick.h"
- #include "lcd.h"
- static unsigned char LcdSystab[] = {0x30,0x87,0x07,0x28,0x2f,0x0f0,0x28,0x00};
- static unsigned char LcdScrtab[] = {0x00,0x00,0x0f0,0x00,0x40,0x0f0,0x00,0x80,0x00,0x00};
- #define CLOCK 9
- /*------------------------------------------------------------
- usÑóê±oˉêy
- ------------------------------------------------------------*/
- void Delay_Nus(unsigned int us)
- {
- uint8_t n;
- while(us--)for(n=0;n<CLOCK;n++);
- }
- /*------------------------------------------------------------
- msÑóê±oˉêy
- ------------------------------------------------------------*/
- void Delay_Nms(unsigned int ms)
- {
- while(ms--)Delay_Nus(1000);
- }
- void LcdDelay(void)
- {
- Delay_Nus(5);
- }
- void chk_busy(void)
- {
- GPIO_BC(GPIOC)=GPIO_PIN_6;
- GPIO_CTL(GPIOB) = (GPIO_CTL(GPIOB) & 0xf0ffffff) | 0x08000000;
-
- WR_1;
- A0_0;
- RD_0;
- Delay_Nus(0);
- while(BUSY==1)
- {
- }
- RD_1;
- GPIO_CTL(GPIOB)= (GPIO_CTL(GPIOB) & 0xf0ffffff) | 0x03000000;
- }
- void send_cmd(uchar cmd)
- {
- chk_busy();
- RD_1;
- A0_1;
- DataOUT = ((DataOUT & 0x00ff) | (((uint16_t)cmd)<<8));
- WR_0;
- Delay_Nus(1);
- WR_1;
- }
- void send_dat(uchar dat)
- {
- chk_busy ();
-
- RD_1;
- A0_0;
- DataOUT = ((DataOUT & 0x00ff) | (((uint16_t)dat)<<8));
- WR_0;
- Delay_Nus(1);
- WR_1;
- }
- uchar Read_data(void)
- {
- uint8_t temp = 0;
-
- GPIO_OCTL(GPIOB) &= 0x00ff;
- GPIO_CTL(GPIOB) = (GPIO_CTL(GPIOB) & 0x00000000) | 0x88888888;
-
- A0_1;
- WR_1;
- RD_0;
- temp = DataIN>>8;
- RD_1;
- GPIO_CTL(GPIOB) = (GPIO_CTL(GPIOB) & 0x00000000) | 0x33333333;
- return temp;
- }
- // -------------------?????????------------------
- void LcdCsrW ( unsigned char x, unsigned char y)
- {
- send_cmd(CsrW);
- LcdDelay();
- send_dat(x);
- LcdDelay();
- send_dat(y);
- LcdDelay();
- return;
- }
- // ????===================================
- void LcdClear(void)
- {
- unsigned int x,y;
- send_cmd(CsrDirR); //????
- LcdDelay();
- LcdCsrW (0,0);
- send_cmd(mWrite);
- for(x = 0;x<40;x++)
- for(y = 0;y<240;y++)
- {
- LcdDelay();
- send_dat(0x0ff);
- }
- }
- void LcdHZ(unsigned char x, unsigned int y,unsigned char *pdata,unsigned char flag)
- {
- unsigned char i,j;
- unsigned int Addr,tpInt;
- unsigned char a ,b;
- send_cmd(CsrDirR);
- LcdDelay();
- for(j = 0; j<2; j++)
- {
- if (x+j>=40)
- return;
-
- for (i = 0; i<16; i++)
- {
- Addr = (y+i)*40 + (x + j);
- a = Addr & 0x0FF;
- b = Addr >> 8;
-
- LcdCsrW (a,b);
- LcdDelay();
-
- send_cmd(mWrite);
- LcdDelay();
-
- a=j*16+i;
-
- tpInt=pdata[a>>1];
- if (a & 0x0001)
- b=tpInt & 0x00FF;
- else
- b=(tpInt>>8) & 0x00FF;
-
- if(!flag)
- {
- b=~b;
- }
- send_dat(b);
-
- }
- }
- }
- //---------------------??------x 0?39 y 0?239 ------8 X 16---------------
- void LcdChar(unsigned char x, unsigned char y, unsigned char *pdata, unsigned char flag) //flag 0?? 1??
- {
- unsigned char i,a,b;
- unsigned int Addr,tpInt;
- send_cmd(CsrDirD); //????
- LcdDelay();
- if (x>=40) // x 0?39
- return;
-
- for (i = 0; i<16; i++)
- {
- Addr = (y+i)*40 + x ;
- a = Addr & 0x0FF;
- b = Addr >> 8;
-
- LcdCsrW (a,b);
- LcdDelay();
-
- send_cmd(mWrite);
- LcdDelay();
- tpInt=pdata[i>>1];
- if (i & 0x0001)
- b=tpInt & 0x00FF;
- else
- b=(tpInt>>8) & 0x00FF;
-
- if(!flag)
- {
- b=~b;
- }
- send_dat(b);
- }
- }
-
- //-----------x=0 ? 319 y=0 ? 239 ----------------------------
- void LcdPoint( unsigned int x, unsigned int y, unsigned char flag) //flag = 0 ??(?), bf=1 ??(?), bf=2 ????(??)
- {
- unsigned int Addr;
- unsigned int a ,b;
- unsigned char temp ;
- unsigned int bitAddr = 0;
- unsigned char bit1;
- a = x >> 3;
- //temp = Sed[a][y];
- bitAddr = x - ( a << 3 );
-
- b = y*40;
- Addr = b + a;
-
- a = Addr & 0x0FF;
- b = Addr >> 8;
- bit1 = 0x80;
- if(bitAddr)
- bit1 = bit1>>bitAddr;
- if(flag == 0)//?
- temp = bit1 | temp;
- else if(flag == 1) //?
- {
- bit1 = 255- bit1;
- temp = (bit1 & temp);
- }
- else if(flag == 2)//??
- temp = bit1 ^ temp;
-
- send_cmd(CsrDirD); //????
- LcdDelay();
- LcdCsrW( a, b);
- LcdDelay();
- send_cmd(mWrite);
- LcdDelay();
- //Sed[x >> 3][y] = temp; //should be here,befor LcdWDataAddr = temp;
- send_dat(temp);
- }
- //-------------x=0 ? 319 y=0 ? 239 ------------------------
- void LcdLine(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned char flag)//flag 0 ??, bf=1 ??, bf=2 ????
- {
- register unsigned char t;
- int xerr=0,yerr=0,delta_x,delta_y,distance;
- int incx,incy;
- unsigned int row,col;
-
- if (x0>x1)
- {
- t=x0;
- x0=x1;
- x1=t;
- }// x0? x1?
- if (y0>y1)
- {
- t=y0;
- y0=y1;
- y1=t;
- }// y0? y1?
-
- if (x1>319)
- x1=319;
- if (y1>239)
- y1=239;
-
- delta_x = x1-x0; //??????
- delta_y = y1-y0;
- col = x0;
- row = y0;
- if(delta_x>0) incx=1; //??????
- else
- {
- if( delta_x==0 ) incx=0; //???
- else {incx=-1;delta_x=-delta_x;}
- }
- if(delta_y>0) incy=1;
- else
- {
- if( delta_y==0 ) incy=0; //???
- else {incy=-1;delta_y=-delta_y;}
- }
- if( delta_x > delta_y ) distance=delta_x; //?????????
- else distance=delta_y;
- for( t=0;t <= distance+1; t++ ) //????
- {
- LcdPoint(col, row, flag); //??
- xerr += delta_x ;
- yerr += delta_y ;
- if( xerr > distance )
- {
- xerr-=distance;
- col+=incx;
- }
- if( yerr > distance )
- {
- yerr-=distance;
- row+=incy;
- }
- }
- }
- // ==========????????=========================================
- void LcdInit(void)
- {
- unsigned char i = 0;
- send_cmd(SystemSet);
- LcdDelay();
- for ( i = 0; i<8; i++){
- LcdDelay();
- send_dat(LcdSystab[i]);
- }
-
- send_cmd(Scroll);
- LcdDelay();
- for ( i = 0; i<10; i++){
- LcdDelay();
- send_dat(LcdScrtab[i]);
- }
-
- send_cmd(HdotSet);
- LcdDelay();
- send_dat(0x00);
- LcdDelay();
-
- send_cmd(Ovlay);
- LcdDelay();
- send_dat(0x0C);
- LcdDelay();
-
- LcdClear();
-
- send_cmd(DispOn);
- LcdDelay();
- send_dat(0x04);
- LcdDelay();
- }
复制代码- #ifndef __LCD_H
- #define __LCD_H
- #include "gd32f3x0.h"
- #include <stdio.h>
- #include "gd32f3x0_eval.h"
- #include "systick.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define A0_0 GPIO_BOP(GPIOC)=GPIO_PIN_2
- #define A0_1 GPIO_BC(GPIOC)=GPIO_PIN_2
- #define WR_0 GPIO_BOP(GPIOC)=GPIO_PIN_3
- #define WR_1 GPIO_BC(GPIOC)=GPIO_PIN_3
- #define RD_0 GPIO_BOP(GPIOC)=GPIO_PIN_4
- #define RD_1 GPIO_BC(GPIOC)=GPIO_PIN_4
- #define CS_0 GPIO_BOP(GPIOC)=GPIO_PIN_5
- #define CS_1 GPIO_BC(GPIOC)=GPIO_PIN_5
- #define BUSY gpio_input_bit_get(GPIOC,GPIO_PIN_6)
- #define DataOUT GPIO_OCTL(GPIOB)
- #define DataIN GPIO_ISTAT(GPIOB)
- /*--------------------------------------------------------------*/
- //???????????
- #define SystemSet 0x40
- #define SleepIn 0x53
- #define DispOn 0x59
- #define DispOff 0x58
- #define Scroll 0x44
- #define Csrform 0x5d
- #define CgramAdr 0x50
- #define CsrDirR 0x4c
- #define CsrDirL 0x4d
- #define CsrDirU 0x4e
- #define CsrDirD 0x4f
- #define HdotSet 0x5a
- #define Ovlay 0x5b
- #define CsrW 0x46
- #define CsrR 0x47
- #define mWrite 0x42
- #define mRead 0x43
- void LCD_PinInit(void);
- void chk_busy(void);
- void send_cmd(uchar cmd);
- void send_dat(uchar dat);
- void Delay_Nms(uint n);
- void Delay_Nus(uint n);
- #define LCD_SCROLL 0x44
- void LcdClear(void);
- //------------flag 0?? 1??-----------------------------------------------
- void LcdHZ(unsigned char x, unsigned int y,unsigned char *pdata,unsigned char flag);
- void LcdChar(unsigned char x, unsigned char y, unsigned char *pdata, unsigned char flag);
- //-----------flag = 0 ??(?), bf=1 ??(?), bf=2 ????(??)-----------
- void LcdPoint( unsigned int x, unsigned int y, unsigned char flag);
- //-----------flag = 0 ??(?), bf=1 ??(?), bf=2 ????(??)-----------
- void LcdLine(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned char flag);
- void LcdInit(void);
- void LcdCsrW ( unsigned char x, unsigned char y);
- void Delay_ns();
- #endif
复制代码
|
|