4462 views |1 replies
Last login 2023-4-18
Online Time 282 hours
Prestige 75 points
Points 44 points
[GD32F350 Development Sharing 5] GPIO simulates SPI to control LCD display
[Copy link]
Everyone should know that if GPIO is used to simulate SPI, it involves the output high and low levels and read levels of GPIO. Let's first look at the GPIO registers
(1) GPIOx_CTL register controls the GPIO mode (Pin0~Pin15) 00: Input mode (reset value) 01: GPIO output mode 10: Alternate function mode 11: Analog mode (2) GPIOx_OMODE register controls the GPIO output mode (Pin0~Pin15) This bit is set and cleared by software. 0: Output push-pull mode (reset value) 1: Output open-drain mode (3) GPIOx_OSPD0 register controls the GPIO output speed (Pin0~Pin15) This bit is set and cleared by software. x0: Output maximum speed 2M (reset value) 01: Output maximum speed 10M 11: Output maximum speed 50M (4) GPIOx_PUD register controls the pull-up and pull-down of GPIO. This bit is set and cleared by software. 00: Floating mode, no pull-up and pull-down (reset value) 01: Port pull-up mode 10: Port pull-down mode 11: Reserved (5) GPIOx_ISTAT register is the port input status register, the lower 16 bits are valid. These bits are set and cleared by software. 0: Pin input signal is low level 1: Pin input signal is high level (6) GPIOx_OCTL register is the port output control register, the lower 16 bits are valid. This bit is set and cleared by software. 0: Pin outputs low level 1: Pin outputs high level The following is the driver for the LCD display #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++); &="" (((uint16_t)cmd)<<8));="" (((uint16_t)dat)<<8));="" ();="" *------------------------------------------------------------="" ------------------------------------------------------------*="" 0x00000000)="" 0x00ff)="" 0x03000000;="" 0x08000000;="" 0x88888888;="" 0xf0ffffff)="" a0_0;="" a0_1;="" chk_busy="" chk_busy();="" chk_busy(void)="" cmd)="" dat)="" dataout="((DataOUT" delay_nms(unsigned="" delay_nus(0);="" delay_nus(1);="" delay_nus(5);="" gpio_bc(gpioc)="GPIO_PIN_6;" gpio_ctl(gpiob)="(GPIO_CTL(GPIOB)" gpio_octl(gpiob)="" int="" lcddelay(void)="" msÑóê±oˉêy="" ms)="" rd_0;="" rd_1;="" read_data(void)="" send_cmd(uchar="" send_dat(uchar="" temp="0;" uchar="" uint8_t="" void="" while(busy="=1)" while(ms--)delay_nus(1000);="" wr_0;="" wr_1;="" {="" |="" }="">>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 复制代码