初级会员

- 积分
- 103
- 金钱
- 103
- 注册时间
- 2021-3-25
- 在线时间
- 28 小时
|
我用原子的f4zgt6的最小系统做的,iic驱动四线的oled但是不知道为啥Oled没反应,希望有人帮我看看,谢谢!
上代码:
/****************** iic.h *********************/
#ifndef _IIC_H
#define _IIC_H
#include "stm32f4xx.h"
#include "mydelay.h"
#define SCL GPIO_Pin_7
#define SDA GPIO_Pin_9
#define GPIO_IIC GPIOC
#define SCL_H() GPIO_SetBits(GPIO_IIC, SCL)
#define SCL_L() GPIO_ResetBits(GPIO_IIC, SCL)
#define SDA_H() GPIO_SetBits(GPIO_IIC, SDA)
#define SDA_L() GPIO_ResetBits(GPIO_IIC, SDA)
#define ReadSDA() GPIO_ReadInputDataBit(GPIO_IIC, SDA)
typedef enum{false,true}bool;
void I2C_IO_Init(void);
void Start(void);
void Stop(void);
void Write(u8 addr, u8 *cmd, u8 len);
//u8 ReadByte(u8 ack);
#endif
/****************** iic.c *********************/
#include "iic.h"
void I2C_IO_Init()
{
GPIO_InitTypeDef GpioIniture;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GpioIniture.GPIO_Mode = GPIO_Mode_OUT;
GpioIniture.GPIO_OType = GPIO_OType_OD;
GpioIniture.GPIO_Pin = SCL | SDA;
GpioIniture.GPIO_PuPd = GPIO_PuPd_UP;
GpioIniture.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_IIC, &GpioIniture);
}
void Start()
{
SCL_H();
SDA_H();
delay_us(5);
SDA_L();
delay_us(5);
SCL_L();
}
void Stop()
{
SDA_L();
SCL_L();
delay_us(5);
SCL_H();
delay_us(5);
SDA_H();
delay_us(5);
}
bool WaitAck()
{
bool re;
SDA_H();
delay_us(5);
SCL_H();
delay_us(5);
if(ReadSDA())
re = true;//非应答
else
re = false;//应答
SCL_L();
delay_us(5);
return re;
}
void WriteByte(u8 byte)
{
u8 i;
for(i = 0; i < 8; i ++)
{
SCL_L();
if((byte & 0x80) >> 7)
SDA_H();
else
SDA_L();
delay_us(2);
SCL_H();
byte <<= 1;
}
SCL_L();
SDA_H();
}
/*****************写数据函数*********************
先发送地址,再发送一组数据,cmd为一组数据
的地址,len为数据的个数
***********************************************/
void Write(u8 addr, u8 *cmd, u8 len)
{
u8 i;
Start();
WriteByte(addr);
if(WaitAck())
return;
for(i = 0; i < len; i ++){
WriteByte(cmd[i]);
if(WaitAck())
return;
}
Stop();
}
/****************** oled.c *********************/
//oled.c里面的函数太多,我就只贴出来几个重要的函数了
#include "oled.h"
#include "oledfont.h"
/********写命令********/
void WriteCom(u8 IIC_Command)
{
u8 com[] = {0x00};
Write(0x78, com, 1);
Write(0x78, &IIC_Command, 1);//地址为0x78
}
/********写数据********/
void WriteDat(u8 IIC_Data)
{
u8 com[] = {0x40};
Write(0x78, com, 1);
Write(0x78, &IIC_Data, 1);//地址为0x78
}
/********向oled写入指令********/
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
WriteDat(dat);
else
WriteCom(dat);
}
/********初始化oled********/
void OLED_Init(void)
{
delay_ms(200);
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3F,OLED_CMD);//--1/32 duty
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
OLED_WR_Byte(0xd3,OLED_CMD);//-set display offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
OLED_WR_Byte(0x80,OLED_CMD);//
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0xF1,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x30,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}
|
|