中级会员
- 积分
- 277
- 金钱
- 277
- 注册时间
- 2020-1-19
- 在线时间
- 94 小时
|
1金钱
想学习使用SPI框架驱动OLED,就准备把中景园的程序移植到RTT上,参考了网上的步骤,在settings里开启spi,在board.c里复制HAL_SPI_MspInit,在board.h和stm32xxxx_hal_config.h里开启了spi,然后代码如下:- #include "OLED.h"
- #define RES_PIN 11
- #define DC_PIN 12
- #define CS_PIN 15
- uint8_t OLED_GRAM[128][8];
- static struct rt_spi_device* spi_dev_ssd1306; /* SPI设备ssd1306对象 */
- int oled_spi_device_init()
- {
- __HAL_RCC_GPIOA_CLK_ENABLE();
- //设备挂载到SPI总线,抽象为 spi10 设备,同时使用时还需进行 rt_spi_configure
- rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_15);
- return RT_EOK;
- }
- INIT_DEVICE_EXPORT(oled_spi_device_init);//自动初始化
- int my_oled_init(void)
- {
- rt_pin_mode(RES_PIN, PIN_MODE_OUTPUT );
- rt_pin_mode(DC_PIN, PIN_MODE_OUTPUT );
- rt_pin_mode(CS_PIN, PIN_MODE_OUTPUT );
- spi_dev_ssd1306 = (struct rt_spi_device *)rt_device_find("spi10");
- if (!spi_dev_ssd1306)
- {
- rt_kprintf("spi sample run failed! can't find %s device!\n", "spi10");
- }
- else
- {
- // owner必须配置,因为默认为空,为空时无法进行初始化操作
- spi_dev_ssd1306->bus->owner = spi_dev_ssd1306; // 必须!!!
- // 配置spi
- struct rt_spi_configuration cfg;
- cfg.data_width = 8;
- cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; // 主机,模式0,高位在前
- cfg.max_hz = 20 * 1000 * 1000; // SPI 接口时钟频率
- rt_spi_configure(spi_dev_ssd1306, &cfg); // 此时才进行MspInit相关的时钟及IO口初始化
- }
- OLED_Init();
- return RT_EOK;
- }
- //OLED写一个字节
- void OLED_WR_Byte(uint8_t dat, uint8_t cmd)
- {
- if(cmd)
- {
- OLED_DC_Set();
- }else{
- OLED_DC_Clr();
- }
- rt_spi_send(spi_dev_ssd1306, &dat, 1);
- OLED_DC_Set();
- }
- //开启OLED显示
- void OLED_DisPlay_On(void)
- {
- OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
- OLED_WR_Byte(0x14,OLED_CMD);//开启电荷泵
- OLED_WR_Byte(0xAF,OLED_CMD);//点亮屏幕
- }
- //关闭OLED显示
- void OLED_DisPlay_Off(void)
- {
- OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
- OLED_WR_Byte(0x10,OLED_CMD);//关闭电荷泵
- OLED_WR_Byte(0xAF,OLED_CMD);//关闭屏幕
- }
- //更新显存到OLED
- void OLED_Refresh(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte(0xb0+i,OLED_CMD); //设置行起始地址
- OLED_WR_Byte(0x00,OLED_CMD); //设置低列起始地址
- OLED_WR_Byte(0x10,OLED_CMD); //设置高列起始地址
- for(n=0;n<128;n++)
- OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
- }
- }
- //清屏函数
- void OLED_Clear(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)
- {
- for(n=0;n<128;n++)
- {
- OLED_GRAM[n][i]=0;//清除所有数据
- }
- }
- OLED_Refresh();//更新显示
- }
- //画点
- //x:0~127
- //y:0~63
- void OLED_DrawPoint(uint8_t x,uint8_t y)
- {
- uint8_t i,m,n;
- i=y/8;
- m=y%8;
- n=1<<m;
- OLED_GRAM[x][i]|=n;
- }
- //清除一个点
- //x:0~127
- //y:0~63
- void OLED_ClearPoint(uint8_t x,uint8_t y)
- {
- uint8_t i,m,n;
- i=y/8;
- m=y%8;
- n=1<<m;
- OLED_GRAM[x][i]=~OLED_GRAM[x][i];
- OLED_GRAM[x][i]|=n;
- OLED_GRAM[x][i]=~OLED_GRAM[x][i];
- }
- //OLED的初始化
- void OLED_Init(void)
- {
- OLED_RES_Clr();
- rt_thread_delay(200);
- OLED_RES_Set();
- OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
- 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 Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
- OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness
- OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
- OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
- OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
- OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WR_Byte(0x00,OLED_CMD);//-not offset
- OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
- OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
- OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
- OLED_WR_Byte(0x12,OLED_CMD);
- OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
- OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
- OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WR_Byte(0x02,OLED_CMD);//
- OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
- OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
- OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
- OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
- OLED_WR_Byte(0xAF,OLED_CMD);
- OLED_Clear();
- OLED_DrawPoint(1, 2);
- OLED_Refresh();
- }
复制代码 在OLED_Init最后想画个点测试是否成功,但是屏幕没有点亮,msh里输入list_device可以看见spi1和spi10是正常开启的,请问是哪个步骤做错了吗
|
|