stm32与51单片机IIC通信(学习)

stm32主机与51单片机从机IIC通信

注意时序两单片机运行速度不一样运用阻塞的方法特别是应答的时候我找了好久的bug

两单片机可以直接导线连接
应答是否成功用32上两个led判断

stm32主机

#include "stm32f10x.h"
#include "iic.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
int main (void)
{
     
	IIC_Init();
	IIC_Start();
	LED_GPIO_Config();
	USART_Config ();
	printf("start");
//	IIC_Send_Byte(0xFE);  //write addr
//	if(IIC_Wait_Ack()){
     
//		IIC_Stop();
//		GPIO_ResetBits(GPIOB,GPIO_Pin_5);//ºì
//	}else{
     
//		GPIO_ResetBits(GPIOE,GPIO_Pin_5);//ÂÌ
//		IIC_Send_Byte(0x0f);
//		IIC_Stop();
//	}//stm32发送数据
	IIC_Send_Byte(0xFF);  //read addr
	if(IIC_Wait_Ack()){
     
		IIC_Stop();
		GPIO_ResetBits(GPIOB,GPIO_Pin_5);
	}else{
     
		GPIO_ResetBits(GPIOE,GPIO_Pin_5);
		printf("%#x\r\n",IIC_Read_Byte());
		IIC_Stop();
	}//接受数据并串口打印 
	return 0;
}

IIC

#include "stm32f10x.h"
#include "delay.h"

#define  SCL_UP          GPIO_SetBits(GPIOB,GPIO_Pin_6)
#define  SCL_DOWN     GPIO_ResetBits(GPIOB,GPIO_Pin_6)

#define  SDA_UP         GPIO_SetBits(GPIOB,GPIO_Pin_7)
#define  SDA_DOWN    GPIO_ResetBits(GPIOB,GPIO_Pin_7)

#define   SDA_READ     GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)

void IIC_Init(void);
void IIC_Start(void);
void IIC_Stop(void);
u8 IIC_Wait_Ack(void);
void IIC_Nack(void);
void IIC_Ack(void);
void IIC_Send_Byte(u8 Byte);
u8 IIC_Read_Byte(void);

#endif
#include "iic.h"
#include "delay.h"

void IIC_Init(void)
{
     
	GPIO_InitTypeDef GPIO_Struct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
	GPIO_Struct.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Struct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //SCL  SDA
	GPIO_Init(GPIOB,&GPIO_Struct);
	
	SysTick_Init(72);
}

static void SDA_Out(void)
{
     
	GPIO_InitTypeDef GPIO_Struct;
	
	GPIO_Struct.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Struct.GPIO_Pin = GPIO_Pin_7;//SDA
	GPIO_Init(GPIOB,&GPIO_Struct);
}

static void SDA_In(void)
{
     
	GPIO_InitTypeDef GPIO_Struct;
	
	GPIO_Struct.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
	GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Struct.GPIO_Pin = GPIO_Pin_7;  //SDA
	GPIO_Init(GPIOB,&GPIO_Struct);
}

void IIC_Start(void)
{
     
	SDA_Out();
	SCL_UP; 
	SDA_UP;
	delay_ms(4);
	SDA_DOWN;
	delay_ms(4);
	SCL_DOWN;
	delay_ms(2);
}

void IIC_Stop(void)
{
     
	SDA_Out();
	SCL_DOWN;
	SDA_DOWN;
	delay_ms(4);
	SCL_UP;
	delay_ms(4);
	SDA_UP;
	delay_ms(2);
}

u8 IIC_Wait_Ack(void)
{
     
	u8 i=0;
	SDA_In();
	SCL_DOWN;
//	SDA_UP;
	delay_ms(2);
	SCL_UP;
	delay_ms(2);
	if(SDA_READ){
     
		i = 1;
	}else{
     
		i = 0;
	}
	SCL_DOWN;
	delay_ms(2);
	return i;
}

