/***********************************************************************************************/
#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 "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(0x63)==0x01)
{
USART_SendData(USART1, (uint8_t) 'A');
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
delay_ms(8000);
}
}
一周热门 更多>