完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
ADE7878示例代码,然后发现SPI读任何寄存器都全是0XFFFFFFFF,感觉应该是没通讯上,但是又不知道问题在哪,有哪位用过的可以帮忙看下吗? 附上示例代码, 诚心请教!
1.硬件原理图: 已经量VREF(C28)两端电压为1.19V 2.软件: SPI配置: void MX_SPI1_Init(void) { hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.Direction = SPI_DIRECTION_2LINES; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; // 80M/64 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.CRCPolynomial = 7; if (HAL_SPI_Init( hspi1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } 然后CLKPolarity HIGH,LOW都试过, CLKPhase 1,2EDGE也都试过,都没用. ADE78XX_SPI.C /************************************************************************************************ Author : CAC (China Applications Support Team) Date : October 2012 File : ADE7878_SPI.h Hardware : ADE7878, ADuC7060 Description : Test with ADE7878 Config ADE7878 to read phase A current rms, voltage rms, energy, period, in linecycle mode *************************************************************************************************/ #include "ADE78xx_spi.h" #include "extern.h" //extern unsigned char szTxData[7]; unsigned char DummyRd; SPI_HandleTypeDef* ADE_hspi = hspi1; uint8_t SPI_comm(uint8_t s_byte); /****************************************************************************** ** Abstract: ** short delay ** Parameters: ** none ** Returns: ** none *******************************************************************************/ void SPIDelay(void) { int i; for(i=0; i<20; i++); } /****************************************************************************** ** Abstract: ** write 4 bytes to ADE7878 ** Parameters: ** unsigned int address: register address ** long int sendtemp : configuration value ** Returns: ** none *******************************************************************************/ void SPIWrite4Bytes(unsigned int address , long int sendtemp) { char i; unsigned char szTxData[7]; CLR_PIN00(); szTxData[0] = 0x00; szTxData[1] = (char)(address>>8); szTxData[2] = (char)(address); szTxData[3] = (char)(sendtemp>>24); szTxData[4] = (char)(sendtemp>>16); szTxData[5] = (char)(sendtemp>>8); szTxData[6] = (char)(sendtemp); for(i=0;i<7;i++) { SPI_comm(szTxData); SPIDelay(); } SET_PIN00(); } /****************************************************************************** ** Abstract: ** write 2 bytes to ADE7878 ** Parameters: ** unsigned int address: register address ** long int sendtemp : configuration value ** Returns: ** none *******************************************************************************/ void SPIWrite2Bytes(unsigned int address , int sendtemp) { char i; unsigned char szTxData[7]; CLR_PIN00(); szTxData[0] = 0x00; szTxData[1] = (char)(address>>8); szTxData[2] = (char)(address); szTxData[3] = (char)(sendtemp>>8); szTxData[4] = (char)(sendtemp); for(i=0;i<5;i++) { SPI_comm(szTxData); SPIDelay(); } SET_PIN00(); } /****************************************************************************** ** Abstract: ** write 1 byte to ADE7878 ** Parameters: ** unsigned int address: register address ** long int sendtemp : configuration value ** Returns: ** none *******************************************************************************/ void SPIWrite1Byte(unsigned int address , char sendtemp) { char i; unsigned char szTxData[7]; CLR_PIN00(); szTxData[0] = 0x00; szTxData[1] = (char)(address>>8); szTxData[2] = (char)(address); szTxData[3] = sendtemp; for(i=0;i<4;i++) { SPI_comm(szTxData); SPIDelay(); } SET_PIN00(); } /****************************************************************************** ** Abstract: ** read 4 bytes from ADE7878 ** Parameters: ** unsigned int address: register address ** Returns: ** readout : read value *******************************************************************************/ long int SPIRead4Bytes(unsigned int address) { long int readout; unsigned char szTxData[7]; char spireadbuf[4]; char i; CLR_PIN00(); szTxData[0] = 0x01; szTxData[1] = (char)(address>>8); szTxData[2] = (char)(address); for(i=0;i<3;i++) { SPI_comm(szTxData); SPIDelay(); } // SPICON |= BIT12; // Flush Rx FIFO // SPICON = ~BIT12; // DummyRd = SPI_comm(0x00); for(i=0;i<4;i++) { SPIDelay(); spireadbuf = SPI_comm(0x00); } readout = (spireadbuf[0]<<24)+(spireadbuf[1]<<16)+(spireadbuf[2]<<8)+spireadbuf[3]; SET_PIN00(); return(readout); } /****************************************************************************** ** Abstract: ** read 2 bytes from ADE7878 ** Parameters: ** unsigned int address: register address ** Returns: ** readout : read value *******************************************************************************/ int SPIRead2Bytes(unsigned int address) { int readout; unsigned char szTxData[7]; char i; char spireadbuf[2]; CLR_PIN00(); szTxData[0] = 0x01; szTxData[1] = (char)(address>>8); szTxData[2] = (char)(address); for(i=0;i<3;i++) { SPI_comm(szTxData); SPIDelay(); } // SPICON |= BIT12; // Flush Rx FIFO // SPICON = ~BIT12; DummyRd = SPI_comm(0x00); for(i=0;i<2;i++) { // SPITX = 0x00; SPIDelay(); spireadbuf = SPI_comm(0x00); } readout = (spireadbuf[0]<<8)+spireadbuf[1]; SET_PIN00(); return(readout); } /****************************************************************************** ** Abstract: ** read 1 byte from ADE7878 ** Parameters: ** unsigned int address: register address ** Returns: ** readout : read value *******************************************************************************/ char SPIRead1Byte(unsigned int address) { char readout; unsigned char szTxData[7]; char i; CLR_PIN00(); szTxData[0] = 0x01; szTxData[1] = (char)(address>>8); szTxData[2] = (char)(address); for(i=0;i<3;i++) { SPI_comm(szTxData); SPIDelay(); } // SPICON |= BIT12; // Flush Rx FIFO // SPICON = ~BIT12; DummyRd = SPI_comm(0x00); // SPITX = 0x00; SPIDelay(); readout = SPI_comm(0x00); SET_PIN00(); return(readout); } uint8_t SPI_comm(uint8_t s_byte) { uint8_t ret = 0; HAL_SPI_TransmitReceive(ADE_hspi, s_byte, ret, 1, 0xfff); return ret; } 以上基本参考的示例程序, 只把发送和接收这里改成了我的库函数. ADE7878.C就是示例程序一模一样了. 然后我在Main里面函数这样调用的. MX_SPI1_Init(); ADE7878Reset(); ADE7878SPICfg(); 然后最后执行读写寄存器,如下图: |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2233 浏览 0 评论
1403 浏览 1 评论
4478 浏览 2 评论
2971 浏览 0 评论
这是汽车360全景控制器上的主板,请问圆圈中的原件是什么,起什么作用?
2710 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 13:57 , Processed in 0.642950 second(s), Total 75, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号