切换到宽版
爱科技/爱创意/爱折腾;电子/数码爱好者的家!欢迎访问新版数码之家网站
  • 2379阅读
  • 11回复

[C51]能不能把数码管改成OLED [复制链接]

上一主题 下一主题
离线wjxhone

发帖
85
M币
6114
专家
7
粉丝
132
只看该作者 10楼 发表于: 02-15
其实驱动SPI接口的OLED屏或液晶屏,比驱动数码管简单多了。我试着用新唐的N76E003写了一下程序,用硬件SPI驱动6448的OLED屏,大部份的程序是从网上开源的T12焊台那边抄的。刚开始用查表法,但只是整数部分有效,小数部分一直不变,所以用了计算法。

#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"
#include "math.h" //数学函数库
bit BIT_TMP;

#define X_WIDTH 128
#define Y_WIDTH 64
#define CHINESE_WIDE    16


sbit A0 = P0^3;
sbit RST = P0^4;


unsigned char code CHINESE_Idx[] =
{
"度内室温"
};


//汉字16X16字符表
//纵向字节倒序
unsigned char code F16X16[] =
{
   0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0xA5,0xA6,0xA4,0xFC,0x24,0x34,0x26,0x04,0x00,0x40,0x20,0x9F,0x80,0x42,0x42,0x26,0x2A,0x12,0x2A,0x26,0x42,0x40,0xC0,0x40,0x00,  //-度-
   0x00,0xF0,0x10,0x10,0x10,0x10,0x90,0x7F,0x10,0x10,0x10,0x10,0x10,0xF8,0x10,0x00,0x00,0xFF,0x00,0x08,0x04,0x02,0x01,0x00,0x01,0x02,0x06,0x4C,0x80,0x7F,0x00,0x00,  //-内-
   0x10,0x0C,0x24,0x24,0xA4,0x64,0x25,0x26,0x24,0x24,0xA4,0x34,0x24,0x14,0x0C,0x00,0x00,0x40,0x40,0x49,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x4B,0x60,0x40,0x00,0x00,  //-室-
   0x10,0x22,0x64,0x0C,0x80,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFF,0x02,0x00,0x00,0x04,0x04,0xFE,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00,  //-温-
};//汉字16X16 end



