大家帮看看,到底哪出问题了,折腾一周有余了。AD5420驱动代码如下:
#include "
STM32f10x.h"
#include "SPI1.H"
void for_delay(u32 delay_
time)
{
u32 i=0;
for(i=0; i<delay_time;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);
}
}
一周热门 更多>