【瑞萨BLE/WIFI模块测评】+ 驱动OLED显示
测试板卡I2C驱动OLED显示模块;一、硬件电路图部分
开发板上预留了I2C模块接口,可以连接I2C接口OLED显示。
引脚P408和P409对应点点taptap安卓的I2C3接口,我在使用这个端口测试时,发现这个板卡的这两个引脚始终没有输出信号。
下面测试使用I2C0接口驱动OLED。
对应的接口是P100和P101引脚。
二、配置I2C
在rasc软件中配置I2C功能
配置完成后,生成代码。
三、测试程序部分
3.1、oled.c
#include "hal_data.h"
#include "oled/fun_oled.h"
#include "oled/codetab.h"
fsp_err_t err = FSP_SUCCESS;
i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
inttimeout_ms = 100;
void sci_i2c0_master_callback(i2c_master_callback_args_t *p_args)
{
i2c_event = I2C_MASTER_EVENT_ABORTED;
if (NULL != p_args)
{
/* capture callback event for validating the i2c transfer event*/
i2c_event = p_args->event;
}
}
void WriteCmd(unsigned char I2C_Command)
{
uint8_t ii={0x00,0x00};
ii=I2C_Command;
err = R_SCI_I2C_Write(&g_i2c0_ctrl, ii, 0x02, true);
assert(FSP_SUCCESS == err);
/* Since there is nothing else to do, block until Callback triggers*/
while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms>0)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MICROSECONDS);
timeout_ms--;
}
if (I2C_MASTER_EVENT_ABORTED == i2c_event)
{
//__BKPT(0);
}
/* Read data back from the I2C slave */
i2c_event = I2C_MASTER_EVENT_ABORTED;
timeout_ms = 100;
}
void WriteDat(unsigned char I2C_Data)
{
uint8_t ii={0x40,0x00};
ii=I2C_Data;
err = R_SCI_I2C_Write(&g_i2c0_ctrl, ii, 0x02, true);
assert(FSP_SUCCESS == err);
/* Since there is nothing else to do, block until Callback triggers*/
while ((I2C_MASTER_EVENT_TX_COMPLETE != i2c_event) && timeout_ms>0)
{
R_BSP_SoftwareDelay(1U, BSP_DELAY_UNITS_MICROSECONDS);
timeout_ms--;
}
if (I2C_MASTER_EVENT_ABORTED == i2c_event)
{
//__BKPT(0);
}
/* Read data back from the I2C slave */
i2c_event = I2C_MASTER_EVENT_ABORTED;
timeout_ms = 100;
}
void OLED_WrCmd(unsigned char IIC_Command)
{
WriteCmd(IIC_Command);
}
void OLED_Init(void)
{
err = R_SCI_I2C_Open(&g_i2c0_ctrl, &g_i2c0_cfg);
assert(FSP_SUCCESS == err);
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
WriteCmd(0xAE); //display off
WriteCmd(0x20); //Set Memory Addressing Mode
WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
WriteCmd(0xc8); //Set COM Output Scan Direction
WriteCmd(0x00); //---set low column address
WriteCmd(0x10); //---set high column address
WriteCmd(0x40); //--set start line address
WriteCmd(0x81); //--set contrast control register
WriteCmd(0xff); //亮度调节 0x00~0xff
WriteCmd(0xa1); //--set segment re-map 0 to 127
WriteCmd(0xa6); //--set normal display
WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
WriteCmd(0x3F); //
WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
WriteCmd(0xd3); //-set display offset
WriteCmd(0x00); //-not offset
WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
WriteCmd(0xf0); //--set divide ratio
WriteCmd(0xd9); //--set pre-charge period
WriteCmd(0x22); //
WriteCmd(0xda); //--set com pins hardware configuration
WriteCmd(0x12);
WriteCmd(0xdb); //--set vcomh
WriteCmd(0x40); //0x20,0.77xVcc
WriteCmd(0x8d); //--set DC-DC enable
WriteCmd(0x14); //
WriteCmd(0xaf); //--turn on oled panel
}
void OLED_SetPos(unsigned char x, unsigned char y)
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
void OLED_Fill(unsigned char fill_Data)//全屏填充
{
unsigned char m,n;
for(m=0;m<8;m++)
{
WriteCmd(0xb0+m); //page0-page1
WriteCmd(0x00); //low column start address
WriteCmd(0x10); //high column start address
for(n=0;n<128;n++)
{
WriteDat(fill_Data);
}
}
}
void OLED_CLS(void)//清屏
{
OLED_Fill(0xff);
}
void OLED_ON(void)
{
WriteCmd(0X8D);//设置电荷泵
WriteCmd(0X14);//开启电荷泵
WriteCmd(0XAF);//OLED唤醒
}
void OLED_OFF(void)
{
WriteCmd(0X8D);//设置电荷泵
WriteCmd(0X10);//关闭电荷泵
WriteCmd(0XAE);//OLED休眠
}
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
unsigned char c = 0,i = 0,j = 0;
switch(TextSize)
{
case 1:
{
while(ch != '\0')
{
c = ch - 32;
if(x > 126)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<6;i++)
WriteDat(F6x8);
x += 6;
j++;
}
}break;
case 2:
{
while(ch != '\0')
{
c = ch - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<8;i++)
WriteDat(F8X16);
OLED_SetPos(x,y+1);
for(i=0;i<8;i++)
WriteDat(F8X16);
x += 8;
j++;
}
}break;
}
}
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
unsigned char wm=0;
unsigned intadder=32*N;
OLED_SetPos(x , y);
for(wm = 0;wm < 16;wm++)
{
WriteDat(F16x16);
adder += 1;
}
OLED_SetPos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
WriteDat(F16x16);
adder += 1;
}
}
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0)
y = y1/8;
else
y = y1/8 + 1;
for(y=y0;y<y1;y++)
{
OLED_SetPos(x0,y);
for(x=x0;x<x1;x++)
{
WriteDat(BMP);
}
}
}
void oled_test(void)
{
OLED_Init();
OLED_Fill(0x00);
OLED_ShowStr(0,0,"RA6M4",2);
OLED_ShowStr(0,2,"I2C OLED TEST",2);
}
3.2、hal_entry.c
#include "hal_data.h"
#include "led/fun_led.h"
#include "uart/fun_uart.h"
#include "oled/fun_oled.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
init_uart();
init_led();
OLED_Init();
oled_test();
while(1)
{
p408_on();
p409_off();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
led1_on();
led2_off();
led3_off();
p408_off();
p409_on();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
led1_off();
led2_on();
led3_off();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
led1_off();
led2_off();
led3_on();
printf("RA-eco-RA6M4 uart test!\r\n");
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
/*******************************************************************************************************************//**
* This function is called at various points during the startup process.This implementation uses the event that is
* called right before main() to set up the pins.
*
* @paramevent Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart (bsp_warm_start_event_t event)
{
if (BSP_WARM_START_RESET == event)
{
#if BSP_FEATURE_FLASH_LP_VERSION != 0
/* Enable reading from data flash. */
R_FACI_LP->DFLCTL = 1U;
/* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
* C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
}
if (BSP_WARM_START_POST_C == event)
{
/* C runtime environment and system clocks are setup. */
/* Configure pins. */
R_IOPORT_Open(&IOPORT_CFG_CTRL, &IOPORT_CFG_NAME);
#if BSP_CFG_SDRAM_ENABLED
/* Setup SDRAM and initialize it. Must configure pins first. */
R_BSP_SdramInit(true);
#endif
}
}
#if BSP_TZ_SECURE_BUILD
FSP_CPP_HEADER
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{
}
FSP_CPP_FOOTER
#endif
四、运行结果
页:
[1]