在线时间376 小时
UID3135871
注册时间2016-10-9
NXP金币97

TA的每日心情 | 怒 2025-5-29 09:38 |
---|
签到天数: 632 天 连续签到: 1 天 [LV.9]以坛为家II
版主
  
- 积分
- 4470

- 最后登录
- 2025-6-10
|
本帖最后由 胤幻1988 于 2021-9-17 12:35 编辑
中秋转眼快到了!我在这里先祝大家中秋节快乐,阖家团圆!~
废话不多说,上祝福:
本次测试板用LPC845自制版+一个仿ARDUINO ADRFRUIT的一个小的LCD shield 来实现。
LCD驱动芯片ST7735,分辨率128*160 ,使用硬件SPI的方法来驱动LCD及SD卡。
初始化代码使用TOOL工具生成:
引脚配置:
时钟配置:
外设配置:
SPI1
USART0:
导出文件,覆盖原MDK工程同名文件,下面就可以愉快的写程序了。
给出代码:
lcd_init.h
- #ifndef __LCD_INIT_H
- #define __LCD_INIT_H
- #include "sys.h"
- #include "fsl_gpio.h"
- #include "fsl_swm.h"
- #include "pin_mux.h"
- #include "delay.h"
- #define USE_HORIZONTAL 1 //设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
- #if USE_HORIZONTAL==0||USE_HORIZONTAL==1
- #define LCD_W 128
- #define LCD_H 160
- #else
- #define LCD_W 160
- #define LCD_H 128
- #endif
- /* Symbols to be used with GPIO driver */
- #define LCD_SCK_GPIO BOARD_INITPINS_SPI1_SCK_GPIO /*!<@brief GPIO peripheral base pointer */
- #define LCD_SCK_GPIO_PIN_MASK BOARD_INITPINS_SPI1_SCK_GPIO_PIN_MASK /*!<@brief GPIO pin mask */
- #define LCD_SCK_PORT BOARD_INITPINS_SPI1_SCK_PORT /*!<@brief PORT device index: 0 */
- #define LCD_SCK_PIN BOARD_INITPINS_SPI1_SCK_PIN /*!<@brief PORT pin number */
- #define LCD_SCK_PIN_MASK BOARD_INITPINS_SPI1_SCK_PIN_MASK /*!<@brief PORT pin mask */
- /* @} */
- /*! @name PIO0_7 (number 45), SPI1_MISO
- @{ */
- /* Symbols to be used with GPIO driver */
- #define LCD_MISO_GPIO BOARD_INITPINS_SPI1_MISO_GPIO /*!<@brief GPIO peripheral base pointer */
- #define LCD_MISO_GPIO_PIN_MASK BOARD_INITPINS_SPI1_MISO_GPIO_PIN_MASK /*!<@brief GPIO pin mask */
- #define LCD_MISO_PORT BOARD_INITPINS_SPI1_MISO_PORT /*!<@brief PORT device index: 0 */
- #define LCD_MISO_PIN BOARD_INITPINS_SPI1_MISO_PIN /*!<@brief PORT pin number */
- #define LCD_MISO_PIN_MASK BOARD_INITPINS_SPI1_MISO_PIN_MASK /*!<@brief PORT pin mask */
- /* @} */
- /*! @name PIO1_19 (number 44), SPI1_MOSI
- @{ */
- /* Symbols to be used with GPIO driver */
- #define LCD_MOSI_GPIO BOARD_INITPINS_SPI1_MOSI_GPIO /*!<@brief GPIO peripheral base pointer */
- #define LCD_MOSI_GPIO_PIN_MASK BOARD_INITPINS_SPI1_MOSI_GPIO_PIN_MASK /*!<@brief GPIO pin mask */
- #define LCD_MOSI_PORT BOARD_INITPINS_SPI1_MOSI_PORT /*!<@brief PORT device index: 1 */
- #define LCD_MOSI_PIN BOARD_INITPINS_SPI1_MOSI_PIN /*!<@brief PORT pin number */
- #define LCD_MOSI_PIN_MASK BOARD_INITPINS_SPI1_MOSI_PIN_MASK /*!<@brief PORT pin mask */
- /* @} */
- /*! @name PIO1_18 (number 43), SPI1_SSEL0
- @{ */
- /* Symbols to be used with GPIO driver */
- #define LCD_SSEL0_GPIO BOARD_INITPINS_SPI1_SSEL0_GPIO /*!<@brief GPIO peripheral base pointer */
- #define LCD_SSEL0_GPIO_PIN_MASK BOARD_INITPINS_SPI1_SSEL0_GPIO_PIN_MASK /*!<@brief GPIO pin mask */
- #define LCD_SSEL0_PORT BOARD_INITPINS_SPI1_SSEL0_PORT /*!<@brief PORT device index: 1 */
- #define LCD_SSEL0_PIN BOARD_INITPINS_SPI1_SSEL0_PIN /*!<@brief PORT pin number */
- #define LCD_SSEL0_PIN_MASK BOARD_INITPINS_SPI1_SSEL0_PIN_MASK /*!<@brief PORT pin mask */
- /* @} */
- /*! @name PIO0_16 (number 19), TFT_DC
- @{ */
- /* Symbols to be used with GPIO driver */
- #define LCD_DC_GPIO BOARD_INITPINS_TFT_DC_GPIO /*!<@brief GPIO peripheral base pointer */
- #define LCD_DC_GPIO_PIN_MASK BOARD_INITPINS_TFT_DC_GPIO_PIN_MASK /*!<@brief GPIO pin mask */
- #define LCD_DC_PORT BOARD_INITPINS_TFT_DC_PORT /*!<@brief PORT device index: 0 */
- #define LCD_DC_PIN BOARD_INITPINS_TFT_DC_PIN /*!<@brief PORT pin number */
- #define LCD_DC_PIN_MASK BOARD_INITPINS_TFT_DC_PIN_MASK /*!<@brief PORT pin mask */
- /* @} */
- /*! @name PIO1_21 (number 59), SD_CS
- @{ */
- /* Symbols to be used with GPIO driver */
- #define SD_CS_GPIO BOARD_INITPINS_SD_CS_GPIO /*!<@brief GPIO peripheral base pointer */
- #define SD_CS_GPIO_PIN_MASK BOARD_INITPINS_SD_CS_GPIO_PIN_MASK /*!<@brief GPIO pin mask */
- #define SD_CS_PORT BOARD_INITPINS_SD_CS_PORT /*!<@brief PORT device index: 1 */
- #define SD_CS_PIN BOARD_INITPINS_SD_CS_PIN /*!<@brief PORT pin number */
- #define SD_CS_PIN_MASK BOARD_INITPINS_SD_CS_PIN_MASK /*!<@brief PORT pin mask */
- /* @} */
- //-----------------LCD端口定义----------------
- #define LCD_SCLK_Clr() GPIO_PinWrite(LCD_SCK_GPIO, LCD_SCK_PORT, LCD_SCK_PIN, 0)//SCL=SCLK
- #define LCD_SCLK_Set() GPIO_PinWrite(LCD_SCK_GPIO, LCD_SCK_PORT, LCD_SCK_PIN, 1)
- #define LCD_MOSI_Clr() GPIO_PinWrite(LCD_MOSI_GPIO, LCD_MOSI_PORT, LCD_MOSI_PIN, 0)//SDA=MOSI
- #define LCD_MOSI_Set() GPIO_PinWrite(LCD_MOSI_GPIO, LCD_MOSI_PORT, LCD_MOSI_PIN, 1)
- #define LCD_RES_Clr() delay_us(1)//GPIO_PinWrite(LCD_SCK_GPIO, LCD_SCK_PORT, LCD_SCK_PIN, 0)//RES
- #define LCD_RES_Set() delay_us(1)//GPIO_PinWrite(LCD_SCK_GPIO, LCD_SCK_PORT, LCD_SCK_PIN, 1)
- #define LCD_DC_Clr() GPIO_PinWrite(LCD_DC_GPIO, LCD_DC_PORT, LCD_DC_PIN, 0)//DC
- #define LCD_DC_Set() GPIO_PinWrite(LCD_DC_GPIO, LCD_DC_PORT, LCD_DC_PIN, 1)
-
- #define LCD_CS_Clr() GPIO_PinWrite(LCD_SSEL0_GPIO, LCD_SSEL0_PORT, LCD_SSEL0_PIN, 0)//CS
- #define LCD_CS_Set() GPIO_PinWrite(LCD_SSEL0_GPIO, LCD_SSEL0_PORT, LCD_SSEL0_PIN, 1)
- #define LCD_BLK_Clr() delay_us(1)//GPIO_PinWrite(LCD_SCK_GPIO, LCD_SCK_PORT, LCD_SCK_PIN, 0)//BLK
- #define LCD_BLK_Set() delay_us(1)//GPIO_PinWrite(LCD_SCK_GPIO, LCD_SCK_PORT, LCD_SCK_PIN, 1)
- #define SD_CS_Clr() GPIO_PinWrite(SD_CS_GPIO, SD_CS_PORT, SD_CS_PIN, 0)//CS
- #define SD_CS_Set() GPIO_PinWrite(SD_CS_GPIO, SD_CS_PORT, SD_CS_PIN, 1)
- u8 spi1_write_read_byte(u8 writedata);
- 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初始化
- #endif
复制代码
lcd_init.c
lcd.h
- #ifndef __LCD_H
- #define __LCD_H
- #include "sys.h"
- #include <math.h>
- #include <stdlib.h>
- 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_ShowChinese12x12(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode);//显示单个12x12汉字
- 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[]);//显示图片
- //画笔颜色
- #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 //浅棕蓝色(选择条目的反色)
- #endif
复制代码
lcd.c
- #include "lcd.h"
- #include "lcd_init.h"
- #include "lcdfont.h"
- #include "delay.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_y;}
- 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==12) LCD_ShowChinese12x12(x,y,s,fc,bc,sizey,mode);
- else 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;
- }
- }
- /******************************************************************************
- 函数说明:显示单个12x12汉字
- 入口数据:x,y显示坐标
- *s 要显示的汉字
- fc 字的颜色
- bc 字的背景色
- sizey 字号
- mode: 0非叠加模式 1叠加模式
- 返回值: 无
- ******************************************************************************/
- void LCD_ShowChinese12x12(u16 x,u16 y,u8 *s,u16 fc,u16 bc,u8 sizey,u8 mode)
- {
- u8 i,j,m=0;
- u16 k;
- u16 HZnum;//汉字数目
- u16 TypefaceNum;//一个字符所占字节大小
- u16 x0=x;
- TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
-
- HZnum=sizeof(tfont12)/sizeof(typFNT_GB12); //统计汉字数目
- for(k=0;k<HZnum;k++)
- {
- if((tfont12[k].Index[0]==*(s))&&(tfont12[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(tfont12[k].Msk[i]&(0x01<<j))LCD_WR_DATA(fc);
- else LCD_WR_DATA(bc);
- m++;
- if(m%sizey==0)
- {
- m=0;
- break;
- }
- }
- else//叠加方式
- {
- if(tfont12[k].Msk[i]&(0x01<<j)) LCD_DrawPoint(x,y,fc);//画一个点
- x++;
- if((x-x0)==sizey)
- {
- x=x0;
- y++;
- break;
- }
- }
- }
- }
- }
- continue; //查找到对应点阵字库立即退出,防止多个汉字重复取模带来影响
- }
- }
- /******************************************************************************
- 函数说明:显示单个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,m=0;
- u16 k;
- u16 HZnum;//汉字数目
- u16 TypefaceNum;//一个字符所占字节大小
- u16 x0=x;
- TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
- 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);
- m++;
- if(m%sizey==0)
- {
- m=0;
- break;
- }
- }
- 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,m=0;
- u16 k;
- u16 HZnum;//汉字数目
- u16 TypefaceNum;//一个字符所占字节大小
- u16 x0=x;
- TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
- 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);
- m++;
- if(m%sizey==0)
- {
- m=0;
- break;
- }
- }
- 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,m=0;
- u16 k;
- u16 HZnum;//汉字数目
- u16 TypefaceNum;//一个字符所占字节大小
- u16 x0=x;
- TypefaceNum=(sizey/8+((sizey%8)?1:0))*sizey;
- 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);
- m++;
- if(m%sizey==0)
- {
- m=0;
- break;
- }
- }
- 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,m=0;
- u16 i,TypefaceNum;//一个字符所占字节大小
- u16 x0=x;
- sizex=sizey/2;
- TypefaceNum=(sizex/8+((sizex%8)?1:0))*sizey;
- num=num-' '; //得到偏移后的值
- LCD_Address_Set(x,y,x+sizex-1,y+sizey-1); //设置光标位置
- for(i=0;i<TypefaceNum;i++)
- {
- if(sizey==12)temp=ascii_1206[num][i]; //调用6x12字体
- else if(sizey==16)temp=ascii_1608[num][i]; //调用8x16字体
- else if(sizey==24)temp=ascii_2412[num][i]; //调用12x24字体
- 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);
- m++;
- if(m%sizex==0)
- {
- m=0;
- break;
- }
- }
- 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;
- u32 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++;
- }
- }
- }
复制代码
sd.h
- #ifndef _SD_H__
- #define _SD_H__
- #include <stdio.h>
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "lcd_init.h"
- #include "ff.h"
- #include "diskio.h"
- #include "fatfs_storage.h"
-
- #define SD_TYPE_ERR 0X00
- #define SD_TYPE_MMC 0X01
- #define SD_TYPE_V1 0X02
- #define SD_TYPE_V2 0X04
- #define SD_TYPE_V2HC 0X06
-
- #define CMD0 0
- #define CMD1 1
- #define CMD8 8
- #define CMD9 9
- #define CMD10 10
- #define CMD12 12
- #define CMD16 16
- #define CMD17 17
- #define CMD18 18
- #define CMD23 23
- #define CMD24 24
- #define CMD25 25
- #define CMD41 41
- #define CMD55 55
- #define CMD58 58
- #define CMD59 59
- #define MSD_DATA_OK 0x05
- #define MSD_DATA_CRC_ERROR 0x0B
- #define MSD_DATA_WRITE_ERROR 0x0D
- #define MSD_DATA_OTHER_ERROR 0xFF
- #define MSD_RESPONSE_NO_ERROR 0x00
- #define MSD_IN_IDLE_STATE 0x01
- #define MSD_ERASE_RESET 0x02
- #define MSD_ILLEGAL_COMMAND 0x04
- #define MSD_COM_CRC_ERROR 0x08
- #define MSD_ERASE_SEQUENCE_ERROR 0x10
- #define MSD_ADDRESS_ERROR 0x20
- #define MSD_PARAMETER_ERROR 0x40
- #define MSD_RESPONSE_FAILURE 0xFF
- #define MAX_BMP_FILES 25
- #define __SD_CS_SET() SD_CS_Set()
- #define __SD_CS_CLR() SD_CS_Clr()
- #define __SD_WRITE_BYTE(__DATA) spi1_write_read_byte(__DATA)
- extern uint8_t SD_Type;
- void SD_port_init(void);
- uint8_t SD_SPI_ReadWriteByte(uint8_t data);
- void SD_SPI_SpeedLow(void);
- void SD_SPI_SpeedHigh(void);
- uint8_t SD_WaitReady(void);
- uint8_t SD_GetResponse(uint8_t Response);
- uint8_t SD_Initialize(void);
- uint8_t SD_ReadDisk(uint8_t*buf,uint32_t sector,uint8_t cnt);
- uint8_t SD_WriteDisk(uint8_t*buf,uint32_t sector,uint8_t cnt);
- uint32_t SD_GetSectorCount(void);
- uint8_t SD_GetCID(uint8_t *cid_data);
- uint8_t SD_GetCSD(uint8_t *csd_data);
-
- #endif
复制代码
sd.c
- #include "sd.h"
- #include "peripherals.h"
-
- uint8_t SD_Type=0; //version of the sd card
- void SD_port_init(void)
- {
- __SD_CS_SET();
- }
- //data: data to be written to sd card.
- //return: data read from sd card.
- uint8_t SD_SPI_ReadWriteByte(uint8_t data)
- {
- return __SD_WRITE_BYTE(data);
- }
- //set spi in low speed mode.
- void SD_SPI_SpeedLow(void)
- {
- // SPI1->CR1&=0XFFC7;
- // SPI1->CR1|=SPI_BaudRatePrescaler_256;
- //SPI_Cmd(SPI1,ENABLE);
- }
- //set spi in high speed mode.
- void SD_SPI_SpeedHigh(void)
- {
- // SPI1->CR1&=0XFFC7;
- // SPI1->CR1|=SPI_BaudRatePrescaler_32;
- //SPI_Cmd(SPI1,ENABLE);
- }
- //released spi bus
- void SD_DisSelect(void)
- {
- __SD_CS_SET();
- SD_SPI_ReadWriteByte(0xff);//providing extra 8 clocks
- }
- //pick sd card and waiting until until it's ready
- //return: 0: succed 1: failure
- uint8_t SD_Select(void)
- {
- __SD_CS_CLR();
- if(SD_WaitReady()==0)return 0;
- SD_DisSelect();
- return 1;
- }
- //waiting for sd card until it's ready
- uint8_t SD_WaitReady(void)
- {
- uint32_t t=0;
- do
- {
- if(SD_SPI_ReadWriteByte(0XFF)==0XFF)return 0;
- t++;
- }while(t<0XFFFFFF);
- return 1;
- }
- //waiting for response from sd card.
- //Response: expect from sd card.
- //return: succeed for 0, fail for other else
- //return: 0 for success, other for failure.
- uint8_t SD_GetResponse(uint8_t Response)
- {
- uint16_t Count=0xFFFF;
- while ((SD_SPI_ReadWriteByte(0XFF)!=Response)&&Count)Count--;
- if (Count==0)return MSD_RESPONSE_FAILURE;
- else return MSD_RESPONSE_NO_ERROR;
- }
- //read a buffer from sd card.
- //*buf: pointer to a buffer.
- //len: length of the buffer.
- //return: 0 for success, other for failure.
- uint8_t SD_RecvData(uint8_t*buf,uint16_t len)
- {
- if(SD_GetResponse(0xFE))return 1;//waiting for start command send back from sd card.
- while(len--)//receiving data...
- {
- *buf=__SD_WRITE_BYTE(0xFF);
- buf++;
- }
- //send 2 dummy write (dummy CRC)
- SD_SPI_ReadWriteByte(0xFF);
- SD_SPI_ReadWriteByte(0xFF);
- return 0;
- }
- //write a buffer containing 512 bytes to sd card.
- //buf: data buffer
- //cmd: command
- //return: 0 for success, other for failure.
- uint8_t SD_SendBlock(uint8_t*buf,uint8_t cmd)
- {
- uint16_t t;
- if(SD_WaitReady())return 1;
- SD_SPI_ReadWriteByte(cmd);
- if(cmd!=0XFD)
- {
- for(t=0;t<512;t++)__SD_WRITE_BYTE(buf[t]);
- SD_SPI_ReadWriteByte(0xFF);//ignoring CRC
- SD_SPI_ReadWriteByte(0xFF);
- t=SD_SPI_ReadWriteByte(0xFF);
- if((t&0x1F)!=0x05)return 2;
- }
- return 0;
- }
- //send a command to sd card
- //cmd??command
- //arg: parameter
- //crc: crc
- //return: response sent back from sd card.
- uint8_t SD_SendCmd(uint8_t cmd, uint32_t arg, uint8_t crc)
- {
- uint8_t r1;
- uint8_t Retry=0;
- SD_DisSelect();
- if(SD_Select())return 0XFF;
- SD_SPI_ReadWriteByte(cmd | 0x40);
- SD_SPI_ReadWriteByte(arg >> 24);
- SD_SPI_ReadWriteByte(arg >> 16);
- SD_SPI_ReadWriteByte(arg >> 8);
- SD_SPI_ReadWriteByte(arg);
- SD_SPI_ReadWriteByte(crc);
- if(cmd==CMD12)SD_SPI_ReadWriteByte(0xff); //Skip a stuff byte when stop reading
- Retry=0X1F;
- do
- {
- r1=SD_SPI_ReadWriteByte(0xFF);
- }while((r1&0X80) && Retry--);
- return r1;
- }
- //obtain CID including manufacturer informationfrom sd card
- //*cid_dat: pointer to the buffer storing CID, at least 16 bytes.
- //return: 0 no error 1 error
- uint8_t SD_GetCID(uint8_t *cid_data)
- {
- uint8_t r1;
- r1=SD_SendCmd(CMD10,0,0x01);
- if(r1==0x00)
- {
- r1=SD_RecvData(cid_data,16);
- }
- SD_DisSelect();
- if(r1)return 1;
- else return 0;
- }
- //obtain CSD including storage and speed.
- //*csd_data : pointer to the buffer storing CSD, at least 16 bytes.
- //return: 0 no error 1 error
- uint8_t SD_GetCSD(uint8_t *csd_data)
- {
- uint8_t r1;
- r1=SD_SendCmd(CMD9,0,0x01);//??CMD9??????CSD send CMD9 in order to get CSD
- if(r1==0)
- {
- r1=SD_RecvData(csd_data, 16);
- }
- SD_DisSelect();
- if(r1)return 1;
- else return 0;
- }
- //obtian the totals of sectors of sd card.
- //return: 0 error, other else for storage of sd card.
- //numbers of bytes of each sector must be 512, otherwise fail to initialization.
- uint32_t SD_GetSectorCount(void)
- {
- uint8_t csd[16];
- uint32_t Capacity;
- uint8_t n;
- uint16_t csize;
-
- if(SD_GetCSD(csd)!=0) return 0;
- //calculation for SDHC below
- if((csd[0]&0xC0)==0x40) //V2.00
- {
- csize = csd[9] + ((uint16_t)csd[8] << 8) + 1;
- Capacity = (uint32_t)csize << 10; //totals of sectors
- }else//V1.XX
- {
- n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
- csize = (csd[8] >> 6) + ((uint16_t)csd[7] << 2) + ((uint16_t)(csd[6] & 3) << 10) + 1;
- Capacity= (uint32_t)csize << (n - 9);
- }
- return Capacity;
- }
- //initialize sd card
- uint8_t SD_Initialize(void)
- {
- uint8_t r1;
- uint16_t retry;
- uint8_t buf[4];
- uint16_t i;
-
- __SD_CS_SET();
- SD_SPI_SpeedLow();
- for(i=0;i<10;i++)SD_SPI_ReadWriteByte(0XFF);
- retry=20;
- do
- {
- r1=SD_SendCmd(CMD0,0,0x95);//enter to idle state
- }while((r1!=0X01) && retry--);
- SD_Type=0;
-
- if(r1==0X01)
- {
- if(SD_SendCmd(CMD8,0x1AA,0x87)==1)//SD V2.0
- {
- for(i=0;i<4;i++)buf[i]=SD_SPI_ReadWriteByte(0XFF); //Get trailing return value of R7 resp
- if(buf[2]==0X01&&buf[3]==0XAA)//is it support of 2.7~3.6V
- {
- retry=0XFFFE;
- do
- {
- SD_SendCmd(CMD55,0,0X01);
- r1=SD_SendCmd(CMD41,0x40000000,0X01);
- }while(r1&&retry--);
- if(retry&&SD_SendCmd(CMD58,0,0X01)==0) //start to identify the SD2.0 version of sd card.
- {
- for(i=0;i<4;i++)buf[i]=SD_SPI_ReadWriteByte(0XFF);//get OCR
- if(buf[0]&0x40)SD_Type=SD_TYPE_V2HC; //check CCS
- else SD_Type=SD_TYPE_V2;
- }
- }
- }else//SD V1.x/ MMC V3
- {
- SD_SendCmd(CMD55,0,0X01);
- r1=SD_SendCmd(CMD41,0,0X01);
- if(r1<=1)
- {
- SD_Type=SD_TYPE_V1;
- retry=0XFFFE;
- do //exit idle state
- {
- SD_SendCmd(CMD55,0,0X01);
- r1=SD_SendCmd(CMD41,0,0X01);
- }while(r1&&retry--);
- }else
- {
- SD_Type=SD_TYPE_MMC;//MMC V3
- retry=0XFFFE;
- do
- {
- r1=SD_SendCmd(CMD1,0,0X01);
- }while(r1&&retry--);
- }
- if(retry==0||SD_SendCmd(CMD16,512,0X01)!=0)SD_Type=SD_TYPE_ERR;
- }
- }
- SD_DisSelect();
- SD_SPI_SpeedHigh();
- if(SD_Type)return 0;
- else if(r1)return r1;
- return 0xaa;
- }
- //read SD card
- //buf: data buffer
- //sector: sector
- //cnt: totals of sectors]
- //return: 0 ok, other for failure
- uint8_t SD_ReadDisk(uint8_t*buf,uint32_t sector,uint8_t cnt)
- {
- uint8_t r1;
- if(SD_Type!=SD_TYPE_V2HC)sector <<= 9;
- if(cnt==1)
- {
- r1=SD_SendCmd(CMD17,sector,0X01);
- if(r1==0)
- {
- r1=SD_RecvData(buf,512);
- }
- }else
- {
- r1=SD_SendCmd(CMD18,sector,0X01);
- do
- {
- r1=SD_RecvData(buf,512);
- buf+=512;
- }while(--cnt && r1==0);
- SD_SendCmd(CMD12,0,0X01);
- }
- SD_DisSelect();
- return r1;//
- }
- //write sd card
- //buf: data buffer
- //sector: start sector
- //cnt: totals of sectors]
- //return: 0 ok, other for failure
- uint8_t SD_WriteDisk(uint8_t*buf,uint32_t sector,uint8_t cnt)
- {
- uint8_t r1;
- if(SD_Type!=SD_TYPE_V2HC)sector *= 512;
- if(cnt==1)
- {
- r1=SD_SendCmd(CMD24,sector,0X01);
- if(r1==0)
- {
- r1=SD_SendBlock(buf,0xFE);
- }
- }else
- {
- if(SD_Type!=SD_TYPE_MMC)
- {
- SD_SendCmd(CMD55,0,0X01);
- SD_SendCmd(CMD23,cnt,0X01);
- }
- r1=SD_SendCmd(CMD25,sector,0X01);
- if(r1==0)
- {
- do
- {
- r1=SD_SendBlock(buf,0xFC);
- buf+=512;
- }while(--cnt && r1==0);
- r1=SD_SendBlock(0,0xFD);
- }
- }
- SD_DisSelect();
- return r1;
- }
复制代码 其中还移植了FATFS系统,通过FATFS来软解BMP图片。FATFS具体移植就不多说了,BMP解码关键程序:
- #include "sys.h"
- #include <string.h>
- #include "sd.h"
- #include "lcd.h"
- #include "lcd_init.h"
- /** @addtogroup STM32_Nucleo_Demo
- * @{
- */
- /** @defgroup STORAGE
- * @brief This file includes the Storage (FatFs) driver for the STM32 Nucleo demo
- * @{
- */
- /** @defgroup STORAGE_Private_Types
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STORAGE_Private_Defines
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STORAGE_Private_Macros
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STORAGE_Private_Variables
- * @{
- */
- uint8_t aBuffer[960];
- FILINFO MyFileInfo;
- DIR MyDirectory;
- FIL MyFile;
- UINT BytesWritten;
- UINT BytesRead;
- /**
- * @}
- */
- /** @defgroup STORAGE_Private_FunctionPrototypes
- * @{
- */
- /**
- * @}
- */
- /** @defgroup STORAGE_Private_Functions
- * @{
- */
- /**
- * @brief Open a file and copy its content to a buffer
- * @param DirName: the Directory name to open
- * @param FileName: the file name to open
- * @param BufferAddress: A pointer to a buffer to copy the file to
- * @param FileLen: the File length
- * @retval err: Error status (0=> success, 1=> fail)
- */
- //#define LCD_W 128
- //#define LCD_H 160
- #define PIXEL(__M) ((((__M) + 31 ) >> 5) << 2)
- uint32_t Storage_OpenReadFile(uint8_t Xpoz, uint16_t Ypoz,uint8_t pic_w, uint8_t pic_h,const char* BmpName)
- {
- uint16_t i, j, k;
-
- uint32_t index = 0, size = 0, width = 0, height = 0;
- uint32_t bmpaddress, bit_pixel = 0;
- FIL file1;
- uint16_t color = 0;
-
- uint16_t pic_h_t=0,pic_w_t=0; //实际位置为起点的长宽的终点位置 不能超过屏幕长宽最大值
- f_open(&file1, BmpName, FA_READ);
- f_read(&file1, aBuffer, 30, &BytesRead);
- bmpaddress = (uint32_t)aBuffer;
- /* Read bitmap size */
- size = *(uint16_t *) (bmpaddress + 2);
- size |= (*(uint16_t *) (bmpaddress + 4)) << 16;
- /* Get bitmap data address offset */
- index = *(uint16_t *) (bmpaddress + 10);
- index |= (*(uint16_t *) (bmpaddress + 12)) << 16;
- /* Read bitmap width */
- width = *(uint16_t *) (bmpaddress + 18);
- width |= (*(uint16_t *) (bmpaddress + 20)) << 16;
- /* Read bitmap height */
- height = *(uint16_t *) (bmpaddress + 22);
- height |= (*(uint16_t *) (bmpaddress + 24)) << 16;
- /* Read bit/pixel */
- bit_pixel = *(uint16_t *) (bmpaddress + 28);
- f_close (&file1);
- if (24 != bit_pixel) {
- return 0;
- }
-
- /* Synchronize f_read right in front of the image data */
- f_open (&file1, (TCHAR const*)BmpName, FA_READ);
- f_read(&file1, aBuffer, index, &BytesRead);
-
- for (i = 0; i < pic_h; i ++) {
- f_read(&file1, aBuffer, PIXEL(pic_w * bit_pixel) >> 1, (UINT *)&BytesRead);
- f_read(&file1, aBuffer + (PIXEL(pic_w * bit_pixel) >> 1), PIXEL(pic_w * bit_pixel) >> 1, (UINT *)&BytesRead);
-
- //lcd_set_cursor(Xpoz + 0, Ypoz + i);
- //LCD_Address_Set(Xpoz + 0, Ypoz + i,Xpoz + 0, Ypoz + i);
- //计算绘图区域的实际终点
- pic_h_t=Ypoz + i; if(pic_h_t>=LCD_H) pic_h_t=LCD_H;
- pic_w_t=Xpoz+pic_w; if(pic_w_t>=LCD_W) pic_w_t=LCD_W;
- LCD_Address_Set(Xpoz + 0, Ypoz + i,pic_w_t, pic_h_t);
- //lcd_write_byte(0x22, LCD_CMD);
- //LCD_WR_REG(0X22);
- for (j = 0; j < pic_w; j ++) {
- k = j * 3;
- color = (uint16_t)(((aBuffer[k + 2] >> 3) << 11 ) | ((aBuffer[k + 1] >> 2) << 5) | (aBuffer[k] >> 3));
- //lcd_draw_point(Xpoz + j, Ypoz + i, color);
- LCD_WR_DATA(color);
-
- }
- }
-
- f_close(&file1);
-
- return 1;
- }
- /**
- * @brief Copy file BmpName1 to BmpName2
- * @param BmpName1: the source file name
- * @param BmpName2: the destination file name
- * @retval err: Error status (0=> success, 1=> fail)
- */
- uint32_t Storage_CopyFile(const char* BmpName1, const char* BmpName2)
- {
- uint32_t index = 0;
- FIL file1, file2;
-
- /* Open an Existent BMP file system */
- f_open(&file1, BmpName1, FA_READ);
- /* Create a new BMP file system */
- f_open(&file2, BmpName2, FA_CREATE_ALWAYS | FA_WRITE);
-
- do
- {
- f_read(&file1, aBuffer, _MAX_SS, &BytesRead);
- f_write(&file2, aBuffer, _MAX_SS, &BytesWritten);
- index+= _MAX_SS;
-
- } while(index < file1.fsize);
-
- f_close(&file1);
- f_close(&file2);
-
- return 1;
- }
- /**
- * @brief Opens a file and copies its content to a buffer.
- * @param DirName: the Directory name to open
- * @param FileName: the file name to open
- * @param BufferAddress: A pointer to a buffer to copy the file to
- * @param FileLen: File length
- * @retval err: Error status (0=> success, 1=> fail)
- */
- uint32_t Storage_CheckBitmapFile(const char* BmpName, uint32_t *FileLen)
- {
- uint32_t err = 0;
- if(f_open(&MyFile, BmpName, FA_READ) != FR_OK)
- {
- err = 2;
- }
-
- return err;
- }
- /**
- * @brief List up to 25 file on the root directory with extension .BMP
- * @param DirName: Directory name
- * @param Files: Buffer to contain read files
- * @retval The number of the found files
- */
- uint32_t Storage_GetDirectoryBitmapFiles(const char* DirName, char* Files[])
- {
- uint32_t i = 0, j = 0;
- FRESULT res;
-
- res = f_opendir(&MyDirectory, DirName);
-
- if(res == FR_OK)
- {
- i = strlen(DirName);
- for (;;)
- {
- res = f_readdir(&MyDirectory, &MyFileInfo);
- if(res != FR_OK || MyFileInfo.fname[0] == 0) break;
- if(MyFileInfo.fname[0] == '.') continue;
-
- if(!(MyFileInfo.fattrib & AM_DIR))
- {
- do
- {
- i++;
- }
- while (MyFileInfo.fname[i] != 0x2E);
-
-
- if(j < MAX_BMP_FILES)
- {
- if((MyFileInfo.fname[i + 1] == 'B') && (MyFileInfo.fname[i + 2] == 'M') && (MyFileInfo.fname[i + 3] == 'P'))
- {
- sprintf(Files[j], "%-11.11s", MyFileInfo.fname);
- j++;
- }
- }
- i = 0;
- }
- }
- }
-
- return j;
- }
- /**
- * @brief Compares two buffers.
- * @param pBuffer1, pBuffer2: buffers to be compared
- * @param BufferLength: buffer's length
- * @retval 0: pBuffer1 identical to pBuffer2
- * 1: pBuffer1 differs from pBuffer2
- */
- uint8_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
- {
- uint8_t ret = 1;
- while (BufferLength--)
- {
- if(*pBuffer1 != *pBuffer2)
- {
- ret = 0;
- }
-
- pBuffer1++;
- pBuffer2++;
- }
-
- return ret;
- }
复制代码
下载,查看屏幕:
|
|