- void SPI_Configuration(void) //SPI配置
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
-
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
-
- /* SPI1 ÅäÖÃ */
- //AD_SPI1_CS PA4 AD_SPI1_SCK PA5 AD_SPI1_MISO PA6 AD_SPI1_MOSI PA7
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ȫ˫¹¤
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //Ö÷ģʽ
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8λ
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //ʱÖÓ¼«ÐÔ ¿ÕÏÐ״̬ʱ,SCK±£³ÖµÍµçƽ
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //ʱÖÓÏàλ Êý¾Ý²ÉÑù´ÓµÚÒ»¸öʱÖÓ ÑØ ¿ªÊ¼
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //Èí¼þ²úÉúNSS
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //²¨ÌØÂÊ¿ØÖÆ SYSCLK/16
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Êý¾ÝµØÎ»ÔÚǰ
- SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC¶àÏîʽ¼Ä´æÆ÷³õʼֵΪ7
- SPI_Init(SPI1, &SPI_InitStructure);
- /* ʹÄÜSPI1 */
- SPI_Cmd(SPI1, ENABLE);
- }ads_cs_0(); //CS LOW ÓÐЧ //main 读取ads1118转换值
- while(1)
- {
- ADS_InitStructure.stru.OS = SINGLE_CONVER;
- ADS_InitStructure.stru.MUX = AINPN_0_GND; //CHANNEL 0 ---GND
- ADS_InitStructure.stru.PGA = PGA_4096;
- ADS_InitStructure.stru.MODE = SINGLE_SHOT;
- ADS_InitStructure.stru.DR = DR_128_SPS;
- ADS_InitStructure.stru.TS_MODE = ADC_MODE; //ad mode
- ADS_InitStructure.stru.PULLUP = PULL_UP_DIS;
- ADS_InitStructure.stru.NOP = DATA_VALID;
- ADS_InitStructure.stru.CNV_RDY_FL = DATA_READY;
- a = SPI_RW_Reg(ADS_InitStructure.word); //write 0x8583 to ads1118 ,twice
-
- while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6)) //loop until DOUT/DRDY set low
- {
- LCD_WriteEnglishSring(0,2,"VaL not ready.");
- delay_ms(1000);
- LCD_Clear();
- delay_ms(5000);
- LCD_WriteEnglishSring(0,0,"ADS1118 TEST !");
- }
- a=SPI_RW_Reg(ADS_InitStructure.word); //read data
- LCD_WriteMunber(0,4,a);
- }
- //ads1118.c
- uint8_t SPI1_SendByte(uint8_t byte)
- {
- /* Ñ»·¼ì²â·¢ËÍ»º´æÊÇ·ñΪ¿Õ */
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
- {
- ;
- }
- /* ͨ¹ýSPI1ÍâÉè·¢³öÊý¾Ý */
- SPI_I2S_SendData(SPI1, byte);
- /* µÈ´ý½ÓÊÕÊý¾Ý Ñ»·¼ì²â·¢ËÍ»º´æ */
- while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
- {
- ;
- }
- /* ·µ»Ø¶Á³öÊý¾Ý */
- return SPI_I2S_ReceiveData(SPI1);
- }
- uint32_t SPI_RW_Reg(uint16_t CofigReg)
- {
- delay_118(10);
- readata1=SPI1_SendByte((uint8_t)(CofigReg>>8));
- readata2=SPI1_SendByte((uint8_t)CofigReg);
- readata3=SPI1_SendByte((uint8_t)(CofigReg>>8));
- readata4=SPI1_SendByte((uint8_t)CofigReg);
- //readata= ((uint32_t)readata4<<8)|((uint32_t)readata3);//|((uint32_t)readata2<<16)|((uint32_t)readata1<<24);
- readata= ((uint32_t)readata4<<8)|((uint32_t)readata3);
- delay_118(1000);
- //ads_cs_1();
- return readata;
- }
//ads1118.h
- //ÅäÖüĴæÆ÷( Config Register )
- //Operational status/single-shot conversion start
- #define CONFIG_BIT_OS (1<<15)
- //MUX[2:0]: Input multiplexer configuration
- #define CONFIG_BIT_MUX (7<<12)
- //PGA[2:0]: Programmable gain amplifier configuration
- #define CONFIG_BIT_PGA (7<<9)
- //MODE: Device operating mode
- #define CONFIG_BIT_MODE (1<<8)
- //DR[2:0]: Data rate
- #define CONFIG_BIT_DR (7<<5)
- //TS_MODE: Temperature sensor mode
- #define CONFIG_BIT_TS_MODE (1<<4)
- //PULL_UP_EN: Pull-up enable
- #define CONFIG_BIT_PULLUP_EN (1<<3)
- //NOP: No operation
- #define CONFIG_BIT_NOP (3<<1)
- //CNV_RDY_FL: Conversion ready flag
- #define CONFIG_BIT_CNV_RDY_FL (1<<0)
- // ------ Public data type declarations ----------------------------
- typedef union
- {
- //uint8_t Data_MSB :8;
- //uint8_t Data_LSB :8;
- struct
- {
- volatile uint8_t CNV_RDY_FL :1; //low
- volatile uint8_t NOP :2;
- volatile uint8_t PULLUP :1;
- volatile uint8_t TS_MODE :1;
- volatile uint8_t DR :3;
- volatile uint8_t MODE :1;
- volatile uint8_t PGA :3;
- volatile uint8_t MUX :3;
- volatile uint8_t OS :1; //high
- } stru;
- volatile uint16_t word;
- } ADS_InitTypeDef;
- typedef enum
- {
- CONVERING = 0x0,
- SINGLE_CONVER = 0x1
- } ADS_OS_TypeDef;
- typedef enum
- {
- AINPN_0_1 = 0x0,
- AINPN_0_3 = 0x1,
- AINPN_1_3 = 0x2,
- AINPN_2_3 = 0x3,
- AINPN_0_GND= 0x4,
- AINPN_1_GND= 0x5,
- AINPN_2_GND= 0x6,
- AINPN_3_GND= 0x7
- } ADS_MUX_TypeDef;
- typedef enum
- {
- PGA_6144 = 0x0,
- PGA_4096 = 0x1,
- PGA_2048 = 0x2,
- PGA_1024 = 0x3,
- PGA_512 = 0x4,
- PGA_256 = 0x5
- } ADS_PGA_TypeDef;
- typedef enum
- {
- CONTIOUS = 0x0,
- SINGLE_SHOT = 0x1
- } ADS_MODE_TypeDef;
- typedef enum
- {
- DR_8_SPS = 0x0,
- DR_16_SPS = 0x1,
- DR_32_SPS = 0x2,
- DR_64_SPS = 0x3,
- DR_128_SPS = 0x4,
- DR_250_SPS = 0x5,
- DR_475_SPS = 0x6,
- DR_860_SPS = 0x7
- } ADS_DATARATE_TypeDef;
- typedef enum
- {
- ADC_MODE = 0x0,
- TEMPERATURE_MODE = 0x1
- } ADS_TSMODE_TypeDef;
- typedef enum
- {
- PULL_UP_DIS = 0x0,
- PULL_UP_EN = 0x1
- } ADS_PULL_TypeDef;
- typedef enum
- {
- DATA_VALID = 0x1,
- DATA_INVALID = 0x0
- } ADS_NOP_TypeDef;
- typedef enum
- {
- DATA_READY = 0x0,
- DATA_NREADY = 0x1
- } ADS_RDY_TypeDef;
- // ------ Public function prototypes -------------------------------
- #define ads_cs_0() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
- #define ads_cs_1() GPIO_SetBits(GPIOA, GPIO_Pin_4)
- void delay_118( uint32_t nCount );
- uint8_t SPI1_SendByte(uint8_t byte);
- uint32_t SPI_RW_Reg(uint16_t CofigReg);
在转化数据中看不明白能给指点吗 a=SPI_RW_Reg(ADS_InitStructure.word); //read data 为什么是这么给寄存器赋值的啊 不应该输入ADS_InitStructure.stru吗?