[数据转换器-信号链] ADS1118

[复制链接]
3018|7
 楼主| assange 发表于 2014-8-8 20:26 | 显示全部楼层 |阅读模式
  1. void SPI_Configuration(void)   //SPI配置
  2. {
  3.     GPIO_InitTypeDef GPIO_InitStructure;
  4.     SPI_InitTypeDef  SPI_InitStructure;
  5.        
  6.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,  ENABLE);
  7.        
  8.                 GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
  9.                 GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
  10.                 GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);       
  11.                 GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);               
  12.        
  13.     /* SPI1 ÅäÖÃ */
  14.     //AD_SPI1_CS  PA4      AD_SPI1_SCK  PA5      AD_SPI1_MISO PA6      AD_SPI1_MOSI PA7
  15.        
  16.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                                          
  17.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  18.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  19.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  20.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  21.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;                    
  22.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  23.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  24.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  25.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  26.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                                          
  27.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  28.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  29.                 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  30.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  32.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;           //ȫ˫¹¤
  33.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;                                  //Ö÷ģʽ
  34.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;                              //8λ
  35.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;                                         //ʱÖÓ¼«ÐÔ ¿ÕÏÐ״̬ʱ,SCK±£³ÖµÍµçƽ
  36.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;                                   //ʱÖÓÏàλ Êý¾Ý²ÉÑù´ÓµÚÒ»¸öʱÖÓ ÑØ ¿ªÊ¼
  37.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                                      //Èí¼þ²úÉúNSS
  38.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;          //²¨ÌØÂÊ¿ØÖÆ SYSCLK/16
  39.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;                                 //Êý¾ÝµØÎ»ÔÚǰ
  40.     SPI_InitStructure.SPI_CRCPolynomial = 7;                                       //CRC¶àÏîʽ¼Ä´æÆ÷³õʼֵΪ7
  41.     SPI_Init(SPI1, &SPI_InitStructure);
  42.     /* ʹÄÜSPI1  */
  43.     SPI_Cmd(SPI1, ENABLE);
  44. }ads_cs_0();              //CS LOW ÓÐЧ  //main 读取ads1118转换值
  45.   while(1)
  46.   {
  47.     ADS_InitStructure.stru.OS         = SINGLE_CONVER;
  48.                 ADS_InitStructure.stru.MUX        = AINPN_0_GND;     //CHANNEL 0 ---GND
  49.                 ADS_InitStructure.stru.PGA        = PGA_4096;
  50.                 ADS_InitStructure.stru.MODE = SINGLE_SHOT;
  51.                 ADS_InitStructure.stru.DR         = DR_128_SPS;
  52.                 ADS_InitStructure.stru.TS_MODE         = ADC_MODE;                                 //ad mode
  53.                 ADS_InitStructure.stru.PULLUP         = PULL_UP_DIS;
  54.                 ADS_InitStructure.stru.NOP                 = DATA_VALID;
  55.                 ADS_InitStructure.stru.CNV_RDY_FL = DATA_READY;
  56.                 a = SPI_RW_Reg(ADS_InitStructure.word);                                                   //write 0x8583 to ads1118  ,twice
  57.        
  58.                 while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6))                        //loop until DOUT/DRDY set low
  59.                 {
  60.                         LCD_WriteEnglishSring(0,2,"VaL not ready.");
  61.                         delay_ms(1000);
  62.                         LCD_Clear();
  63.                         delay_ms(5000);
  64.                         LCD_WriteEnglishSring(0,0,"ADS1118 TEST !");
  65.                 }
  66.                 a=SPI_RW_Reg(ADS_InitStructure.word);                                        //read data
  67.                 LCD_WriteMunber(0,4,a);
  68.         }
  69. //ads1118.c
  70. uint8_t SPI1_SendByte(uint8_t byte)
  71. {
  72.     /* Ñ­»·¼ì²â·¢ËÍ»º´æÊÇ·ñΪ¿Õ */
  73.     while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
  74.     {
  75.         ;
  76.     }
  77.     /* ͨ¹ýSPI1ÍâÉè·¢³öÊý¾Ý */
  78.     SPI_I2S_SendData(SPI1, byte);
  79.     /* µÈ´ý½ÓÊÕÊý¾Ý Ñ­»·¼ì²â·¢ËÍ»º´æ */
  80.     while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
  81.     {
  82.         ;
  83.     }
  84.     /* ·µ»Ø¶Á³öÊý¾Ý */
  85.     return SPI_I2S_ReceiveData(SPI1);
  86. }




  87. uint32_t SPI_RW_Reg(uint16_t CofigReg)
  88. {

  89.     delay_118(10);
  90.     readata1=SPI1_SendByte((uint8_t)(CofigReg>>8));
  91.     readata2=SPI1_SendByte((uint8_t)CofigReg);
  92.     readata3=SPI1_SendByte((uint8_t)(CofigReg>>8));
  93.     readata4=SPI1_SendByte((uint8_t)CofigReg);
  94.     //readata= ((uint32_t)readata4<<8)|((uint32_t)readata3);//|((uint32_t)readata2<<16)|((uint32_t)readata1<<24);
  95.                 readata= ((uint32_t)readata4<<8)|((uint32_t)readata3);
  96.     delay_118(1000);
  97.     //ads_cs_1();
  98.     return readata;
  99. }
