- /*
- * File: lcd_init.h
- * Author: Administrator
- *
- * Created on August 19, 2021, 3:23 PM
- */
- #ifndef __LCD_INIT_H_
- #define __LCD_INIT_H_
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "type.h"
-
- #define USE_HORIZONTAL 0 //?????????? 0?1??? 2?3???
- #define LCD_W 240
- #define LCD_H 240
- //-----------------LCD????----------------
-
- #define LCD_SCLK_Clr() LCD_SCL_SetLow()//SCL=SCLK
- #define LCD_SCLK_Set() LCD_SCL_SetHigh()
- #define LCD_MOSI_Clr() LCD_SDA_SetLow()//SDA=MOSI
- #define LCD_MOSI_Set() LCD_SDA_SetHigh()
- #define LCD_RES_Clr() LCD_RES_SetLow()//RES
- #define LCD_RES_Set() LCD_RES_SetHigh()
- #define LCD_DC_Clr() LCD_DC_SetLow()//DC
- #define LCD_DC_Set() LCD_DC_SetHigh()
- #define LCD_BLK_Clr() LCD_BLK_SetLow()//BLK
- #define LCD_BLK_Set() LCD_BLK_SetHigh()
- void LCD_GPIO_Init(void);//???GPIO
- void LCD_Writ_Bus(u8 dat);//??SPI??
- void LCD_WR_DATA8(u8 dat);//??????
- void LCD_WR_DATA(u16 dat);//??????
- void LCD_WR_REG(u8 dat);//??????
- void LCD_Address_Set(u16 x1,u16 y1,u16 x2,u16 y2);//??????
- void LCD_Init(void);//LCD???
- #ifdef __cplusplus
- }
- #endif
- #endif /* LCD_INIT_H */
lcd_init.c
lcd.h
- /*
- * File: lcd.h
- * Author: Administrator
- *
- * Created on August 19, 2021, 3:22 PM
- */
- #ifndef __LCD_H_
- #define __LCD_H_
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "type.h"
- //????
- #define WHITE 0xFFFF
- #define BLACK 0x0000
- #define BLUE 0x001F
- #define BRED 0XF81F
- #define GRED 0XFFE0
- #define GBLUE 0X07FF
- #define RED 0xF800
- #define MAGENTA 0xF81F
- #define GREEN 0x07E0
- #define CYAN 0x7FFF
- #define YELLOW 0xFFE0
- #define BROWN 0XBC40 //??
- #define BRRED 0XFC07 //???
- #define GRAY 0X8430 //??
- #define DARKBLUE 0X01CF //???
- #define LIGHTBLUE 0X7D7C //???
- #define GRAYBLUE 0X5458 //???
- #define LIGHTGREEN 0X841F //???
- #define LGRAY 0XC618 //???(PANNEL),?????
- #define LGRAYBLUE 0XA651 //????(?????)
- #define LBBLUE 0X2B12 //????(???????)
-
- void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color);//????????
- void LCD_DrawPoint(u16 x,u16 y,u16 color);//?????????
- void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//?????????
- void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color);//??????????
- void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color);//?????????
- void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//?????
- void LCD_ShowChinese16x16(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//????16x16??
- void LCD_ShowChinese24x24(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//????24x24??
- void LCD_ShowChinese32x32(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//????32x32??
- void LCD_ShowChar(u16 x,u16 y,u8 num,u16 fc,u16 bc,u8 sizey,u8 mode);//??????
- void LCD_ShowString(u16 x,u16 y,const u8 *p,u16 fc,u16 bc,u8 sizey,u8 mode);//?????
- u32 mypow(u8 m,u8 n);//??
- void LCD_ShowIntNum(u16 x,u16 y,u16 num,u8 len,u16 fc,u16 bc,u8 sizey);//??????
- void LCD_ShowFloatNum1(u16 x,u16 y,float num,u8 len,u16 fc,u16 bc,u8 sizey);//????????
- void LCD_ShowPicture(u16 x,u16 y,u16 length,u16 width,const u8 pic[]);//????
- #ifdef __cplusplus
- }
- #endif
- #endif /* LCD_H */
lcd.c
- #include "lcd.h"
- #include "lcd_init.h"
- #include "lcdfont.h"
- /******************************************************************************
- ??????????????
- ?????xsta,ysta ????
- xend,yend ????
- color ??????
- ???? ?
- ******************************************************************************/
- void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
- {
- u16 i,j;
- LCD_Address_Set(xsta,ysta,xend-1,yend-1);//??????
- for(i=ysta;i<yend;i++)
- {
- for(j=xsta;j<xend;j++)
- {
- LCD_WR_DATA(color);
- }
- }
- }
- /******************************************************************************
- ????????????
- ?????x,y ????
- color ????
- ???? ?
- ******************************************************************************/
- void LCD_DrawPoint(u16 x,u16 y,u16 color)
- {
- LCD_Address_Set(x,y,x,y);//??????
- LCD_WR_DATA(color);
- }
- /******************************************************************************
- ???????
- ?????x1,y1 ????
- x2,y2 ????
- color ????
- ???? ?
- ******************************************************************************/
- void LCD_DrawLine(u16 x1,u16 y1,u16 x2,u16 y2,u16 color)
- {
- u16 t;
- int xerr=0,yerr=0,delta_x,delta_y,distance;
- int incx,incy,uRow,uCol;
- delta_x=x2-x1; //??????
- delta_y=y2-y1;
- uRow=x1;//??????
- uCol=y1;
- 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_x;}
- if(delta_x>delta_y)distance=delta_x; //?????????
- else distance=delta_y;
- for(t=0;t<distance+1;t++)
- {
- LCD_DrawPoint(uRow,uCol,color);//??
- xerr+=delta_x;
- yerr+=delta_y;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
- /******************************************************************************
- ????????
- ?????x1,y1 ????
- x2,y2 ????
- color ?????
- ???? ?
- ******************************************************************************/
- void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2,u16 color)
- {
- LCD_DrawLine(x1,y1,x2,y1,color);
- LCD_DrawLine(x1,y1,x1,y2,color);
- LCD_DrawLine(x1,y2,x2,y2,color);
- LCD_DrawLine(x2,y1,x2,y2,color);
- }
- /******************************************************************************
- ???????
- ?????x0,y0 ????
- r ??
- color ????
- ???? ?
- ******************************************************************************/
- void Draw_Circle(u16 x0,u16 y0,u8 r,u16 color)
- {
- int a,b;
- a=0;b=r;
- while(a<=b)
- {
- LCD_DrawPoint(x0-b,y0-a,color); //3
- LCD_DrawPoint(x0+b,y0-a,color); //0
- LCD_DrawPoint(x0-a,y0+b,color); //1
- LCD_DrawPoint(x0-a,y0-b,color); //2
- LCD_DrawPoint(x0+b,y0+a,color); //4
- LCD_DrawPoint(x0+a,y0-b,color); //5
- LCD_DrawPoint(x0+a,y0+b,color); //6
- LCD_DrawPoint(x0-b,y0+a,color); //7
- a++;
- if((a*a+b*b)>(r*r))//??????????
- {
- b--;
- }
- }
- }
- /******************************************************************************
- ??????????
- ?????x,y????
- *s ???????
- fc ????
- bc ?????
- sizey ?? ?? 16 24 32
- mode: 0????? 1????
- ???? ?
- ******************************************************************************/
- void LCD_ShowChinese(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- while(*s!=0)
- {
- if(sizey==16) LCD_ShowChinese16x16(x,y,s,fc,bc,sizey,mode);
- else if(sizey==24) LCD_ShowChinese24x24(x,y,s,fc,bc,sizey,mode);
- else if(sizey==32) LCD_ShowChinese32x32(x,y,s,fc,bc,sizey,mode);
- else return;
- s+=2;
- x+=sizey;
- }
- }
- /******************************************************************************
- ?????????16x16??
- ?????x,y????
- *s ??????
- fc ????
- bc ?????
- sizey ??
- mode: 0????? 1????
- ???? ?
- ******************************************************************************/
- void LCD_ShowChinese16x16(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- u8 i,j;
- u16 k;
- u16 HZnum;//????
- u16 TypefaceNum;//??????????
- u16 x0=x;
- TypefaceNum=sizey/8*sizey;//??????????????????8??????
- //?????????????,??????????
- HZnum=sizeof(tfont16)/sizeof(typFNT_GB16); //??????
- for(k=0;k<HZnum;k++)
- {
- if ((tfont16[k].Index[0]==*(s))&&(tfont16[k].Index[1]==*(s+1)))
- {
- LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
- for(i=0;i<TypefaceNum;i++)
- {
- for(j=0;j<8;j++)
- {
- if(!mode)//?????
- {
- if(tfont16[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
- else LCD_WR_DATA(bc);
- }
- else//????
- {
- if(tfont16[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);//????
- x++;
- if((x-x0)==sizey)
- {
- x=x0;
- y++;
- break;
- }
- }
- }
- }
- }
- continue; //????????????????????????????
- }
- }
- /******************************************************************************
- ?????????24x24??
- ?????x,y????
- *s ??????
- fc ????
- bc ?????
- sizey ??
- mode: 0????? 1????
- ???? ?
- ******************************************************************************/
- void LCD_ShowChinese24x24(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- u8 i,j;
- u16 k;
- u16 HZnum;//????
- u16 TypefaceNum;//??????????
- u16 x0=x;
- TypefaceNum=sizey/8*sizey;//??????????????????8??????
- //?????????????,??????????
- HZnum=sizeof(tfont24)/sizeof(typFNT_GB24); //??????
- for(k=0;k<HZnum;k++)
- {
- if ((tfont24[k].Index[0]==*(s))&&(tfont24[k].Index[1]==*(s+1)))
- {
- LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
- for(i=0;i<TypefaceNum;i++)
- {
- for(j=0;j<8;j++)
- {
- if(!mode)//?????
- {
- if(tfont24[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
- else LCD_WR_DATA(bc);
- }
- else//????
- {
- if(tfont24[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);//????
- x++;
- if((x-x0)==sizey)
- {
- x=x0;
- y++;
- break;
- }
- }
- }
- }
- }
- continue; //????????????????????????????
- }
- }
- /******************************************************************************
- ?????????32x32??
- ?????x,y????
- *s ??????
- fc ????
- bc ?????
- sizey ??
- mode: 0????? 1????
- ???? ?
- ******************************************************************************/
- void LCD_ShowChinese32x32(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- u8 i,j;
- u16 k;
- u16 HZnum;//????
- u16 TypefaceNum;//??????????
- u16 x0=x;
- TypefaceNum=sizey/8*sizey;//??????????????????8??????
- //?????????????,??????????
- HZnum=sizeof(tfont32)/sizeof(typFNT_GB32); //??????
- for(k=0;k<HZnum;k++)
- {
- if ((tfont32[k].Index[0]==*(s))&&(tfont32[k].Index[1]==*(s+1)))
- {
- LCD_Address_Set(x,y,x+sizey-1,y+sizey-1);
- for(i=0;i<TypefaceNum;i++)
- {
- for(j=0;j<8;j++)
- {
- if(!mode)//?????
- {
- if(tfont32[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
- else LCD_WR_DATA(bc);
- }
- else//????
- {
- if(tfont32[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);//????
- x++;
- if((x-x0)==sizey)
- {
- x=x0;
- y++;
- break;
- }
- }
- }
- }
- }
- continue; //????????????????????????????
- }
- }
- /******************************************************************************
- ???????????
- ?????x,y????
- num ??????
- fc ????
- bc ?????
- sizey ??
- mode: 0????? 1????
- ???? ?
- ******************************************************************************/
- void LCD_ShowChar(u16 x,u16 y,u8 num,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- u8 temp,sizex,t;
- u16 i,TypefaceNum;//??????????
- u16 x0=x;
- sizex=sizey/2;
- TypefaceNum=sizex/8*sizey;
- num=num-' '; //???????
- LCD_Address_Set(x,y,x+sizex-1,y+sizey-1); //??????
- for(i=0;i<TypefaceNum;i++)
- {
- if(sizey==16)temp=ascii_1608[num][i]; //??8x16??
- else if(sizey==32)temp=ascii_3216[num][i]; //??16x32??
- else return;
- for(t=0;t<8;t++)
- {
- if(!mode)//?????
- {
- if(temp&(0x01<<t))LCD_WR_DATA(fc);
- else LCD_WR_DATA(bc);
- }
- else//????
- {
- if(temp&(0x01<<t))LCD_DrawPoint(x,y,fc);//????
- x++;
- if((x-x0)==sizex)
- {
- x=x0;
- y++;
- break;
- }
- }
- }
- }
- }
- /******************************************************************************
- ??????????
- ?????x,y????
- *p ???????
- fc ????
- bc ?????
- sizey ??
- mode: 0????? 1????
- ???? ?
- ******************************************************************************/
- void LCD_ShowString(u16 x,u16 y,const u8 *p,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- while(*p!='\0')
- {
- LCD_ShowChar(x,y,*p,fc,bc,sizey,mode);
- x+=sizey/2;
- p++;
- }
- }
- /******************************************************************************
- ?????????
- ?????m???n??
- ???? ?
- ******************************************************************************/
- u32 mypow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- /******************************************************************************
- ???????????
- ?????x,y????
- num ???????
- len ??????
- fc ????
- bc ?????
- sizey ??
- ???? ?
- ******************************************************************************/
- void LCD_ShowIntNum(u16 x,u16 y,u16 num,u8 len,u16 fc,u16 bc,u8 sizey)
- {
- u8 t,temp;
- u8 enshow=0;
- u8 sizex=sizey/2;
- for(t=0;t<len;t++)
- {
- temp=(num/mypow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- LCD_ShowChar(x+t*sizex,y,' ',fc,bc,sizey,0);
- continue;
- }else enshow=1;
-
- }
- LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
- }
- }
- /******************************************************************************
- ?????????????
- ?????x,y????
- num ???????
- len ??????
- fc ????
- bc ?????
- sizey ??
- ???? ?
- ******************************************************************************/
- void LCD_ShowFloatNum1(u16 x,u16 y,float num,u8 len,u16 fc,u16 bc,u8 sizey)
- {
- u8 t,temp,sizex;
- u16 num1;
- sizex=sizey/2;
- num1=num*100;
- for(t=0;t<len;t++)
- {
- temp=(num1/mypow(10,len-t-1))%10;
- if(t==(len-2))
- {
- LCD_ShowChar(x+(len-2)*sizex,y,'.',fc,bc,sizey,0);
- t++;
- len+=1;
- }
- LCD_ShowChar(x+t*sizex,y,temp+48,fc,bc,sizey,0);
- }
- }
- /******************************************************************************
- ?????????
- ?????x,y????
- length ????
- width ????
- pic[] ????
- ???? ?
- ******************************************************************************/
- void LCD_ShowPicture(u16 x,u16 y,u16 length,u16 width,const u8 pic[])
- {
- u16 i,j,k=0;
- LCD_Address_Set(x,y,x+length-1,y+width-1);
- for(i=0;i<length;i++)
- {
- for(j=0;j<width;j++)
- {
- LCD_WR_DATA8(pic[k*2]);
- LCD_WR_DATA8(pic[k*2+1]);
- k++;
- }
- }
- }
数据类型的通用定义:
type.h
- /*
- * File: type.h
- * Author: Administrator
- *
- * Created on August 19, 2021, 3:25 PM
- */
- #ifndef TYPE_H
- #define TYPE_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "stdint.h"
- typedef int32_t s32;
- typedef int16_t s16;
- typedef int8_t s8;
- typedef const int32_t sc32; /*!< Read Only */
- typedef const int16_t sc16; /*!< Read Only */
- typedef const int8_t sc8; /*!< Read Only */
- typedef uint32_t u32;
- typedef uint16_t u16;
- typedef uint8_t u8;
- typedef const uint32_t uc32; /*!< Read Only */
- typedef const uint16_t uc16; /*!< Read Only */
- typedef const uint8_t uc8; /*!< Read Only */
- #ifdef __cplusplus
- }
- #endif
- #endif /* TYPE_H */
在main函数里面调用:
- #include "mcc_generated_files/mcc.h"
- #include "LCD/lcd_init.h"
- #include "LCD/lcd.h"
- #include "LCD/picture.h"
- /*
- Main application
- */
- void main(void)
- {
- //u8 i,j;
- float t=0;//22DAY S
- // Initialize the device
- SYSTEM_Initialize();
- // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
- // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
- // Use the following macros to:
- // Enable the Global Interrupts
- //INTERRUPT_GlobalInterruptEnable();
- // Disable the Global Interrupts
- //INTERRUPT_GlobalInterruptDisable();
- LCD_Init();//LCD???
- LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
- //LCD_ShowPicture(0,0,89,80,gImage_ZQ3);//89*80
- //while(1){}
- while (1)
- {
- // Add your application code
- LCD_ShowChinese(50,0,"中秋快乐",RED,WHITE,32,0);
- LCD_ShowString(0,40,"HappyMid-Autumn",RED,WHITE,32,0);
-
- LCD_ShowString(0,80+16,"Increaseing Nun:",RED,WHITE,16,0);
- LCD_ShowFloatNum1(128,80+16,t,5,RED,WHITE,16);
- t+=0.1;
- LCD_ShowPicture(31,120+16,89,80,gImage_ZQ3);//89*80
- LCD_ShowPicture(121,120+16,89,80,gImage_ZQ3);//89*80
- }
- }
- /**
- End of File
- */
这里载入了一个很小的“中秋快乐”的小图片~
下载,查看:
好了,驱动SPI屏幕就到这了~