久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2668|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

2.4寸9341的STM32驅(qū)動代碼 直接可以顯示 8位

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:289967 發(fā)表于 2019-9-7 18:50 | 只看該作者 回帖獎勵 |倒序?yàn)g覽 |閱讀模式
2.4寸 tftlcd 9341 驅(qū)動代碼部分程序預(yù)覽:

單片機(jī)源程序如下:
  1. /****************************************Copyright (c)***************************************************
  2. **--------------文件信息---------------------------------------------------------------------------------
  3. **文   件   名: bsp.c
  4. **描        述: STM32F4xxx  板基代碼
  5. **
  6. **電 路  板 號:
  7. **版        本: V1.0
  8. **
  9. **------------------------------------當(dāng)前版本修訂-------------------------------------------------------
  10. **
  11. **當(dāng) 前 版  本:
  12. **修   改   人:
  13. **修 改  日        期:
  14. **修 改  描 述:
  15. **
  16. **-------------------------------------------------------------------------------------------------------
  17. *********************************************************************************************************/

  18. #include "lcd.h"
  19. #include "font.h"

  20. char str[10];
  21. char *getStringToInt(int16_t data,uint8_t type)
  22. {
  23.    
  24.     char *tmpstr = str;
  25.     memset(str,0,10);
  26.     if((type != 10) && (type != 16))
  27.     {
  28.         tmpstr = "0";
  29.     } else
  30.     {
  31.         if(type == 16)
  32.         {
  33.             sprintf(tmpstr,"%-8X",data);
  34.             if(data <= 0x0f)
  35.             {
  36.                 str[1] = 0;
  37.             }
  38.         } else if (type == 10)
  39.         {
  40.             sprintf(tmpstr,"%d",data);
  41.             if((data <= 9) && (data >=0))
  42.             {
  43.                 str[1] = 0;
  44.             }
  45.         }
  46.     }
  47.     return  tmpstr;
  48. }

  49. char *getStringToFloat(float data)
  50. {
  51.    
  52.     char *tmpstr = str;
  53.     memset(str,0,10);
  54.     sprintf(tmpstr,"%.1f",data);
  55.     return  tmpstr;
  56. }

  57. #define  DELAY_MS(n)   BSP_WaitUntilTime(n);

  58. //LCD的畫筆顏色和背景色          
  59. u16 POINT_COLOR=WHITE;        //畫筆顏色
  60. u16 BACK_COLOR=BLUE;  //背景色
  61. _lcd_dev lcddev;
  62. //u16  POINT_COLOR;//默認(rèn)紅色   
  63. //u16  BACK_COLOR; //背景顏色.默認(rèn)為白色


  64. //void DELAY_MS(n)
  65. //{
  66. //        BSP_WaitUntilTime(n);
  67. //}


  68. //讀LCD數(shù)據(jù)
  69. //返回值:讀到的值
  70. u16 LCD_RD_DATA(void)
  71. {
  72.         vu16 ram;                        //防止被優(yōu)化
  73.         ram=TFT_RAM;       
  74.         return ram;         
  75. }       


  76. //寫寄存器
  77. //LCD_Reg:寄存器地址
  78. //LCD_RegValue:要寫入的數(shù)據(jù)

  79. void LCD_DataOut (uint8_t data) {
  80.     uint16_t ReadData = GPIOB->ODR;
  81.    
  82.     ReadData = (ReadData & 0x00FF) | (data << 8);
  83.     GPIOB->ODR = ReadData;
  84. }

  85. uint8_t LCD_ReadData (void)
  86. {
  87.     uint16_t ReadData = GPIOB->IDR;
  88.    
  89.     return (ReadData >> 8);
  90. }

  91. /////////////////////////////////
  92. void LCD_WR_REG(u16 regval)
  93. {   
  94. //   uint8_t TestData;   
  95.         //TFT_REG=regval;//寫入要寫的寄存器序號       
  96.     LCD_RS_L();
  97.         LCD_CS_L();
  98.         LCD_DataOut(regval&0xFF);
  99.    
  100.         LCD_WR_L();
  101.         LCD_WR_H();
  102.         LCD_CS_H();
  103. }
  104. //////////////////////////////////
  105. //寫LCD數(shù)據(jù)
  106. //data:要寫入的值
  107. void LCD_WR_DATA(u16 data)
  108. {         
  109.         LCD_RS_H();
  110.         LCD_RD_H();
  111.         LCD_CS_L();
  112.         LCD_DataOut(data&0xff);
  113.         LCD_WR_L();
  114.         LCD_WR_H();
  115.         LCD_CS_H();                 
  116. }
  117. u16 LCD_ReadReg(u16 LCD_Reg)
  118. {                                                                                  
  119.         LCD_WR_REG(LCD_Reg);                //寫入要讀的寄存器序號
  120.         DELAY_MS(5);                  
  121.         return LCD_RD_DATA();                //返回讀到的值
  122. }   
  123. //開始寫GRAM
  124. void LCD_WriteRAM_Prepare(void)
  125. {
  126.         LCD->LCD_REG=lcddev.wramcmd;          
  127. }         
  128. //LCD寫GRAM
  129. //RGB_Code:顏色值
  130. void LCD_WriteRAM(u8 RGB_Code)
  131. {                                                            
  132.         LCD->LCD_RAM = RGB_Code;//寫十六位GRAM
  133. }


  134. void LCD_WriteReg(u16 LCD_Reg,u16 LCD_RegValue)
  135. {       
  136.         TFT_REG = LCD_Reg;                //寫入要寫的寄存器序號         
  137.         TFT_RAM = LCD_RegValue;//寫入數(shù)據(jù)                             
  138. }

  139. const uint8_t ComTable[]={7,6,5,4,3,2,1,0,};
  140. void LCD2_GPIO_Init()
  141. {
  142.         GPIO_InitTypeDef GPIO_InitStructure;

  143.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |  RCC_APB2Periph_AFIO, ENABLE);
  144.        
  145.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  146.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

  147.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                                 //PB0 推挽輸出 背光
  148.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽輸出
  149.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  150.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  151.        
  152.        
  153.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7| GPIO_Pin_1;
  154.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  155.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                         //口線翻轉(zhuǎn)速度為50MHz
  156.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  157.        
  158.         //8位數(shù)據(jù)輸出
  159.         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;
  160.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  161.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                         //口線翻轉(zhuǎn)速度為50MHz
  162.         GPIO_Init(GPIOB, &GPIO_InitStructure);       

  163.     LCD_CS_H();
  164.     LCD_RS_L();
  165.                
  166.         lcddev.id=0X9341;
  167.         lcddev.dir=1;
  168.         lcddev.width=320;
  169.         lcddev.height = 240;
  170.         lcddev.wramcmd=0X2C;
  171.         lcddev.setxcmd=0X2A;
  172.         lcddev.setycmd=0X2B;
  173. }

  174. //////////////////////////////////////////////////////////////////
  175. //最底層數(shù)據(jù)傳輸函數(shù)

  176. //m^n函數(shù)
  177. //返回值:m^n次方.
  178. u32 LCD_Pow(u8 m,u8 n)
  179. {
  180.         u32 result=1;         
  181.         while(n--)result*=m;   
  182.         return result;
  183. }                         
  184. //顯示數(shù)字,高位為0,則不顯示
  185. //x,y :起點(diǎn)坐標(biāo)         
  186. //len :數(shù)字的位數(shù)
  187. //size:字體大小
  188. //color:顏色
  189. //num:數(shù)值(0~4294967295);         
  190. void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
  191. {                
  192.         u8 t,temp;
  193.         u8 enshow=0;                                                  
  194.         for(t=0;t<len;t++)
  195.         {
  196.                 temp=(num/LCD_Pow(10,len-t-1))%10;
  197.                 if(enshow==0&&t<(len-1))
  198.                 {
  199.                         if(temp==0)
  200.                         {
  201.                                 LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
  202.                                 continue;
  203.                         }else enshow=1;
  204.                           
  205.                 }
  206.                  LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0);
  207.         }
  208. }
  209. //顯示數(shù)字,高位為0,還是顯示
  210. //x,y:起點(diǎn)坐標(biāo)
  211. //num:數(shù)值(0~999999999);         
  212. //len:長度(即要顯示的位數(shù))
  213. //size:字體大小
  214. //mode:
  215. //[7]:0,不填充;1,填充0.
  216. //[6:1]:保留
  217. //[0]:0,非疊加顯示;1,疊加顯示.
  218. void LCD_ShowxNum(u16 x,u16 y,u32 num,u8 len,u8 size,u8 mode)
  219. {  
  220.         u8 t,temp;
  221.         u8 enshow=0;                                                  
  222.         for(t=0;t<len;t++)
  223.         {
  224.                 temp=(num/LCD_Pow(10,len-t-1))%10;
  225.                 if(enshow==0&&t<(len-1))
  226.                 {
  227.                         if(temp==0)
  228.                         {
  229.                                 if(mode&0X80)LCD_ShowChar(x+(size/2)*t,y,'0',size,mode&0X01);  
  230.                                 else LCD_ShowChar(x+(size/2)*t,y,' ',size,mode&0X01);  
  231.                                 continue;
  232.                         }else enshow=1;
  233.                           
  234.                 }
  235.                  LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode&0X01);
  236.         }
  237. }
  238. //顯示字符串
  239. //x,y:起點(diǎn)坐標(biāo)
  240. //width,height:區(qū)域大小  
  241. //size:字體大小
  242. //*p:字符串起始地址                  
  243. void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
  244. {         
  245.         u8 x0=x;
  246.         width+=x;
  247.         height+=y;
  248.     while((*p<='~')&&(*p>=' '))//判斷是不是非法字符!
  249.     {      
  250.         if(x>=width){x=x0;y+=size;}
  251.         if(y>=height)break;//退出
  252.         LCD_ShowChar(x,y,*p,size,0);
  253.         x+=size/2;
  254.         p++;
  255.     }  
  256. }
  257. void LCD_ShowStringT(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p,int mode)
  258. {         
  259.         u8 x0=x;
  260.         width+=x;
  261.         height+=y;
  262.     while((*p<='~')&&(*p>= ' '))//判斷是不是非法字符!
  263.     {      
  264.         if(x>=width){x=x0;y+=size;}
  265.         if(y>=height)break;//退出
  266.         LCD_ShowChar(x,y,*p,size,mode);
  267.         x+=size/2;
  268.         p++;
  269.     }  
  270. }
  271. void draw_line(int x1,int y1,int x2,int y2,int color)  
  272. {  
  273.     int dx,dy,e;  
  274.     dx=x2-x1;   
  275.     dy=y2-y1;  
  276.     if(dx>=0)  
  277.     {  
  278.         if(dy >= 0) // dy>=0  
  279.         {  
  280.             if(dx>=dy) // 1/8 octant  
  281.             {  
  282.                 e=dy-dx/2;  
  283.                 while(x1<=x2)  
  284.                 {  
  285.                     LCD_OutP(x1,y1,color);   
  286.                     if(e>0){y1+=1;e-=dx;}     
  287.                     x1+=1;  
  288.                     e+=dy;  
  289.                 }  
  290.             }  
  291.             else        // 2/8 octant  
  292.             {  
  293.                 e=dx-dy/2;  
  294.                 while(y1<=y2)  
  295.                 {  
  296.                     LCD_OutP(x1,y1,color);  
  297.                     if(e>0){x1+=1;e-=dy;}     
  298.                     y1+=1;  
  299.                     e+=dx;  
  300.                 }  
  301.             }  
  302.         }  
  303.         else           // dy<0  
  304.         {  
  305.             dy=-dy;   // dy=abs(dy)  
  306.             if(dx>=dy) // 8/8 octant  
  307.             {  
  308.                 e=dy-dx/2;  
  309.                 while(x1<=x2)  
  310.                 {  
  311.                     LCD_OutP(x1,y1,color);  
  312.                     if(e>0){y1-=1;e-=dx;}     
  313.                     x1+=1;  
  314.                     e+=dy;  
  315.                 }  
  316.             }  
  317.             else        // 7/8 octant  
  318.             {  
  319.                 e=dx-dy/2;  
  320.                 while(y1>=y2)  
  321.                 {  
  322.                     LCD_OutP(x1,y1,color);  
  323.                     if(e>0){x1+=1;e-=dy;}     
  324.                     y1-=1;  
  325.                     e+=dx;  
  326.                 }  
  327.             }  
  328.         }     
  329.     }  
  330.     else //dx<0  
  331.     {  
  332.         dx=-dx;     //dx=abs(dx)  
  333.         if(dy >= 0) // dy>=0  
  334.         {  
  335.             if(dx>=dy) // 4/8 octant  
  336.             {  
  337.                 e=dy-dx/2;  
  338.                 while(x1>=x2)  
  339.                 {  
  340.                     LCD_OutP(x1,y1,color);  
  341.                     if(e>0){y1+=1;e-=dx;}     
  342.                     x1-=1;  
  343.                     e+=dy;  
  344.                 }  
  345.             }  
  346.             else        // 3/8 octant  
  347.             {  
  348.                 e=dx-dy/2;  
  349.                 while(y1<=y2)  
  350.                 {  
  351.                     LCD_OutP(x1,y1,color);  
  352.                     if(e>0){x1-=1;e-=dy;}     
  353.                     y1+=1;  
  354.                     e+=dx;  
  355.                 }  
  356.             }  
  357.         }  
  358.         else           // dy<0  
  359.         {  
  360.             dy=-dy;   // dy=abs(dy)  
  361.             if(dx>=dy) // 5/8 octant  
  362.             {  
  363.                 e=dy-dx/2;  
  364.                 while(x1>=x2)  
  365.                 {  
  366.                     LCD_OutP(x1,y1,color);  
  367.                     if(e>0){y1-=1;e-=dx;}     
  368.                     x1-=1;  
  369.                     e+=dy;  
  370.                 }  
  371.             }  
  372.             else        // 6/8 octant  
  373.             {  
  374.                 e=dx-dy/2;  
  375.                 while(y1>=y2)  
  376.                 {  
  377.                     LCD_OutP(x1,y1,color);  
  378.                     if(e>0){x1-=1;e-=dy;}     
  379.                     y1-=1;  
  380.                     e+=dx;  
  381.                 }  
  382.             }  
  383.         }     
  384.     }  
  385. }  


  386. void draw_lineS(int x1,int y1,int x2,int y2,int num,int color)
  387. {
  388.         int con=0;
  389.         int i;
  390.         for(i=0;i<num;i++)
  391.         {
  392.                 draw_line(x1+con,y1+con,x2+con,y2+con,color);
  393.                 con++;
  394.         }
  395. }
  396. void LCDOpenWindows(u16 x, u16 y, u16 len, u16 wid)
  397. {
  398.         LCD_WR_REG(0X2A);
  399.         LCD_WR_DATA(x>>8);        //start
  400.         LCD_WR_DATA(x-((x>>8)<<8));
  401.         LCD_WR_DATA((x+len-1)>>8);        //end
  402.         LCD_WR_DATA((x+len-1)-(((x+len-1)>>8)<<8));
  403.         
  404.         LCD_WR_REG(0X2B);
  405.         LCD_WR_DATA(y>>8);   //start
  406.         LCD_WR_DATA(y-((y>>8)<<8));
  407.         LCD_WR_DATA((y+wid-1)>>8);   //end
  408.         LCD_WR_DATA((y+wid-1)-(((y+wid-1)>>8)<<8));        
  409.         LCD_WR_REG(0x2C);
  410. }

  411. void  fnImageShow( const unsigned char *img , u16 x , u16 y )
  412. {
  413.         u16 i;
  414.     u16 HighFirst=0;
  415.     u16 Width,Hight; //圖像的寬度和高度(16位數(shù)據(jù))
  416.     u16 data;

  417.         //0:WORD類型高低位字節(jié)順序與PC相同,1:WORD類型高低位字節(jié)順序與PC相反。                
  418.         if( img[0] & (1<<4) ) // 1,高位在前,低位在后.
  419.     {
  420.        HighFirst=1;
  421.     }
  422.     else              //0,低位在前,高位在后.
  423.     {
  424.        HighFirst=0;
  425.     }        
  426.     if(HighFirst==0)  //高位在后
  427.     {
  428.            Width   = img[3]; //img[3]是圖像寬度高8位,賦值
  429.            Width <<= 8;    //左移8位變成16位數(shù)據(jù)的高8位
  430.            Width  |= img[2];//與低八位數(shù)據(jù)字節(jié)img[2]相或組成16位數(shù)據(jù)
  431.                              
  432.            Hight   = img[5]; //img[5]是圖像寬度高8位,賦值
  433.            Hight <<= 8;    //左移8位變成16位數(shù)據(jù)的高8位
  434.            Hight  |= img[4];//與低八位數(shù)據(jù)字節(jié)img[4]相或組成16位數(shù)據(jù)                  
  435.     }
  436.     else             //高位在前
  437.         {
  438.            Width   = img[2];//img[2]是圖像寬度高8位,賦值   
  439.            Width <<= 8; //左移8位變成16位數(shù)據(jù)的高8位   
  440.            Width  |= img[3];//與低八位數(shù)據(jù)字節(jié)img[3]相或組成16位數(shù)據(jù)
  441.                   
  442.            Hight   = img[4];  //img[4]是圖像寬度高8位,賦值
  443.            Hight <<= 8;     //左移8位變成16位數(shù)據(jù)的高8位
  444.            Hight  |= img[5]; //與低八位數(shù)據(jù)字節(jié)img[5]相或組成16位數(shù)據(jù)
  445.         }         
  446.                                              
  447. /*        if((X_Position+Width)>240||(Y_Position+Hight)>320)//如果寬度與高度超過范圍,則警告
  448.         {
  449.           //可在此寫警告
  450.         }
  451.         else
  452.         {
  453.         }*/
  454.                
  455.         img=img+8;                     //圖片數(shù)據(jù)中 前8個字節(jié)是圖片信息 從第9個開始是圖片像素信息(注意 img是從img[0]開始的,所以第9個字節(jié)是img[8])
  456.     LCD_SetPos( x , y , x+Width-1 , y+Hight-1 );//設(shè)置坐標(biāo)
  457.         LCD_WriteReg(0x0020,x);          //寬度位置坐標(biāo)
  458.         LCD_WriteReg(0x0021,y);          //高度位置坐標(biāo)
  459.         LCD_WriteRAM(data);                  //  開始寫入
  460.     for(i=0; i <Width*Hight; i++)  //  Hight Y_Position是高起始坐標(biāo),Y_Position+Hight是高終止坐標(biāo)
  461.     {                                                                                                             
  462.           if(HighFirst==0)
  463.           {
  464.         data  = *(img++);
  465.                 data |= ((u16)*img)<<8;
  466.                 LCD_WR_REG(data);                                       
  467.         img += 1;       

  468.           }                          
  469.           else                                
  470.           {                                       
  471.                 data  =((u16)*(img++));
  472.                 data |=((u16)*img)<<8;
  473.                 LCD_WR_REG(data);
  474.                 img+=1;                                                  
  475.           }                                                                
  476.    }       

  477. }