//ads1118.h
  1. //ÅäÖüĴæÆ÷( Config Register )
  2. //Operational status/single-shot conversion start
  3. #define     CONFIG_BIT_OS       (1<<15)
  4. //MUX[2:0]: Input multiplexer configuration
  5. #define     CONFIG_BIT_MUX      (7<<12)
  6. //PGA[2:0]: Programmable gain amplifier configuration
  7. #define     CONFIG_BIT_PGA      (7<<9)
  8. //MODE: Device operating mode
  9. #define     CONFIG_BIT_MODE     (1<<8)
  10. //DR[2:0]: Data rate
  11. #define     CONFIG_BIT_DR       (7<<5)
  12. //TS_MODE: Temperature sensor mode
  13. #define     CONFIG_BIT_TS_MODE  (1<<4)
  14. //PULL_UP_EN: Pull-up enable
  15. #define     CONFIG_BIT_PULLUP_EN    (1<<3)
  16. //NOP: No operation
  17. #define     CONFIG_BIT_NOP      (3<<1)
  18. //CNV_RDY_FL: Conversion ready flag
  19. #define     CONFIG_BIT_CNV_RDY_FL   (1<<0)
  20. // ------ Public data type declarations ----------------------------
  21. typedef union
  22. {
  23.     //uint8_t Data_MSB  :8;
  24.     //uint8_t   Data_LSB    :8;
  25.     struct
  26.     {
  27.         volatile uint8_t    CNV_RDY_FL  :1;   //low
  28.         volatile uint8_t    NOP         :2;
  29.         volatile uint8_t    PULLUP      :1;
  30.         volatile uint8_t    TS_MODE     :1;
  31.         volatile uint8_t    DR          :3;
  32.         volatile uint8_t    MODE        :1;
  33.         volatile uint8_t    PGA         :3;
  34.         volatile uint8_t    MUX         :3;
  35.         volatile uint8_t    OS          :1;   //high
  36.     } stru;
  37.     volatile uint16_t word;

  38. } ADS_InitTypeDef;
  39. typedef enum
  40. {
  41.     CONVERING = 0x0,
  42.     SINGLE_CONVER = 0x1
  43. } ADS_OS_TypeDef;
  44. typedef enum
  45. {
  46.     AINPN_0_1 = 0x0,
  47.     AINPN_0_3 =   0x1,
  48.     AINPN_1_3 =   0x2,
  49.     AINPN_2_3 =   0x3,
  50.     AINPN_0_GND=  0x4,
  51.     AINPN_1_GND=  0x5,
  52.     AINPN_2_GND=  0x6,
  53.     AINPN_3_GND=  0x7
  54. } ADS_MUX_TypeDef;
  55. typedef enum
  56. {
  57.     PGA_6144 = 0x0,
  58.     PGA_4096 = 0x1,
  59.     PGA_2048 = 0x2,
  60.     PGA_1024 = 0x3,
  61.     PGA_512 = 0x4,
  62.     PGA_256 = 0x5
  63. } ADS_PGA_TypeDef;
  64. typedef enum
  65. {
  66.     CONTIOUS  =  0x0,
  67.     SINGLE_SHOT = 0x1
  68. } ADS_MODE_TypeDef;
  69. typedef enum
  70. {
  71.     DR_8_SPS   =   0x0,
  72.     DR_16_SPS  =   0x1,
  73.     DR_32_SPS  =   0x2,
  74.     DR_64_SPS  =   0x3,
  75.     DR_128_SPS =   0x4,
  76.     DR_250_SPS =   0x5,
  77.     DR_475_SPS =   0x6,
  78.     DR_860_SPS =   0x7
  79. } ADS_DATARATE_TypeDef;
  80. typedef enum
  81. {
  82.     ADC_MODE    =   0x0,
  83.     TEMPERATURE_MODE =  0x1
  84. } ADS_TSMODE_TypeDef;
  85. typedef enum
  86. {
  87.     PULL_UP_DIS = 0x0,
  88.     PULL_UP_EN  = 0x1
  89. } ADS_PULL_TypeDef;

  90. typedef enum
  91. {
  92.     DATA_VALID      = 0x1,
  93.     DATA_INVALID    = 0x0
  94. } ADS_NOP_TypeDef;
  95. typedef enum
  96. {
  97.     DATA_READY = 0x0,
  98.     DATA_NREADY = 0x1
  99. } ADS_RDY_TypeDef;
  100. // ------ Public function prototypes -------------------------------
  101. #define ads_cs_0()     GPIO_ResetBits(GPIOA, GPIO_Pin_4)
  102. #define ads_cs_1()    GPIO_SetBits(GPIOA, GPIO_Pin_4)

  103. void delay_118( uint32_t nCount );
  104. uint8_t SPI1_SendByte(uint8_t byte);
  105. uint32_t SPI_RW_Reg(uint16_t CofigReg);
