高级会员

- 积分
- 716
- 金钱
- 716
- 注册时间
- 2017-4-25
- 在线时间
- 203 小时
|
发表于 2017-9-6 23:12:33
|
显示全部楼层
之前发的是2个stm8的iic主从机,中断里发送和接收现在继续更....
现在贴stm32-stm32的iic主从机
[mw_shl_code=c,true]/* 主机程序中的read段对应从机程序中的1-send段; 主机程序中的write段对应从机程序中2-receive段 */
/* iic主机 型号stm32f103c8t6 采用模拟iic (PB6->SCL B7->SDA) */
u8 b[16];
int main(void)
{
u8 i; //used for circle
delay_init(); //延时初始化
JTAG_Set(JTAG_SWD_DISABLE); //关闭JTAG接口
JTAG_Set(SWD_ENABLE); //打开SWD接口(用于烧写调试)
uart_init(115200); //串口1初始化,波特率115200
LED_Init(); //初始化LED
Count=0; //test led亮(PB13)
printf("%c",0xff); //test uart
IIC_Init(); //软件模拟iic初始化
while(1)
{
/***************read start*******************/
IIC_Start(); //产生iic起始信号
//1 - read slave address
IIC_Send_Byte( 0x30|0x01); //读器件地址(器件地址0x30+读0x01)
while(IIC_Wait_Ack());
printf("%c",0x50); //match address
delay_ms(5); //等待从机处理一个字节地址位
//2 - start for receiving data
for(i=0;i<15;i++)
{
b = IIC_Read_Byte(1);
delay_us(10); //stm32-stm32 此处不需要延时
//stm32-stm8 需要几us速度延时,来匹配速度
}
b[15] = IIC_Read_Byte(0);
for(i=0;i<16;i++) //打印输出
{
printf("%c",b);
}
printf("%c",0x51); //show communication ending
}
/****************read end******************/
/****************write start*****************
IIC_Start(); //产生iic起始信号
//1 - write slave address
IIC_Send_Byte(0x30|0x00); //写器件地址(器件地址0x30+写0x00)
while(IIC_Wait_Ack()); //wait for ack
printf("%c",0x55); //match address
//2 - start for sending data
IIC_Send_Byte(0x01);
while(IIC_Wait_Ack());
delay_ms(1); //延时太低传输数据会出错,因为从机还没处理完数据
IIC_Send_Byte(0x02);
while(IIC_Wait_Ack());
delay_ms(1);
IIC_Send_Byte(0x04);
while(IIC_Wait_Ack());
delay_ms(1);
IIC_Send_Byte(0x08);
while(IIC_Wait_Ack());
delay_ms(1);
IIC_Send_Byte(0x10);
while(IIC_Wait_Ack());
delay_ms(1);
IIC_Send_Byte(0x20);
while(IIC_Wait_Ack());
delay_ms(1);
IIC_Send_Byte(0x40);
while(IIC_Wait_Ack());
delay_ms(1);
IIC_Send_Byte(0x80);
while(IIC_Wait_Ack());
delay_ms(1);
//3 - stop communication
IIC_Stop();
printf("%c",0xfe); //show communication ending
***************write end*****************/
}[/mw_shl_code]
[mw_shl_code=c,true]/* iic从机 型号stm32f103c8t6 采用硬件iic (PB6->SCL B7->SDA) */
unsigned char a[16]={0x00,0X01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
int main(void)
{
uint8_t *p1; //存放数据的指针
u8 i; //used for circle
delay_init(); //延时初始化
JTAG_Set(JTAG_SWD_DISABLE); //关闭JTAG接口
JTAG_Set(SWD_ENABLE); //打开SWD接口(用于烧写调试)
uart_init(115200); //串口1初始化,波特率115200
LED_Init(); //初始化LED
Count=1; //test led off(PB13)
I2C_Slave_Init(); //硬件iic初始化
printf("%c",0xff); //测试下串口初始化是否正常
while(1)
{
p1 = a; //指针p1用来,接收主机数据和发送数据
/**********************************1-send start********************************
//1 - judge if address matched as slave send data to matser
while(!I2C_CheckEvent(I2C1,I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED));//地址匹配+从机作为发送方
printf("%c",0x55); //match address
for(i=0;i<15;i++)
{
I2C1->DR = *(p1++);
while( !I2C_CheckEvent(I2C1,I2C_EVENT_SLAVE_BYTE_TRANSMITTED ));
}
I2C1->DR = *(p1++);
while( !I2C_CheckEvent(I2C1,I2C_EVENT_SLAVE_ACK_FAILURE ));
printf("%c",0xfe);
*********************************1-send end*********************************/
/*******************************2-receive start******************************/
//1 - judge if address matched as slave receive data from master
while(!I2C_CheckEvent(I2C1,I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED));//地址匹配+从机作为接收方
printf("%c",0x55); //show match success
//2 - receive data from master
for(i=0;i<8;i++)
{
while( !I2C_CheckEvent(I2C1,I2C_EVENT_SLAVE_BYTE_RECEIVED )); //当收到一字节
*(p1++) = I2C_ReceiveData(I2C1); //将接收到的iic data放入接收指针p1
printf("%c",*(p1-1));
}
//3 - stop communication
while(!I2C_CheckEvent(I2C1,I2C_EVENT_SLAVE_STOP_DETECTED ));
printf("%c",0xfe); //show communication ending
/*******************************2-receive end********************************/
}
}
//用于硬件iic的初始化函数
void I2C_Slave_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
/*STM32F103C8T6芯片的硬件I2C: PB6 -- SCL; PB7 -- SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;//I2C必须开漏输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C1);//使用I2C1
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x30;//从机的I2C地址,随便写的
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;//400K
I2C_Cmd(I2C1, ENABLE); //一定要在I2C_Init前面
I2C_Init(I2C1, &I2C_InitStructure); //一定要在I2C_Cmd后面
}
[/mw_shl_code]
只做了简单的测试,从机也没放中断里,要放得话的跟前面我发的stm8的中断差不多吧
主机的模拟iic用原子哥的就可以了 不发工程了
|
|