#include "DSP28_Device.h" unsigned int Spi_VarRx[100]; unsigned int i,j; #define WHO_AM_I 0x0f #define OFFSET_X 0x16 #define OFFSET_Y 0x17 #define OFFSET_Z 0x18 #define GAIN_X 0x19 #define GAIN_Y 0x1a #define GAIN_Z 0x1b #define CTRL_REG1 0x20 #define CTRL_REG2 0x21 #define CTRL_REG3 0x22 #define HP_FILTER_RESET 0x23 #define STATUS_REG 0x27 #define OUTX_L 0x28 #define OUTX_H 0x29 #define OUTY_L 0x2a #define OUTY_H 0x2b #define OUTZ_L 0x2c #define OUTZ_H 0x2d #define FF_WU_CFG 0x30 #define FF_WU_SRC 0x31 #define FF_WU_ACK 0x32 #define FF_WU_THS_L 0x34 #define FF_WU_THS_H 0x35 #define FF_WU_DURATION 0x36 #define DD_CFG 0x38 #define DD_SRC 0x39 #define DD_ACK 0x3a #define DD_THSI_L 0x3c #define DD_THSI_H 0x3d #define DD_THSE_L 0x3e #define DD_THSE_H 0x3f
void delay_loop(void); void spi_init(void); void spi_xmit(int a); int spi_rev(); void spi_fifo_init(void); void Lis3lv02dq_init(void); int read_LIS_register( unsigned char register_name); void write_LIS_register(unsigned char register_name, unsigned char register_value); void Lis3lv02dq_init(void); void LIS_read_x(unsigned int *x); void LIS_read_y(unsigned int *y); void LIS_read_z(unsigned int *z); void read_LIS_x_y_z(unsigned int *x, unsigned int *y, unsigned int *z);
unsigned int var1=0; unsigned int var2=0; unsigned int var3=0; unsigned int Test_count=0; unsigned int Test_flag1=0; unsigned int Test_flag2=0; unsigned int Test_flag3=0; unsigned int Test_flag4=0; unsigned int Test_var=0; unsigned int Test_status[32]; unsigned int PASS_flag=0; unsigned int Ret_var1=0; unsigned int Ret_var2=0; int ID_Lis3lv02dq; //interrupt void ISRSciaTx(void); //interrupt void ISRSciaRx(void); #define LED0_ON GpioDataRegs.GPFDAT.bit.GPIOF6=1 #define LED0_OFF GpioDataRegs.GPFDAT.bit.GPIOF6=0 #define LED1_ON GpioDataRegs.GPFDAT.bit.GPIOF7=1 #define LED1_OFF GpioDataRegs.GPFDAT.bit.GPIOF7=0
void main(void) { /*初始化系统*/ InitSysCtrl(); /* 关中断 */ DINT; IER = 0x0000; IFR = 0x0000; /* 初始化PIE控制寄存器 */ InitPieCtrl(); /* 初始化PIE参数表 */ InitPieVectTable(); /* 初始化外设寄存器 */ InitPeripherals();
//Step4,使用特定的函数重新分配向量使能中断 spi_fifo_init(); // 初始化Spi FIFO
spi_init(); //设置SPI工作模式,SPI初始化
Lis3lv02dq_init(); //加速度计Lis3lv02dq初始化
// write_LIS_register(CTRL_REG1,0xD7); //写加速度计地址20h,使加速度计从休眠模式唤醒(D7),160khz采样频率,使能xyz轴
ID_Lis3lv02dq = read_LIS_register(WHO_AM_I); //读取LIS的ID号
read_LIS_x_y_z(&var1,&var2,&var3);
LED0_OFF; for(;;
}
//Step5,所有中断服务程序和函数 void delay_loop() { long i; for (i=0;i<1000000;i++){} }
//测试1,SPI 8位字符,自测试,主模式,上升沿,波特率设置0x07F void spi_init() { SpiaRegs.SPICCR.all=0x0007; //bit6~SPI复位,设置时钟为上升沿传输数据,8位字符长度 SpiaRegs.SPICTL.all=0x0006; // bit2使能主模式,标准相位 // bit1使能Talk(使能发送,对于4引脚选项,保证使能接 //收器的spiste引脚),SPIINT屏蔽(bit0) SpiaRegs.SPIBRR = 0x7C; //300KHZ设置波特率
//Step2,配置芯片的通用数字I/O为希望的功能引脚 EALLOW; GpioMuxRegs.GPFMUX.all=0x000F; //设置GPIO工作在SPI的功能 //Port F MUX-x000 0000 0000 1111 EDIS;
//退出复位状态 SpiaRegs.SPICCR.all=0x0087; //使SPI(正常工作状态),设置时钟为上升沿传输数据,8位字符长度 // SpiaRegs.SPICCR.all=0x0097; //使能SPI自测试模式bit4,sIMO/SOMI线路被内部连接在一起,用于自测试 }
void spi_fifo_init() { //初始化SPI FIFO寄存器 SpiaRegs.SPIFFTX.all=0xE040; SpiaRegs.SPIFFRX.all=0x204F; SpiaRegs.SPIFFCT.all=0x0; }
//发送一个读指令到加速度传感器 int read_LIS_register( unsigned char register_name) { int Rev_data; int Tx_data =(register_name | 0x0080)<<8; SpiaRegs.SPICTL.bit.TALK=0; //spiste引脚高阻
spi_xmit(Tx_data); //发送读取register_name指令,后8位位空操作指令 SpiaRegs.SPICTL.bit.TALK=1; //spiste引脚拉低
if(SpiaRegs.SPISTS.bit.INT_FLAG == 1) //读命令传输完成,数据可读 { int Rev_data = spi_rev(); //接收数据 }
return(Rev_data); //返回读取到数据 }
//发送一个写指令到加速度传感器 void write_LIS_register(unsigned char register_name, unsigned char register_value) { int Tx_data =(register_name & 0x007F)<<8 + register_value ;
SpiaRegs.SPICTL.bit.TALK=0; //spiste引脚高阻
spi_xmit(Tx_data); //发送写register_name指令,后8位为写该寄存器的值
SpiaRegs.SPICTL.bit.TALK=1; //spiste引脚拉低
while(!SpiaRegs.SPISTS.bit.BUFFULL_FLAG ) //确认字节已经移位到SPODAT continue; }
//SPI发送 void spi_xmit(int a) { SpiaRegs.SPITXBUF=a; //向SPI发送缓冲写数据 }
//SPI接收 int spi_rev() { return SpiaRegs.SPIRXBUF; //读SPI接收缓冲数据 }
//加速度计Lis3lv02dq初始化 void Lis3lv02dq_init(void) { unsigned char id; unsigned int w;
write_LIS_register(CTRL_REG1, 0xD7); //设置采样率,160khz。正常工作模式 delay_loop(); write_LIS_register(CTRL_REG2, 0x14); //从内flash校准参数,如X/Y/Z轴的偏移量和增益, //调整到出厂设置,调往boot位置o delay_loop(); write_LIS_register(CTRL_REG1, 0xD7); //设置采样率,160khz。正常工作模式 delay_loop(); write_LIS_register(CTRL_REG2, 0x44); //读取完高字节后数据更新,确保数据传输正确, 使能数据更新信号
id = read_LIS_register(OFFSET_X); //读取X轴加速度偏移量 printf(" 0x%x ", id); id = read_LIS_register(OFFSET_Y); //读取Y轴加速度偏移量 printf(" 0x%x ", id); id = read_LIS_register(OFFSET_Z); //读取Z轴加速度偏移量 printf(" 0x%x ", id); id = read_LIS_register(GAIN_X); //读取X轴加速度增益 printf(" 0x%x ", id); id = read_LIS_register(GAIN_Y); //读取Y轴加速度增益 printf(" 0x%x ", id); id = read_LIS_register(GAIN_Z); //读取Z轴加速度增益 printf(" 0x%x ", id); write_LIS_register(CTRL_REG1, 0xD7); //写CTRL_REG1工作状态,正常工作 id = read_LIS_register(CTRL_REG1); printf(" 0x%x ", id); id = read_LIS_register(CTRL_REG2); //读取CTRL_REG2 printf(" 0x%x ", id);
LIS_read_x(&w); //读取X轴加速度 LIS_read_y(&w); //读取Y轴加速度 LIS_read_z(&w); //读取Z轴加速度 }
//读取X轴加速度 void LIS_read_x(unsigned int *x) { unsigned char low, high;
low = read_LIS_register(0x28); high = read_LIS_register(0x29); //printf(" 0x%x ", high); *x = (((unsigned int)high)<< | low; }
//读取Y轴加速度 void LIS_read_y(unsigned int *y) { unsigned char low, high;
low = read_LIS_register(0x2A); high = read_LIS_register(0x2B); *y = (((unsigned int)high)<< | low; }
//读取Z轴加速度 void LIS_read_z(unsigned int *z) { unsigned char low, high;
low = read_LIS_register(0x2C); high = read_LIS_register(0x2D); *z = (((unsigned int)high)<< | low; }
void read_LIS_x_y_z(unsigned int *x, unsigned int *y, unsigned int *z) { LIS_read_y(y); LIS_read_z(z); LIS_read_x(x); }
//=========================================================================== // No more. //=========================================================================== |