求指教............SMT8L驱陀螺仪
unsigned int EIR=0,status=0,a=0,b=0;
unsigned int x[4],y[4],z[4];
void delay(unsigned char t)
{
unsigned char i,j;
for(i=0;i<t;i++)
for(j=0;j<t;j++);
}
void IO_init() //初始化IO口
{
PB_DDR |= MASK_PB_DDR_DDR5 | MASK_PB_DDR_DDR6;
PB_CR1 |= MASK_PB_CR1_C15 | MASK_PB_CR1_C16;
PB_CR2 |= MASK_PB_CR2_C25 | MASK_PB_CR2_C26;
//PB5(CLK),PB6(MOSI)设置为快速推挽输出,PB7(MISO)为悬浮输入
PD_DDR |= MASK_PD_DDR_DDR7;
PD_CR1 |= MASK_PD_CR1_C17;
//PD(CS)为推挽输出
}
void SPI1_MOSI_init() //初始化SPI
{
CLK_CKDIVR = 0X03; //时钟8KHZ
CLK_PCKENR1 = 0X10; //开外部时钟门控
SPI1_CR2 |= MASK_SPI1_CR2_SSM; //设置为软件控制NSS
SPI1_CR2 |= MASK_SPI1_CR2_SSI; //设置为主模式
SPI1_CR1 |= MASK_SPI1_CR1_MSTR | MASK_SPI1_CR1_SPE; //开启主模式,SPI1使能
}
void L3G4200D_write(unsigned char a, unsigned char b)
{
CS = 0;
while(!(SPI1_SR & 0X02));
SPI1_DR = a;
while(!(SPI1_SR & 0X02));
SPI1_DR = b;
CS = 1;
}
long int L3G4200D_read(unsigned char m, unsigned char n)
{
CS = 0;
while(!(SPI1_SR & 0X02));
SPI1_DR = m | 0X80; //read signal
while(!(SPI1_SR & 0X01));
a = SPI1_DR;
while(!(SPI1_SR & 0X02));
SPI1_DR = n;
while(!(SPI1_SR & 0X01));
b = SPI1_DR;
a = a | (b << 8);
CS = 1;
return (a);
}
void L3G4200D_init() //初始化陀螺仪
{
L3G4200D_write(L3G4200D_CTRL_REG1,0x4f);
L3G4200D_write(L3G4200D_CTRL_REG2,0x20);
L3G4200D_write(L3G4200D_CTRL_REG5,0x10);
}
void L3G4200D_value_x() //读X的值
{
x[0] = L3G4200D_read(L3G4200D_OUT_X_L,0X00);
x[1] = L3G4200D_read(L3G4200D_OUT_X_H,0X00);
x[2] = x[0] | (x[1] << 8);
}
int main( void )
{
IO_init();
SPI1_MOSI_init();
L3G4200D_init();
EIR = L3G4200D_read(L3G4200D_WHO_AM_I,0X00); //读设备标识
status = L3G4200D_read(L3G4200D_STATUS_REG,0X00); //读状态
while(!(status & 0X08));
L3G4200D_value_x();
while(1)
{
}
}
读出X轴的值不变........
[解决办法]
有没有开SPI中断