OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4481|回复: 3

f4最小系统板,iic驱动四线oled的问题

[复制链接]

4

主题

14

帖子

0

精华

初级会员

Rank: 2

积分
103
金钱
103
注册时间
2021-3-25
在线时间
28 小时
发表于 2021-7-5 18:09:42 | 显示全部楼层 |阅读模式
我用原子的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
}  



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

6

主题

156

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
464
金钱
464
注册时间
2021-4-28
在线时间
158 小时
发表于 2021-7-5 18:17:31 | 显示全部楼层
回复 支持 反对

使用道具 举报

6

主题

890

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
1481
金钱
1481
注册时间
2020-8-19
在线时间
336 小时
发表于 2021-7-6 10:16:17 | 显示全部楼层
论坛上很多可以参考的代码
回复 支持 反对

使用道具 举报

1

主题

26

帖子

0

精华

新手上路

积分
43
金钱
43
注册时间
2021-7-13
在线时间
6 小时
发表于 2021-7-14 19:51:10 | 显示全部楼层
henb

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-8 11:08

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表