在转化数据中看不明白能给指点吗 a=SPI_RW_Reg(ADS_InitStructure.word);                                        //read data    为什么是这么给寄存器赋值的啊 不应该输入ADS_InitStructure.stru吗?


angerbird 发表于 2014-8-8 23:14 | 显示全部楼层
晚上过来先参考下的啊,谢谢分享这么多的。
 楼主| assange 发表于 2014-8-9 12:32 | 显示全部楼层
angerbird 发表于 2014-8-8 23:14
晚上过来先参考下的啊,谢谢分享这么多的。

研究了之后能多多交流吗?
youluo235 发表于 2014-8-9 12:34 | 显示全部楼层
楼主应该看寄存器的相关内容啊。采集出来的数据和需要显示的和你的REF是有关系的。另外你有CCS没。参考下这个?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
数码小叶 发表于 2014-8-9 15:04 | 显示全部楼层
看看,目前手边还没1118的片子
江枫渔火 发表于 2014-8-9 19:14 | 显示全部楼层

根据你提供的代码,
ADS_InitStructure.word  和 ADS_InitStructure.stru 是联合体中的两个元素,请温故而知新C语言中有关联合体的讲解。
使用这种方式可以“零存整取”。需要修改ADC某些参数位时, 在 ADS_InitStructure.stru 中对相关位段赋值比较方便,而传输这些数据的时候,按 ADS_InitStructure.word 比较方便。 实际上他们都指向了同一个数据区域而已。

评分

参与人数 1威望 +10 收起 理由
xyz549040622 + 10

查看全部评分

 楼主| assange 发表于 2014-8-10 16:06 | 显示全部楼层
江枫渔火 发表于 2014-8-9 19:14
根据你提供的代码,
ADS_InitStructure.word  和 ADS_InitStructure.stru 是联合体中的两个元素,请温故而 ...

你的解释好犀利,这就去补习一下。谢谢
 楼主| assange 发表于 2014-8-11 14:11 | 显示全部楼层
youluo235 发表于 2014-8-9 12:34
楼主应该看寄存器的相关内容啊。采集出来的数据和需要显示的和你的REF是有关系的。另外你有CCS没。参考下这 ...

有的,只不过现在在一直到STM32上去
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

38

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部