復(fù)制代碼

所有資料51hei提供下載:
Desktop.zip (9.42 KB, 下載次數(shù): 29)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 色接久久| 紧缚调教一区二区三区视频 | 日韩精品一区中文字幕 | 国产成人久久精品一区二区三区 | 免费看国产一级特黄aaaa大片 | 国产日韩一区二区三免费高清 | 中文字幕亚洲在线 | 国产精品国产三级国产aⅴ中文 | 夜夜骑av | 天天干天天操天天爽 | 日本a在线 | 久久国产区 | 最新国产在线 | 国产精品 欧美精品 | 国内精品久久久久久久 | 亚洲免费在线视频 | 欧美视频福利 | 久久精品一区二区 | 国产一区在线免费 | av大片 | 精品国产精品三级精品av网址 | 久久av一区二区 | 91麻豆精品国产91久久久更新资源速度超快 | 99久久99久久精品国产片果冰 | 成人动慢 | 亚洲欧美日韩精品 | 日韩不卡一区二区 | 99热免费在线 | 高清国产午夜精品久久久久久 | 免费三级网站 | 亚洲网站在线 | 最新日韩精品 | 欧美a级成人淫片免费看 | 日韩精品区 | 黄在线 | 毛片一级网站 | 中文字幕在线观看精品 | 国产不卡一区在线观看 | 久草青青草 | 亚州精品天堂中文字幕 | 午夜小视频在线观看 |