调试两天,终于把这篇ADC的驱动调试好了。怪自己不够用心。效率太低。
/**********************(c)COPYRIGHT SNAIL 2013 ******************************************
** File name : LTC1867.c
** Author : snail
** Version : v1.0 (using library 3.5 )
** Date : 2013/07/07
** Description : 16bits ADC's drivering program
****************************************************************************************/
#include"LTC1867.h"
/**************************************************************************
**Function name: AD_GPIO_Init
**Description : Initilize LTC1867L's GPIO
AD1:SCK -> PB3,(SPI3_SCK ),STM32-output
SDO -> PB4,(SPI3_MISO),STM32-input
SDI -> PB5,(SPI3_MOSI),STM32-output
CS -> PB6,STM32-output
AD2:SCK -> PB13,(SPI2_SCK ),STM32-output
SDO -> PB14,(SPI2_MISO),STM32-input
SDI -> PB15,(SPI2_MOSI),STM32-output
CS -> PB12,(SPI2_NSS)STM32-output
**Input : none
**Output : none
**Return : none
**************************************************************************/
void AD_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
#if 0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
#else
//AD1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//AD2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI·¢ËͽÓÊÕ16λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //Ñ¡ÔñÁË´®ÐÐʱÖÓµÄÎÈ̬:ʱÖÓÐü¿ÕµÍµçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //Êý¾Ý²¶»ñÓÚµÚÒ»¸öʱÖÓÑØ
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //72000/256=281KHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI2, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
SPI_Cmd(SPI2, ENABLE);//ʹÄÜSPI1ÍâÉè
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI·¢ËͽÓÊÕ16λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //Ñ¡ÔñÁË´®ÐÐʱÖÓµÄÎÈ̬:ʱÖÓÐü¿ÕµÍµçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //Êý¾Ý²¶»ñÓÚµÚÒ»¸öʱÖÓÑØ
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //72000/256=281KHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI3, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
SPI_Cmd(SPI3, ENABLE);//ʹÄÜSPI1ÍâÉè
#endif
}
/**************************************************************************
**Function name: AD_Conv
**Description : ÓÃÄ£ÄâSPI µÄ·½Ê½À´ÊµÏÖADת»»
**Input : which channel(0~7)
**Output : none
**Return : ADC's digital code
**************************************************************************/
uint16_t AD_Conv(uint8_t channel)
{
uint8_t command=0x42,i = 0;//01 0 0 0 100
uint16_t AD_Value =0x0000;
switch(channel)
{
case 0: command = 0x42;break;
case 1: command = 0x62;break;
case 2: command = 0x4a;break;
case 3: command = 0x6a;break;
case 4: command = 0x52;break;
case 5: command = 0x72;break;
case 6: command = 0x5a;break;
case 7: command = 0x7a;break;
default: command = 0x42;break;
}
command <<= 1;
// SCK_L;
// AD1_CS_H;
// delay_us(5);
AD1_CS_L;
delay_us(5);
for(i=0;i<7;i++)
{
if(command & 0x80)
{
SDI_H;
}
else
{
SDI_L;
}
SCK_L;
// delay_us(5);
SCK_H;
// delay_us(5);
command <<= 1;
}
AD1_CS_H;
delay_us(10);
AD1_CS_L;
for(i=0;i<16;i++) //¶ÁÈ¡SPIÊý¾Ý
{
SCK_L;
delay_us(2);
if(SDO)
AD_Value |= 0x01;
AD_Value <<= 1;
SCK_H;
delay_us(2);
}
AD1_CS_H;
return AD_Value;
}
/**************************************************************************
**Function name: AD1_SPI_Conv
**Description : ÓÃÓ²¼þSPI µÄ·½Ê½À´ÊµÏÖADת»»
**Input : which channel(0~7)
**Output : none
**Return : ADC's digital code
**************************************************************************/
uint16_t AD1_SPI_Conv(uint8_t channel)
{
uint8_t command=0x42;
uint16_t AD_Value=0x00;
uint8_t Data_H,Data_L;
switch(channel)
{
case 0: command = 0x42;break;
case 1: command = 0x62;break;
case 2: command = 0x4a;break;
case 3: command = 0x6a;break;
case 4: command = 0x52;break;
case 5: command = 0x72;break;
case 6: command = 0x5a;break;
case 7: command = 0x7a;break;
default: command = 0x42;break;
}
command <<= 1;
AD1_CS_L;
delay_us(10);
AD1_WriteCmd(command);
AD1_CS_H;
AD1_CS_L;
Data_H = AD1_ReadData();
Data_L = AD1_ReadData();
AD1_CS_H;
AD_Value = Data_H;
AD_Value = (AD_Value<<8) + Data_L;
return AD_Value;
}
/**************************************************************************
**Function name: SPI_WriteCmd
**Description : SPI дÃüÁî
**Input : uint8_t command
**Output : none
**Return : none
**************************************************************************/
void AD1_WriteCmd(uint8_t command)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //·¢ËÍ»º´æ¿Õ±ê־λ
SPI_I2S_SendData(SPI2, command);
}
/**************************************************************************
**Function name: AD1_ReadData
**Description : AD2 read conversion data
**Input : none
**Output : none
**Return : uint16_t
**************************************************************************/
uint8_t AD1_ReadData(void)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±ê־λ
return SPI_I2S_ReceiveData(SPI2); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý
}
用户377235 2015-6-29 15:13
用户377235 2014-6-17 10:47