入手–新唐NUC240与点亮OLED
一、SDK与烧录
1.SDK
新唐官网
可以在官网下载你所需要的SDK与pack。
2.烧录
首先下载官网自带模拟IIC的SDK。
打开代码,选择NUlink烧录。
二、代码修改
由于官网自带的是EEPRON的软件IIC,我们需要稍微修改一下。
EEPRON写入时序为sta(1 byte)+address(2 byte)+data(1 byte)
IIC写入时序为address(1 byte)+control(1 byte)+data(1 byte)
修改I2C_GPIO_Write_Byte()函数代码如下
void I2C_GPIO_Write_Byte(uint16_t u16Cmd, uint8_t u8Data)
{
uint8_t u8Addr, u8cmd;
u8Addr = u16Cmd >> 8;
u8cmd = (uint8_t)u16Cmd;
I2C_Start();
I2C_Write(u8Addr);
I2C_Write(u8cmd);
I2C_Write(u8Data);
I2C_Stop();
}
三、点亮OLED
//OLED初始化
void OLED_Init(void)
{
I2C_GPIO_Write_Byte(0x7800, 0xAE);
I2C_GPIO_Write_Byte(0x7800, 0xD5);
I2C_GPIO_Write_Byte(0x7800, 0x50);
I2C_GPIO_Write_Byte(0x7800, 0xA8);
I2C_GPIO_Write_Byte(0x7800, 0X3F);
I2C_GPIO_Write_Byte(0x7800, 0xD3);
I2C_GPIO_Write_Byte(0x7800, 0X00);
I2C_GPIO_Write_Byte(0x7800, 0x40);
I2C_GPIO_Write_Byte(0x7800, 0x8D);
I2C_GPIO_Write_Byte(0x7800, 0x14);
I2C_GPIO_Write_Byte(0x7800, 0x20);
I2C_GPIO_Write_Byte(0x7800, 0x02);
I2C_GPIO_Write_Byte(0x7800, 0xA0);
I2C_GPIO_Write_Byte(0x7800, 0xC0);
I2C_GPIO_Write_Byte(0x7800, 0xDA);
I2C_GPIO_Write_Byte(0x7800, 0x12);
I2C_GPIO_Write_Byte(0x7800, 0x81);
I2C_GPIO_Write_Byte(0x7800, 0xEF);
I2C_GPIO_Write_Byte(0x7800, 0xD9);
I2C_GPIO_Write_Byte(0x7800, 0xf1);
I2C_GPIO_Write_Byte(0x7800, 0xDB);
I2C_GPIO_Write_Byte(0x7800, 0x30);
I2C_GPIO_Write_Byte(0x7800, 0xA4);
I2C_GPIO_Write_Byte(0x7800, 0xA6);
I2C_GPIO_Write_Byte(0x7800, 0xAF);
CLK_SysTickDelay(10000);//延时10MS
OLED_Clear();//清屏
}
//OLED Clear
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
I2C_GPIO_Write_Byte(0x7800,0xb0+i);
I2C_GPIO_Write_Byte(0x7800,0x00);
I2C_GPIO_Write_Byte(0x7800,0x10);
for(n=0;n<128;n++)
{
I2C_GPIO_Write_Byte(0x7840,0x00);
}
}
}
//OLED Set pos
void OLED_Set_Pos(unsigned char x,unsigned char y)
{
I2C_GPIO_Write_Byte(0x7800,0xb0+y);
I2C_GPIO_Write_Byte(0x7800,((x&0xf0)>>4)|0x10);
I2C_GPIO_Write_Byte(0x7800,(x&0x0f));
}
//显示字符
void OLED_ShowChar(u8 x,u8 y,u8 string,u8 size)
{
unsigned char data=0,i=0;
data = string - ' ';//得到偏移后的值
if(x>128-1)
{
x = 0;
y = y + 2;
}
if(size == 16)
{
OLED_Set_Pos(x,y);
for(i=0; i<8; i++)
I2C_GPIO_Write_Byte(0x7840,F8X16[data*16+i]);
OLED_Set_Pos(x,y+1);
for(i=0; i<8; i++)
I2C_GPIO_Write_Byte(0x7840,F8X16[data*16+i+8]);
}
else
{
OLED_Set_Pos(x,y);
for(i=0; i<6; i++)
I2C_GPIO_Write_Byte(0x7840,F6x8[data][i]);
}
}
//显示字符串
void OLED_ShowString(u8 x,u8 y,u8* string,u8 size)
{
unsigned char i=0;
while(string[i]!='\0')
{
OLED_ShowChar(x,y,string[i],size);
x+=8;
if(x > 120)//大于一行
{
x=0;
y+=2;
}
i++;
}
}
//main添加初始化
OLED_Init();
OLED_ShowString(1,1,"Hello World",16);
将代码烧录到MCU
效果如下