完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
大家帮看看,到底哪出问题了,折腾一周有余了。AD5420驱动代码如下:
#include "STM32f10x.h" #include "SPI1.H" void for_delay(u32 delay_time) { u32 i=0; for(i=0; i /******************************************************************************* * º¯ÊýÃû³Æ: SPI_COM_Init() * ¹¦ÄÜÃèÊö: SPI³õʼ»¯ ¸ù¾Ýºê¶¨Òå½øÐÐÑ¡Ôñ--ͨ³£ÊÇSPI1 * ÊäÈë²ÎÊý: void * ·µ»Ø²ÎÊý: ÎÞ ********************************************************************************/ void SPI_GPIO_Init(void) { // SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI1 and GPIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /*!< SPI_FLASH_SPI Periph clock enable */ // RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); /*!< Configure SPI pins: SCK */ GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN|SPI_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /*!< Configure AD5420 latch */ GPIO_InitStructure.GPIO_Pin = SPI1_CS ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,SPI1_CS); } /******************************************************** º¯ÊýÃû³Æ£º miran_spi1_init ¹¦ÄÜÃèÊö: ³õʼ»¯SPI1 ²ÎÊý˵Ã÷£º ·µ»Ø˵Ã÷£º ÌØÊâ˵Ã÷: Éè ¼Æ ÈË: Éè¼Æʱ¼ä£º ********************************************************/ void miran_spi1_init(void) { SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); SPI_Cmd(SPI1, DISABLE); SPI_I2S_DeInit(SPI1); /* SPIy Config -------------------------------------------------------------*/ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); } /******************************************************** º¯ÊýÃû³Æ£º SPI1_SEND_DATA ¹¦ÄÜÃèÊö: ·¢ËÍ8λÊý¾Ý ²ÎÊý˵Ã÷£º data Òª·¢Ë͵ÄÊý¾ÝÖµ ·µ»Ø˵Ã÷£º ÌØÊâ˵Ã÷: Éè ¼Æ ÈË£º Éè¼Æʱ¼ä£º ********************************************************/ void SPI1_SEND_DATA(u8 data) { SPI_I2S_SendData(SPI1, data); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); } /******************************************************** º¯ÊýÃû³Æ£º ADC5420_WRITE_DATA ¹¦ÄÜÃèÊö: ÅäÖüĴæÆ÷ ²ÎÊý˵Ã÷£º regist ¼Ä´æÆ÷µØÖ·, data Êý¾Ý ·µ»Ø˵Ã÷£º ÌØÊâ˵Ã÷: Éè ¼Æ ÈË£º Éè¼Æʱ¼ä£º ********************************************************/ void ADC5420_WRITE_DATA(u8 regist, u16 data) { // GPIO_ResetBits(GPIOA, SPI1_CS); //cs ÖÃµÍ // SPI1_SEND_DATA(regist); //5420 ¼Ä´æÆ÷µØÖ· // SPI1_SEND_DATA(data>>8); //¸ß×Ö½Ú // SPI1_SEND_DATA(data); //µÍ×Ö½Ú // // for_delay(100); // GPIO_SetBits(GPIOA, SPI1_CS); //cs ÖÃ¸ß unsigned char data_low,data_high; data_low = data &0xFF; data_high = (data >>8) &0xff; GPIO_SetBits(GPIOA, SPI1_CS); for_delay(40); SPI1_SEND_DATA(regist); //5420 ¼Ä´æÆ÷µØÖ· SPI1_SEND_DATA(data_high); //¸ß×Ö½Ú SPI1_SEND_DATA(data_low); //µÍ×Ö½Ú GPIO_ResetBits(GPIOA, SPI1_CS); for_delay(100); GPIO_SetBits(GPIOA, SPI1_CS); //cs ÖÃ¸ß } /******************************************************** º¯ÊýÃû³Æ£º ADC5420_init ¹¦ÄÜÃèÊö: ÅäÖüĴæÆ÷ ²ÎÊý˵Ã÷£º regist ¼Ä´æÆ÷µØÖ·, data Êý¾Ý ·µ»Ø˵Ã÷£º ÌØÊâ˵Ã÷: Éè ¼Æ ÈË£º Éè¼Æʱ¼ä£º ********************************************************/ void ADC5420_init() { ADC5420_WRITE_DATA(0X56,0X0001); for_delay(100); ADC5420_WRITE_DATA(0X55,0X2015); ADC5420_WRITE_DATA(0X55,0X1000); ADC5420_WRITE_DATA(0X01,0X1166); } /******************************************************** º¯ÊýÃû³Æ£º ADC5420_RECIVE_DATA ¹¦ÄÜÃèÊö: ¶ÁADC5420¶ÔÓ¦µÄ¼Ä´æÆ÷Öµ ²ÎÊý˵Ã÷£º SPIx SPI½Ó¿Ú, regist ÐèÒª¶Á5420µÄ¼Ä´æÆ÷µØÖ·±àºÅ ·µ»Ø˵Ã÷£º ·µ»Ø16λµÄ5420µÄ¼Ä´æÆ÷Öµ ÌØÊâ˵Ã÷: Éè ¼Æ ÈË£º Éè¼Æʱ¼ä£º ********************************************************/ u32 ADC5420_RECIVE_DATA(SPI_TypeDef* SPIx, u8 regist) { u32 data=0; GPIO_ResetBits(GPIOA, SPI1_CS); //cs ÖÃµÍ for_delay(5); SPI1_SEND_DATA(0x02); SPI1_SEND_DATA(0X00); SPI1_SEND_DATA(regist); for_delay(100); GPIO_SetBits(GPIOA, SPI1_CS); //cs ÖÃ¸ß for_delay(50); GPIO_ResetBits(GPIOA, SPI1_CS); //cs ÖÃµÍ SPI1_SEND_DATA(0X00); for_delay(150); data|= (SPI_I2S_ReceiveData(SPIx)&0x00FF)<<16; SPI1_SEND_DATA(0X00); for_delay(150); data|= (SPI_I2S_ReceiveData(SPIx)&0x00FF)<<8; SPI1_SEND_DATA(0X00); for_delay(150); data|= (SPI_I2S_ReceiveData(SPIx)&0x00FF); data=data&0x00ffff; for_delay(100); GPIO_SetBits(GPIOA, SPI1_CS); //cs ÖÃ¸ß for_delay(150); return data; } /******************************************************** º¯ÊýÃû³Æ£º Ma_to_ADC5420 ¹¦ÄÜÃèÊö: °ÑMAֵת»»³ÉÒªÊäÈëµÄADCÖµ ²ÎÊý˵Ã÷£º MAÖµ ·¶Î§4~20 ·µ»Ø˵Ã÷£º ¶ÔÓ¦µÄADCÖµ ÌØÊâ˵Ã÷: Éè ¼Æ ÈË£º Éè¼Æʱ¼ä£º ********************************************************/ u16 Ma_to_ADC5420(float MA) { u16 adc=0; adc=(u16)(((MA)*65535)/24); // (((MA)*65535)/24) (((MA-4)*65535)/16) return adc; } /******************************************************** º¯ÊýÃû³Æ£ºADC5420_CLEAR ¹¦ÄÜÃèÊö: CLEARΪ¸ßµçƽÓÐЧÇåÁ㣬Êä³öµçÁ÷ÇåÁãÖÁ±à³Ì·¶Î§µÄµ×²¿ ²ÎÊý˵Ã÷£º ·µ»Ø˵Ã÷£º ÌØÊâ˵Ã÷: Éè ¼Æ ÈË£º Éè¼Æʱ¼ä£º ********************************************************/ void ADC5420_CLEAR(void) { GPIO_SetBits(GPIOA, CLEAR); for_delay(50000); GPIO_ResetBits(GPIOA, CLEAR); } main函数如下: int main(void) { Stm32_Clock_Init(9); delay_init(72); LED_GPIO_Config(); SPI_GPIO_Init(); miran_spi1_init(); ADC5420_init(); while (1) { Ma_to_ADC5420(10.00); } } |
|
相关推荐
6个回答
|
|
到底软件问题还是硬件的,没检查出来,有用过的进来指导下
|
|
|
|
|
|
|
|
好的,看来只能一步一步慢慢来了
|
|
|
|
AD5420原来用过,等我找找程序,其实你只要按照ADI的例子写就可以了
|
|
|
|
谢谢楼上的了,我也参考过好多的例程,可就是不行,也不知道硬件电路有没问题没?
|
|
|
|
找出原因了,应该是片子的问题,换了AD5410就有4mA的输出,之前的两片AD5420都没有配置成功
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1085 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1041 浏览 2 评论
2145 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1240 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1660 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 01:13 , Processed in 0.687866 second(s), Total 84, Slave 67 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号