找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3013|回复: 6
打印 上一主题 下一主题
收起左侧

ADS7142的STM32模拟IIC源程序

[复制链接]
跳转到指定楼层
楼主
ADS7142的IIC软件与普通的存在小小的差别,多了一个opcodd配置。详细的请看附件~


单片机源程序如下:
  1. #include "i2c.h"
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "data.h"
  5. #include "env.h"
  6. #include "atc.h"
  7. #include "ads7142.h"


  8. #define SCL_H       GPIO_SetBits(GPIOB, GPIO_Pin_6)
  9. #define SCL_L       GPIO_ResetBits(GPIOB, GPIO_Pin_6)

  10. #define SDA_H       GPIO_SetBits(GPIOB, GPIO_Pin_7)
  11. #define SDA_L       GPIO_ResetBits(GPIOB, GPIO_Pin_7)

  12. #define SDA_read    GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)

  13. #define BUSY_read   GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14)



  14. static void DelayNus(uint32_t N)
  15. {
  16.         uint8_t i;
  17.           do{
  18.                 for(i=6;i>0;i--) //8MHZ:=7; 12MHZ:=11
  19.                         ;
  20.             }while(--N);
  21. }



  22. /**
  23.   * @brief  IIC Init
  24.   * @param  None
  25.   * @retval None
  26.   */
  27. static void ads7142_gpio_init(void)
  28. {
  29.     //I2C_InitTypeDef I2C_InitStructure;
  30.     GPIO_InitTypeDef  GPIO_InitStructure;

  31.     /* Configure IIC2 pins: PB10->SCL and PB11->SDA */
  32.            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  33.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

  34.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  35.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  36.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  37.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  38.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  39.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  40.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;  // PA1 -- ADC Channel 1 -- Work
  41.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  42.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  43.         GPIO_Init(GPIOB, &GPIO_InitStructure);  
  44. }


  45. /**
  46.   * @brief  Set SDA Pin as Output Mode
  47.   * @param  None
  48.   * @retval None
  49.   */  
  50. static void SDA_OUT(void)   
  51. {   
  52.     GPIO_InitTypeDef  GPIO_InitStructure;
  53.    
  54.     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;   
  55.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;   
  56.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;   
  57.     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  58.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
  59.     GPIO_Init(GPIOB, &GPIO_InitStructure);   
  60. }   


  61. /**
  62.   * @brief  Set SDA Pin as Input Mode
  63.   * @param  None
  64.   * @retval None
  65.   */  
  66. static void SDA_IN()   
  67. {   
  68.     GPIO_InitTypeDef  GPIO_InitStructure;
  69.    
  70.     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;   
  71.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;  
  72.     GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;// !!!  
  73.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     
  74.     GPIO_Init(GPIOB, &GPIO_InitStructure);   
  75. }   
  76.   
  77. /**
  78.   * @brief  create start signal
  79.   * @param  None
  80.   * @retval 1 or 0
  81.   */
  82. static uint8_t IIC_Start(void)
  83. {
  84.     SDA_OUT();     //sda output  
  85.         SDA_H;
  86.         SCL_H;
  87.         DelayNus(6);
  88.         if (!SDA_read)
  89.         {
  90.                 return 0;
  91.         }
  92.         SDA_L;
  93.         DelayNus(6);
  94.         if (SDA_read)
  95.         {
  96.                 return 0;
  97.         }
  98.         SDA_L;
  99.         DelayNus(6);
  100.         return 1;
  101. }


  102. /**
  103.   * @brief  Simulate IIC conmunication : Create Stop signal
  104.   * @param  None
  105.   * @retval None
  106.   */  
  107. static void IIC_Stop(void)
  108. {
  109.         SCL_L;
  110.     SDA_OUT();    //sda output mode  
  111.         DelayNus(6);
  112.         SDA_L;
  113.         DelayNus(6);
  114.         SCL_H;
  115.         DelayNus(6);
  116.         SDA_H;
  117.         DelayNus(6);
  118. }


  119. /**
  120.   * @brief  Simulate IIC conmunication : wait for target device's ACK
  121.   * @param  None
  122.   * @retval ACK (1) : receive success
  123.   *         NACK(0) : receive unsuccess
  124.   */  
  125. static uint8_t IIC_WaitAck(void)
  126. {
  127.     uint8_t err_time = 0;
  128.         SCL_L;
  129.     SDA_IN();      //set as input mode  
  130.         DelayNus(6);
  131.         SDA_H;                       
  132.         DelayNus(6);
  133.         SCL_H;
  134.         DelayNus(6);
  135.         while(SDA_read)
  136.         {
  137.         err_time++;

  138.         if (err_time > 250)
  139.         {
  140.                 printf("ERROR:IIC_WaitAck()->SDA_read\n\r");
  141.             return 0;
  142.         }
  143.         }
  144.         SCL_L;
  145.         return 1;
  146. }


  147. /**
  148.   * @brief  Simulate IIC conmunication : make an ACK
  149.   * @param  None
  150.   * @retval None
  151.   */  
  152. static void IIC_Ack(void)
  153. {
  154.         SCL_L;
  155.     SDA_OUT();  
  156.         DelayNus(6);
  157.         SDA_L;
  158.         DelayNus(6);
  159.         SCL_H;
  160.         DelayNus(6);
  161.         SCL_L;
  162.         DelayNus(6);
  163. }


  164. /**
  165.   * @brief  Simulate IIC conmunication : don't make an ACK
  166.   * @retval None
  167.   */  
  168. static void IIC_NoAck(void)
  169. {       
  170.         SCL_L;
  171.         SDA_OUT();
  172.     DelayNus(6);
  173.         SDA_H;
  174.         DelayNus(6);
  175.         SCL_H;
  176.         DelayNus(6);
  177.         SCL_L;
  178.         DelayNus(6);
  179. }


  180. /**
  181.   * @brief  Simulate IIC conmunication : Transmit one byte Data
  182.   * @param  SendByte: data to be transmit
  183.   * @retval None
  184.   */  
  185. static void IIC_SendByte(uint8_t SendByte)
  186. {
  187.     uint8_t i=8;
  188.     SDA_OUT();
  189.     while(i--)
  190.     {
  191.         SCL_L;
  192.         DelayNus(6);
  193.         if(SendByte&0x80)
  194.             SDA_H;
  195.         else
  196.             SDA_L;
  197.           
  198.         SendByte <<= 1;
  199.         DelayNus(6);
  200.                 SCL_H;
  201.         DelayNus(6);
  202.     }
  203.     SCL_L;
  204. }


  205. /**
  206.   * @brief  Simulate IIC conmunication : Receive one byte Data
  207.   * @param  ack ->Whether transmit ACK
  208.   * @retval the data have been receive
  209.   */  
  210. static uint8_t IIC_ReceiveByte(void)
  211. {
  212.     uint8_t i=8;
  213.     uint8_t ReceiveByte=0;

  214.     SDA_H;                               
  215.     while(i--)
  216.     {
  217.         ReceiveByte<<=1;      
  218.         SCL_L;
  219.         DelayNus(4);
  220.             SCL_H;
  221.         DelayNus(4);       
  222.         if(SDA_read)
  223.         {
  224.             ReceiveByte |= 0x01;
  225.         }
  226.     }
  227.     SCL_L;
  228.     return ReceiveByte;
  229. }


  230. /**
  231.   * @brief  IIC write byte to device
  232.   * @param  SendByte        ->the write data
  233.   *         WriteAddress    ->device reg address
  234.   *         DeviceAddresss  ->device iic address
  235.   * @retval 1 or 0
  236.   */
  237. static uint8_t IIC_WriteByte(uint8_t SendByte, uint16_t WriteAddress, uint8_t DeviceAddress)
  238. {               
  239.     if(!IIC_Start())
  240.     {
  241.         printf("ERROR:IIC_WriteByte()->IIC_Start()\n\r");
  242.                 return 0;
  243.     }
  244.    
  245.     IIC_SendByte( DeviceAddress & 0xFE);                // iic write address

  246.     if(!IIC_WaitAck())
  247.         {
  248.                 IIC_Stop();
  249.                 printf("ERROR:IIC_WriteByte()->IIC_SendByte()\n\r");
  250.             return 0;
  251.         }
  252.    
  253.     IIC_SendByte(SINGLE_REG_WRITE);
  254.     if(!IIC_WaitAck())
  255.     {
  256.         IIC_Stop();
  257.         printf("write ERROR:SINGLE_REG_READ\n\r");
  258.         return 0;
  259.     }

  260.    
  261.     IIC_SendByte((uint8_t)((WriteAddress) & 0xFF));     // reg address     
  262.     IIC_WaitAck();        
  263.     IIC_SendByte(SendByte);                             // write data
  264.     IIC_WaitAck();   
  265.     IIC_Stop();

  266.     //DelayNus(5000); // test

  267.     return 1;
  268. }


  269. /**
  270.   * @brief  IIC read byte from device reg
  271.   * @param  pBuffer         ->store address for read data
  272.   *         length          ->could read 1byte or continuously
  273.   *         WriteAddress    ->device reg address
  274.   *         DeviceAddresss  ->device iic address
  275.   * @retval 1 or 0
  276.   */
  277. static uint8_t IIC_ReadByte(uint8_t* pBuffer,uint8_t length,uint8_t ReadAddress, uint8_t DeviceAddress)
  278. {               
  279.     if(!IIC_Start())
  280.     {
  281.                 printf("ERROR:IIC_ReadByte()->IIC_Start()\n\r");
  282.                 return 0;
  283.     }

  284.     IIC_SendByte((DeviceAddress & 0xFE));   // iic write address
  285.     if(!IIC_WaitAck())
  286.         {
  287.                 IIC_Stop();
  288.                 printf("read ERROR:IIC_ReadByte()->IIC_SendByte()\n\r");
  289.             return 0;
  290.         }

  291.    
  292.     IIC_SendByte(SINGLE_REG_READ);   // single register read
  293.     if(!IIC_WaitAck())
  294.     {
  295.         IIC_Stop();
  296.         printf("read ERROR:SINGLE_REG_READ\n\r");
  297.         return 0;
  298.     }

  299.     IIC_SendByte((uint8_t)(ReadAddress & 0xFF));  // reg address
  300.     IIC_WaitAck();
  301.     IIC_Start();
  302.     IIC_SendByte((DeviceAddress & 0xFE) | 0x01);    // iic read address
  303.     IIC_WaitAck();
  304.    
  305.     while (length)
  306.     {
  307.         *pBuffer = IIC_ReceiveByte();
  308.         if(length == 1)
  309.                       IIC_NoAck();
  310.         else
  311.                       IIC_Ack();
  312.         pBuffer++;
  313.         length--;
  314.     }
  315.    
  316.     IIC_Stop();
  317.    
  318.     return 1;
  319. }


  320. /**
  321.   * @brief  ads7142 register read
  322.   * @param  reg_addr ->the register address
  323.   *         buf      ->data store address
  324.   *         length   ->read length
  325.   * @retval 1 or 0
  326.   */
  327. int ads7142_register_single_read(uint8_t reg_addr, uint8_t *buf)
  328. {
  329.     int res = 0;
  330.    
  331.     res = IIC_ReadByte(buf, 1, reg_addr, ADS7142_I2C_ADDRESS);

  332.     return res;
  333. }


  334. /**
  335.   * @brief  ads7142 register write
  336.   * @param  reg_addr ->the register address
  337.   *         data     ->write data
  338.   * @retval 1 or 0
  339.   */
  340. int ads7142_register_single_write(uint8_t reg_addr, uint8_t data)
  341. {
  342.     int res = 0;

  343.     res = IIC_WriteByte(data, reg_addr, ADS7142_I2C_ADDRESS);
  344.     if(res == 0)
  345.     {
  346.         printf ("ads7142 write error\r\n");
  347.     }

  348.     return res;
  349. }


  350. int ADS7142Calibrate(void)
  351. {
  352.     //This function aborts the present conversion sequence and triggers offset calibration

  353.     //Abort the present sequence
  354.     ads7142_register_single_write(ADS7142_REG_ABORT_SEQUENCE, ADS7142_VAL_ABORT_SEQUENCE);

  355.     //Perform Offset Calibration
  356.     ads7142_register_single_write(ADS7142_REG_OFFSET_CAL, ADS7142_VAL_TRIG_OFFCAL);

  357.     //Return no errors
  358.     return 0;
  359. }


  360. int ads7142_init(void)
  361. {
  362.     uint8_t tmp = 0;
  363.    
  364.     //DelayNus(100000);
  365.     ads7142_gpio_init();

  366.     //Calibrate the offset out of ADS7142
  367.     ADS7142Calibrate();

  368.     //Let's put the ADS7142 into High Precision Mode with both channels enabled in Single-Ended Configuration
  369.     //Select the channel input configuration
  370.     ads7142_register_single_write(ADS7142_REG_CHANNEL_INPUT_CFG, ADS7142_VAL_CHANNEL_INPUT_CFG_2_CHANNEL_SINGLE_ENDED);

  371.     //Select the operation mode of the device
  372.     ads7142_register_single_read(ADS7142_REG_CHANNEL_INPUT_CFG, &tmp);
  373.     printf ("set 0x24 = %02x\r\n", tmp);


  374.     //Select the operation mode of the device
  375.     ads7142_register_single_write(ADS7142_REG_OPMODE_SEL, ADS7142_VAL_OPMODE_SEL_HIGH_PRECISION_MODE);

  376.     //Confirm the operation mode selection
  377.     ads7142_register_single_read(ADS7142_REG_OPMODE_SEL, &tmp);
  378.     printf ("set 0x1C = %02x\r\n", tmp);

  379.     //ads7142_register_read(ADS7142_REG_OPMODE_I2CMODE_STATUS, &tmp, 1);

  380.     //Auto Sequence both channels 0 and 1
  381.     ads7142_register_single_write(ADS7142_REG_AUTO_SEQ_CHEN, ADS7142_VAL_AUTO_SEQ_CHENAUTO_SEQ_CH0_CH1);//ADS7142_VAL_AUTO_SEQ_CHENAUTO_SEQ_CH1);//

  382.     //Confirm Auto Sequencing is enabled
  383.     ads7142_register_single_read(ADS7142_REG_AUTO_SEQ_CHEN, &tmp);
  384.     printf ("set 0x20 = %02x\r\n", tmp);

  385.     //Select the Low Power Oscillator or high speed oscillator
  386.     ads7142_register_single_write(ADS7142_REG_OSC_SEL, ADS7142_VAL_OSC_SEL_HSZ_HSO);
  387. ……………………

  388. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
ads7142.rar (5.97 KB, 下载次数: 40)


评分

参与人数 1黑币 +5 收起 理由
樱木花道2018 + 5

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:390977 发表于 2018-8-28 21:55 | 只看该作者
有参考价值
回复

使用道具 举报

板凳
ID:394139 发表于 2018-9-6 10:46 | 只看该作者
刚好在做这个芯片,多谢分享
回复

使用道具 举报

地板
ID:394139 发表于 2018-9-6 10:52 | 只看该作者
刚好在做7142,多谢分享
回复

使用道具 举报

5#
ID:561490 发表于 2019-6-13 09:25 | 只看该作者
下来看看!!!
回复

使用道具 举报

6#
ID:561958 发表于 2019-6-13 17:45 | 只看该作者
努力学习中。感谢分享
回复

使用道具 举报

7#
ID:1113825 发表于 2024-3-22 16:24 | 只看该作者
感谢,非常有参考价值
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表