大神教你如何用ML51单片机点亮
扫描二维码
随时随地手机看文章
网站:21ic.com
驱动芯片是SSD1315
第一步,点亮屏幕:
得到一块新的屏幕,最重要的就是看能否点亮,点亮了才能使用做各种显示应用。而这个店家没有提供驱动代码,懒得自己写啊,这种通用的肯定有,I2C很容易GPIO模拟,然后搜其他店家,有不少提供驱动代码的,下载了移植到我的ML51开发板上试试。下面链接是厂家提供的驱动程序。
链接: https://pan.baidu.com/s/1jqHcX9fIyKdUirFoZ3qSCA 提取码: mj6e 复制这段内容后打开百度网盘手机App,操作更方便哦为方便下载,提供其中源码与取模软件的本地下载,资料说明看不看无所谓。
//对选用的管脚设置为开漏模式
MFP_P20_GPIO;
MFP_P21_GPIO;
P20_OPENDRAIN_MODE;
P21_OPENDRAIN_MODE;
sbit OLED_SCL=P2^0;//SCL
sbit OLED_SDA=P2^1;//SDA
sbit OLED_RES =P2^2;//RES
unsigned char code Hzk[][32]={
{0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0xFC,0x40,0x40,0x40,0x40,0x60,0xE0,0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x02,0x02,0x02,0x07,0x00,0x00},/*"中",0*/
{0x00,0x00,0xF8,0x08,0x08,0x28,0x28,0x28,0xE8,0x28,0x28,0x38,0x28,0xF8,0x08,0x00,0x00,0x00,0x7F,0x20,0x20,0x29,0x29,0x29,0x2F,0x29,0x2F,0x2D,0x28,0x7F,0x00,0x00},/*"国",1*/
{0x00,0x00,0x00,0xF0,0x20,0x20,0x20,0xFC,0x20,0x20,0x20,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x05,0x05,0x05,0x7F,0x45,0x45,0x45,0x4F,0x40,0x60,0x78,0x60},/*"电",2*/
{0x00,0x00,0x10,0x10,0x10,0x10,0x10,0xD0,0x30,0x30,0x10,0x08,0x80,0x80,0x00,0x00,0x01,0x01,0x01,0x01,0x21,0x21,0x41,0x7F,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00},/*"子",3*/
{0x00,0x00,0xF8,0x10,0x50,0x90,0x90,0x70,0x70,0x90,0xD0,0x70,0x30,0xF8,0x00,0x00,0x00,0x00,0x7F,0x00,0x08,0x06,0x01,0x1E,0x08,0x06,0x03,0x4E,0x40,0x7F,0x00,0x00},/*"网",4*/
};
//#include "ML51.h"
#include "oled.h"
#include "bmp.h"
int main(void)
{
unsigned char t=' ';
OLED_Init();//初始化OLED
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
while(1)
{
// OLED_DrawBMP(0,0,128,64,BMP1);
// delay_ms(5);
// OLED_Clear();
// OLED_ShowChinese(0,0,0,16);
OLED_ShowChinese(18,0,0,16);//中
OLED_ShowChinese(36,0,1,16);//国
OLED_ShowChinese(54,0,2,16);//电
OLED_ShowChinese(72,0,3,16);//子
OLED_ShowChinese(90,0,4,16);//网
// OLED_ShowChinese(108,0,6,16);
OLED_ShowString(8,2,"bbs.21ic.com",16);
OLED_ShowString(20,4,"2020/11/17",16);
OLED_ShowString(0,6,"ASCII:",16);
OLED_ShowString(63,6,"CODE:",16);
OLED_ShowChar(48,6,t,16);
t++;
if(t>'~')t=' ';
OLED_ShowNum(103,6,t,3,16);
// delay_ms(5);
// OLED_Clear();
}
}
第二步,应用到自己的项目中:
如果需要显示温湿度的话,就可以利用这些函数组合出你要显示的内容,例如显示“温度:36.5℃”。
则需要先生成所需的汉字与符号字模。
unsigned char code Hzk[][32]={
{0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0xFC,0x40,0x40,0x40,0x40,0x60,0xE0,0x00,0x00,0x00,0x00,0x07,0x02,0x02,0x02,0x02,0x7F,0x02,0x02,0x02,0x02,0x02,0x07,0x00,0x00},/*"中",0*/
{0x00,0x00,0xF8,0x08,0x08,0x28,0x28,0x28,0xE8,0x28,0x28,0x38,0x28,0xF8,0x08,0x00,0x00,0x00,0x7F,0x20,0x20,0x29,0x29,0x29,0x2F,0x29,0x2F,0x2D,0x28,0x7F,0x00,0x00},/*"国",1*/
{0x00,0x00,0x00,0xF0,0x20,0x20,0x20,0xFC,0x20,0x20,0x20,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x05,0x05,0x05,0x7F,0x45,0x45,0x45,0x4F,0x40,0x60,0x78,0x60},/*"电",2*/
{0x00,0x00,0x10,0x10,0x10,0x10,0x10,0xD0,0x30,0x30,0x10,0x08,0x80,0x80,0x00,0x00,0x01,0x01,0x01,0x01,0x21,0x21,0x41,0x7F,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00},/*"子",3*/
{0x00,0x00,0xF8,0x10,0x50,0x90,0x90,0x70,0x70,0x90,0xD0,0x70,0x30,0xF8,0x00,0x00,0x00,0x00,0x7F,0x00,0x08,0x06,0x01,0x1E,0x08,0x06,0x03,0x4E,0x40,0x7F,0x00,0x00},/*"网",4*/
{0x00,0x80,0x80,0xC8,0x30,0x00,0x80,0x40,0x30,0x18,0x10,0x60,0xC0,0x80,0x80,0x80,0x00,0x00,0x00,0x3F,0x12,0x09,0x04,0x7F,0x44,0x44,0x42,0x43,0x42,0x78,0x60,0x00},/*"论",5*/
{0x40,0x40,0x40,0xF8,0x40,0x60,0xC0,0x90,0x90,0x90,0x90,0x90,0x90,0x98,0xD0,0x80,0x00,0x10,0x10,0x1F,0x08,0x08,0x60,0x30,0x2C,0x27,0x23,0x25,0x28,0x30,0x60,0x00},/*"坛",6*/
{0x00,0x00,0x00,0x18,0xF8,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x1B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",7*/
{0x00,0x40,0xC0,0x18,0x90,0x60,0xF8,0xA8,0xA8,0xA8,0xA8,0xA8,0xF8,0x08,0x00,0x00,0x00,0x04,0x7C,0x7E,0x01,0x00,0x21,0x3E,0x22,0x3E,0x22,0x3E,0x23,0x3E,0x22,0x20},/*"温",8*/
{0x00,0x00,0x00,0xF0,0x90,0x90,0x90,0xF4,0x98,0x90,0x90,0xF0,0x90,0xD8,0x90,0x00,0x00,0x40,0x30,0x0F,0x40,0x44,0x44,0x4F,0x35,0x25,0x35,0x6F,0x64,0x40,0x40,0x00},/*"度",9*/
{0x00,0x40,0xC0,0x98,0x60,0x00,0xF8,0xA8,0xA8,0xA8,0xA8,0xA8,0xA8,0xF8,0x08,0x00,0x04,0x04,0x7E,0x01,0x20,0x24,0x3D,0x20,0x3F,0x20,0x20,0x3F,0x24,0x27,0x33,0x20},/*"湿",10*/
{0x00,0x00,0x00,0xF0,0x90,0x90,0x90,0xF4,0x98,0x90,0x90,0xF0,0x90,0xD8,0x90,0x00,0x00,0x40,0x30,0x0F,0x40,0x44,0x44,0x4F,0x35,0x25,0x35,0x6F,0x64,0x40,0x40,0x00},/*"度",11*/
{0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x19,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",12*/
{0x00,0x00,0x78,0x48,0x78,0xC0,0xE0,0x10,0x08,0x08,0x08,0x08,0x10,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0x08,0x10,0x10,0x10,0x10,0x08,0x08,0x04,0x00},/*"℃",13*/
};
最后显示的效果如下图所示,如果将温度的数字通过读取传感器继续更新,就可以看到实时的温度了。
然后合理的利OLED厂家提供的库函数组合。
//#include "ML51.h"
int main(void)
{
char i=0;
unsigned char t=' ';
OLED_Init();//初始化OLED
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
while(1)
{
//第一行显示8个字符16*8==128
for(i=0;i<8;i++)
OLED_ShowChinese(i*16,0,i,16);
//第二行显示 温度:
OLED_ShowChinese(0,2,8,16);//温
OLED_ShowChinese(16,2,9,16);//度
OLED_ShowChinese(32,2,12,16);//:
//在第二行,第四个字符位置显示36
OLED_ShowNum(48,2,36,2,16);
OLED_ShowChar(66,2,'.',16);
OLED_ShowNum(74,2,5,1,16);
OLED_ShowChinese(90,2,13,16);//℃
//在第三行显示字符串
OLED_ShowString(8,4,"bbs.21ic.com",16);
//delay_ms(5);
}
}
在使用中遇到了一个问题,我在main.c和oled.h都引用了头文件#include "ML51.h"
导致编译后报错重复定义。然后我删除了main.c中的,重新设置了引用头文件路径,完美解决。
另外如果你修改了目录,或者重建了目录结构,需要在这里也修改一下,不然工程无法找到对应的exe。
..\Library\Startup\exe\hex2bin.exe .\output\@L.hex
运行该命令可以生成所需的hex文件。
第三步,添加其他的外设驱动,实现显示检测数据显示功能
这里我使用的是ADC检测温度,然后通过该屏幕显示。
已经添加了NTC传感器。效果非常好
还有就是使用的GPIO,一定要初始化为GPIO模式的开漏模式,不然无法驱动屏幕。
//***********************************************************************************************************
// File Function: 通过ML51的GPIO P20、P21驱动I2C显示屏,通过P24_ADC_CH1测量NTC传感器阻抗,然后换算成温度显示在I2C屏幕上
//***********************************************************************************************************
//#include "ML51.h"
/*
P20,P21接I2C的OLED屏幕SCL与SDA接口,
P24接NTC传感器接口,读取温度,并显示在OLED上
*/
void main (void)
{
unsigned int ADCRESULT;
float Rval;
float temp;
// 定义温度整数部分与小数部分
unsigned int temp_H,temp_L;
char i=0;
ADC_Open(ADC_SINGLE,1); //Enable P24_ADC_CH1
ADC_ConvertTime(3,7);
//对选用的管脚设置为开漏模式
MFP_P20_GPIO;
MFP_P21_GPIO;
P20_OPENDRAIN_MODE;
P21_OPENDRAIN_MODE;
OLED_Init();//初始化OLED
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
while(1)
{
set_ADCCON0_ADCS; // Software trig adc start
while((ADCCON0|CLR_BIT7)==CLR_BIT7); // wait ADCF = 1;
ADCRESULT = (ADCRH<<4)+ADCRL;
Rval=1000*((ADCRESULT*6.2)/(4095-ADCRESULT));
temp=Res_to_Tem(Rval);
temp_H=((unsigned int)(temp*10.0))/10;
temp_L=((unsigned int)(temp*10.0))%10;
for(i=0;i<8;i++)
OLED_ShowChinese(i*16,0,i,16);
//第二行显示 温度:
OLED_ShowChinese(0,2,8,16);//温
OLED_ShowChinese(16,2,9,16);//度
OLED_ShowChinese(32,2,12,16);//:
//在第二行,第四个字符位置显示36
OLED_ShowNum(48,2,temp_H,2,16);
OLED_ShowChar(64,2,'.',16);
OLED_ShowNum(72,2,temp_L,1,16);
OLED_ShowChinese(82,2,13,16);//℃
//在第三行显示字符串
OLED_ShowString(8,4,"bbs.21ic.com",16);
//在第4行显示字符串
// OLED_ShowString(8,6,"gaoyang9992006",16);
OLED_ShowString(0,6,"0123456789123456",16);
Timer2_Delay(24000000,128,5,100000);
}
}
另外对于字符是使用的8*16的字模,所以每行可以显示16个字符,共计可以显示4行。
本文系21ic论坛网友gaoyang9992006原创
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!