硬件准备
ADSP-EDU-BF533:BF533开发板
AD-HP530ICE:ADI DSP仿真器
软件准备
Visual DSP++软件
硬件链接
功能介绍
代码实现了读取 SD 卡中“/txt/test.txt”路径下的 TXT 文件,将 TXT 文件内容显示到液晶屏上,通过按键“Lift->UP”和“Lift->Down”进行上下翻页。
代码读取 TXT 文档后,会根据读取的数据信息,将数据信息及格式进行判断,将内容通过调用字库显示到液晶屏上,并将每一页的地址保存在页数组中,在上下翻页时,会根据保存的信息找到以前的页面。
代码使用说明
代码主要调用了文件系统函数和 TXT 文档解析函数,文件系统将 SD 卡内指定路径下的测试文件打开并读取,TXT 解析函数将数据进行解析,并将相应的文字调入液晶显示内存中,并做了自动换行、换页、向上翻页的机制。
display_txt(unsigned char *pbuff,WORD len,int color)
将 pbuff 指针指向的地址数据调用并解析显示,调用长度参数为 len,颜色参数为 color。
代码实验步骤
- 将 SD 卡根目录下的 TXT 文件夹中,保存一个叫做”test.txt”的文件,文件中写入要显示的内容或小说。
- 将 SD 卡插入开发板 SD 卡接口。
- 编译并运行代码,待提示“开始阅读电子书,按按键翻页查看”时,按下“Lift->Down”按键,通过“Lift->Down”
和“Lift->UP”按键可进行上下翻页。
代码实验结果
在液晶屏上可以看到 test.txt 文件的文字内容。
程序源码
cpu.c
#include <cdefBF533.h>
void Set_PLL(int pmsel,int pssel)
{
int new_PLL_CTL;
*pPLL_DIV = pssel;
asm(“ssync;”);
new_PLL_CTL = (pmsel & 0x3f) << 9;
*pSIC_IWR |= 0xffffffff;
if (new_PLL_CTL != *pPLL_CTL)
{
*pPLL_CTL = new_PLL_CTL;
asm(“ssync;”);
asm(“idle;”);
}
}
void Init_SDRAM(void)
{
*pEBIU_SDRRC = 0x00000817;
*pEBIU_SDBCTL = 0x00000013;
*pEBIU_SDGCTL = 0x0091998d;
ssync();
}
void Init_EBIU(void)
{
*pEBIU_AMBCTL0 = 0x7bb07bb0;
*pEBIU_AMBCTL1 = 0x7bb07bb0;
*pEBIU_AMGCTL = 0x000f;
}
void Init_Timers0(int dat)
{
*pTIMER0_CONFIG = 0x0019;
*pTIMER0_WIDTH = dat;
*pTIMER0_PERIOD = 2000;
}
void Enable_Timers0(void)
{
*pTIMER_ENABLE|= 0x0001;
asm(“ssync;”);
}
void Disable_Timers0(void)
{
*pTIMER_DISABLE |= 0x0001;
}
void delay(volatile int tem)
{
volatile int i;
while(tem–)
for(i=6; i>0; i–);
}
diskio.c
#include “diskio.h”
#include “stdio.h”
#include <cdefBF533.h>
void MMC_HardwareInitial(void)
{
*pSPI_FLG = FLS2;
*pSPI_BAUD = 550;
*pSPI_CTL = 0x0000;
*pSPI_CTL = 0x0001 | MSTR ;
*pSPI_CTL = (*pSPI_CTL | SPE);
}
DSTATUS disk_initialize ()
{
MMC_HardwareInitial();
MMC_Init();
return 0;
}
DSTATUS disk_shutdown ()
{
return 0;
}
DSTATUS disk_status ()
{
return 0;
}
DRESULT disk_read (BYTE *buff, DWORD sect, BYTE cnt)//cnt = sector num
{
// fseek(fp ,sect, SEEK_SET);
// fread(buff, 512, cnt, fp);
while(cnt--){
MMC_read_sector(sect,buff);
sect += 1;
buff += 512;
}
return 0;
}
#ifndef _READONLY
DRESULT disk_write (const BYTE* buff, DWORD sect, BYTE cnt)
{
// fseek(fp ,sect, SEEK_SET);
// fwrite(buff, 512, cnt, fp);
while(cnt–){
MMC_Write_sector(sect,buff);
sect += 1;
buff += 512;
}
return 0;
}
#endif
tftlcd.c
#include <cdefBF533.h>
section(“sdram0_bank1”) unsigned char DisplayBuffer[272][1440] ;
section(“sdram0_bank1”) unsigned char DisplayBuffer_565[272][1440] ;
section(“sdram0_bank1”) unsigned char TempBuffer_img[272][1440] ;
section(“sdram0_bank1”) unsigned char Inputdata[391734];
void InitDMA(void)
{
int addr;
addr = &DisplayBuffer_565;
addr -= 1920;
*pDMA0_START_ADDR = addr;
*pDMA0_X_COUNT = 480;
*pDMA0_X_MODIFY = 2;
*pDMA0_Y_COUNT = 286;
*pDMA0_Y_MODIFY = 2;
*pDMA0_CONFIG = 0x1034;
}
void InitPPI(void)
{
*pPPI_CONTROL = 0x781e;
*pPPI_DELAY = 0;
*pPPI_COUNT = 479;
*pPPI_FRAME = 286;
}
void InitTimer(void)
{
*pTIMER1_PERIOD = 525;
*pTIMER1_WIDTH = 41;
*pTIMER1_CONFIG = 0x00a9;
*pTIMER2_PERIOD = 150150;
*pTIMER2_WIDTH = 5250;
*pTIMER2_CONFIG = 0x00a9;
}
void PPI_TMR_DMA_Enable(void)
{
*pDMA0_CONFIG |= 0x1;
asm(“ssync;”);
InitTimer();
*pPPI_CONTROL |= 0x1;
asm(“ssync;”);
*pTIMER_ENABLE|= 0x0006;
asm(“ssync;”);
}
void PPI_TMR_DMAR_Disable(void)
{
*pDMA0_CONFIG &= (~0x1);
*pPPI_CONTROL &= (~0x1);
}
void bgrtorgb24(void)
{
int i,j;
int a,b,c;
for(i=0;i<272;i++)
{
for(j=0;j<1440;j++)
{
TempBuffer_img[i][j] = Inputdata[i1440+j+54];
}
}
for(i=0;i<272;i++)
{
for(j=0;j<480;j++)
{
a = TempBuffer_img[i][j3];
b = TempBuffer_img[i][j3+1];
c = TempBuffer_img[i][j3+2];
TempBuffer_img[i][j3] = c;
TempBuffer_img[i][j3+1] = b;
TempBuffer_img[i][j*3+2] = a;
}
}
for(i=0;i<272;i++)
{
for(j=0;j<1440;j++)
{
DisplayBuffer[i][j] = (TempBuffer_img[271-i][j]);
}
}
}
void color_bar(void)
{
int i,j;
for(i=0;i<272;i++)
{
for(j=0;j<40;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0x00;
}
for(j=40;j<80;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0x00; DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0x00;//red
}
for(j=80;j<120;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x00;//green
}
for(j=120;j<160;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0xff;//blue
}
for(j=160;j<200;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x0;//red+green
}
for(j=200;j<240;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0xff;//red+blue
}
for(j=240;j<280;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0xff;//green+blue
}
for(j=280;j<320;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0xff;
}
for(j=320;j<360;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x00;//green
}
for(j=360;j<400;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0x00;
DisplayBuffer[i][j*3+2] = 0xff;//blue
}
for(j=400;j<440;j++)
{
DisplayBuffer[i][j*3+0] = 0xff;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0x0;//red+green
}
for(j=440;j<480;j++)
{
DisplayBuffer[i][j*3+0] = 0x00;
DisplayBuffer[i][j*3+1] = 0xff;
DisplayBuffer[i][j*3+2] = 0xff;//red+blue
}
}
}
void RGB888_RGB565(unsigned char *src, int src_len, unsigned char *dst)
{
int i = 0;
int j = 0;
if (src_len % 3 != 0)
{
return;
}
for (i = 0; i < src_len; i += 3)
{
dst[j+1] = src[i+2] &0xf8; //B
dst[j+1] |= ((src[i+1]>>5) & 0x07); //GH
dst[j] = ((src[i+1]<<3) & 0xe0); //GL
dst[j] |= ((src[i]>>3) &0x1f); //R
j += 2;
}
}
ziku.c
#include “ascii16_8.h”
#include “hzk16.h”
extern unsigned char DisplayBuffer[272][1440];
void PutPixel(int x,int y,unsigned int color)
{
if((x<480)&&(y<272))
{
DisplayBuffer[y][3x] = (color>>16)&0xff;
DisplayBuffer[y][3x+1] = (color>>8)&0xff;
DisplayBuffer[y][3*x+2] = (color)&0xff;
}
}
/以特定颜色填充一块区域/
void Rect(int x,int y,int width,int high,unsigned int color)
{
int i,j;
// for(i=y;i<y+high+4;i++) //加上暗线4条
for(i=y;i<y+high;i++)
{
for(j=x;j<(x+width);j++)
{
DisplayBuffer[i][3j] = (color>>16)&0xff;
DisplayBuffer[i][3j+1] = (color>>8)&0xff;
DisplayBuffer[i][3*j+2] = (color)&0xff;
}
}
}
//水平方向显示字符串
void Glib_disp_ascii16x8_v(int x,int y,char *s,int colour)
{
int i,j,k;
while(*s)
{
for(i=0;i<91;i++)
{
if(*s==ASCII_16x8[i].Index)
{
for (j=0;j<16;j++)
{
for(k=0;k<8;k++)
{
if( ((ASCII_16x8[i].ASCII_Dot[j]>>(7-k)) & 0x1)!=0 )
{
PutPixel(x+k,y+j,colour); //加4条暗线
}
}
}
}
}
s+=1;
x+=8;
}
}
//显示汉字
void Glib_disp_hzk16_v(int x,int y,char s,int colour)
{
char buffer[32]; / 32字节的字模缓冲区 */
int i,j,k;
unsigned char qh,wh;
unsigned long location;
while(*s)
{
qh=*s-0xa0; /* 计算区码 */
wh=*(s+1)-0xa0; /* 计算位码 */
location=(94*(qh-1)+(wh-1))*32L; /* 计算字模在文件中的位置 */
memcpy(buffer, &__HZK16X16__[location], 32); /* 获取汉字字模 */
for(i=0;i<16;i++) /* 每一行 */
{
for(j=0;j<2;j++) /* 一行两个字节 */
{
for(k=0;k<8;k++) /* 每个字节按位显示 */
{
if(((buffer[i*2+j]>>(7-k)) & 0x1) != 0)
// PutPixel(x+8*(j)+k,y+4+i,colour); /* 显示一位 */
PutPixel(x+8*(j)+k,y+i,colour);
}
}
}
s+=2; /* 下一个汉字 */
x+=16; /* 汉字间距 */
}
}
void Mouse(int x,int y,int color)
{
int i,j,k;
for (i=0;i<16;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<8;k++)
{
if( ((mouse[i*2+j]>>(7-k)) & 0x1)!=0 )
{
PutPixel(x+8*j+k,y+i,color);
}
}
}
}
}
main.c
#include <cdefBF533.h>
#include “integer.h”
#include “ff.h”
FIL infile, outfile;
FRESULT retval = 0;
extern unsigned char DisplayBuffer[272][1440] ;
extern unsigned char DisplayBuffer_565[272][1440] ;
extern unsigned char Inputdata[];
unsigned char buffer[0x200000];
unsigned char zixing_code[2];
unsigned int keydata=0;
unsigned int lenth;
unsigned int read_addr = 0;
unsigned int old_addr[100];
unsigned int page=0;
unsigned int end_flag = 0;
unsigned int count = 0;
unsigned int file_count[1];
unsigned int dir_count[1];
unsigned char file_name[100][12];
unsigned char dir_name[50][12];
display_txt(unsigned char *pbuff,WORD len,int color)
{
unsigned int x=0,y=0;
int i;
memset(DisplayBuffer,'\0',391680);
for(i=0;i<len;i++)
{
zixing_code[0] = pbuff[i];
if(zixing_code[0]<127)
{
if(zixing_code[0] == 0x0d)
{
zixing_code[1] = pbuff[i+1];
if(zixing_code[1] == 0x0a)
{
x = 0;
y+=16;
}
}
else
{
zixing_code[1] = 0;
if(x>(479-7))
{
x = 0;
y+=16;
}
if(y>(271-15))
{
y = 0;
x =0;
page = i-1;
break;
}
Glib_disp_ascii16x8_v(x,y,zixing_code,color);
x+=8;
}
}
else
{
zixing_code[1] = pbuff[i+1];
i++;
if(x>(479-15))
{
x = 0;
y+=16;
}
if(y>271-15)
{
y = 0;
x =0;
page = i-1;
break;
}
Glib_disp_hzk16_v(x,y,zixing_code,color);
x+=16;
}
}
}
void main(void)
{
Set_PLL(16,4);
Init_EBIU();
Init_SDRAM();
LCDBK_Disable();
SD_Enable();
f_mountdrv();
scan_files("/txt",file_name,file_count,dir_name,dir_count);
retval = f_open(&infile,"/txt/test.txt", FA_OPEN_ALWAYS | FA_READ);
if (retval != 0){
printf("can't open file\n");
exit(-1);
}
f_read(&infile, buffer, 0x200000, &lenth);
f_close(&infile);
Glib_disp_hzk16_v(100,136,"开始阅读电子书,按按键翻页查看",0xffffff);
RGB888_RGB565(DisplayBuffer,391680,DisplayBuffer_565);
InitDMA();
InitPPI();
InitTimer();
PPI_TMR_DMA_Enable();
LCD_Enable();
Init_Timers0(1999);//1~1999 控制背光亮度
Enable_Timers0();
LCDBK_Enable();
delay(100000);
while(1)
{
keydata = *pKEY_DAT; //读键值
switch(keydata)
{
case 0xfe:
delay(1000);
while((*pKEY_DAT&0xff)!=0xff);
if(keydata == 0xfe)
{
if(count==0)
break;
count --;
read_addr = old_addr[count];
lenth += page;
display_txt(&buffer[read_addr],lenth,0xffffff);
RGB888_RGB565(DisplayBuffer,391680,DisplayBuffer_565);
}
break;
case 0xfb:
delay(1000);
while((*pKEY_DAT&0xff)!=0xff);
if(lenth<page)
break;
old_addr[count] = read_addr;
count++;
read_addr += page;
lenth -= page;
memset(DisplayBuffer,'\0',391680);
display_txt(&buffer[read_addr],lenth,0xffffff);
RGB888_RGB565(DisplayBuffer,391680,DisplayBuffer_565);
break;
default: break;
}
}
}