ucAddr = ((Address<<1)&0x7E)|0x80;
时间: 2024-05-25 22:13:52 浏览: 11
这段代码的作用是将一个8位的地址左移1位,并将第7位设为1,第0位设为0,用于向某些设备发送地址信息。具体解释如下:
- `Address`:8位地址变量;
- `<<1`:左移1位,相当于将二进制数整体向左移动一位,相当于乘以2;
- `&0x7E`:将结果与 `0x7E` 进行按位与操作,相当于将第7位至第1位设为0,只保留第6位至第2位的值;
- `|0x80`:将结果与 `0x80` 进行按位或操作,相当于将第7位设为1,第0位设为0,得到最终的 `ucAddr` 值。
因此,这段代码可以将一个8位地址转换成符合某些设备通信协议的地址格式。
相关问题
STM32的IIC(模拟)读写AT24c02
以下是STM32的IIC(模拟)读写AT24c02的代码:
```
#include "stm32f10x.h"
#include "delay.h"
#define IIC_SCL_H GPIOB->BSRR = GPIO_Pin_10
#define IIC_SCL_L GPIOB->BRR = GPIO_Pin_10
#define IIC_SDA_H GPIOB->BSRR = GPIO_Pin_11
#define IIC_SDA_L GPIOB->BRR = GPIO_Pin_11
#define IIC_SDA GPIOB->IDR & GPIO_Pin_11
void IIC_Start()
{
IIC_SDA_H;
IIC_SCL_H;
delay_us(5);
IIC_SDA_L;
delay_us(5);
IIC_SCL_L;
}
void IIC_Stop()
{
IIC_SDA_L;
IIC_SCL_H;
delay_us(5);
IIC_SDA_H;
delay_us(5);
}
void IIC_Ack()
{
IIC_SDA_L;
delay_us(5);
IIC_SCL_H;
delay_us(5);
IIC_SCL_L;
IIC_SDA_H;
}
void IIC_NoAck()
{
IIC_SDA_H;
delay_us(5);
IIC_SCL_H;
delay_us(5);
IIC_SCL_L;
}
uint8_t IIC_WaitAck()
{
uint8_t ucErrTime = 0;
IIC_SDA_H;
delay_us(1);
IIC_SCL_H;
delay_us(1);
while (IIC_SDA)
{
ucErrTime++;
if (ucErrTime > 250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL_L;
return 0;
}
void IIC_SendByte(uint8_t ucByte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if ((ucByte << i) & 0x80)
{
IIC_SDA_H;
}
else
{
IIC_SDA_L;
}
delay_us(1);
IIC_SCL_H;
delay_us(1);
IIC_SCL_L;
}
}
uint8_t IIC_ReadByte()
{
uint8_t i, ucByte = 0;
IIC_SDA_H;
for (i = 0; i < 8; i++)
{
ucByte <<= 1;
IIC_SCL_H;
delay_us(1);
if (IIC_SDA)
{
ucByte |= 0x01;
}
IIC_SCL_L;
delay_us(1);
}
return ucByte;
}
void IIC_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
IIC_SDA_H;
IIC_SCL_H;
}
void AT24C02_WriteByte(uint8_t ucAddr, uint8_t ucData)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(ucAddr);
IIC_WaitAck();
IIC_SendByte(ucData);
IIC_WaitAck();
IIC_Stop();
delay_ms(10);
}
uint8_t AT24C02_ReadByte(uint8_t ucAddr)
{
uint8_t ucData;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(ucAddr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
ucData = IIC_ReadByte();
IIC_NoAck();
IIC_Stop();
return ucData;
}
int main(void)
{
uint8_t ucData;
IIC_Init();
AT24C02_WriteByte(0x00, 0x55);
ucData = AT24C02_ReadByte(0x00);
while (1);
}
```
请给出IO中断模拟IIC从机GD32F130的例程
以下是IO中断模拟IIC从机GD32F130的例程,请参考:
```C++
#include "gd32f1x0.h"
#include "stdio.h"
#include "systick.h"
#define I2C2_SCL_PIN GPIO_PIN_6 /*!< GPIO pin of the I2C clock */
#define I2C2_SCL_GPIO_PORT GPIOB /*!< GPIO port of the I2C clock */
#define I2C2_SDA_PIN GPIO_PIN_7 /*!< GPIO pin of the I2C data */
#define I2C2_SDA_GPIO_PORT GPIOB /*!< GPIO port of the I2C data */
#define I2C2_RCC_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB) /*!< Enable GPIOB clock */
#define I2C2_RCC_CLK_DISABLE() rcu_periph_clock_disable(RCU_GPIOB) /*!< Disable GPIOB clock */
#define I2C2_SCL_HIGH() gpio_bit_set(I2C2_SCL_GPIO_PORT, I2C2_SCL_PIN) /*!< Set GPIOB pin 6 */
#define I2C2_SCL_LOW() gpio_bit_reset(I2C2_SCL_GPIO_PORT, I2C2_SCL_PIN) /*!< Reset GPIOB pin 6 */
#define I2C2_SDA_HIGH() gpio_bit_set(I2C2_SDA_GPIO_PORT, I2C2_SDA_PIN) /*!< Set GPIOB pin 7 */
#define I2C2_SDA_LOW() gpio_bit_reset(I2C2_SDA_GPIO_PORT, I2C2_SDA_PIN) /*!< Reset GPIOB pin 7 */
#define I2C2_SDA_READ() gpio_input_bit_get(I2C2_SDA_GPIO_PORT, I2C2_SDA_PIN) /*!< Read GPIOB pin 7 */
void delay(uint32_t count)
{
uint32_t index = 0;
for(index = count; index != 0; index--)
{
}
}
void I2C2_GPIO_Config(void)
{
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_GPIOB);
/*configure GPIO pin of I2C2*/
gpio_init(I2C2_SCL_GPIO_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, I2C2_SCL_PIN);
gpio_init(I2C2_SDA_GPIO_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, I2C2_SDA_PIN);
/*configure I2C2 as a master*/
gpio_init(I2C2_SCL_GPIO_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C2_SCL_PIN);
gpio_init(I2C2_SDA_GPIO_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C2_SDA_PIN);
gpio_pin_remap_config(GPIO_SPI2_REMAP,I2C2_MAP);
}
void I2C2_Start(void)
{
I2C2_SDA_HIGH();
I2C2_SCL_HIGH();
delay(5);
I2C2_SDA_LOW();
delay(5);
I2C2_SCL_LOW();
}
void I2C2_Stop(void)
{
I2C2_SDA_LOW();
I2C2_SCL_HIGH();
delay(5);
I2C2_SDA_HIGH();
delay(5);
}
uint8_t I2C2_WaitAck(void)
{
uint8_t ucErrTime = 0;
I2C2_SDA_HIGH();
delay(1);
I2C2_SCL_HIGH();
delay(1);
while(I2C2_SDA_READ())
{
ucErrTime++;
if(ucErrTime > 50)
{
I2C2_Stop();
return 1;
}
delay(1);
}
I2C2_SCL_LOW();
return 0;
}
void I2C2_Ack(void)
{
I2C2_SCL_LOW();
I2C2_SDA_LOW();
delay(2);
I2C2_SCL_HIGH();
delay(2);
I2C2_SCL_LOW();
}
void I2C2_NAck(void)
{
I2C2_SCL_LOW();
I2C2_SDA_HIGH();
delay(2);
I2C2_SCL_HIGH();
delay(2);
I2C2_SCL_LOW();
}
void I2C2_SendByte(uint8_t ucData)
{
uint8_t ucCount = 0;
for(ucCount = 0; ucCount < 8; ucCount++)
{
if(ucData & 0x80)
I2C2_SDA_HIGH();
else
I2C2_SDA_LOW();
ucData <<= 1;
delay(2);
I2C2_SCL_HIGH();
delay(2);
I2C2_SCL_LOW();
}
}
uint8_t I2C2_ReadByte(uint8_t ucAck)
{
uint8_t ucCount = 0, ucData = 0;
I2C2_SDA_HIGH();
for(ucCount = 0; ucCount < 8; ucCount++)
{
ucData <<= 1;
I2C2_SCL_LOW();
delay(2);
I2C2_SCL_HIGH();
delay(2);
if(I2C2_SDA_READ())
ucData++;
}
if(!ucAck)
I2C2_NAck();
else
I2C2_Ack();
return ucData;
}
void GD32_I2C2_Init(void)
{
I2C2_GPIO_Config();
}
uint8_t GD32_I2C2_ReadByte(uint8_t ucAddr, uint8_t ucReg)
{
uint8_t ucData = 0;
I2C2_Start();
I2C2_SendByte(ucAddr << 1 | 0);
if(I2C2_WaitAck())
{
I2C2_Stop();
return 0;
}
I2C2_SendByte(ucReg);
I2C2_WaitAck();
I2C2_Start();
I2C2_SendByte(ucAddr << 1 | 1);
I2C2_WaitAck();
ucData = I2C2_ReadByte(0);
I2C2_Stop();
return ucData;
}
void GD32_I2C2_WriteByte(uint8_t ucAddr, uint8_t ucReg, uint8_t ucValue)
{
I2C2_Start();
I2C2_SendByte(ucAddr << 1 | 0);
if(I2C2_WaitAck())
{
I2C2_Stop();
return;
}
I2C2_SendByte(ucReg);
I2C2_WaitAck();
I2C2_SendByte(ucValue);
I2C2_WaitAck();
I2C2_Stop();
}
int main(void)
{
uint8_t ucData = 0;
GD32_I2C2_Init();
while(1)
{
ucData = GD32_I2C2_ReadByte(0x68, 0x75);
printf("WHO_AM_I is %X \r\n", ucData);
delay_1ms(1000);
}
}
```