现在在用SPI读ADXL345,发现开始读时,写入的第一个命令对不对都会输出正确的IDE5,然后就没有然后了
用示波器看波形MOSI不稳定,幅值不高,但有输出,波形好像和CLk端一样,
主要参考的是
http://blog.sina.com.cn/s/blog_6754612e0101ag39.html
程序源码如下
[mw_shl_code=c,true]int main(void)
{
RCC_Configuration();
RTC_Configuration(); //???????±?±??
delay_init(72);
LED_Init();
Sensor_Adxl = vect_new(3);
GPIOA_USART1_Configuration();
USART1_Init();
// GPIO_SetBits(GPIOC,GPIO_Pin_4|GPIO_Pin_5);
// GPIO_SetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1);
GPIOA_SPI1_Configuration();
SPIx_Init(SPI1);
Sensor_CSport_Init();
while(1)
{
write_byte(0x1E,0x00); delay_ms(100);
write_byte(0x1F,0x00); delay_ms(100);
write_byte(0x20,0x00); delay_ms(100);
write_byte(0x2C,0x0A); delay_ms(100);
write_byte(0x2D,0x00); delay_ms(100);
write_byte(0x2E,0x80); delay_ms(100);
write_byte(0x2F,0x00); delay_ms(100);
write_byte(0x31,0x0B); delay_ms(100);
while(1)
{
write_byte(0x33,0x08);
delay_ms(100)
a = read_byte(0x33);
}
}
void GPIOA_SPI1_Configuration(void)
{
GPIO_InitTypeDef GPIOA_InitStructure;
GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIOA_InitStructure);
GPIO_WriteBit(GPIOA,GPIO_Pin_4,Bit_SET);//??STM32?è?????÷?ú
GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7;
GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì????
GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIOA_InitStructure);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE );
}
void SPIx_Init(SPI_TypeDef *SPIx)
{
// GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
SPI1_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //?è??SPI???ò?ò?????ò??????????:SPI?è???????????ò?????¤
SPI1_InitStructure.SPI_Mode = SPI_Mode_Master; //?è??SPI?¤×÷????:?è?????÷SPI
SPI1_InitStructure.SPI_DataSize = SPI_DataSize_16b; //?è??SPI???????ó??:SPI·???????8?????á??
SPI1_InitStructure.SPI_CPOL = SPI_CPOL_High; //???????®???±????????:?±???ü????
SPI1_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //?????????????????±????
SPI1_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS???????????¨NSS?????©?????í???¨????SSI???©???í:????NSS??????SSI??????
SPI1_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //?¨???¨?????¤·???????:?¨?????¤·???????256
SPI1_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //???¨??????????MSB??????LSB??????:??????????MSB??????
SPI1_InitStructure.SPI_CRCPolynomial = 7; //CRC?????????à?????¨?????©
SPI_Init(SPIx, &SPI1_InitStructure); //?ù??SPI_InitStruct?????¨???????????????èSPIx?????÷
SPI_Cmd(SPIx, ENABLE); //????SPI???è
// SPIx_ReadWriteByte(0xff); //????????
}
void write_byte(u8 add,u8 val)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_1);
SPI_I2S_SendData(SPI1,add<<8|val);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
GPIO_SetBits(GPIOB,GPIO_Pin_1);
SPI_I2S_ReceiveData(SPI1) & 0xff;
}
uint8_t read_byte(u8 add)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
SPI_I2S_SendData(SPI1,(add|0x80)<<8|0x00);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
return SPI_I2S_ReceiveData(SPI1)&0xff;
}[/mw_shl_code]
一周热门 更多>