您好,歡迎訪問上海意泓電子科技有限責任公司網(wǎng)站!
      4新聞資訊
      您的位置: 首頁 ->  新聞資訊 -> 單片機

      ?STM8S 模擬I2C程序

      文章出處:單片機 責任編輯:上海意泓電子科技有限責任公司 發(fā)表時間:
      2018
      05-20

      STM8S的硬件I2C還是存在問題,不敢貿(mào)然使用.

      #define SCL PE_ODR_ODR1
      #define SDA PE_ODR_ODR2
      #define SDAM PE_IDR_IDR2
      #define SET_SCL_OUT() {PE_DDR_DDR1=1; PE_CR1_C11 = 1; PE_CR2_C21 = 0;}
      #define SET_SDA_OUT() {PE_DDR_DDR2=1; PE_CR1_C12 = 1; PE_CR2_C22 = 0;}
      #define SET_SDA_IN() {PE_DDR_DDR2=0; PE_CR1_C12 = 0; PE_CR2_C22 = 0;}
      //--------------------------------------------------------------
      // Prototype : void I2C_Start(void)
      // Calls : Delay_5us()
      // Description : Start Singnal
      //--------------------------------------------------------------
      void I2C_Init(void)
      {
      I2C_CR1 &= 0xFE; // PE=0, disable I2C module 
      SET_SCL_OUT();
      SET_SDA_OUT(); 
      }

      //--------------------------------------------------------------
      // Prototype : void Delay_5us(void)
      // Description : 大約延時5us
      //--------------------------------------------------------------
      void Delay_5us(void)
      {
      uchar i; //fcpu 8MHz 時
      for (i=50; i>0; i--);
      }

      //--------------------------------------------------------------
      // Prototype : void I2C_Start(void)
      // Calls : Delay_5us()
      // Description : Start Singnal
      //--------------------------------------------------------------
      void I2C_Start(void)
      {
      // SDA 1->0 while SCL High
      SDA = 1; 
      SCL = 1; 
      Delay_5us(); 
      SDA = 0; 
      Delay_5us(); 
      SCL = 0; 
      }

      //--------------------------------------------------------------
      // Prototype : void I2C_Stop(void)
      // Calls : Delay_5us()
      // Description : Stop Singnal
      //-------------------------------------------------------------- 
      void I2C_Stop(void)
      {
      // SDA 0->1 while SCL High
      SDA = 0; 
      SCL = 1; 
      Delay_5us(); 
      SDA = 1; 
      Delay_5us(); 
      }
      //--------------------------------------------------------------
      // Prototype : void I2C_SendACK(uchar ack);
      // Calls : Delay_5us()
      // Parameters : bit ack:1-noack, 0-ack
      // Description : Master device send ACK to slave device.
      //--------------------------------------------------------------
      void I2C_SendACK(uchar ack)
      {
      if(ack == 0)
      {
      SDA = 0; 

      else
      {
      SDA = 1; 
      }

      SCL = 1; 
      Delay_5us(); 

      SCL = 0; 
      Delay_5us(); 
      }
      //--------------------------------------------------------------
      // Prototype : uchar I2C_SendByte(uchar sendDAT)
      // Calls : Delay_5us()
      // Parameters : uchar sendDAT---data to be send
      // Return Value : CY--slave ack (1---noack,0---ack)
      // Description : Send one byte to I2C
      //--------------------------------------------------------------
      uchar I2C_SendByte(uchar sendDAT)
      {
      uchar i,revack;
      for (i= 0 ; i< 8; i++) 
      {
      SCL = 0; 
      Delay_5us(); 
      if (sendDAT & 0x80) // write data
      {
      SDA = 1;
      }
      else 
      {
      SDA = 0;
      }
      sendDAT <<= 1;
      SCL = 1; 
      Delay_5us(); 


      SCL = 0; 
      Delay_5us(); 

      SET_SDA_IN();

      SCL = 1; 
      asm("nop");
      asm("nop");

      revack = (uchar)SDAM;

      Delay_5us(); 
      SCL = 0; 

      SET_SDA_OUT();

      Delay_5us(); 
      return revack;
      }

      //--------------------------------------------------------------
      // Prototype : uchar I2C_RecvByte()
      // Calls : Delay_5us()
      // Parameters : none
      // Return Value : revDAT- received data
      // Description : Receive one byte from I2C
      //--------------------------------------------------------------
      uchar I2C_RecvByte()
      {
      uchar i;
      uchar revDAT = 0;

      SDA = 1; // latch the Data port befor reading

      SET_SDA_IN();

      for (i=0; i<8; i++) 

      revDAT <<= 1;

      SCL = 1; 

      asm("nop");
      asm("nop");

      if (SDAM)
      {
      revDAT |= 0x01;
      }
      else
      {
      revDAT &= 0xfe;
      }
      Delay_5us(); 
      SCL = 0; 
      Delay_5us(); 
      }

      SET_SDA_OUT();

      return revDAT;
      }

      //--------------------------------------------------------------
      // Prototype : void Write_PT2314(uchar wdata)
      // Calls : I2C_Start(), I2C_Stop(),I2C_SendByte()
      // Parameters : RegAddr-- target memory address, 
      // wrdata--- data to be writing
      // Description : Write one byte to target memory
      //--------------------------------------------------------------
      void Write_24c02(uchar regadd,uchar wdata)
      {
      I2C_Start(); 
      I2C_SendByte(0xA0); // Device Addr + Write (operation) 
      I2C_SendByte(regadd); 
      I2C_SendByte(wdata); 
      I2C_Stop(); 
      }
      uchar Read_24c02(uchar regadd)
      {
      uchar revdata;

      I2C_Start(); 
      I2C_SendByte(0xA0); // Device Addr + Write (operation) 
      I2C_SendByte(regadd); 

      I2C_Start(); 
      I2C_SendByte(0xA1); // Device Addr + Write (operation) 

      revdata = I2C_RecvByte(); 
      I2C_SendACK(1);

      I2C_Stop(); 

      return revdata;
      }


      上海意泓電子科技有限責任公司 版權所有 未經(jīng)授權禁止復制或鏡像

      CopyRight 2020-2025 www.topsschool.com All rights reserved   滬ICP備2021005866號

      亚洲精品无码va人在线观看,少妇 逼小 水多,国产区免费av,国产精品无码观看

          久久精品欧美美99亚洲洲在| 亚洲 欧美 综合 精品 在线| 一级国产精品 免费 观看| 国产精品一二三区免费播放| 五月婷婷丁香综合中文字幕| 久久久久久久国产| 国产成人精品国内自产色| 亚洲欧美不卡高清在线观看| 日韩高清无码一区| 潮喷视频无码一区| 亚洲精品无码av中文字幕电影网站| 日本综合一区二区人妻| 午夜成人无码在线播放| 久久超碰熟女中文字幕| 亚洲无码高清一区二区三区视频| 亚洲免费性色无码视频| 女教师巨大乳孔中文字幕| 制服丝袜中文字幕国内自拍| 亚洲综合色噜噜狠狠99| 亚洲精品天堂在线观看2020| 国产片免费福利片永久不卡| 成人精品国产日本语音| 久久人妻少妇嫩草AV无码专区| 亚洲一区久久| 亚洲三级视频在线观看| 亚洲无码在线精品色资源| 青青青国产在线观看| 国产一级黄| 免费**毛片免费观看欧美大片|