用Cs5460测量直压,STM32读出电压有效值不正确,请高手分析下(没有校准),程序如下
oid Spi2_Init(void)
{
RCC->APB2ENR|=1<<3;
RCC->APB2ENR|=1<<5;
RCC->APB1ENR|=1<<14;
GPIOD->CRH&=0xFFFFFF0F;
GPIOD->CRH|=0x00000030;
GPIOB->CRH&=0x0000FFFF;
GPIOB->CRH|=0xB8B30000;
GPIOB->ODR|=7<<13;
SPI2->CR1|=0<<10;
SPI2->CR1|=1<<9;
SPI2->CR1|=1<<8;
SPI2->CR1|=1<<2;
SPI2->CR1|=0<<11;
SPI2->CR1|=1<<13;
SPI2->CR1|=0<<1;
SPI2->CR1|=1<<0;
SPI2->CR1|=2<<3; //ê±ÖóÅäÖÃ 9M/8=1.125M
SPI2->CR1|=0<<7; //MSBfirst
SPI2->CR1|=1<<6; //SPIé豸ê1Äü
}
void Cs5460a_Init(void)
{
u32 temp,p;
Spi2_Init();
Cs5460a_Rst=0;
delay_ms(30);
Cs5460a_Rst=1;
Cs5460a_cs=0;
__nop(); __nop(); __nop(); __nop();
SPI2_RWByte(0x0ff);
SPI2_RWByte(0x0ff);
SPI2_RWByte(0x0ff);
SPI2_RWByte(0x0fe);
Cs5460a_cs=1;
__nop(); __nop(); __nop(); __nop();
Write_Cs5460a(0x00,0x000001);
Write_Cs5460a(0x0f,0xffffff);
Write_Cs5460a(0x1A,0x000000);
Write_Cs5460a(0x1C,0x000000);
Write_Cs5460a(0x05,0x000190);
// Write_Cs5460a(0x04,0x400000);
temp=Read_Cs5460a(0x0f);
Write_Cs5460a(0x0f,temp);
Write_Cs5460a_CMD(0xe8);
}
u8 SPI2_RWByte(u8 TxData)
{
while((SPI2->SR&1<<1)==0)
{
}
SPI2->DR=TxData;
while((SPI2->SR&1<<0)==0)
{
}
return SPI2->DR;
}
void Write_Cs5460a_CMD(u8 cmd)
{
Cs5460a_cs=0;
__nop(); __nop(); __nop(); __nop();
SPI2_RWByte(cmd);
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
Cs5460a_cs=1;
__nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop(); __nop();
}
void Write_Cs5460a(u8 addr,u32 data)
{
u8 addr_temp ;
Cs5460a_cs=0;
__nop(); __nop(); __nop(); __nop();
addr_temp=(addr<<1)|0x40;
SPI2_RWByte(addr_temp);
SPI2_RWByte((u8)(data>>16));
SPI2_RWByte((u8)(data>>8));
SPI2_RWByte((u8)(data));
__nop(); __nop(); __nop(); __nop();__nop(); __nop(); __nop(); __nop();
Cs5460a_cs=1;
__nop(); __nop(); __nop(); __nop();__nop(); __nop(); __nop(); __nop();
}
u32 Read_Cs5460a( u8 addr)
{
u32 temp=0;
u8 temp1;
Cs5460a_cs=0;
__nop();__nop();__nop();__nop();
temp1=addr<<1;
SPI2_RWByte(temp1);
temp=SPI2_RWByte(0Xfe);
temp<<=8;
temp1=SPI2_RWByte(0Xfe);
temp=temp|temp1;
temp<<=8;
temp1=SPI2_RWByte(0Xfe);
temp=temp|temp1;
__nop(); __nop(); __nop(); __nop();
Cs5460a_cs=1;
__nop(); __nop(); __nop(); __nop();
return temp;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>