//======================================================
// 128X64I液晶底层驱动[8X16]字体库
// 设计者: powerint
// 描  述: [8X16]西文字符的字模数据 (纵向取模,字节倒序)
// !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//======================================================
unsigned char code F8X16[]=
{
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
  0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//!1
  0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"2
  0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//#3
  0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$4
  0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//%5
  0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//&6
  0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'7
  0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//(8
  0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//)9
  0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//*10
  0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+11
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//,12
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//-13
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//.14
  0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,///15
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//016
  0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//117
  0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//218
  0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//319
  0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//420
  0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//521
  0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//622
  0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//723
  0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//824
  0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//925
  0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//:26
  0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//;27
  0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//<28
  0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//=29
  0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//>30
  0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//?31
  0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@32
  0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A33
  0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B34
  0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C35
  0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D36
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E37
  0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F38
  0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G39
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H40
  0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I41
  0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J42
  0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K43
  0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L44
  0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M45
  0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N46
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O47
  0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P48
  0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q49
  0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R50
  0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S51
  0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T52
  0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U53
  0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V54
  0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W55
  0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X56
  0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y57
  0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z58
  0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[59
  0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\60
  0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//]61
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^62
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_63
    0x00,0x00,0x00,0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//℃上的圈64 按`出来
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a65
  0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b66
  0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c67
  0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d68
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e69
  0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f70
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g71
  0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h72
  0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i73
  0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j74
  0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k75
  0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l76
  0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m77
  0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n78
  0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o79
  0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p80
  0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q81
  0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r82
  0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s83
  0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t84
  0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u85
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v86
  0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w87
  0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x88
  0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y89
  0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z90
  0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{91
  0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//|92
  0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//}93
  0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~94

};


    
void Timer0_Delay1ms(UINT32 u32CNT)  //1ms延时
{
    clr_T0M;                                        //T0M=0, Timer0 Clock = Fsys/12
    TMOD |= 0x01;                                   //Timer0 is 16-bit mode
    set_TR0;                                        //Start Timer0
    while (u32CNT != 0)
    {
        TL0 = 0xcb;         //Find  define in "Function_define.h" "TIMER VALUE"
        TH0 = 0xfa;
        while (TF0 != 1);                           //Check Timer0 Time-Out Flag
        clr_TF0;
        u32CNT --;
    }
    clr_TR0;                                        //Stop Timer0
}



void OLED_WrCmd(UINT8 dat)  
{
    A0 = 0;
    SS = 0;
    SPDR = dat;
    while(!(SPSR & SET_BIT7));
  clr_SPIF;
    SS = 1;
}


void OLED_WrDat(UINT8 dat)
{
    A0= 1;
    SS = 0;
    SPDR = dat;
    while(!(SPSR & SET_BIT7));
  clr_SPIF;
    SS = 1;
}




void SPI_Init(void)
{      
        P15_Quasi_Mode;                                                        // P15 (SS) Quasi mode
        P10_Quasi_Mode;                                                        // P10(SPCLK) Quasi mode
        P00_Quasi_Mode;                                                        // P00 (MOSI) Quasi mode
    P01_Quasi_Mode;                                                        // P01 (MISO) Quasi mode  此脚悬空,不接入电路
      P03_Quasi_Mode;                                                    
    P04_Quasi_Mode;

    
    set_DISMODF;                                // SS General purpose I/O ( No Mode Fault )
    clr_SSOE ;
  
    clr_LSBFE;                                  // MSB first        

    set_CPOL;                                   // The SPI clock is low in idle mode
    set_CPHA;                                   // The data is sample on the second edge of SPI clock
    
    
    set_MSTR;                                   // SPI in Master mode
    SPICLK_DIV2;                                        // Select SPI clock
    set_SPIEN;                                  // Enable SPI function
    
}

void OLED_Set_Pos(unsigned char x, unsigned char y)
{    
    OLED_WrCmd(0xb0+y);
    OLED_WrCmd(((32+x)&0x0f));        //SSD1306&SSD1309
  OLED_WrCmd((((32+x)&0xf0)>>4)|0x10);    
}


void OLED_Fill(unsigned char bmp_data)
{
    unsigned char y,x;
    
    for(y=0;y<8;y++)
    {
        OLED_WrCmd(0xb0+y);
        OLED_WrCmd(0x01);
        OLED_WrCmd(0x10);
        for(x=0;x<X_WIDTH;x++)
            OLED_WrDat(bmp_data);
    }
}
void OLED_CLS(void)
{
    unsigned char y,x;    
    for(y = 0;y < 8;y++)
    {
        OLED_WrCmd(0xb0+y);
        OLED_WrCmd(0x01);
        OLED_WrCmd(0x10);
        for(x=0;x < 128;x++)
            OLED_WrDat(0);
        OLED_WrCmd(0xe3);//(nop)
    }
    OLED_WrCmd(0xe3);//(nop)

}

//初始化LCD/////////////////////////////////////////////////////////////////////////
void OLED_Init(void)
{
    RST=0;
    Timer0_Delay1ms(20);
    RST=1;
    Timer0_Delay1ms(20);
    
  OLED_WrCmd(0xae);//关闭OLED面板
  OLED_WrCmd(0x00);//--设置低列地址
  OLED_WrCmd(0x10);//--设置低列地址
  OLED_WrCmd(0x40);//--设置起始行地址 设置映射RAM显示起始行(0x00~0x3F)
  OLED_WrCmd(0x81);//--设置对比控制寄存器
    OLED_WrCmd(0x7f);//(取值0x00-0xff),00最暗,ff最亮//7f
  OLED_WrCmd(0xcf); // 设置SEG输出电流量亮度
  OLED_WrCmd(0xa1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  OLED_WrCmd(0xc8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  OLED_WrCmd(0xa6);//--设置正常显示
  OLED_WrCmd(0xa8);//--设定复用比例(1 to 64)
  OLED_WrCmd(0x3f);//--1/64 duty
  OLED_WrCmd(0xd3);//-设置显示偏移量    移位映射RAM计数器    (0x00~0x3F)
  OLED_WrCmd(0x00);//-不抵消(移动)
  OLED_WrCmd(0xd5);//--设置 显示时钟分频比/振荡器频率
  OLED_WrCmd(0x80);//--设置分频比,设置时钟为100帧/秒
  OLED_WrCmd(0xd9);//--设置预充电时间
  OLED_WrCmd(0xf1);//设置预充电作为15个时钟&放电1个时钟
  OLED_WrCmd(0xda);//--设置COM脚位硬件配置
  OLED_WrCmd(0x12);
  OLED_WrCmd(0xdb);//--set vcomh
  OLED_WrCmd(0x40);//Set VCOM Deselect Level
  OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  OLED_WrCmd(0x02);//
  OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
  OLED_WrCmd(0x14);//--set(0x10) disable
  OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
  OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
  OLED_WrCmd(0xaf);//--turn on oled panel
  OLED_Fill(0x00);  //初始清屏
  OLED_Set_Pos(0,0);  

}



//==============================================================
//函数名:OLED_P8x16Str(unsigned char x,unsigned char y,unsigned char *p)
//功能描述:写入一组标准ASCII字符串
//参数:显示的位置(x,y),y为页范围0~7,要显示的字符串
//返回:无
//==============================================================  
void OLED_P8x16Str(unsigned char x,unsigned char y,unsigned char ch[])
{
  unsigned char c=0,i=0,j=0;
        
  while (ch[j]!='\0')
  {    
    c =ch[j]-32;
    if(x > 122){x=0;y++;}
    OLED_Set_Pos(x,y);    
      for(i=0;i<8;i++)    
        OLED_WrDat(F8X16[c*16+i]);
      OLED_Set_Pos(x,y+1);    
      for(i=0;i<8;i++)    
        OLED_WrDat(F8X16[c*16+i+8]);  
      x+=8;
      j++;
  }
}



/*
* 汉字16X16字
*/
void OLED_P16x16Str(unsigned char x,unsigned char y,unsigned char ch[])
{
    unsigned char str_offset = 0,ii = 0;//
    unsigned int adder = 1; //汉字码对应数组号
    
    while(ch[ii] != '\0')//索引字符串检查是否结尾
    {
        str_offset = 0;    //汉字字符偏移量
        adder = 1;        //初始码
        /*
        *     计算汉字索引查表
        */
        while(CHINESE_Idx[str_offset] > 127)//判断是否为汉字的ASCII码值
        {
            if(CHINESE_Idx[str_offset] == ch[ii])
            {
                if(CHINESE_Idx[str_offset + 1] == ch[ii + 1])
                {
                    adder = str_offset * CHINESE_WIDE;//adder 获取
                    break;//
                }
            }
            str_offset += 2;//    偏移增加        
        }
        if(x>122){x=0;y++;}
        OLED_Set_Pos(x , y);
        if(adder != 1)// 显示汉字                    
        {
            OLED_Set_Pos(x , y);//当前行显示
            for(str_offset = 0;str_offset < CHINESE_WIDE;str_offset++)              
            {
                OLED_WrDat(F16X16[adder]);//    
                adder += 1;
            }      
            OLED_Set_Pos(x,y + 1); //换行显示
            for(str_offset = 0;str_offset < CHINESE_WIDE;str_offset++)          
            {
                OLED_WrDat(F16X16[adder]);
                adder += 1;
            }          
        }
        else              //显示空白字符            
        {
            ii += 1;
          OLED_Set_Pos(x,y);
            for(str_offset = 0;str_offset < 16;str_offset++)
            {
                    OLED_WrDat(0);
            }
            OLED_Set_Pos(x,y + 1);
            for(str_offset = 0;str_offset < 16;str_offset++)
            {          
                    OLED_WrDat(0);    
            }
        }
        x += CHINESE_WIDE;//显示X坐标偏移
        ii += 2;//汉字占2个字节
    }
}


//输出汉字和字符混合字符串
void OLED_Print(unsigned char x, unsigned char y, unsigned char ch[])
{
    unsigned char buff[3];
    unsigned int ii=0;        
    while(ch[ii] != '\0')//判断显示字符是否结尾
    {
        if(ch[ii] > 127)//判断->汉字ASCII字码
        {
            buff[0] = ch[ii];
             buff[1] = ch[ii + 1];
            buff[2] = '\0';            //汉字为两个字节
            OLED_P16x16Str(x , y, buff);    //显示汉字
            x += 16;//显示偏移坐标
            ii += 2;//字码偏移
        }
        else//判断->字母ASCII
        {
            buff[0] = ch[ii];    
            buff[1] = '\0';            //字母占一个字节
            OLED_P8x16Str(x , y , buff);    //显示字母
            x += 8;//显示偏移坐标
            ii+= 1;//字码偏移
        }
    }
}




UINT16 Get_ADC10bitResult(char channels)//获取ADC通道ADC值
{
       UINT16 AD_Value;
              
        switch(channels)
        {
                case 1: //室外温度
                        Enable_ADC_AIN0;                      
                        break;
                case 2:  //室内温度
                        Enable_ADC_AIN1;
                        break;                                
                                default: break;
        }
                
                clr_ADCF;
        set_ADCS;               // ADC start trig signal
        while(ADCF == 0);//等待完成
        AD_Value = ADCRH;        //取高8位
        AD_Value <<= 4;        
        AD_Value = AD_Value|(ADCRL&0x0F);        //取低4位
      
        AD_Value = AD_Value >> 2;//12位转10位
        return  AD_Value;    
}





const float Rp=10000.0;             //10K
const float T2 = (273.15+25.0); //T2
const float Bx = 3950.0;              //B
const float Ka = 273.15;

float temp;  //环境温度
float Rt;  //热敏电阻的阻值
float voltage;  //热敏电阻两端的电压



unsigned char str_buff[20];                //显示缓存


void main()
{
    
    SPI_Init();
    OLED_Init();

    OLED_Print(0,2,"室内温度");

      while (1)
        {
                    voltage=(float)Get_ADC10bitResult(2)/1023*5.0;  //5.0是AD转换参考电压
                    Rt=(10000*voltage)/(5.0-voltage);  //10000是与热敏电阻串联的电阻的阻值
              temp = (1/(log(Rt/Rp)/Bx + (1/T2)))-273.15+0.1; //加0.1误差矫正
  
                            
                    sprintf((char *)str_buff , "%2.1f`C " ,temp);
                    OLED_Print(8,5,str_buff);
                Timer0_Delay1ms(500);
        }
    
    
}
本文内容包含图片或附件,获取更多资讯,请 登录 后查看;或者 注册 成为会员获得更多权限
离线linrumiao

发帖
38
M币
522
专家
0
粉丝
7
只看该作者 11楼 发表于: 02-20
可惜我没新唐单片机,哎,能不能移到其它单片机上??
[ 此帖被linrumiao在2019-02-26 10:03重新编辑 ]