久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4079|回復: 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, 下載次數: 42)


評分

參與人數 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 一区日韩 | 久久精品一区二区三区四区 | 9久久精品 | 爱爱视频日本 | 一级全黄少妇性色生活免费看 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 紧缚调教一区二区三区视频 | 日韩伦理电影免费在线观看 | 欧美日韩成人 | 欧美aⅴ片 | 超碰免费在 | 国产精品日日做人人爱 | 亚洲欧美一区二区三区国产精品 | 7777精品伊人久久精品影视 | 黄色成人在线观看 | 日韩精品在线看 | 玖玖视频 | 成人综合视频在线 | 一区二区三区中文字幕 | 亚洲精品久久久久久久久久久久久 | 亚洲欧美一区二区三区国产精品 | 91在线观看| 亚洲第一天堂无码专区 | 欧美精品在线视频 | 日本三级电影在线观看视频 | 亚洲码欧美码一区二区三区 | 特级黄一级播放 | 国产三级大片 | 五月天激情综合网 | 国产精品视频专区 | 免费日韩网站 | www.887色视频免费 | 日韩精品一区二区久久 | 酒色成人网 | 国产视频中文字幕在线观看 | 久久久精品视频一区二区三区 | 日韩一区二区免费视频 | 久久精品色视频 | 成人午夜电影网 | 干干天天 | 久久久久久久久久久久久久国产 |