找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32模拟I2C总线程序

[复制链接]
跳转到指定楼层
楼主
ID:302749 发表于 2018-4-6 14:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32模拟I2C总线程序,测试可用

单片机源程序如下:
  1. #include  <includes.h>
  2. #include "myiic.h"
  3. #include "delay.h"
  4. #define SCL_H         GPIOB->BSRR = GPIO_Pin_6  
  5. #define SCL_L         GPIOB->BRR  = GPIO_Pin_6   
  6.          
  7. #define SDA_H         GPIOB->BSRR = GPIO_Pin_7  
  8. #define SDA_L         GPIOB->BRR  = GPIO_Pin_7  
  9.       
  10. #define SCL_read      GPIOB->IDR  & GPIO_Pin_6  
  11. #define SDA_read      GPIOB->IDR  & GPIO_Pin_7  

  12. OS_SEM        IIC_SemLock;

  13. //初始化IIC
  14. void IIC_Init(void)
  15. {                       
  16.                 OS_ERR err = OS_ERR_NONE;
  17.                 GPIO_InitTypeDef GPIO_InitStructure;
  18.                 RCC_APB2PeriphClockCmd(        RCC_APB2Periph_GPIOB, ENABLE );       
  19.                          
  20.                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
  21.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD ;   //推挽输出
  22.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23.                 GPIO_Init(GPIOB, &GPIO_InitStructure);
  24.                 GPIO_SetBits(GPIOB,GPIO_Pin_6|GPIO_Pin_7);         //PB6,PB7 输出高
  25.                 OSSemCreate(&IIC_SemLock,"",1,&err);
  26. }

  27. void IIC_Lock(void)
  28. {
  29.                 OS_ERR err = OS_ERR_NONE;
  30.     OSSemPend(&IIC_SemLock,0,OS_OPT_PEND_BLOCKING,NULL,&err);
  31. }

  32. void IIC_UnLock(void)
  33. {
  34.                 OS_ERR err = OS_ERR_NONE;
  35.                 OSSemPost(&IIC_SemLock,OS_OPT_POST_ALL,&err);
  36. }

  37. u8 IIC_Start(void)  
  38. {  
  39.     SDA_H;  
  40.     SCL_H;  
  41.     delay_us(5);
  42.     if(!SDA_read)return 0;
  43.     SDA_L;  
  44.     delay_us(5);
  45.     if(SDA_read)return 0;  
  46.     SDA_L;  
  47.     delay_us(5);
  48.     return 1;  
  49. }  

  50. void IIC_Stop(void)  
  51. {  
  52.     SCL_L;  
  53.     delay_us(5);
  54.     SDA_L;  
  55.     delay_us(5);
  56.     SCL_H;  
  57.     delay_us(5);
  58.     SDA_H;  
  59.     delay_us(5);
  60. }  

  61. void IIC_Ack(void)  
  62. {     
  63.     SCL_L;  
  64.     delay_us(5);
  65.     SDA_L;  
  66.     delay_us(5);
  67.     SCL_H;  
  68.     delay_us(5);
  69.     SCL_L;  
  70.     delay_us(5);
  71. }  

  72. void IIC_NoAck(void)  
  73. {     
  74.     SCL_L;  
  75.     delay_us(5);
  76.     SDA_H;  
  77.     delay_us(5);
  78.     SCL_H;  
  79.     delay_us(5);
  80.     SCL_L;  
  81.     delay_us(5);
  82. }  

  83. u8 IIC_WaitAck(void)     
  84. {  
  85.                
  86.     SCL_L;  
  87.     delay_us(5);
  88.     SDA_H;            
  89.     delay_us(5);
  90.     SCL_H;  
  91.     delay_us(5);
  92.     if(SDA_read)  
  93.     {  
  94.         SCL_L;  
  95.         return 0;  
  96.     }  
  97.     SCL_L;  
  98.     return 1;  
  99. }  

  100. void IIC_SendByte(u8 SendByte)   
  101. {  
  102.     u8 i=8;  
  103.     while(i--)  
  104.     {  
  105.         SCL_L;  
  106.         delay_us(5);
  107.         if(SendByte&0x80)  
  108.             SDA_H;   
  109.         else  
  110.             SDA_L;     
  111.         SendByte<<=1;  
  112.         delay_us(5);
  113.         SCL_H;  
  114.         delay_us(5);
  115.     }  
  116.     SCL_L;  
  117. }  

  118. u8 IIC_ReceiveByte(void)   
  119. {   
  120.     u8 i=8;  
  121.     u8 ReceiveByte=0;  
  122.     SDA_H;               
  123.     while(i--)  
  124.     {  
  125.         ReceiveByte<<=1;        
  126.         SCL_L;  
  127.         delay_us(5);
  128.         SCL_H;  
  129.         delay_us(5);     
  130.         if(SDA_read)  
  131.         {  
  132.             ReceiveByte|=0x01;  
  133.         }  
  134.     }  
  135.     SCL_L;  
  136.     return ReceiveByte;  
  137. }  

  138. u8 IIC_BufferRead(u8* pBuffer,u16 length,u8 DeviceAddress)  
  139. {  
  140.     if(!IIC_Start())return 0;  
  141.     IIC_SendByte(DeviceAddress|0x01);  
  142.     if(!IIC_WaitAck())  
  143.                 {  
  144.                                 IIC_Stop();   
  145.                                 return 0;  
  146.                 }
  147.                 while(length)
  148.                 {
  149.                                 *pBuffer++=IIC_ReceiveByte();
  150.                                 length--;
  151.                                 if(length>0)IIC_Ack();
  152.                                 else IIC_NoAck();
  153.                 }
  154.     IIC_Stop();
  155.                 delay_ms(1);
  156.     return 1;  
  157. }

  158. u8 IIC_BufferWrite(u8* pBuffer, u16 length, u8 DeviceAddress)  
  159. {  
  160.     if(!IIC_Start())return 0;  
  161.     IIC_SendByte(DeviceAddress);
  162.     if(!IIC_WaitAck())  
  163.                 {  
  164.                                 IIC_Stop();   
  165.                                 return 0;  
  166.                 }  
  167.                 while(length)
  168.                 {
  169.                                 IIC_SendByte(* pBuffer);  
  170.                                 IIC_WaitAck();  
  171.                                 length--;
  172.                                 pBuffer++;
  173.                 }
  174.     IIC_Stop();
  175.                 delay_ms(5);
  176.     return 1;  
  177. }
  178. ……………………

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

所有资料51hei提供下载:
IIC.rar (1.86 KB, 下载次数: 36)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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