#include "adc.h"
#include "stm32f4xx.h"
#include "delay.h"
// CS VDD
//CLK SPI-SCK PB3
//DOUT MISO PB4
// DIN MOSI PB5
// DRDY PB8
//RST PB9
//ÒÔÏÂÊÇSPIÄ£¿éµÄ³õʼ»¯´úÂ룬ÅäÖóÉÖ÷»úģʽ
//SPI¿Ú³õʼ»¯
//ÕâÀïÕëÊǶÔSPI1µÄ³õʼ»¯
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ʹÄÜGPIOBʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//PB8
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//ÆÕͨÊäÈëģʽ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOF, &GPIO_InitStructure);//³õʼ»¯
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//PB9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_Init(GPIOF, &GPIO_InitStructure);//³õʼ»¯
GPIO_SetBits(GPIOB,GPIO_Pin_9);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//ʹÄÜSPI1ʱÖÓ
//GPIOB3,4,5³õʼ»¯ÉèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//PB3~5¸´Óù¦ÄÜÊä³ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); //PB3¸´ÓÃΪ SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); //PB4¸´ÓÃΪ SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); //PB5¸´ÓÃΪ SPI1
SPI_Cmd(SPI1, DISABLE); //ʧÄÜÄÜSPIÍâÉè
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µÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //´®ÐÐͬ²½Ê±ÖӵĿÕÏÐ״̬Ϊ¸ßµçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //´®ÐÐͬ²½Ê±Öӵĵڶþ¸öÌø±äÑØ£¨ÉÏÉý»òϽµ£©Êý¾Ý±»²ÉÑù
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI1, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉè
SPI1_ReadWriteByte(0xff);//Æô¶¯´«Êä
}
//SPI1ËÙ¶ÈÉèÖú¯Êý
//SPIËÙ¶È=fAPB2/·ÖƵϵÊý
//@ref SPI_BaudRate_Prescaler:SPI_BaudRatePrescaler_2~SPI_BaudRatePrescaler_256
//fAPB2ʱÖÓÒ»°ãΪ84Mhz£º
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//ÅжÏÓÐЧÐÔ
SPI1->CR1&=0XFFC7;//λ3-5ÇåÁ㣬ÓÃÀ´ÉèÖò¨ÌØÂÊ
SPI1->CR1|=SPI_BaudRatePrescaler; //ÉèÖÃSPI1ËÙ¶È
SPI_Cmd(SPI1,ENABLE); //ʹÄÜSPI1
}
//SPI1 ¶Áдһ¸ö×Ö½Ú
//TxData:ҪдÈëµÄ×Ö½Ú
//·µ»ØÖµ:¶ÁÈ¡µ½µÄ×Ö½Ú
u8 SPI1_ReadWriteByte(u8 TxData)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)== RESET){}//µÈ´ý·¢ËÍÇø¿Õ
SPI_I2S_SendData(SPI1, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öbyte Êý¾Ý
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} //µÈ´ý½ÓÊÕÍêÒ»¸öbyte
return SPI_I2S_ReceiveData(SPI1); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý
}
//====================================
//º¯ÊýÃû:£Svoid AD7705_WriteByte(u8 Dst_Addr)
//×÷Ó㺠AD7705д¹¦ÄÜ
//ÊäÈ룺
//·µ»ØÖµ£º
//===================================
void AD7705_WriteByte(u8 Dst_Addr)
{
SPI1_ReadWriteByte(Dst_Addr);
}
//====================================
//º¯ÊýÃû:£Svoid Init_AD7705(u8 chnanel)
//×÷Ó㺠AD7705³õʼ»¯º¯Êý
//ÊäÈ룺
//·µ»ØÖµ£º
//===================================
void Init_AD7705(u8 chnanel)
{
u8 i;
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
delay_ms(2);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
delay_ms(2);
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
delay_ms(2);
for(i=0;i<4;i++)//¶àÓÚÁ¬Ðø32¸ö DIN=1 ʹ´®¿Ú¸´Î»
{
AD7705_WriteByte(0xff);//³ÖÐøDIN¸ßµçƽд²Ù×÷£¬»Ö¸´AD7705½Ó¿Ú
}
delay_ms(2);
AD7705_WriteByte(0x20); // дͨѶ¼Ä´æÆ÷ÉèÖÃʱÖӼĴæÆ÷
AD7705_WriteByte(0x02); // 2.4576MHzʱÖÓ.100HzÊý¾Ý¸üÐÂËÙÂÊ
AD7705_WriteByte(0x10); //Ñ¡ÔñÉèÖüĴæÆ÷,ʹÓÃchnanel 1
AD7705_WriteByte(0x44); //дÉèÖüĴæÆ÷ ,ÉèÖóÉË«¼«ÐÔ¡¢ÎÞ»º³å¡¢ÔöÒæΪ1¡¢Â˲¨Æ÷¹¤×÷¡¢×ÔУ׼
}
//====================================
//º¯ÊýÃû:£Su16 GetData7705_CH1(void)
//×÷Ó㺠¶ÁAD7705ת»»Êý¾Ý
//ÊäÈ룺 ÊäÈëͨµÀchannel
//·µ»ØÖµ£º ¶ÁÈ¡Öµ
//===================================
u16 GetData7705_CH1(void)
{
u16 temp1 = 0;
u16 DataL = 0;
u16 DataH = 0;
Init_AD7705(1); //³õʼ»¯Í¨µÀ1
delay_ms(1);
AD7705_WriteByte(0x38); //Ñ¡ÖÐCH1Êý¾Ý¼Ä´æÆ÷¶Á
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8)==1)
{;} //´ýÊý¾Ý×¼±¸ºÃAdDrdy=0
delay_ms(10);
DataH = SPI1_ReadWriteByte(0xff);
DataL = SPI1_ReadWriteByte(0xff);
delay_us(200);
DataH = DataH << 8;
temp1 = DataH | DataL;
return temp1;
}
//====================================
//º¯ÊýÃû:£Sfloat Get_Data_AD7705_ch1(u8 chnanel)
//×÷Ó㺠Êý¾Ý´¦Àí....3´Î²É¼¯È¡Æ½¾ù
//ÊäÈ룺 ͨµÀÖµ
//·µ»ØÖµ£º ²É¼¯Æ½¾ùÖµ
//×¢£º×ÜÑÓʱͨµÀ1 220*6
//===================================
float Get_Data_AD7705_ch1(u8 chnanel)
{
u32 sum=0;
u16 ad_get=0;
float Get_Data_AD7705=0;
u8 i=0;
for(i=0;i<3;i++)
{
ad_get = GetData7705_CH1(); // = GetData7705_CH1();
sum=sum+ad_get;
ad_get=0;
}
Get_Data_AD7705=sum/3;
return Get_Data_AD7705;
}
老是在判断DRDY上出现问题,drdy接地却只能显示65535,大佬们看下是因为我引脚配置错了吗?spi用的是f4历程里面的
|