硬件准备
ADSP-EDU-BF533:BF533开发板
AD-HP530ICE:ADI DSP仿真器
软件准备
Visual DSP++软件
硬件链接
MEMS三轴加速度传感器
我做了一个三轴加速度传感器的子卡,插在这个板子上,然后写了一些有意思的应用程序。
代码实现功能
代码实现了通过 MEMS 控制液晶屏上的红色光标程序,当板卡倾斜,屏幕上的光标会随着倾斜的方向,向相应的方向移动。
代码使用说明
代码将读取的 MEMS 坐标值与液晶屏坐标对应,将读取的 X 轴和 Y 轴数据转换成液晶屏上的坐标,并在该坐标显示红色光标。
adxl345_read_xyzdat(buf_data); //读取坐标值
tem_x = buf_data[0];
tem_y = buf_data[1];
tem_x = tem_x271/512; //将坐标数据转为液晶屏坐标
tem_y = tem_y479/512;
if(abs(tem_xx - tem_x)>3) //比较与上次移动位置变化,去轻微抖动
{
tem_xx = tem_x; //读取本次坐标值,并保存
memcpy(DisplayBuffer_565,TempBuffer_img,261120); //刷新背景数据
Mouse(tem_yy,tem_xx,DisplayBuffer_565); //填充光标
}
if(abs(tem_yy - tem_y)>4) ///比较与上次移动位置变化,去轻微抖动
{
tem_yy = tem_y; //读取本次坐标值,并保存
memcpy(DisplayBuffer_565,TempBuffer_img,261120); //刷新背景数据
Mouse(tem_yy,tem_xx,DisplayBuffer_565); //填充光标
}
代码实验步骤
- 将板卡连接仿真器,将 MEMS 子卡板正确插入扩展接口,板卡上电,运行 VDSP 软件并连接板卡。
- 将工程 BF53x_MEMS_LCD.dpj 载入 VDSP 软件,编译并运行。
- 改变板卡倾斜角度,观察液晶屏上红色光标的移动情况。
代码实验结果
当板卡平放在桌子上,红色光标位于液晶屏中心点,当板卡倾斜,红色光标会随着液晶屏倾斜方向移动。
程序源码
adxl.c
#include <cdefBF533.h>
#include"adxl345.h"
#define DELAY_DATA 500
void SPIinit(void)
{
*pSPI_BAUD = 50;
*pSPI_FLG |=FLS2;
*pSPI_CTL = 0x1001|CPHA| CPOL|EMISO ;
*pSPI_CTL = (*pSPI_CTL | SPE);
}
unsigned char spi_byte_rw(unsigned char value)
{
unsigned char incoming=0;
while(!(*pSPI_STAT & SPIF));
*pSPI_TDBR = value;
while(*pSPI_STAT & RXS)
incoming = *pSPI_RDBR;
return(incoming);
}
adxl345_write(unsigned char data,unsigned char address)
{
*pSPI_FLG &= ~FLG2;
delay(DELAY_DATA);
spi_byte_rw(address|0x40);
spi_byte_rw(data);
delay(DELAY_DATA);
*pSPI_FLG |= FLG2;
delay(DELAY_DATA);
}
unsigned char adxl345_read(unsigned char address)
{
unsigned char read_data;
*pSPI_FLG &= ~FLG2;
delay(DELAY_DATA);
spi_byte_rw(address|0xc0);
delay(DELAY_DATA);
read_data = spi_byte_rw(0xff);
delay(DELAY_DATA);
read_data = spi_byte_rw(0xff);
delay(DELAY_DATA);
*pSPI_FLG |= FLG2;
delay(DELAY_DATA);
return read_data;
}
unsigned char adxl345_read_id(void)
{
unsigned char id = 0;
id = adxl345_read(DEVID);
return id;
}
void adxl345_init (void) //ADXL345初始化设置
{
adxl345_write(0xfe,OFSX); //X\Y\Z轴校正偏移
adxl345_write(0x00,OFSY);
adxl345_write(0x08,OFSZ);
adxl345_write(0x77,ACT_INACT_CTL); //X\Y\Z轴使能
adxl345_write(0x01,BW_RATE); //功率选择及输出数据速率
adxl345_write(0x38,POWER_CTL); //测量、待机及测量模式控制
adxl345_write(0x0b,DATA_FORMAT); //数据及通信形式控制
adxl345_write(0x03,THRESH_ACT);
adxl345_write(0x03,THRESH_INACT);
adxl345_write(0x01,TIME_INACT); //加速度时间阈值
adxl345_write(0x77,ACT_INACT_CTL); //使能控制
adxl345_write(0xa0,THRESH_FF); //自由落体加速度阈值
adxl345_write(0xff,TIME_FF); //自由落体时间阈值
adxl345_write(0x00,TAP_AXES);
adxl345_write(0x77,ACT_TAP_STATUS);
adxl345_write(0x0a,BW_RATE);
adxl345_write(0x38,POWER_CTL);
}
unsigned short adxl345_read_xyzdat(unsigned short *buffer)
{
unsigned short tem_x = 0,tem_y = 0,tem_z = 0;
unsigned char i;
signed short dlXDiff0,dlXDiff1,dlXDiff2;
signed short dlYDiff0,dlYDiff1,dlYDiff2;
signed short dlZDiff0,dlZDiff1,dlZDiff2;
unsigned short ptx[9];
unsigned short pty[9];
unsigned short ptz[9];
unsigned short pax[3];
unsigned short pay[3];
unsigned short paz[3];
for(i=0;i<9;i++)
{
ptx[i] =(adxl345_read(DATAX1)<<8|adxl345_read(DATAX0))+256;
pty[i] =(adxl345_read(DATAY1)<<8|adxl345_read(DATAY0))+256;
ptz[i] =(adxl345_read(DATAZ1)<<8|adxl345_read(DATAZ0))+256;
}
///
pax[0]=(ptx[0]+ptx[1]+ptx[2])/3;
pax[1]=(ptx[3]+ptx[4]+ptx[5])/3;
pax[2]=(ptx[6]+ptx[7]+ptx[8])/3;
dlXDiff0 = pax[ 0 ] - pax[ 1 ];
dlXDiff1 = pax[ 1 ] - pax[ 2 ];
dlXDiff2 = pax[ 2 ] - pax[ 0 ];
dlXDiff0 = dlXDiff0 > 0 ? dlXDiff0 : -dlXDiff0;
dlXDiff1 = dlXDiff1 > 0 ? dlXDiff1 : -dlXDiff1;
dlXDiff2 = dlXDiff2 > 0 ? dlXDiff2 : -dlXDiff2;
if ( dlXDiff0 < dlXDiff1 )
{
if ( dlXDiff2 < dlXDiff0 )
{
tem_x = ( ( pax[ 0 ] + pax[ 2 ] ) >> 1 ) ;
}
else
{
tem_x = ( ( pax[ 0 ] + pax[ 1 ] ) >> 1 );
}
}
else if ( dlXDiff2 < dlXDiff1 )
{
tem_x = ( ( pax[ 0 ] + pax[ 2 ] ) >> 1 ) ;
}
else
{
tem_x= ( ( pax[ 1 ] + pax[ 2 ] ) >> 1 ) ;
}
///
pay[0]=(pty[0]+pty[1]+pty[2])/3;
pay[1]=(pty[3]+pty[4]+pty[5])/3;
pay[2]=(pty[6]+pty[7]+pty[8])/3;
dlYDiff0 = pay[ 0 ] - pay[ 1 ];
dlYDiff1 = pay[ 1 ] - pay[ 2 ];
dlYDiff2 = pay[ 2 ] - pay[ 0 ];
dlYDiff0 = dlYDiff0 > 0 ? dlYDiff0 : -dlYDiff0;
dlYDiff1 = dlYDiff1 > 0 ? dlYDiff1 : -dlYDiff1;
dlYDiff2 = dlYDiff2 > 0 ? dlYDiff2 : -dlYDiff2;
if ( dlYDiff0 < dlYDiff1 )
{
if ( dlYDiff2 < dlYDiff0 )
{
tem_y = ( ( pay[ 0 ] + pay[ 2 ] ) >> 1 ) ;
}
else
{
tem_y = ( ( pay[ 0 ] + pay[ 1 ] ) >> 1 );
}
}
else if ( dlYDiff2 < dlYDiff1 )
{
tem_y = ( ( pay[ 0 ] + pay[ 2 ] ) >> 1 ) ;
}
else
{
tem_y= ( ( pay[ 1 ] + pay[ 2 ] ) >> 1 ) ;
}
//
paz[0]=(ptz[0]+ptz[1]+ptz[2])/3;
paz[1]=(ptz[3]+ptz[4]+ptz[5])/3;
paz[2]=(ptz[6]+ptz[7]+ptz[8])/3;
dlZDiff0 = paz[ 0 ] - paz[ 1 ];
dlZDiff1 = paz[ 1 ] - paz[ 2 ];
dlZDiff2 = paz[ 2 ] - paz[ 0 ];
dlZDiff0 = dlZDiff0 > 0 ? dlZDiff0 : -dlZDiff0;
dlZDiff1 = dlZDiff1 > 0 ? dlZDiff1 : -dlZDiff1;
dlZDiff2 = dlZDiff2 > 0 ? dlZDiff2 : -dlZDiff2;
if ( dlZDiff0 < dlZDiff1 )
{
if ( dlZDiff2 < dlZDiff0 )
{
tem_z = ( ( paz[ 0 ] + paz[ 2 ] ) >> 1 ) ;
}
else
{
tem_z = ( ( paz[ 0 ] + paz[ 1 ] ) >> 1 );
}
}
else if ( dlZDiff2 < dlZDiff1 )
{
tem_z = ( ( paz[ 0 ] + paz[ 2 ] ) >> 1 ) ;
}
else
{
tem_z= ( ( paz[ 1 ] + paz[ 2 ] ) >> 1 ) ;
}
*buffer++ = tem_x;
*buffer++ = tem_y;
*buffer = tem_z;
}
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 Stop_Timers0(void)
{
*pTIMER_ENABLE &= ~0x0001;
}
void delay(volatile int tem)
{
volatile int i;
while(tem–)
for(i=6; i>0; i–);
}
main.c
#include <cdefBF533.h>
extern unsigned char DisplayBuffer[272][1440] ;
extern unsigned char DisplayBuffer_565[272][960] ;
extern unsigned char TempBuffer_img[272][960] ;
extern unsigned char Inputdata[];
unsigned int tem_xx = 0,tem_yy=0;
void main(void)
{
unsigned char interrupt=0;
unsigned short tem_x = 0,tem_y = 0,tem_z = 0;
unsigned char i;
int lenth;
unsigned short buf_data[3];
Set_PLL(16,3);
Init_EBIU();
Init_SDRAM();
LCDBK_Disable();
ExtSPI0_Enable();
SPIinit();
InitDMA();
InitPPI();
InitTimer();
PPI_TMR_DMA_Enable();
adxl345_init();
Init_Timers0(1999);//1~1999 控制背光亮度
Enable_Timers0();
LCD_Enable();
LCDBK_Enable();
memcpy(DisplayBuffer_565,TempBuffer_img,261120);
Mouse(240,136,DisplayBuffer_565);
while(1)
{
adxl345_read_xyzdat(buf_data);
tem_x = buf_data[0];
tem_y = buf_data[1];
tem_x = tem_x271/512;
tem_y = tem_y479/512;
if(abs(tem_xx - tem_x)>3)
{
tem_xx = tem_x;
memcpy(DisplayBuffer_565,TempBuffer_img,261120);
Mouse(tem_yy,tem_xx,DisplayBuffer_565);
}
if(abs(tem_yy - tem_y)>4)
{
tem_yy = tem_y;
memcpy(DisplayBuffer_565,TempBuffer_img,261120);
Mouse(tem_yy,tem_xx,DisplayBuffer_565);
}
}
}
lcd.c
#include <cdefBF533.h>
#include <ccblkfn.h>
#include <sys\exception.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
section(“sdram0_bank1”) unsigned char DisplayBuffer[272][1440];
section(“sdram0_bank1”) unsigned char DisplayBuffer_565[272][960];
section(“sdram0_bank2”) unsigned char TempBuffer_img[272][960]=
{
// #include"1.dat"
}
;
section(“sdram0_bank1”) unsigned char Inputdata[391734];
static unsigned char mouse[]=
{
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
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;
}
}
unsigned char palette[3]={0xff,0xff,0x00};
void play_point(unsigned int x,unsigned int y,unsigned char pdispbuf)
{
pdispbuf[2x+1+y960] = 0x00;
pdispbuf[2x+0+y*960] = 0x1f ;
}
void osd(unsigned int x,unsigned int y,unsigned char *pdispbuf)
{
int i,j,k;
unsigned char temp;
if(x > 479)
x = 479;
if(y > 271)
y = 271;
play_point(x,y,pdispbuf);
}
void Mouse(int x,int y,unsigned char *pdispbuf)
{
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 )
{
play_point(x+8*j+k,y+i,pdispbuf);
}
}
}
}
}