zoukankan      html  css  js  c++  java
  • 软件模拟I2C时输入与输出切换

    一 为达到类似C51的操作需要添加以下位带操作:#include "stm32f10x_gpio.h"
    #include "stm32f10x_conf.h"

    /* IO地址映射*/
    #define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C
    #define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C
    #define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C
    #define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C
    #define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C
    #define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C    
    #define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C    

    #define GPIOA_IDR_Addr    (GPIOA_BASE+8) //0x40010808
    #define GPIOB_IDR_Addr    (GPIOB_BASE+8) //0x40010C08
    #define GPIOC_IDR_Addr    (GPIOC_BASE+8) //0x40011008
    #define GPIOD_IDR_Addr    (GPIOD_BASE+8) //0x40011408
    #define GPIOE_IDR_Addr    (GPIOE_BASE+8) //0x40011808
    #define GPIOF_IDR_Addr    (GPIOF_BASE+8) //0x40011A08
    #define GPIOG_IDR_Addr    (GPIOG_BASE+8) //0x40011E08

    /* 输入 */
    #define PAin(n)        *((volatile unsigned long *)(0x42000000+((GPIOA_IDR_Addr-0x40000000)<<5)+(n<<2)))
    #define PBin(n)        *((volatile unsigned long *)(0x42000000+((GPIOB_IDR_Addr-0x40000000)<<5)+(n<<2)))
    #define PCin(n)        *((volatile unsigned long *)(0x42000000+((GPIOC_IDR_Addr-0x40000000)<<5)+(n<<2)))
    #define PDin(n)        *((volatile unsigned long *)(0x42000000+((GPIOD_IDR_Addr-0x40000000)<<5)+(n<<2)))
    #define PEin(n)        *((volatile unsigned long *)(0x42000000+((GPIOE_IDR_Addr-0x40000000)<<5)+(n<<2)))
    #define PFin(n)        *((volatile unsigned long *)(0x42000000+((GPIOF_IDR_Addr-0x40000000)<<5)+(n<<2)))

    /* 输出 */
    #define PAout(n)    *((volatile unsigned long *)(0x42000000+((GPIOA_ODR_Addr-0x40000000)<<5)+(n<<2)))
    #define PBout(n)    *((volatile unsigned long *)(0x42000000+((GPIOB_ODR_Addr-0x40000000)<<5)+(n<<2)))
    #define PCout(n)    *((volatile unsigned long *)(0x42000000+((GPIOC_ODR_Addr-0x40000000)<<5)+(n<<2)))
    #define PDout(n)    *((volatile unsigned long *)(0x42000000+((GPIOD_ODR_Addr-0x40000000)<<5)+(n<<2)))
    #define PEout(n)    *((volatile unsigned long *)(0x42000000+((GPIOE_ODR_Addr-0x40000000)<<5)+(n<<2)))

    二 假设SDA=PB0,SCL=PB1:

    #define SDA_IN()  {GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=8<<0;} /*也就是对应的GPIO输入输出配置寄存器进行操作:PB0的输入上拉*/
    #define SDA_OUT() {GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=3<<0;}/*PB0的上拉,输出速度50MHZ*/
    #define IIC_SCL    PBout(1) /*相当于操作函数GPIO_WriteBit*/
    #define IIC_SDA    PBout(0)         
    #define READ_SDA   PBin(0)  /*相当于操作函数GPIO_ReadInputDataBit*/

  • 相关阅读:
    异构网络中的并行传输问题
    如何编程实现快速获取一个整型数中的bit流中1的个数
    对单例模式的一个简单思考
    OsgEearh 中的 FeatureEditor的实现原理
    关于在osgearth 中 出现 arial.ttf : file not handled 的问题
    Qt 中 this->size() this->rect() event->size() 三者差异
    Qt 中QPainter 使用中出现的问题
    对c语言中static函数的理解
    对声明和定义的理解
    个人对头文件的理解
  • 原文地址:https://www.cnblogs.com/jieruishu/p/9258672.html
Copyright © 2011-2022 走看看