void IIC_Ack(void)
{
     
	SDA_Out();
	SCL_DOWN;
	SDA_DOWN;
	delay_ms(2);
	SCL_UP;
	delay_ms(2);
	SCL_DOWN;
	delay_ms(2);
	SDA_UP;
	delay_ms(2);
}

void IIC_Nack(void)
{
     
	SDA_Out();
	SCL_DOWN;
	SDA_DOWN;
	delay_ms(2);
	SDA_UP;
	delay_ms(2);
	SCL_UP;
	delay_ms(2);
	SCL_DOWN;
	delay_ms(2);
}

void IIC_Send_Byte(u8 Byte)
{
     
	u8 i = 0;
	SDA_Out();
	for(i=0;i<8;i++){
     
		if(Byte&0x80){
     
				SDA_UP;
		}else{
     
			SDA_DOWN;
		}
	delay_ms(2);
	SCL_UP;
	delay_ms(2);
	SCL_DOWN;
	Byte <<= 1;
	delay_ms(2);
	}
	SDA_UP;
}

u8 IIC_Read_Byte(void)
{
     
	u8 i = 0;
	u8 Byte = 0;
	SDA_In();
	for(i=0;i<8;i++){
     
		Byte <<= 1;
		SCL_UP;
		delay_ms(2);
		if(SDA_READ){
     
			Byte |= 0x01; 
		}else{
     
			Byte |= 0x00;
		}
		SCL_DOWN;
		delay_ms(2);
	}
	return Byte;
}

51单片机(从机)

#include 
#include 

int main(void)
{
     
	P1 = 0xFF;
	while(1){
     
		switch(IIC_Addr_RW()){
     
			case 0: P1 = IIC_Read();break;
			case 1: IIC_Send_Byte(0x88);break;		
			case 2: P1 = 0x00;break;		
		}
	}
	return 0;
}

IIc

#include 

sbit SCL = P2^1;
sbit SDA = P2^0;

unsigned int IIC_Addr_RW(void)
{
     
	unsigned int Addr_Check=0x00,i=0;
	while(!SCL);
	while(SDA);//wait iic start
	for(i=0;i<8;i++){
     
		while(SCL);
 		while(!SCL);
		Addr_Check <<=1;
		if(SDA == 1){
     
			Addr_Check |= 0x01;
		}else{
     
			Addr_Check |= 0x00;
		}
	}
	if (Addr_Check == Addr_W){
     
		SDA = 0; //ack
		return 0;
		} else if(Addr_Check == Addr_R){
     
			SDA = 0; //ack
			return 1;	
		} else{
     
			 SDA = 0; 
	         return 2;  // addr error
		}
    
}

unsigned int IIC_Read(void)
{
     
	unsigned int Byte=0x00,i=0;
	while(SCL);
	while(!SCL);
	while(SCL);//±£Ö¤51ºÍ32ʱÐòÏàͬ(¹ýÂËÖ÷»úµÄwaitack)
	SDA = 1; 	//ÊÍ·Å×ÜÏß
	for(i=0;i<8;i++){
     
		while(SCL);
 		while(!SCL);
		Byte <<= 1;
		if(SDA == 1){
     
			Byte |= 0x01;
		 }else{
     
			Byte |= 0x00;
		}
	}
	SDA = 0;
	return Byte;
}


void IIC_Send_Byte(unsigned int Byte)
{
     
	unsigned int i = 0;
	while(SCL);
	while(!SCL);
	while(SCL);//±£Ö¤51ºÍ32ʱÐòÏàͬ
	for(i=0;i<8;i++){
     
		while(!SCL);
		if(Byte&0x80){
     
			SDA = 1;
		}else{
     
			SDA = 0;
		}
	    Byte <<= 1;
		while(SCL);
	}
	SDA = 1;   
}

你可能感兴趣的:(c语言,STM32,51单片机,stm32,单片机,c语言)