硬件准备
ADSP-EDU-BF533:BF533开发板
AD-HP530ICE:ADI DSP仿真器
软件准备
Visual DSP++软件
硬件链接
MEMS三轴加速度传感器
我做了一个三轴加速度传感器的子卡,插在这个板子上,然后写了一些有意思的应用程序。
代码实现功能
代码实现了电子水平仪的功能,运行代码后,MEMS 子卡板如果处于水平状态,子卡板上绿色的灯就会亮起,如果出现倾斜,倾斜方向的红色 LED 就会亮起,随着倾斜角度增大,亮的 LED 会增多。
代码使用说明
读取 adxl345 数据函数:
unsigned short adxl345_read_xyzdat(unsigned short *buffer)
unsigned short *buffer:数据返回指针,将返回的 X,Y,Z 轴的数据传入指定的地址。
点亮 LED 函数:
void LED_UP(unsigned char led_num,unsigned char flag)
unsigned char led_num:点亮 LED 的编号。
unsigned char flag: 点亮 LED 的模式:
如果为 1,则小于指定编号的 LED 都会点亮。
如果为 0,则只是点亮指定编号的 LED。
代码实验步骤
- 将板卡连接仿真器,将 MEMS 子卡板正确插入扩展接口,板卡上电,运行 VDSP 软件并连接板卡。
- 将工程 BF53x_MEMS_LED.dpj 载入 VDSP 软件,编译并运行。
- 改变板卡倾斜角度,观察 MEMS 子卡板上 LED 灯的工作情况。
代码实验结果
当板卡平放在桌子上,MEMS 子卡板上中间绿色的 LED 灯点亮,当板卡发生倾斜,倾斜方向的红色 LED 灯会随着倾斜角度依次连续点亮。当板卡垂直时,位于倾斜方向垂直坐标的 LED 会全部点亮。
程序源码
adxl.c
#include <stdio.h>
#include <cdefBF533.h>
#include <sys\exception.h>
#include <ccblkfn.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);
}
void 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(0xfe,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>
/****************************************************************************
- 名称 : Init_PLL
- 功能 : 配置处理器的内核与系统时钟
- 入口参数 :无
- 返回值 :无
****************************************************************************/
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 = 0xffc07bb0;
*pEBIU_AMGCTL = 0x000f;
}
void delay(volatile int tem)
{
volatile int i;
while(tem–)
for(i=6; i>0; i–);
}
iic.c
#include <cdefBF533.h>
#include “i2c.h”
#define CORE_CLK_IN 25 * 1000 * 1000
#define SET_PF(pf)
do{
*pFIO_FLAG_S = (pf);
ssync();
}while(0)
#define CLR_PF(pf)
do{
*pFIO_FLAG_C = (pf);
ssync();
}while(0)
#define SET_PF_OUTPUT(pf)
do{
*pFIO_INEN &= ~(pf);
*pFIO_DIR |= (pf);
ssync();
}while(0)
#define SET_PF_INPUT(pf)
do{
*pFIO_DIR &= ~(pf);
*pFIO_INEN |= (pf);
ssync();
}while(0)
int get_core_clk(void)
{
int tempPLLCTL;
int _DF;
int VCO;
int MSEL1;
tempPLLCTL = *pPLL_CTL;
MSEL1 = ((tempPLLCTL & 0x7E00) >> 9);
_DF = tempPLLCTL & 0x0001;
VCO = MSEL1 * __CORE_CLK_IN__;
if(_DF == 1)
VCO /= 2;
return VCO;
}
void delay_ns(unsigned int core_clock, unsigned long long count)
{
count *= core_clock;
count /= 1000000000;
while(count–);
}
int _get_sdata(i2c_device * dev)
{
return ((*pFIO_FLAG_D & dev->sdata) ? 1 : 0);
}
void i2c_init(i2c_device * dev)
{
dev->core_clock = get_core_clk();
dev->delay_ns = delay_ns;
*pFIO_DIR |= dev->sclk | dev->sdata;
ssync();
}
void i2c_deinit(i2c_device * dev)
{
dev->sclk = 0;
dev->sdata = 0;
*pFIO_DIR &= ~(dev->sclk | dev->sdata);
ssync();
}
void i2c_start(i2c_device * dev)
{
SET_PF_OUTPUT(dev->sdata);
SET_PF_OUTPUT(dev->sclk);
SET_PF(dev->sdata);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
CLR_PF(dev->sdata);
delay_ns(dev->core_clock, dev->low_ns);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
}
void i2c_stop(i2c_device * dev)
{
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
SET_PF_OUTPUT(dev->sdata);
CLR_PF(dev->sdata);
delay_ns(dev->core_clock, dev->low_ns);
SET_PF_INPUT(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
SET_PF_INPUT(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns);
}
int i2c_read_ack(i2c_device * dev)
{
int ret = 0;
SET_PF_INPUT(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns/3);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns/3);
ret = _get_sdata(dev);
delay_ns(dev->core_clock, dev->high_ns/3);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
SET_PF_OUTPUT(dev->sdata);
return ret;
}
int i2c_wait_slave(i2c_device * dev, unsigned int time_out)
{
int ret;
int count = time_out * 2 / dev->high_ns;
SET_PF_INPUT(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns/2);
do{
ret = *pFIO_FLAG_D & dev->sclk;
if(ret)
break;
delay_ns(dev->core_clock, dev->high_ns/2);
}while(count--);
SET_PF_OUTPUT(dev->sclk);
return !ret;
}
void i2c_write_ack(i2c_device * dev)
{
SET_PF_OUTPUT(dev->sdata);
CLR_PF(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns/2);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
}
int i2c_write(i2c_device * dev, unsigned char value, int need_ack)
{
int ret = -1;
unsigned char index;
SET_PF_OUTPUT(dev->sdata);
//send 8 bits to slave
for(index = 0; index < 8; index++){
//send one bit to the i2c bus
if((value<<index) & 0x80){
SET_PF(dev->sdata);
} else {
CLR_PF(dev->sdata);
}
delay_ns(dev->core_clock, dev->low_ns/2);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns/2);
}
if(need_ack){
ret = i2c_read_ack(dev);
}
return ret;
}
int i2c_read(i2c_device * dev, unsigned char * value, int send_ack)
{
unsigned char index;
*value = 0x00;
SET_PF_INPUT(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns/2);
//get 8 bits from the device
for(index = 0; index < 8; index++){
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns/2);
*value <<= 1;
*value |= _get_sdata(dev);
delay_ns(dev->core_clock, dev->high_ns/2);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
}
// send ack to slave
if(send_ack){
i2c_write_ack(dev);
}
return *value;
}
main.c
#include <cdefBF533.h>
#include"adxl345.h"
void main(void)
{
unsigned int tem_x = 0,tem_y=0;
unsigned char buf[3];
int i;
unsigned char up1,down1,left1,right1;
unsigned char center =2;
unsigned short buf_data[3];
Set_PLL(16,4);
Init_EBIU();
IIC_Enable();
ExtSPI0_Enable();
SPIinit();
adxl345_init ();
init_zlg7290();
while(1)
{
adxl345_read_xyzdat(buf_data);
tem_x = buf_data[0];
tem_y = buf_data[1];
//
if(tem_x>0&&tem_x<42)
{
up1 = 1;
down1 = 0;
}
if(tem_x>41&&tem_x<83)
{
up1 = 2;
down1 = 0;
}
if(tem_x>82&&tem_x<124)
{
up1 = 3;
down1 = 0;
}
if(tem_x>123&&tem_x<165)
{
up1 = 4;
down1 = 0;
}
if(tem_x>164&&tem_x<206)
{
up1 = 5;
down1 = 0;
}
if(tem_x>205&&tem_x<236)
{
up1 = 6;
down1 = 0;
}
//
if((tem_y>235&&tem_y<275)&&(tem_x>235&&tem_x<275))
{
center = 0;
up1=0;
down1=0;
left1=0;
right1=0;
}
else
{
center = 2;
}
if((tem_y>235&&tem_y<275)&&((tem_x>0&&tem_x<235)||(tem_x>275&&tem_x<600)))
{
center = 2;
// up1=0;
// down1=0;
left1=0;
right1=0;
}
if((tem_x>235&&tem_x<275)&&((tem_y>0&&tem_y<235)||(tem_y>275&&tem_y<600)))
{
center = 2;
up1=0;
down1=0;
// left1=0;
// right1=0;
}
if(tem_x>274&&tem_x<308)
{
down1 = 6;
up1 = 0;
}
if(tem_x>307&&tem_x<349)
{
down1 = 5;
up1 = 0;
}
if(tem_x>348&&tem_x<390)
{
down1 = 4;
up1 = 0;
}
if(tem_x>389&&tem_x<431)
{
down1 = 3;
up1 = 0;
}
if(tem_x>430&&tem_x<472)
{
down1 = 2;
up1 = 0;
}
if(tem_x>471&&tem_x<600)
{
down1 = 1;
up1 = 0;
}
///
if(tem_y>0&&tem_y<42)
{
left1 = 1;
right1 = 0;
}
if(tem_y>41&&tem_y<83)
{
left1 = 2;
right1 = 0;
}
if(tem_y>82&&tem_y<124)
{
left1 = 3;
right1 = 0;
}
if(tem_y>123&&tem_y<165)
{
left1 = 4;
right1 = 0;
}
if(tem_y>164&&tem_y<206)
{
left1 = 5;
right1 = 0;
}
if(tem_y>205&&tem_y<236)
{
left1 = 6;
right1 = 0;
}
///
if(tem_y>274&&tem_y<308)
{
right1 = 6;
left1 = 0;
}
if(tem_y>307&&tem_y<349)
{
right1 = 5;
left1 = 0;
}
if(tem_y>348&&tem_y<390)
{
right1 = 4;
left1 = 0;
}
if(tem_y>389&&tem_y<431)
{
right1 = 3;
left1 = 0;
}
if(tem_y>430&&tem_y<472)
{
right1 = 2;
left1 = 0;
}
if(tem_y>471&&tem_y<600)
{
right1 = 1;
left1 = 0;
}
LED_UP(up1,1);
LED_DOWN(down1,1);
LED_LEFT(left1,1);
LED_RIGHT(right1,1);
LED_CENTER(center,1);
}
}
zlg.c
#include “i2c.h”
#include <cdefBF533.h>
#define ZLG7290_ADDRESS 0x70
static i2c_device mcu_i2c;
void init_zlg7290(void);
int zlg7290_write(unsigned char addr, unsigned char dat);
int zlg7290_read(unsigned char addr, unsigned char * buf);
/****************************************************************************
- 名称 :zlg7290_write
- 功能 : 写zlg7290寄存器函数
- 入口参数 :addr:寄存器偏移地址
dat:寄存器配置值 - 出口参数 :返回0
****************************************************************************/
int zlg7290_write(unsigned char addr, unsigned char dat)
{
int ret = -1;
i2c_start(&mcu_i2c);
if(i2c_write(&mcu_i2c, ZLG7290_ADDRESS, 1)){
i2c_stop(&mcu_i2c);
return ret;
}
if(i2c_write(&mcu_i2c, addr, 1)){
i2c_stop(&mcu_i2c);
return ret;
}
i2c_write(&mcu_i2c, dat, 1);
i2c_stop(&mcu_i2c);
return 0;
}
/****************************************************************************
- 名称 :zlg7290_read
- 功能 : 读zlg7290寄存器函数
- 入口参数 :addr:寄存器偏移地址
buf:寄存器读取数据缓存 - 出口参数 :返回0
****************************************************************************/
int zlg7290_read(unsigned char addr, unsigned char * buf)
{
unsigned char *p = buf;
int ret = -1;
i2c_start(&mcu_i2c);
//send slave address
if(i2c_write(&mcu_i2c, ZLG7290_ADDRESS, 1)){
i2c_stop(&mcu_i2c);
return ret;
}
//send sub-address of slave
if(i2c_write(&mcu_i2c, addr, 1)){
i2c_stop(&mcu_i2c);
return ret;
}
i2c_stop(&mcu_i2c);
i2c_start(&mcu_i2c);
// send slave address (+1 read mode)
if(i2c_write(&mcu_i2c, ZLG7290_ADDRESS+1, 1)){
i2c_stop(&mcu_i2c);
return ret;
}
if(i2c_wait_slave(&mcu_i2c, 1000)){
i2c_stop(&mcu_i2c);
return ret;
}
i2c_read(&mcu_i2c, p++, 1); // send ack
i2c_stop(&mcu_i2c);
return 0;
}
/****************************************************************************
- 名称 :init_zlg7290
- 功能 : 音频模块的内部初始化
- 入口参数 :无
- 出口参数 :无
****************************************************************************/
void init_zlg7290(void)
{
mcu_i2c.sclk = PF0; //时钟PF脚
mcu_i2c.sdata = PF1; //数据PF脚
mcu_i2c.low_ns =3000; //低电平延时 ns
mcu_i2c.high_ns =3000; //高电平延时 ns
i2c_init(&mcu_i2c);
zlg7290_write(0x0d,0x04);
zlg7290_write(0x0c,0x00);
}
void LED_UP(unsigned char led_num,unsigned char flag) //led_num要点亮的LED灯,flag 1表示连着亮,0表示单亮
{
unsigned char i;
unsigned char tmp=0x80;
if(flag)
{
if(led_num==0)
{
zlg7290_write(0x12,0x00);
}
else
{
for(i=0;i<6-led_num;i++)
{
tmp = (tmp>>1)+0x80;
}
zlg7290_write(0x12,tmp);
}
}
else
{
zlg7290_write(0x12,0x02<<led_num);
}
}
void LED_DOWN(unsigned char led_num,unsigned char flag) //led_num要点亮的LED灯,flag 1表示连着亮,0表示单亮
{
unsigned char i;
unsigned char tmp=0x80;
if(flag)
{
if(led_num==0)
{
zlg7290_write(0x10,0x00);
}
else
{
for(i=0;i<6-led_num;i++)
{
tmp = (tmp>>1)+0x80;
}
zlg7290_write(0x10,tmp);
}
}
else
{
zlg7290_write(0x10,0x02<<led_num);
}
}
void LED_LEFT(unsigned char led_num,unsigned char flag) //led_num要点亮的LED灯,flag 1表示连着亮,0表示单亮
{
unsigned char i;
unsigned char tmp=0x80;
if(flag)
{
if(led_num==0)
{
zlg7290_write(0x11,0x00);
}
else
{
for(i=0;i<6-led_num;i++)
{
tmp = (tmp>>1)+0x80;
}
zlg7290_write(0x11,tmp);
}
}
else
{
zlg7290_write(0x11,0x02<<led_num);
}
}
void LED_RIGHT(unsigned char led_num,unsigned char flag) //led_num要点亮的LED灯,flag 1表示连着亮,0表示单亮
{
unsigned char i;
unsigned char tmp=0x80;
if(flag)
{
if(led_num==0)
{
zlg7290_write(0x13,0x00);
}
else
{
for(i=0;i<6-led_num;i++)
{
tmp = (tmp>>1)+0x80;
}
zlg7290_write(0x13,tmp);
}
}
else
{
zlg7290_write(0x13,0x02<<led_num);
}
}
void LED_CENTER(unsigned char x)
{
zlg7290_write(0x14,0x80>>x);
}