本人自用STM32驅動2.4寸顯示屏顯示SD卡的圖片,Modbus通信控制顯示文字與圖片
供大家參考
一個完整的工程給大家分享一下,內容如標題所示
顯示屏:采用國產機通用2.4寸顯示屏
主控:STM32F103VET6(100腳)
SD卡座:MINI型。
光藕:TLP-621
電源芯片:初定TD1410+ASM1117
字庫:GT23L32S4W
485接口:未定義
原理圖
[attachment=6]
通信協議為Modbus rtu
主程序如下
/*******************************************************************************
文件名: main.c
說 明: 主程序
作 者: 汪平
版 本: ver1.0
日 期: 2012.07.28
郵箱: gdwhwp@163.com
個人網站: www.gdwhwp.com
*******************************************************************************/
#include "ffnFAT.h"
#include
#include
#include
#include "SPI_SD_driver.h"
#include "stm_res.h"
//#include "LCD_Driver\TFT_Driver.h"
#define COLOR(r,g,b) (((r) << 8) & 0xF800) | (((g) << 3) & 0x7E0) | ((b) >> 3); //r紅色分量 0~31 g綠色分量 0~63 b藍色分量 0~31
#define Picture_Reg RegisterData[0] //第一個寄存是圖片顯示
#define Keyboard_Reg RegisterData[1] //第二個寄存器按鍵狀態
#define WordType_Reg RegisterData[2] //顯示文字字體
#define wordcolor_Reg RegisterData[3] //顯示文字顏色
#define backcolor_Reg RegisterData[4] //顯示文字背景顏色
unsigned char Dev_No; //設備號
struct znFAT_Init_Arg *pArg; //用于指針文件系統參數集合的指針
struct znFAT_Init_Arg Init_Arg_SDCARD; //文件系統參數集合,用于記錄文件系統的重要參數
//調試打開SD卡根目錄下test.txt文件
struct FileInfoStruct FileInfo_FFN; //文件參數集合
unsigned char read_Buffer[512];
char Show_HANZI1[]="顯示屏只是提供參考";
char Show_HANZI2[]="設計者:汪平";
char Show_wangzan[]="gdwhwp.com";
#define Show_String_Size 24
char Show_String[30][40] =
{
/* 1 */ "設計者:汪平",
/* 2 */ "個人網站:gdwhwp.com",
/* 3 */ "QQ:570542229",
/* 4 */ "寫下你要顯示的內容",
/* 5 */ "隨意。。。。",
/* 6 */ "設計者:汪平",
/* 7 */ "個人網站:gdwhwp.com",
/* 8 */ "QQ:570542229",
/* 9 */ "寫下你要顯示的內容",
/* 10 */ "隨意。。。。",
};
unsigned char Picture_Show = 0;
extern unsigned short Key_State;
UINT32 FileTotalSector;
UINT32 counter = 0; //記錄實時讀取扇區
UINT8 flagdebug = 0xff;
UINT8 VOL_VALUE=0x25; //
UINT8 CurrentFileNum = 0;
UINT16 SongFileTotal = 0;
UINT8 UserHitKeyReturn = 0;
struct
{
u16 dcolor; //顯示顏色
u16 bcolor; //背景顏色
u16 type; //字體類型
u16 start_y; //背景顏色
}Show_Word;
///////////////紅外///////////////
extern u8 irnum; //紅外用全局變量
extern volatile u8 irtime;
u8 irpro_ok,irok; //完成標志位
extern UINT8 Uart1_Get_Flag; //串口1接收到數據標志
extern UINT8 Uart1_Get_Data; //串口1接收的數據
extern u8 bSerialDataOK;
extern u16 RegisterData[20];
volatile u8 MP3_irRmote_flag = 0;
volatile u8 g_HiPlayer_Stop_Flag = 0;
volatile u8 g_HiPlayer_Pause_Flag = 0;
extern unsigned short Master_UART_Data;
///////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//函數聲明
void SearchNearestLocationPoint(UINT32 nGps_x,UINT32 nGps_y);
extern void VS1003SendDat(u8 dat);
extern void VS1003_FlushBuffer() ;
extern void VS1003_SetHighSpeedSPI();
extern UINT32 znFAT_Read_File(struct FileInfoStruct *pfi,UINT32 offset,UINT32 len,UINT8 *pbuf);
extern unsigned char const image[3200];
////////////////////////////////////////////////////////////////////////////////////
void Device_Init(void)
{
STM_ChipInResConfigInit();
GPIO_GT23L32S4W_Init();
SD_Init();
//文件系統初始化
pArg=&Init_Arg_SDCARD; //指針指向SD卡文件系統參數集合,znFAT將從這個集合中獲取參數
Dev_No=SDCARD; //設備號為SDCARD,ffnFAT依照此設備號選擇存儲設備驅動 SDCARD 在ffnFAT.h里定義
znFAT_Init(); //初始化FAT32文件系統
}
void Show_String_Sreen(void)
{
GT23L32S4W_Read_ZF_ASC((WordType_Reg+2)*(Picture_Reg-200)+WordType_Reg/2,Show_Word.start_y+WordType_Reg/2,Show_String[Picture_Reg-200],WordType_Reg,wordcolor_Reg,backcolor_Reg);
}
void Display_Bmp_Pic()
{
unsigned int i=0;
unsigned int j=0;
unsigned int k=0;
unsigned int color=0;
// set_window();
LCD_Set_area(0,0,239,319);
for(i=0;i<1920;i++) //讀取76800個點的顏色數據
{
znFAT_Read_File(&FileInfo_FFN,(unsigned long)i*120+54,120,read_Buffer);
for(j=0;j<40;j++)
{
color=COLOR(read_Buffer[2+3*j],read_Buffer[1+3*j],read_Buffer[0+3*j]);
// color = (unsigned int)file_buf[k*2+1]<<8|(unsigned int)file_buf[k*2];
LCD_Write_Data_16(color);
// UART_Put_Num(file_buf);
}
}
Picture_Show = 0;
// MAX485_Putc(9);
//UART_Send_Str("圖像顯示完畢\n");
// FAT32_File_Close(&FileInfo);
//UART_Send_Str("文件已關閉\n");
}
#if 1
//主函數 WHY1S-LSUIR-MCSJV-S6T0S-VQ5IS-2NITP
int main(void)
{
int dly = 0;
unsigned int i,j,k;
unsigned short Color = 0;
unsigned char item=0;
wordcolor_Reg = Show_Word.dcolor = BLUE;
backcolor_Reg = Show_Word.bcolor = WHITE;
Show_Word.type = WordType_Reg = 24;
Show_Word.start_y = 0;
Device_Init();
LCD_IO_Init();
Keyboard_Init();
GPIO_ResetBits(GPIOA, GPIO_Pin_8); //背光
LCD_Init(); //tft初始化
// Clr_Cs; //打開片選使能
Pant(0xffff); //清屏
//初始化串口
USART1_Config(9600);
MAX485_Puts("UART1");
//初始化NVIC
NVIC_Configuration();
show_asc_8X16_str(30,60,"Hello !Wellcom to use",BLUE,WHITE);
GT23L32S4W_Read_ZF_ASC(100,80,Show_wangzan,24,BLUE,WHITE);
GT23L32S4W_Read_ZF_ASC(200,80,Show_HANZI2,24,RED,WHITE);
LCD_Delay_Ms(2000);
GT23L32S4W_Read_ZF_ASC(150,50,Show_HANZI1,24,RED,WHITE);
LCD_Delay_Ms(2000);
Pant(RED); //清屏
LCD_Delay_Ms(1000);
Pant(BLUE); //清屏
LCD_Delay_Ms(1000);
Pant(GREEN); //清屏
LCD_Delay_Ms(2000);
Pant(0xffff); //清屏
//SD_ReadSingleSector(0,read_Buffer);
while(!znFAT_Open_File(&FileInfo_FFN, "\\*.bmp",SongFileTotal,SongFileTotal+1)) //打開U盤根目錄下所有目錄中的第n個
{
SongFileTotal++;
}
//VS1003_SetHighSpeedSPI(); //SPI高速
GPIO_SetBits(GPIOA, GPIO_Pin_2);
while(1)
{
LCD_Delay_Ms(20);
Board_Key_Scan();
if(bSerialDataOK == 1) //03
{
DoWithRevData();
bSerialDataOK = 0;
}
else if(bSerialDataOK == 2) //06
{
bSerialDataOK = 0;
}
if(Picture_Reg >= 100 && Picture_Reg < 200) //圖片
{
CurrentFileNum = Picture_Reg - 100;
if(CurrentFileNum
{
Picture_Show = 1;
Master_UART_Data = 0;
Picture_Reg=0;
}
// MAX485_Putc(1);
}
else if(Picture_Reg >=200) //字
{
Show_String_Sreen();
Picture_Reg=0;
}
while(Picture_Show == 1)
{
if(!znFAT_Open_File(&FileInfo_FFN,"\\*.bmp",CurrentFileNum,SongFileTotal))
{
Display_Bmp_Pic();
}
}
}
}
#endif
字庫程序
har CHR_MSB;
unsigned char CHR_LSB;
CHR_MSB=*P_chr;
if(CHR_MSB<0x80)//ASCII ,半角
{
if( CHR_MSB < 0x20 )
CHR_MSB = 0x20;
CHR_Address = CHR_MSB - 0x20;
if( CHR_type == TYPE_16 ) //8X16 點 ASCII 字符
CHR_Address = CHR_Address*16 + ASC0816YZF_ADDR;
else if( CHR_type == TYPE_24 )//12*24號半角ASCII為點陣信息為16*24
CHR_Address = CHR_Address*48 + ASC1224YZF_ADDR;
else if( CHR_type == TYPE_32 )//16X32 點 ASCII 字符
CHR_Address = CHR_Address*64 + ASC1632YZF_ADDR;
if( CHR_type <=16 )
num = CHR_type;
else
num = 2*CHR_type;
}
else //中文GB2312
{
P_chr++;
CHR_LSB=*P_chr;
num = (CHR_type + CHR_type%8)*CHR_type/8;
//符號區,全角 846+96 = 1038
if( (CHR_MSB>=0xA1 && CHR_MSB <= 0xAB && CHR_LSB>=0xa1)||(CHR_MSB>=0xa8 && CHR_MSB <= 0xa9 && CHR_LSB<0xa1) )
{
if( CHR_MSB>=0xA1 && CHR_MSB <= 0xAB && CHR_LSB>=0xa1 )//雙字節字符1區 846
CHR_Address = (CHR_MSB - 0xA1) * 94 + (CHR_LSB - 0xA1);
else //雙字節字符5區 96
{
if(CHR_LSB>0x7f)
CHR_LSB--;
CHR_Address=(CHR_MSB-0xa8)*96 +(CHR_LSB-0x40)+846;
}
if( CHR_type == TYPE_16 )
CHR_Address = CHR_Address *32 + ZF1616Y_ADDR;
else if( CHR_type == TYPE_24 )
CHR_Address = CHR_Address *72 + ZF2424Y_ADDR;
else if( CHR_type == TYPE_32 )
CHR_Address = CHR_Address *128 + ZF3232Y_ADDR;
else
CHR_Address = 0x000000;
}
//GBK雙字節2區 6768
else if(CHR_MSB>=0xb0 && CHR_MSB <= 0xf7 && CHR_LSB>=0xa1)
{
CHR_Address= (CHR_MSB - 0xB0) * 94 + (CHR_LSB - 0xA1);
if( CHR_type == TYPE_16 )
CHR_Address = (unsigned int)(CHR_Address*32) + (unsigned int)(HZ1616Y_ADDR);
else if( CHR_type == TYPE_24 )
CHR_Address = (unsigned int)((CHR_Address + 846)*72) + 0x68190;
else if( CHR_type == TYPE_32 )
CHR_Address = (unsigned int)((CHR_Address + 846)*128) + 0xEDF00;
else
CHR_Address=0x000000;
}
}
更多內容參考下邊鏈接
http://worldcreativedesign.com/read.php?tid=11
|