|
2.4寸 tftlcd 9341 驅(qū)動代碼部分程序預(yù)覽:
單片機(jī)源程序如下:
- /****************************************Copyright (c)***************************************************
- **--------------文件信息---------------------------------------------------------------------------------
- **文 件 名: bsp.c
- **描 述: STM32F4xxx 板基代碼
- **
- **電 路 板 號:
- **版 本: V1.0
- **
- **------------------------------------當(dāng)前版本修訂-------------------------------------------------------
- **
- **當(dāng) 前 版 本:
- **修 改 人:
- **修 改 日 期:
- **修 改 描 述:
- **
- **-------------------------------------------------------------------------------------------------------
- *********************************************************************************************************/
- #include "lcd.h"
- #include "font.h"
- char str[10];
- char *getStringToInt(int16_t data,uint8_t type)
- {
-
- char *tmpstr = str;
- memset(str,0,10);
- if((type != 10) && (type != 16))
- {
- tmpstr = "0";
- } else
- {
- if(type == 16)
- {
- sprintf(tmpstr,"%-8X",data);
- if(data <= 0x0f)
- {
- str[1] = 0;
- }
- } else if (type == 10)
- {
- sprintf(tmpstr,"%d",data);
- if((data <= 9) && (data >=0))
- {
- str[1] = 0;
- }
- }
- }
- return tmpstr;
- }
- char *getStringToFloat(float data)
- {
-
- char *tmpstr = str;
- memset(str,0,10);
- sprintf(tmpstr,"%.1f",data);
- return tmpstr;
- }
- #define DELAY_MS(n) BSP_WaitUntilTime(n);
- //LCD的畫筆顏色和背景色
- u16 POINT_COLOR=WHITE; //畫筆顏色
- u16 BACK_COLOR=BLUE; //背景色
- _lcd_dev lcddev;
- //u16 POINT_COLOR;//默認(rèn)紅色
- //u16 BACK_COLOR; //背景顏色.默認(rèn)為白色
- //void DELAY_MS(n)
- //{
- // BSP_WaitUntilTime(n);
- //}
- //讀LCD數(shù)據(jù)
- //返回值:讀到的值
- u16 LCD_RD_DATA(void)
- {
- vu16 ram; //防止被優(yōu)化
- ram=TFT_RAM;
- return ram;
- }
- //寫寄存器
- //LCD_Reg:寄存器地址
- //LCD_RegValue:要寫入的數(shù)據(jù)
- void LCD_DataOut (uint8_t data) {
- uint16_t ReadData = GPIOB->ODR;
-
- ReadData = (ReadData & 0x00FF) | (data << 8);
- GPIOB->ODR = ReadData;
- }
- uint8_t LCD_ReadData (void)
- {
- uint16_t ReadData = GPIOB->IDR;
-
- return (ReadData >> 8);
- }
- /////////////////////////////////
- void LCD_WR_REG(u16 regval)
- {
- // uint8_t TestData;
- //TFT_REG=regval;//寫入要寫的寄存器序號
- LCD_RS_L();
- LCD_CS_L();
- LCD_DataOut(regval&0xFF);
-
- LCD_WR_L();
- LCD_WR_H();
- LCD_CS_H();
- }
- //////////////////////////////////
- //寫LCD數(shù)據(jù)
- //data:要寫入的值
- void LCD_WR_DATA(u16 data)
- {
- LCD_RS_H();
- LCD_RD_H();
- LCD_CS_L();
- LCD_DataOut(data&0xff);
- LCD_WR_L();
- LCD_WR_H();
- LCD_CS_H();
- }
- u16 LCD_ReadReg(u16 LCD_Reg)
- {
- LCD_WR_REG(LCD_Reg); //寫入要讀的寄存器序號
- DELAY_MS(5);
- return LCD_RD_DATA(); //返回讀到的值
- }
- //開始寫GRAM
- void LCD_WriteRAM_Prepare(void)
- {
- LCD->LCD_REG=lcddev.wramcmd;
- }
- //LCD寫GRAM
- //RGB_Code:顏色值
- void LCD_WriteRAM(u8 RGB_Code)
- {
- LCD->LCD_RAM = RGB_Code;//寫十六位GRAM
- }
- void LCD_WriteReg(u16 LCD_Reg,u16 LCD_RegValue)
- {
- TFT_REG = LCD_Reg; //寫入要寫的寄存器序號
- TFT_RAM = LCD_RegValue;//寫入數(shù)據(jù)
- }
- const uint8_t ComTable[]={7,6,5,4,3,2,1,0,};
- void LCD2_GPIO_Init()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PB0 推挽輸出 背光
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7| GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口線翻轉(zhuǎn)速度為50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- //8位數(shù)據(jù)輸出
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口線翻轉(zhuǎn)速度為50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- LCD_CS_H();
- LCD_RS_L();
-
- lcddev.id=0X9341;
- lcddev.dir=1;
- lcddev.width=320;
- lcddev.height = 240;
- lcddev.wramcmd=0X2C;
- lcddev.setxcmd=0X2A;
- lcddev.setycmd=0X2B;
- }
- //////////////////////////////////////////////////////////////////
- //最底層數(shù)據(jù)傳輸函數(shù)
- //m^n函數(shù)
- //返回值:m^n次方.
- u32 LCD_Pow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- //顯示數(shù)字,高位為0,則不顯示
- //x,y :起點(diǎn)坐標(biāo)
- //len :數(shù)字的位數(shù)
- //size:字體大小
- //color:顏色
- //num:數(shù)值(0~4294967295);
- void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
- {
- u8 t,temp;
- u8 enshow=0;
- for(t=0;t<len;t++)
- {
- temp=(num/LCD_Pow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
- continue;
- }else enshow=1;
-
- }
- LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0);
- }
- }
- //顯示數(shù)字,高位為0,還是顯示
- //x,y:起點(diǎn)坐標(biāo)
- //num:數(shù)值(0~999999999);
- //len:長度(即要顯示的位數(shù))
- //size:字體大小
- //mode:
- //[7]:0,不填充;1,填充0.
- //[6:1]:保留
- //[0]:0,非疊加顯示;1,疊加顯示.
- void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode)
- {
- u8 t,temp;
- u8 enshow=0;
- for(t=0;t<len;t++)
- {
- temp=(num/LCD_Pow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);
- else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);
- continue;
- }else enshow=1;
-
- }
- LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01);
- }
- }
- //顯示字符串
- //x,y:起點(diǎn)坐標(biāo)
- //width,height:區(qū)域大小
- //size:字體大小
- //*p:字符串起始地址
- void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
- {
- u8 x0=x;
- width+=x;
- height+=y;
- while((*p<='~')&&(*p>=' '))//判斷是不是非法字符!
- {
- if(x>=width){x=x0;y+=size;}
- if(y>=height)break;//退出
- LCD_ShowChar(x,y,*p,size,0);
- x+=size/2;
- p++;
- }
- }
- void LCD_ShowStringT(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p,int mode)
- {
- u8 x0=x;
- width+=x;
- height+=y;
- while((*p<='~')&&(*p>= ' '))//判斷是不是非法字符!
- {
- if(x>=width){x=x0;y+=size;}
- if(y>=height)break;//退出
- LCD_ShowChar(x,y,*p,size,mode);
- x+=size/2;
- p++;
- }
- }
- void draw_line(int x1,int y1,int x2,int y2,int color)
- {
- int dx,dy,e;
- dx=x2-x1;
- dy=y2-y1;
- if(dx>=0)
- {
- if(dy >= 0) // dy>=0
- {
- if(dx>=dy) // 1/8 octant
- {
- e=dy-dx/2;
- while(x1<=x2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){y1+=1;e-=dx;}
- x1+=1;
- e+=dy;
- }
- }
- else // 2/8 octant
- {
- e=dx-dy/2;
- while(y1<=y2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){x1+=1;e-=dy;}
- y1+=1;
- e+=dx;
- }
- }
- }
- else // dy<0
- {
- dy=-dy; // dy=abs(dy)
- if(dx>=dy) // 8/8 octant
- {
- e=dy-dx/2;
- while(x1<=x2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){y1-=1;e-=dx;}
- x1+=1;
- e+=dy;
- }
- }
- else // 7/8 octant
- {
- e=dx-dy/2;
- while(y1>=y2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){x1+=1;e-=dy;}
- y1-=1;
- e+=dx;
- }
- }
- }
- }
- else //dx<0
- {
- dx=-dx; //dx=abs(dx)
- if(dy >= 0) // dy>=0
- {
- if(dx>=dy) // 4/8 octant
- {
- e=dy-dx/2;
- while(x1>=x2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){y1+=1;e-=dx;}
- x1-=1;
- e+=dy;
- }
- }
- else // 3/8 octant
- {
- e=dx-dy/2;
- while(y1<=y2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){x1-=1;e-=dy;}
- y1+=1;
- e+=dx;
- }
- }
- }
- else // dy<0
- {
- dy=-dy; // dy=abs(dy)
- if(dx>=dy) // 5/8 octant
- {
- e=dy-dx/2;
- while(x1>=x2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){y1-=1;e-=dx;}
- x1-=1;
- e+=dy;
- }
- }
- else // 6/8 octant
- {
- e=dx-dy/2;
- while(y1>=y2)
- {
- LCD_OutP(x1,y1,color);
- if(e>0){x1-=1;e-=dy;}
- y1-=1;
- e+=dx;
- }
- }
- }
- }
- }
- void draw_lineS(int x1,int y1,int x2,int y2,int num,int color)
- {
- int con=0;
- int i;
- for(i=0;i<num;i++)
- {
- draw_line(x1+con,y1+con,x2+con,y2+con,color);
- con++;
- }
- }
- void LCDOpenWindows(u16 x, u16 y, u16 len, u16 wid)
- {
- LCD_WR_REG(0X2A);
- LCD_WR_DATA(x>>8); //start
- LCD_WR_DATA(x-((x>>8)<<8));
- LCD_WR_DATA((x+len-1)>>8); //end
- LCD_WR_DATA((x+len-1)-(((x+len-1)>>8)<<8));
-
- LCD_WR_REG(0X2B);
- LCD_WR_DATA(y>>8); //start
- LCD_WR_DATA(y-((y>>8)<<8));
- LCD_WR_DATA((y+wid-1)>>8); //end
- LCD_WR_DATA((y+wid-1)-(((y+wid-1)>>8)<<8));
- LCD_WR_REG(0x2C);
- }
- void fnImageShow( const unsigned char *img , u16 x , u16 y )
- {
- u16 i;
- u16 HighFirst=0;
- u16 Width,Hight; //圖像的寬度和高度(16位數(shù)據(jù))
- u16 data;
- //0:WORD類型高低位字節(jié)順序與PC相同,1:WORD類型高低位字節(jié)順序與PC相反。
- if( img[0] & (1<<4) ) // 1,高位在前,低位在后.
- {
- HighFirst=1;
- }
- else //0,低位在前,高位在后.
- {
- HighFirst=0;
- }
- if(HighFirst==0) //高位在后
- {
- Width = img[3]; //img[3]是圖像寬度高8位,賦值
- Width <<= 8; //左移8位變成16位數(shù)據(jù)的高8位
- Width |= img[2];//與低八位數(shù)據(jù)字節(jié)img[2]相或組成16位數(shù)據(jù)
-
- Hight = img[5]; //img[5]是圖像寬度高8位,賦值
- Hight <<= 8; //左移8位變成16位數(shù)據(jù)的高8位
- Hight |= img[4];//與低八位數(shù)據(jù)字節(jié)img[4]相或組成16位數(shù)據(jù)
- }
- else //高位在前
- {
- Width = img[2];//img[2]是圖像寬度高8位,賦值
- Width <<= 8; //左移8位變成16位數(shù)據(jù)的高8位
- Width |= img[3];//與低八位數(shù)據(jù)字節(jié)img[3]相或組成16位數(shù)據(jù)
-
- Hight = img[4]; //img[4]是圖像寬度高8位,賦值
- Hight <<= 8; //左移8位變成16位數(shù)據(jù)的高8位
- Hight |= img[5]; //與低八位數(shù)據(jù)字節(jié)img[5]相或組成16位數(shù)據(jù)
- }
-
- /* if((X_Position+Width)>240||(Y_Position+Hight)>320)//如果寬度與高度超過范圍,則警告
- {
- //可在此寫警告
- }
- else
- {
- }*/
-
- img=img+8; //圖片數(shù)據(jù)中 前8個字節(jié)是圖片信息 從第9個開始是圖片像素信息(注意 img是從img[0]開始的,所以第9個字節(jié)是img[8])
- LCD_SetPos( x , y , x+Width-1 , y+Hight-1 );//設(shè)置坐標(biāo)
- LCD_WriteReg(0x0020,x); //寬度位置坐標(biāo)
- LCD_WriteReg(0x0021,y); //高度位置坐標(biāo)
- LCD_WriteRAM(data); // 開始寫入
- for(i=0; i <Width*Hight; i++) // Hight Y_Position是高起始坐標(biāo),Y_Position+Hight是高終止坐標(biāo)
- {
- if(HighFirst==0)
- {
- data = *(img++);
- data |= ((u16)*img)<<8;
- LCD_WR_REG(data);
- img += 1;
- }
- else
- {
- data =((u16)*(img++));
- data |=((u16)*img)<<8;
- LCD_WR_REG(data);
- img+=1;
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
Desktop.zip
(9.42 KB, 下載次數(shù): 29)
2019-9-7 18:48 上傳
點(diǎn)擊文件名下載附件
2.4寸9341驅(qū)動代碼 下載積分: 黑幣 -5
|
評分
-
查看全部評分
|