主机
/***********************************************************************************/
#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "usart.h"
int main(void)
{
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_Configuration(); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶 LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
OLED_Init(); //3õê¼»ˉOLED
uart_init(9600);
while(1)
{
if(Read_IIC_Data((0xA1<<1)+1)==0x01)
{
USART_SendData(USART1, (uint8_t) 0x01);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
else
{
USART_SendData(USART1, (uint8_t) Read_IIC_Data((0xA1<<1)+1));
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
delay_ms(1000);
}
}
/***********************************************************************************/
#include "oled.h"
#include "stdlib.h"
#include "delay.h"
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüA¶Ë¿úê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//Ëù¶è50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //3õê¼»ˉGPIOD3,6
GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);
delay_ms(800);
}
void IIC_Start()
{
SDA_OUT();
OLED_SCLK_Set();
OLED_SDIN_Set();
delay_us(5);
OLED_SDIN_Clr();
delay_us(4);
OLED_SCLK_Clr();
}
void IIC_Stop()
{
SDA_OUT();
OLED_SCLK_Clr();
OLED_SDIN_Clr();
delay_us(4);
OLED_SCLK_Set();
OLED_SDIN_Set();
delay_us(4);
}
void IIC_Ack(void)
{
OLED_SCLK_Clr();
SDA_OUT();
OLED_SDIN_Clr();
delay_us(2);
OLED_SCLK_Set();
delay_us(2);
OLED_SCLK_Clr();
}
void IIC_Wait_Ack()
{
OLED_SCLK_Set();
OLED_SCLK_Clr();
}
void IIC_NAck(void)
{
OLED_SCLK_Clr();
SDA_OUT();
OLED_SDIN_Set();
delay_us(2);
OLED_SCLK_Set();
delay_us(2);
OLED_SCLK_Clr();
}
u8 Read_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i,receive=0;
SDA_IN();
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
delay_us(2);
OLED_SCLK_Set();
receive<<=1;
if(READ_SDA)
receive++;
delay_us(1);
}
if(!IIC_Byte)
IIC_NAck();
else
IIC_Ack();
return receive;
}
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m,da;
da=IIC_Byte;
OLED_SCLK_Clr();
for(i=0;i<8;i++)
{
m=da;
m=m&0x80;
if(m==0x80)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
da=da<<1;
OLED_SCLK_Set();
OLED_SCLK_Clr();
}
}
u8 Read_IIC_Data(u16 ReadAddr)
{
u8 rec=0;
IIC_Start();
Write_IIC_Byte(ReadAddr);
IIC_Wait_Ack();
rec=Read_IIC_Byte(0);
IIC_Stop();
return rec;
}
void Delay_50ms(unsigned int Del_50ms)
{
unsigned int m;
for(;Del_50ms>0;Del_50ms--)
for(m=6245;m>0;m--);
}
void Delay_1ms(unsigned int Del_1ms)
{
unsigned char j;
while(Del_1ms--)
{
for(j=0;j<123;j++);
}
}
/******************************从机*********************************************/
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "encoder.h"
#include "24cxx.h"
#include "STM32f10x_i2c.h"
#include "usart.h"
const u8 TEXT_Buffer[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37};
int main(void)
{
u8 keyval=0;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
IIC_Init(); //IIC3õê¼»ˉ
I2c_Init();
uart_init(9600);
while(1)
{
I2C_SendData(I2C1,TEXT_Buffer[1]);
}
}
/***********************************************************************************/
#include "myiic.h"
#include "delay.h"
#include "stm32f10x_i2c.h"
//3õê¼»ˉIIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11); //PB10,PB11 êä3ö¸ß
}
void I2c_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_DeInit(I2C1);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1=(0xA1<<1);
I2C_InitStructure.I2C_Ack=I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed=I2C_Speed;
I2C_Init(I2C1,&I2C_InitStructure);
I2C_Cmd(I2C1,ENABLE);
I2C_AcknowledgeConfig(I2C1,ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>