时钟 SCL -- PB6 数据 SDA -- PB7 复位 RES -- PB5
void oled_i2c_hardware_init(void)
{
GPIO_Config_T gpioConfigStruct;
I2C_Config_T i2cConfigStruct;
/** Enable I2C related Clock */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB | RCM_APB2_PERIPH_AFIO);
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_I2C1);
/** Free I2C_SCL and I2C_SDA */
gpioConfigStruct.mode = GPIO_MODE_AF_OD;
gpioConfigStruct.speed = GPIO_SPEED_50MHz;
gpioConfigStruct.pin = GPIO_PIN_6;
GPIO_Config(GPIOB, &gpioConfigStruct);
gpioConfigStruct.mode = GPIO_MODE_AF_OD;
gpioConfigStruct.speed = GPIO_SPEED_50MHz;
gpioConfigStruct.pin = GPIO_PIN_7;
GPIO_Config(GPIOB, &gpioConfigStruct);
/** Config I2C1 */
I2C_Reset(I2C1);
i2cConfigStruct.mode = I2C_MODE_I2C;
i2cConfigStruct.dutyCycle = I2C_DUTYCYCLE_2;
i2cConfigStruct.ackAddress = I2C_ACK_ADDRESS_7BIT;
//i2cConfigStruct.ownAddress1 = 0XA0;
i2cConfigStruct.ownAddress1 = SSD1306_ADDRESS;
i2cConfigStruct.ack = I2C_ACK_ENABLE;
i2cConfigStruct.clockSpeed = 400000;
I2C_Config(I2C1, &i2cConfigStruct);
/** Enable I2Cx */
I2C_Enable(I2C1);
i2c_dma_init();
}
void i2c_dma_init(void)
{
DMA_Config_T DMA_ConfigStruct;
RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);
DMA_Reset(DMA1_Channel6);
DMA_ConfigStruct.peripheralBaseAddr = (uint32_t)(I2C1_BASE + 0x10) ;
DMA_ConfigStruct.memoryBaseAddr = (uint32_t)NULL;
DMA_ConfigStruct.dir = DMA_DIR_PERIPHERAL_DST;
DMA_ConfigStruct.bufferSize = 0;
DMA_ConfigStruct.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
DMA_ConfigStruct.memoryInc = DMA_MEMORY_INC_ENABLE;
DMA_ConfigStruct.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
DMA_ConfigStruct.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
DMA_ConfigStruct.loopMode = DMA_MODE_NORMAL;
DMA_ConfigStruct.priority = DMA_PRIORITY_HIGH;
DMA_ConfigStruct.M2M = DMA_M2MEN_DISABLE;
DMA_Config(DMA1_Channel6, &DMA_ConfigStruct);
I2C_EnableDMA(I2C1);
}
void i2c_dma_transmit_buffer(unsigned char *buffer, unsigned int length)
{
DMA_Disable(DMA1_Channel6);
DMA1_Channel6->CHMADDR = (uint32_t)buffer;
DMA1_Channel6->CHNDATA = length;
DMA_Enable(DMA1_Channel6);
while (DMA_ReadStatusFlag(DMA1_FLAG_TC6) == RESET);
}
void oled_register_config()
{
oled_i2c_wr_byte(0xAE, OLED_CMD);
oled_i2c_wr_byte(0xAE, OLED_CMD); //--turn off oled panel
oled_i2c_wr_byte(0x00, OLED_CMD); //---set low column address
oled_i2c_wr_byte(0x10, OLED_CMD); //---set high column address
oled_i2c_wr_byte(0x40, OLED_CMD); //--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
oled_i2c_wr_byte(0x81, OLED_CMD); //--set contrast control register
oled_i2c_wr_byte(0xCF, OLED_CMD); // Set SEG Output Current Brightness
oled_i2c_wr_byte(0xA0, OLED_CMD); //oled_i2c_wr_byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
oled_i2c_wr_byte(0xC0, OLED_CMD); //oled_i2c_wr_byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
oled_i2c_wr_byte(0xA6, OLED_CMD); //--set normal display
oled_i2c_wr_byte(0xA8, OLED_CMD); //--set multiplex ratio(1 to 64)
oled_i2c_wr_byte(0x3f, OLED_CMD); //--1/64 duty
oled_i2c_wr_byte(0xD3, OLED_CMD); //-set display offset Shift Mapping RAM Counter (0x00~0x3F)
oled_i2c_wr_byte(0x00, OLED_CMD); //-not offset
oled_i2c_wr_byte(0xd5, OLED_CMD); //--set display clock divide ratio/oscillator frequency
oled_i2c_wr_byte(0x80, OLED_CMD); //--set divide ratio, Set Clock as 100 Frames/Sec
oled_i2c_wr_byte(0xD9, OLED_CMD); //--set pre-charge period
oled_i2c_wr_byte(0xF1, OLED_CMD); //Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
oled_i2c_wr_byte(0xDA, OLED_CMD); //--set com pins hardware configuration
oled_i2c_wr_byte(0x12, OLED_CMD);
oled_i2c_wr_byte(0xDB, OLED_CMD); //--set vcomh
oled_i2c_wr_byte(0x40, OLED_CMD); //Set VCOM Deselect Level
oled_i2c_wr_byte(0x20, OLED_CMD); //-Set Page Addressing Mode (0x00/0x01/0x02) 设置地址模式
oled_i2c_wr_byte(0x00, OLED_CMD); //00b, Horizontal Addressing Mode 水平地址模式
oled_i2c_wr_byte(0x20, OLED_CMD); //-Set Page Addressing Mode (0x00/0x01/0x02)
oled_i2c_wr_byte(0x02, OLED_CMD); //10b, Page Addressing Mode (RESET)
oled_i2c_wr_byte(0x8D, OLED_CMD); //--set Charge Pump enable/disable
oled_i2c_wr_byte(0x14, OLED_CMD); //--set(0x10) disable
oled_i2c_wr_byte(0xA4, OLED_CMD); // Disable Entire Display On (0xa4/0xa5)
oled_i2c_wr_byte(0xA6, OLED_CMD); // Disable Inverse Display On (0xa6/a7)
oled_i2c_wr_byte(0xAF, OLED_CMD); //--turn on oled panel
oled_i2c_wr_byte(0xAF, OLED_CMD); /*display ON*/
}
void oled_i2c_write_buffer(unsigned char *buffer, unsigned int length)
{
I2C_EnableGenerateStart(I2C1);
while (!I2C_ReadEventStatus(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //EV5
I2C_Tx7BitAddress(I2C1, SSD1306_ADDRESS, I2C_DIRECTION_TX);
while (!I2C_ReadEventStatus(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //EV6
I2C_TxData(I2C1, 0x40);
while (!I2C_ReadEventStatus(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING)); //EV8
i2c_dma_transmit_buffer(buffer, length); //DMA Transmit
}
//刷新整个屏幕
void oled_refresh(unsigned char mem[])
{
oled_i2c_write_buffer(mem, OLED_MEM_SIZE);
oled_delay_ms(200);
}
int main(void)
{
oled_init();
while(1)
{
oled_refresh(&mario1[BMP_OFFSET]);
oled_refresh(&mario2[BMP_OFFSET]);
oled_refresh(&mario3[BMP_OFFSET]);
oled_refresh(&mario4[BMP_OFFSET]);
oled_refresh(&mario5[BMP_OFFSET]);
oled_refresh(&mario6[BMP_OFFSET]);
oled_refresh(&mario7[BMP_OFFSET]);
oled_refresh(&mario8[BMP_OFFSET]);
oled_refresh(&mario9[BMP_OFFSET]);
oled_refresh(&mario10[BMP_OFFSET]);
oled_refresh(&mario11[BMP_OFFSET]);
oled_refresh(&mario12[BMP_OFFSET]);
}
}
while(1)
{
oled_refresh(&mario1[BMP_OFFSET]);
oled_refresh(&mario2[BMP_OFFSET]);
oled_refresh(&mario3[BMP_OFFSET]);
oled_refresh(&mario4[BMP_OFFSET]);
oled_refresh(&mario5[BMP_OFFSET]);
oled_refresh(&mario6[BMP_OFFSET]);
oled_refresh(&mario7[BMP_OFFSET]);
oled_refresh(&mario8[BMP_OFFSET]);
oled_refresh(&mario9[BMP_OFFSET]);
oled_refresh(&mario10[BMP_OFFSET]);
oled_refresh(&mario11[BMP_OFFSET]);
oled_refresh(&mario12[BMP_OFFSET]);
}
END