本帖最后由 lclight 于 2023-11-27 21:33 编辑
- /**
- * @file main.c
- * @author lclight
- * @brief
- * @version 0.1
- * @date 2023-11-26
- *
- * @copyright Copyright (c) 2023
- *
- */
- // 头文件,为省事直接写了一大堆
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <FreeRTOS.h>
- #include <task.h>
- #include <time.h>
- #include <unistd.h>
- #include "board.h"
- #include "log.h"
- #include "bflb_mtimer.h"
- #include "bflb_i2c.h"
- #include "bflb_gpio.h"
- #include "bflb_audac.h"
- #include "bflb_dma.h"
- #include "bl616_glb.h"
- #include "bflb_flash.h"
- #include <queue.h>
- #include <semphr.h>
- #include <timers.h>
- // 选择支持i2c的两个针脚,接线也要按这个来接
- #define SDA GPIO_PIN_31
- #define SCL GPIO_PIN_30
- // sleep函数,封装一层,方便修改
- // 因为精度不够,这里用1太耗时,改为0比较合适,用usleep同样不行
- #define waittime(t) vTaskDelay(0)
- struct bflb_device_s *gpio;
- // 从机地址,从手册或者卖家给的例子中获得,如果没有,甚至可以用for从0~127逐个初始化再确定是哪个
- uint8_t addr = 0x78;
- // 显示的图片字模,本应该放在头文件的,可以自己修改,宽×高(像素): 128×64
- const uint8_t *point;
- const uint8_t picture_tab[]={
- 0x1,0x2,0x4,0x8,0xF0,0xA0,0x0,0x10,0x10,0x10,0x8,0x8,0x8,0x18,0x10,0x11,
- 0x1A,0xC,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x1,0x1,0x1,
- 0x2,0xFF,0x81,0x0,0x1C,0x30,0xED,0x87,0xC3,0x1,0x0,0x40,0xE0,0x80,0x0,0x0,
- 0x0,0x0,0x0,0x0,0x1,0x2,0x4,0xE4,0x33,0x38,0x2E,0xE9,0xC8,0xD8,0x90,0x20,
- 0x20,0x40,0xC0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x1F,0x60,
- 0x80,0x0,0x0,0x80,0xC8,0x7C,0x4,0x3,0x0,0x0,0x80,0x80,0xC0,0x40,0x40,0x20,
- 0x20,0x20,0x20,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x20,0x60,0x60,0x60,0xE0,0xC0,
- 0xC0,0xC0,0x80,0x0,0x0,0x0,0x0,0x1,0x3,0x4,0x8,0x8,0x5,0x1,0x0,0x0,
- 0x6,0x4,0x8,0x10,0x20,0x40,0x80,0x1,0x3,0x7,0xD9,0x1A,0x6,0x6,0x4,0x0,
- 0x4,0x8,0x88,0xC8,0xC8,0xA0,0xA0,0xB0,0x10,0x10,0x10,0x10,0x10,0x10,0x90,0x90,
- 0x9C,0xB7,0x60,0x43,0x42,0xA6,0xB9,0x70,0xC0,0x40,0x81,0x81,0x1,0x0,0x0,0x0,
- 0x0,0x0,0x80,0x80,0x40,0x20,0x20,0xCF,0x39,0xE9,0x28,0x98,0xD8,0x58,0x58,0x59,
- 0x7E,0x74,0x7C,0x78,0x31,0x31,0xB1,0xB2,0xB2,0x72,0x36,0x14,0x94,0x94,0x94,0x94,
- 0x94,0x10,0x18,0x18,0x18,0x18,0x1C,0x1E,0x1A,0x19,0x11,0x10,0x10,0x10,0x10,0x10,
- 0x10,0x10,0x30,0x20,0x20,0x10,0x50,0x50,0xB0,0xA0,0x20,0x20,0x40,0x40,0xC0,0x80,
- 0x81,0x1,0x1,0x3,0x7,0x6,0xE,0x14,0x2C,0x58,0xB0,0x60,0xC0,0x80,0x0,0x0,
- 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0xFE,0xE6,0x20,0x0,0x0,0x80,0xF0,0xDC,
- 0xEE,0xF5,0x35,0x10,0x18,0x18,0x10,0x14,0x14,0x18,0x38,0x30,0x60,0xE0,0xC0,0xC0,
- 0x1E,0x81,0x70,0xFE,0x0,0x0,0xFE,0x70,0xC0,0x7,0x1B,0xE3,0x85,0x89,0x91,0x91,
- 0xB1,0xF9,0x70,0x58,0xE8,0x78,0x68,0xD7,0x4C,0x7,0xE0,0x63,0x23,0x23,0xFB,0x3B,
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x38,0x14,0xA,0x5,0x3,0x1,0x0,0x0,0x0
- };
- // i2c协议的开始位
- void i2c_start()
- {
- bflb_gpio_set(gpio, SDA);
- waittime(1);
- bflb_gpio_set(gpio, SCL);
- waittime(1);
- bflb_gpio_reset(gpio, SDA);
- waittime(1);
- bflb_gpio_reset(gpio, SCL);
- }
- // i2c协议的结束位
- void i2c_stop()
- {
- bflb_gpio_reset(gpio, SDA);
- waittime(1);
- bflb_gpio_set(gpio, SCL);
- waittime(1);
- bflb_gpio_set(gpio, SDA);
- }
- // i2c协议发送一个字节
- void send_byte(uint8_t dat)
- {
- uint8_t i;
- for (i = 0; i<8; i++)
- {
- if (dat & 0x80)
- {
- bflb_gpio_set(gpio, SDA);
- }
- else
- {
- bflb_gpio_reset(gpio, SDA);
- }
- waittime(1);
- bflb_gpio_set(gpio, SCL);
- waittime(1);
- bflb_gpio_reset(gpio, SCL);
- waittime(1);
- dat <<= 1;
- }
- bflb_gpio_set(gpio, SDA);
- waittime(1);
- bflb_gpio_set(gpio, SCL);
- waittime(1);
- bflb_gpio_reset(gpio, SCL);
- waittime(1);
- }
- // 发送一帧数据
- void oled_wr_byte(uint8_t dat, uint8_t mode)
- {
- i2c_start();
- send_byte(addr);
- mode ? send_byte(0x40) : send_byte(0x00);
- send_byte(dat);
- i2c_stop();
- }
- // 发送一帧命令数据
- void oled_cmd(uint8_t cmd)
- {
- printf("cmd:%d\r\n", cmd);
- oled_wr_byte(cmd, 0);
- }
- // 发送一帧Data数据
- void oled_data(uint8_t dat)
- {
- oled_wr_byte(dat, 1);
- }
- // 发送定位到页的命令
- void page_set(uint8_t page)
- {
- oled_cmd(0xb0 + page);
- }
- // 发送定位到列的命令
- void column_set(uint8_t col)
- {
- oled_cmd(0x10 | (col >> 4));
- oled_cmd(0x00 | (col & 0x0f));
- }
- // 清屏,就是把填满数据0
- void oled_clear()
- {
- uint8_t page,col;
- for (page = 0; page < 8; ++page)
- {
- page_set(page);
- column_set(0);
- for (col = 0; col < 128; ++col)
- {
- oled_data(0x00);
- }
- }
- }
- // 清屏,就是把填满数据1
- void oled_full()
- {
- uint8_t page,col;
- for (page = 0; page < 8; ++page)
- {
- page_set(page);
- column_set(0);
- for (col = 0; col < 128; ++col)
- {
- oled_data(0xff);
- }
- }
- }
- // 显示图片
- void oled_display(const uint8_t *ptr_pic)
- {
- uint8_t page,col;
- for (page = 0; page < 8; ++page)
- {
- page_set(page);
- column_set(0);
- for (col = 0; col < 128; ++col)
- {
- oled_data(*ptr_pic++);
- }
- }
- }
- // 显示图片,1和0反转,就是反色
- void oled_display_r(const uint8_t *ptr_pic)
- {
- uint8_t page,col,data;
- for (page = 0; page < 8; ++page)
- {
- page_set(page);
- column_set(0);
- for (col = 0; col < 128; ++col)
- {
- data=*ptr_pic++;
- data=~data;
- oled_data(data);
- }
- }
- }
- // 初始化,点亮屏幕,按手册执行一些列命令即可,有些命令不是必要的
- void init_display()
- {
- uint8_t cmds[25] =
- {
- 0xAE,//关闭显示
- 0xD5,//设置时钟分频因子,震荡频率
- 0x80, //[3:0],分频因子;[7:4],震荡频率
- 0xA8,//设置驱动路数
- 0X3F,//默认0X3F(1/64)
- 0xD3,//设置显示偏移
- 0X00,//默认为0
- 0x40,//设置显示开始行 [5:0],行数.
- 0x8D,//电荷泵设置
- 0x14,//bit2,开启/关闭
- 0x20,//设置内存地址模式
- 0x02,//[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;
- 0xA1,//段重定义设置,bit0:0,0->0;1,0->127;
- 0xC8,//设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数
- 0xDA,//设置COM硬件引脚配置
- 0x12,//[5:4]配置
- 0x81,//对比度设置
- 0xEF,//1~255;默认0X7F (亮度设置,越大越亮)
- 0xD9,//设置预充电周期
- 0xf1,//[3:0],PHASE 1;[7:4],PHASE 2;
- 0xDB,//设置VCOMH 电压倍率
- 0x30,//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
- 0xA4,//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
- 0xA6,//设置显示方式;bit0:1,反相显示;0,正常显示
- 0xAF,//开启显示
- };
- uint8_t i;
- for (i = 0; i < 25; ++i)
- {
- oled_cmd(cmds[i]);
- }
- sleep(1);
- }
- void led_run(void* param)
- {
- gpio = bflb_device_get_by_name("gpio");
- /* I2C0_SDA */
- bflb_gpio_init(gpio, SDA, GPIO_OUTPUT | GPIO_PULLUP);
- /* I2C0_SCL */
- bflb_gpio_init(gpio, SCL, GPIO_OUTPUT | GPIO_PULLUP);
- // 初始化,点亮屏幕
- // 图片轮播
- init_display();
- point= &picture_tab[0];
- while (true)
- {
- sleep(3);
- oled_display(point);
- sleep(3);
- oled_display_r(point);
- }
-
- }
- int main(void)
- {
- board_init();
- xTaskCreate(led_run, (char*)"led_run", 1024*4, NULL, 1, NULL);
- vTaskStartScheduler();
- }
复制代码 代码好像被吞了,我再试试
|