C8051f系列单片机是一种高速,集成度很高的单片机,功能还是蛮强大的。我在用此款单片机读取ST 公司的三轴加速度计的时候发现输出总是全1。于是,我用keil c的单步调试功能。一步一步调试,于是我发现执行到一条把某一管脚置1的语句之后,watch窗口显示的相应管教状态仍是0.
于是我觉得可能是硬件的问题,引脚一直接地被拉低。果然,我在查看电路板的线路时发现sdi和sdo两根线与另外两个引脚相连,而这个连接是我修改版图时无意连接上的。而且一个引脚被配置成高电平,另外一个被配置成低电平。所以sdi和sdo都一直保持低电平和高电平。所以不能对传感器进行正常的配置和读取数据。
另附lis3lv02dq读取程序,采用的是虚拟spi总线。
#include "main.H"
unsigned char spi_comm(unsigned char outgoing_byte)
{
uchar incoming_byte, x;
acc_spc = 1; //Pull SPI clock high
for(x = 0 ; x < 8 ; x++)
{
if(outgoing_byte&0x80)
acc_sdi =1; //Put bit on SPI data bus
else acc_sdi = 0;
outgoing_byte <<= 1; //Rotate byte 1 to the left
acc_spc = 0; //Toggle the SPI clock
acc_spc = 1;
incoming_byte <<= 1; //Rotate byte 1 to the left
if(acc_sdo) //Read bit on SPI data bus
incoming_byte += 0x01;
}
return(incoming_byte);
}
unsigned char read_register(unsigned char register_name)
{
uchar in_byte;
register_name |= 0x80; //Set D7 to 1 for read mode
acc_cs = 0; //Select LIS
in_byte = spi_comm(register_name); //SPI read 6-bit register from LIS
//printf("In1 = %h ", in_byte);
//in_byte is nothing, we need to clock in another 8 bits
in_byte = spi_comm(0x00); //Send nothing, but we should get back the register value
acc_cs = 1;
return(in_byte);
}
//Sends a write command to LIS
void write_register(unsigned char register_name, unsigned char register_value)
{
unsigned char in_byte;
register_name &= 0x7f; //Clear D7 to 0 for write mode
acc_cs = 0; //Select LIS
in_byte = spi_comm(register_name); //SPI read 6-bit register from LIS
//printf("In1 = %h ", in_byte);
//in_byte is nothing, we need to clock in the value to write
in_byte = spi_comm(register_value); //Send nothing, but we should get back the register value
acc_cs = 1;
//Return nothing
}
void acc_spi(void)
{
acc_cs = 1; //Unselect LIS
// CK = 0;
// acc[0] = read_register(0x0F); //Read register WHO_AM_I
write_register(0x20,0xc7); //Power on device, enable all axis, and turn off self test - CTRL_REG1
delay(5);
write_register(0x21,0x44);
while(!acc_rdy);
acc[1] = read_register(0x28); //Read outx_l
acc[0] = read_register(0x29); //Read outx_h
acc[3] = read_register(0x2a);
acc[2] = read_register(0x2b);
acc[5] = read_register(0x2c);
acc[4] = read_register(0x2d);
}
上一篇:AT89S52单片机基础知识详解
下一篇:C51编译器的扩展C语言关键字
推荐阅读最新更新时间:2024-03-16 16:18