• 基于STC89C52的oled红外遥控闹钟


    这个红外遥控主要是程序通过对按下的键的键码进行解析,并运行相应的功能代码

             一次按键动作的遥控编码信息为 32 位串行二进制码。对于二进制信号“0”,一个脉冲占 1.2ms;对于二进制信号“1”,一个脉冲占 2.4ms,而每一脉冲内低电平均为 0.6ms。从起始标志到 32 位编码脉冲发完大约需80ms,此后遥控信号维持高电平。若按键未释放,则从起始标志起每隔 108ms 发出 3 个脉冲的重复标志。在 32 位的编码脉冲中,前 16 位码不随按键的不同而变化,我们称之为用户码。它是为了表示特定用户而设置的一个辨识标志,以区别不同机种和不同用户发射的遥控信号,防止误操作。后 16 位码随着按键的不同而改变,我们就是要读取这 16 位按键编码,经解码得到按键键号,转而执行相应控制动作。

    主要运用DS1302时钟芯片,红外遥控,oled显示屏,蜂鸣器,定时器,中断实现

    研究过程:

    1.工程的建立和文件存放分配

    2.根据原理图进行全局参数,引脚申明,类型说明头文件的编写

    3.功能模块调试

       1>oled液晶显示屏功能调试

       2>蜂鸣器功能调试     //定时器2

       3>DS1302读写调试

       4>红外遥控按键功能的调试    //定时器1和外部中断0

    4.所有显示程序的调试

    5.程序总体功能的调试

                         

    7引脚的oled接线:

    GND接GND,VCC接VCC,D0,D1,RST,DC接自定义引脚,CS接地。

     引脚申明类型说明

     1 #ifndef _CONFIG_H_
     2 #define _CONFIG_H_
     3 
     4 #include"reg52.h"
     5 #include"intrins.h"
     6  
     7  typedef signed char int8;
     8  typedef signed int  int16;
     9  typedef unsigned int u16;
    10  typedef unsigned char u8;
    11  typedef signed long int32;
    12 
    13 #define LED P2
    14 #define SYS_MCLK  1000000
    15 
    16  sbit GPIO_KEY=P1;
    17      
    18  sbit OLED_SCLK=P1^0;  //D0时钟管脚
    19  sbit OLED_SDA=P1^1;   //D1数据管脚
    20  sbit OLED_RST=P1^2;   //电源复位,低电平有效
    21  sbit OLED_DC=P1^3;    //数据或命令控制管脚
    22 
    23  sbit DS_IO=P3^4 ;
    24  sbit DS_SCLK=P3^6;
    25  sbit DS_RST=P3^5;
    26 
    27 
    28  sbit  BUZZER=P1^5;
    29 
    30  sbit DSPORT=P3^7;
    31 
    32  sbit IRIN=P3^2;
    33 
    34 #endif    

    oled显示屏代码 

      1 #define _OLED_C
      2 #include"config.h"
      3 #include"OLED.h"
      4 #define _OLED_C
      5 void OLED_SetPos(u8 x,u8 y);
      6  //命令函数
      7 const u8 code F6x8[][6] =
      8 {
      9     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   //sp0
     10     { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },   // !1
     11     { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },   // "2
     12     { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #3
     13     { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $4
     14     { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },   // %5
     15     { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },   // &6
     16     { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },   // '7
     17     { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (8
     18     { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )9
     19     { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *10
     20     { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +11
     21     { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 },   // ,12
     22     { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },   // -13
     23     { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },   // .14
     24     { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },   // /15
     25     { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 016
     26     { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 117
     27     { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },   // 218
     28     { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 319
     29     { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 420
     30     { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },   // 521
     31     { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 622
     32     { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },   // 723
     33     { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },   // 824
     34     { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E },   // 925
     35     { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },   // :26
     36     { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;27
     37     { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },   // <28
     38     { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },   // =29
     39     { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },   // >30
     40     { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?31
     41     { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E },   // @32
     42     { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C },   // A33
     43     { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B34
     44     { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C35
     45     { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D36
     46     { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E37
     47     { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F38
     48     { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G39
     49     { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H40
     50     { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I41
     51     { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J42
     52     { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K43
     53     { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L44
     54     { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M45
     55     { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N46
     56     { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O47
     57     { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P48
     58     { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q49
     59     { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R50
     60     { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },   // S51
     61     { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T52
     62     { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U53
     63     { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V54
     64     { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W55
     65     { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },   // X56
     66     { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y57
     67     { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z58
     68     { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [59
     69     { 0x00, 0x02, 0x04, 0x08, 0x10, 0x20 },   // 60
     70     { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]61
     71     { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^62
     72     { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },   // _63
     73     { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },   // '64
     74     { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },   // a65
     75     { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b66
     76     { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },   // c67
     77     { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F },   // d68
     78     { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },   // e69
     79     { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f70
     80     { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C },   // g71
     81     { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h72
     82     { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i73
     83     { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 },   // j74
     84     { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k75
     85     { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l76
     86     { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m77
     87     { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n78
     88     { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },   // o79
     89     { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 },   // p80
     90     { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC },   // q81
     91     { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r82
     92     { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },   // s83
     93     { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t84
     94     { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u85
     95     { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v86
     96     { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w87
     97     { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },   // x88
     98     { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C },   // y89
     99     { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 },   // z90
    100     { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }    // horiz lines91
    101 };
    102 
    103 const u8 code F8X16[]=
    104 {
    105     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
    106     0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,//!1
    107     0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//"2
    108     0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//#3
    109     0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$4
    110     0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//%5
    111     0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//&6
    112     0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//'7
    113     0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//(8
    114     0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//)9
    115     0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//*10
    116     0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+11
    117     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//,12
    118     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//-13
    119     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//.14
    120     0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,///15
    121     0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//016
    122     0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//117
    123     0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//218
    124     0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//319
    125     0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//420
    126     0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//521
    127     0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//622
    128     0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//723
    129     0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//824
    130     0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//925
    131     0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//:26
    132     0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//;27
    133     0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//<28
    134     0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//=29
    135     0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//>30
    136     0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//?31
    137     0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@32
    138     0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A33
    139     0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B34
    140     0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C35
    141     0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D36
    142     0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E37
    143     0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F38
    144     0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G39
    145     0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H40
    146     0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I41
    147     0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J42
    148     0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K43
    149     0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L44
    150     0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M45
    151     0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N46
    152     0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O47
    153     0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P48
    154     0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q49
    155     0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R50
    156     0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S51
    157     0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T52
    158     0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U53
    159     0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V54
    160     0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W55
    161     0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X56
    162     0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y57
    163     0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z58
    164     0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[59
    165     0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//60
    166     0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//]61
    167     0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^62
    168     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_63
    169     0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//`64
    170     0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a65
    171     0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b66
    172     0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c67
    173     0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d68
    174     0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e69
    175     0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f70
    176     0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g71
    177     0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h72
    178     0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i73
    179     0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j74
    180     0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k75
    181     0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l76
    182     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m77
    183     0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n78
    184     0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o79
    185     0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p80
    186     0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q81
    187     0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r82
    188     0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s83
    189     0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t84
    190     0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u85
    191     0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v86
    192     0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w87
    193     0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x88
    194     0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y89
    195     0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z90
    196     0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{91
    197     0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//|92
    198     0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//}93
    199     0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~94
    200 };
    201 
    202 
    203 
    204 const u8 code F14x16_Idx[] = 
    205 {
    206    "星期一二日六五四三"
    207 };
    208 
    209 //中文字符串编码
    210 const u8 code F14x16[] =  
    211 
    212 {
    213  
    214       0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,0x2A,0x2A,0x2A,0x3E,0x00,0x00,
    215     0x00,0x44,0x42,0x49,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x49,0x41,0x40,//
    216     0x00,0x04,0xFF,0x24,0x24,0x24,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,
    217     0x88,0x48,0x2F,0x09,0x09,0x19,0xAF,0x48,0x30,0x0F,0x02,0x42,0x82,0x7F,//
    218     0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
    219     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//
    220     0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
    221     0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,//
    222     0x00,0x00,0x00,0xFE,0x82,0x82,0x82,0x82,0x82,0x82,0x82,0xFE,0x00,0x00,
    223     0x00,0x00,0x00,0xFF,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0xFF,0x00,0x00,//
    224     0x20,0x20,0x20,0x20,0x20,0x20,0x21,0x22,0x2C,0x20,0x20,0x20,0x20,0x20,
    225     0x00,0x40,0x20,0x10,0x0C,0x03,0x00,0x00,0x00,0x01,0x02,0x04,0x18,0x60,//
    226     0x00,0x02,0x42,0x42,0x42,0xC2,0x7E,0x42,0x42,0x42,0x42,0xC2,0x02,0x02,
    227     0x40,0x40,0x40,0x40,0x78,0x47,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,//
    228     0x00,0xFC,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0xFC,
    229     0x00,0x7F,0x28,0x24,0x23,0x20,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x7F,//
    230     0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x84,0x04,
    231     0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20//
    232 
    233 };
    234 
    235 
    236   void OLED_WriteCmd(u8 Cmd)
    237 {
    238    u8 i=8 ;
    239    OLED_DC=0;
    240    OLED_SCLK=0;
    241    while(i--)
    242   {
    243     if(Cmd&0x80)                        
    244     {
    245        OLED_SDA=1;
    246     }
    247     else
    248     {
    249       OLED_SDA=0;
    250     }
    251      OLED_SCLK= 1;
    252    _nop_();
    253      OLED_SCLK=0;
    254    Cmd<<=1;
    255   }
    256    return;
    257 }
    258 
    259  void OLED_WriteDat(u8 Dat)
    260 {
    261    u8 i=8 ;
    262    OLED_DC=1;
    263    OLED_SCLK=0;
    264    while(i--)
    265   {
    266     if(Dat&0x80)                        
    267     {
    268        OLED_SDA=1;
    269     }
    270     else
    271     {
    272       OLED_SDA=0;
    273     }
    274      OLED_SCLK= 1;
    275    _nop_();
    276      OLED_SCLK=0;
    277    Dat<<=1;
    278   }
    279 
    280     return;
    281 }
    282 
    283  void OLED_FILL(u8 date)
    284  {
    285     u8 Page,Column;
    286    for( Page=0;Page<8;Page++)
    287     {
    288        OLED_WriteCmd(0xb0+Page);
    289        OLED_WriteCmd(0x00);
    290        OLED_WriteCmd(0x10);
    291       for(Column=0;Column<128;Column++)
    292        {
    293           OLED_WriteDat( date );
    294        }
    295     }
    296  }
    297           
    298   //初始化
    299 
    300   void OledInit()
    301 {
    302   
    303     u8 i;
    304 
    305     OLED_SCLK = 1;      
    306     OLED_RST = 0;
    307     
    308     for(i = 0; i < 100; i++)
    309     {
    310         _nop_();            //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕      
    311     }
    312     
    313     OLED_RST = 1;
    314   OLED_WriteCmd(0xae);
    315   OLED_WriteCmd(0x00);
    316   OLED_WriteCmd(0x10);
    317   OLED_WriteCmd(0x40);
    318   OLED_WriteCmd(0x81);
    319   OLED_WriteCmd(0xcf);
    320   OLED_WriteCmd(0xa1);
    321   OLED_WriteCmd(0xc8);
    322   OLED_WriteCmd(0xa6);
    323   OLED_WriteCmd(0xa8);
    324   OLED_WriteCmd(0x3f);
    325   OLED_WriteCmd(0xd3);
    326   OLED_WriteCmd(0x00);
    327   OLED_WriteCmd(0xd5);
    328   OLED_WriteCmd(0x80);
    329   OLED_WriteCmd(0xd9);
    330   OLED_WriteCmd(0xf1);
    331   OLED_WriteCmd(0xda);
    332   OLED_WriteCmd(0x12);
    333   OLED_WriteCmd(0xdb);
    334   OLED_WriteCmd(0x40);
    335   OLED_WriteCmd(0x20);
    336   OLED_WriteCmd(0x02);
    337   OLED_WriteCmd(0x8d);
    338   OLED_WriteCmd(0x14);    
    339   OLED_WriteCmd(0xa4);
    340   OLED_WriteCmd(0xa6);
    341   OLED_WriteCmd(0xaf);
    342   OLED_FILL(0x00);
    343   OLED_SetPos(0,0);
    344 }
    345 
    346  void OLED_SetPos(u8 x,u8 y)
    347  {
    348    OLED_WriteCmd(0xb0 + y);
    349    OLED_WriteCmd(((x & 0xf0)>> 4) | 0x10);
    350    OLED_WriteCmd((x & 0x0f) | 0x01); 
    351  }
    352 
    353  void OLED6x8Showstr(u8 x,u8 y,u8 str[])
    354  {     
    355       
    356      u8 c=0,i=0,j=0;
    357      while(str[j]!='')
    358      {
    359        c=str[j]-32;
    360        if(x>122)
    361        {
    362          x=0;
    363          y++;
    364        }
    365        OLED_SetPos(x,y);
    366        for(i=0;i<6;i++)
    367        {
    368          OLED_WriteDat(F6x8[c][i]);
    369        }
    370        x+=6;
    371        j++;
    372      }
    373     return;
    374  }
    375    void OLED_6x8C(u8 x,u8 y,u8 ascii)
    376  {
    377       
    378      u8 c=0,i=0,j=0;
    379      
    380        c=ascii-32;
    381        if(x>122)
    382        {
    383          x=0;
    384          y++;
    385        }
    386        OLED_SetPos(x,y);
    387        for(i=0;i<6;i++)
    388        {
    389          OLED_WriteDat(F6x8[c][i]);
    390        }
    391        x+=6;
    392        j++;
    393     return;
    394  }
    395    void OLED_8x16C(u8 x,u8 y, u8 ascii)
    396  {
    397      u8 i,j=0,ucDataTmp;
    398 
    399      y <<= 1;
    400   
    401         ucDataTmp = ascii - 32;
    402 
    403         if(x > 120)
    404         {
    405             x = 0;
    406             y += 2;
    407         }
    408 
    409         OLED_SetPos(x, y);   
    410         
    411         for(i = 0; i < 8; i++) 
    412         {
    413             OLED_WriteDat(F8X16[(ucDataTmp << 4) + i]);
    414         }
    415         
    416         OLED_SetPos(x, y + 1);   
    417         
    418         for(i = 0; i < 8; i++) 
    419         {
    420             OLED_WriteDat(F8X16[(ucDataTmp << 4) + i + 8]);
    421         }
    422         x += 8;
    423         
    424     
    425 
    426     return;
    427 }  
    428 
    429     
    430 
    431 
    432  void OLED_P8x16Str(u8 x, u8 y, u8 ucDataStr[])
    433 {
    434     u8 i, j, ucDataTmp;
    435 
    436     y <<= 1;
    437     
    438     for (j = 0; ucDataStr[j] != ''; j++)
    439     {    
    440         ucDataTmp = ucDataStr[j] - 32;
    441 
    442         if(x > 120)
    443         {
    444             x = 0;
    445             y += 2;
    446         }
    447 
    448         OLED_SetPos(x, y);   
    449         
    450         for(i = 0; i < 8; i++) 
    451         {
    452             OLED_WriteDat(F8X16[(ucDataTmp << 4) + i]);
    453         }
    454         
    455         OLED_SetPos(x, y + 1);   
    456         
    457         for(i = 0; i < 8; i++) 
    458         {
    459             OLED_WriteDat(F8X16[(ucDataTmp << 4) + i + 8]);
    460         }
    461         x += 8;
    462         
    463     }
    464 
    465     return;
    466 }
    467 
    468 
    469  void LED_P14x16Str(u8 x, u8 y, u8 ucDataStr[])
    470 {
    471    u8 i, j;
    472     u16 usAddress; 
    473     
    474     y <<= 1;
    475     
    476     j = 0;      
    477     while(ucDataStr[j] != '')
    478     {    
    479          i = 0;
    480         usAddress = 1;
    481         while(F14x16_Idx[i] > 127)
    482         {
    483             if(F14x16_Idx[i]==ucDataStr[j])
    484             {
    485                 if(F14x16_Idx[i + 1]==ucDataStr[j + 1])
    486                 {
    487                     usAddress = i * 14;
    488                     break;
    489                 }
    490             }
    491 
    492             i += 2;            
    493         }
    494         
    495         if(x > 114)
    496         {
    497             x = 0;
    498             y += 2;
    499         }
    500         
    501         if(usAddress != 1)// 显示汉字                   
    502         {
    503             OLED_SetPos(x, y);
    504             for(i = 0; i < 14; i++)               
    505             {
    506                 OLED_WriteDat(F14x16[usAddress]);   
    507                 usAddress++;
    508             }
    509             
    510             OLED_SetPos(x,y + 1); 
    511             for(i = 0;i < 14;i++)          
    512             {
    513                 OLED_WriteDat(F14x16[usAddress]);
    514                 usAddress++;
    515             }
    516             j+=2;                          
    517         }
    518     else
    519     {
    520        OLED_SetPos(x,y);
    521        for(i=0;i<14;i++)
    522        {
    523          OLED_WriteDat(0);
    524        }
    525        OLED_SetPos(x,y+1);
    526        for(i=0;i<14;i++)
    527        {
    528           OLED_WriteDat(0);
    529        }
    530           j++;
    531     }
    532       x+=14;
    533    }
    534   return;            
    535  }
    536 
    537 /*****************************************************************************
    538  函 数 名  : LED_PXx16MixStr
    539  功能描述  : 输出14x16汉字和字符混合字符串 (字符串表格中需含有此字)
    540  输入参数  : UCHAR8 ucIdxX       为显示的横坐标0~114
    541              UCHAR8 ucIdxY       为页范围0~3
    542              UCHAR8 ucDataStr[]  要显示的中文字符串
    543  输出参数  : NONE
    544  返 回 值  : NONE
    545 *****************************************************************************/
    546 void OLED_P14x16Str(u8 x, u8 y, u8 ucDataStr[])
    547 {
    548     u8 ucTmpStr[3];
    549     u8 i = 0;    
    550     
    551     while(ucDataStr[i] != '')
    552     {
    553         if(ucDataStr[i] > 127)
    554         {
    555             ucTmpStr[0] = ucDataStr[i];
    556             ucTmpStr[1] = ucDataStr[i + 1];
    557             ucTmpStr[2] = '';          //汉字为两个字节
    558             LED_P14x16Str(x, y, ucTmpStr);  //显示汉字
    559             x += 14;
    560             i += 2;
    561         }
    562         else
    563         {
    564             ucTmpStr[0] = ucDataStr[i];    
    565             ucTmpStr[1] = '';          //字母占一个字节
    566             OLED_P8x16Str(x, y, ucTmpStr);  //显示字母
    567             x += 8;
    568             i += 1;
    569         }
    570     }
    571 
    572     return;
    573 } 
    574 /*****************************************************************************
    575  函 数 名  : LED_PrintImage
    576  功能描述  : 将图像显示出来
    577  输入参数  : UCHAR8 *pucTable     二维图像数组的地址
    578              USHORT16 usRowNum    二维图像的行数1~64
    579              USHORT16 usColumnNum 二维图像的列数1~128
    580  输出参数  : none
    581  返 回 值  : none
    582 *****************************************************************************/
    583 void LED_PrintImage(u8 *pucTable, u16 usRowNum, u16 usColumnNum)
    584 {
    585     u8 ucData;
    586     u16 i,j,k,m,n;
    587     u16 usRowTmp;
    588 
    589     m = usRowNum >> 3;   //计算图片行数以8位为一组完整的组数
    590     n = usRowNum % 8;    //计算分完组后剩下的行数
    591     
    592     for(i = 0; i < m; i++) //完整组行扫描
    593     {
    594         OLED_SetPos(0,(u8)i);
    595         usRowTmp = i << 3;    //计算当前所在行的下标                  
    596         for(j = 0; j < usColumnNum; j++) //列扫描        
    597         {
    598             ucData = 0;
    599             for(k = 0; k < 8; k++) //在i组中对这8行扫描
    600             {
    601                 ucData = ucData >> 1;
    602                 if((pucTable + (usRowTmp + k) * usColumnNum)[j] == 128)
    603                 {
    604                     ucData = ucData | 0x80;
    605                 }
    606                 
    607             }
    608             OLED_WriteDat(ucData);
    609         }
    610     }
    611     
    612     OLED_SetPos(0,(u8)i); //设置剩下的行显示的起始坐标
    613     usRowTmp = i << 3;       //计算当前所在行的下标                  
    614     for(j = 0; j < usColumnNum; j++) //列扫描        
    615     {
    616         ucData = 0;
    617         for(k = 0; k < n; k++) //对剩下的行扫描
    618         {
    619             ucData = ucData >> 1;
    620             if((pucTable + (usRowTmp + k) * usColumnNum)[j] == 128)
    621             {
    622                 ucData = ucData | 0x80;
    623             }
    624             
    625         }
    626         ucData = ucData >> (8 - n);
    627         OLED_WriteDat(ucData);
    628     }
    629 
    630     return;
    631 }
    632 
    633     
    oled.c

    DS1302时钟芯片 

      1   1 #define _DS1302_C
      2   2  #include"config.h"
      3   3  #include"ds1302.h"
      4   4 
      5   5  u8 code READ_RTC_ADDR[7] = {0x8d,0x89,0x87,0x8b,0x85,0x83,0x81};
      6   6  u8 code WRITE_RTC_ADDR[7] = {0x8c,0x88,0x86,0x8a,0x84,0x82,0x80};
      7   7 
      8   8 //---DS1302时钟初始化2016年5月7日星期六12点00分00秒。---//
      9   9 //---存储顺序是秒分时日月周年,存储格式是用BCD码---//
     10  10 u8 time[7] = {0x19,0x04,0x12,0x05,0x21,0x43,0};
     11  11 
     12  12 /*******************************************************************************
     13  13 * 函 数 名         : Ds1302Write
     14  14 * 函数功能           : 向DS1302命令(地址+数据)
     15  15 * 输    入         : addr,dat
     16  16 * 输    出         : 无
     17  17 *******************************************************************************/
     18  18 
     19  19 void Ds1302Write(u8 addr, u8 dat)
     20  20 {
     21  21     u8 n;
     22  22     DS_RST = 0;
     23  23     _nop_();
     24  24 
     25  25     DS_SCLK = 0;//先将SCLK置低电平。
     26  26     _nop_();
     27  27     DS_RST = 1; //然后将RST(CE)置高电平。
     28  28     _nop_();
     29  29 
     30  30     for (n=0; n<8; n++)//开始传送八位地址命令
     31  31     {
     32  32         DS_IO = addr & 0x01;//数据从低位开始传送
     33  33         addr >>= 1;
     34  34         DS_SCLK = 1;//数据在上升沿时,DS1302读取数据
     35  35         _nop_();
     36  36         DS_SCLK = 0;
     37  37         _nop_();
     38  38     }
     39  39     for (n=0; n<8; n++)//写入8位数据
     40  40     {
     41  41         DS_IO = dat & 0x01;
     42  42         dat >>= 1;
     43  43         DS_SCLK = 1;//数据在上升沿时,DS1302读取数据
     44  44         _nop_();
     45  45         DS_SCLK = 0;
     46  46         _nop_();    
     47  47     }    
     48  48          
     49  49     DS_RST = 0;//传送数据结束
     50  50     _nop_();
     51  51 }
     52  52 
     53  53 /*******************************************************************************
     54  54 * 函 数 名         : Ds1302Read
     55  55 * 函数功能           : 读取一个地址的数据
     56  56 * 输    入         : addr
     57  57 * 输    出         : dat
     58  58 *******************************************************************************/
     59  59 
     60  60 u8 Ds1302Read(u8 addr)
     61  61 {
     62  62     u8 n,dat,dat1;
     63  63     DS_RST = 0;
     64  64     _nop_();
     65  65 
     66  66     DS_SCLK = 0;//先将SCLK置低电平。
     67  67     _nop_();
     68  68     DS_RST = 1;//然后将RST(CE)置高电平。
     69  69     _nop_();
     70  70 
     71  71     for(n=0; n<8; n++)//开始传送八位地址命令
     72  72     {
     73  73         DS_IO = addr & 0x01;//数据从低位开始传送
     74  74         addr >>= 1;
     75  75         DS_SCLK = 1;//数据在上升沿时,DS1302读取数据
     76  76         _nop_();
     77  77         DS_SCLK = 0;//DS1302下降沿时,放置数据
     78  78         _nop_();
     79  79     }
     80  80     _nop_();
     81  81     for(n=0; n<8; n++)//读取8位数据
     82  82     {
     83  83         dat1 = DS_IO;//从最低位开始接收
     84  84         dat = (dat>>1) | (dat1<<7);
     85  85         DS_SCLK = 1;
     86  86         _nop_();
     87  87         DS_SCLK = 0;//DS1302下降沿时,放置数据
     88  88         _nop_();
     89  89     }
     90  90 
     91  91     DS_RST = 0;
     92  92     _nop_();    //以下为DS1302复位的稳定时间,必须的。
     93  93     DS_SCLK = 1;
     94  94     _nop_();
     95  95     DS_IO = 0;
     96  96     _nop_();
     97  97     DS_IO = 1;
     98  98     _nop_();
     99  99     return dat;    
    100 100 }
    101 101 
    102 102 /*******************************************************************************
    103 103 * 函 数 名         : Ds1302Init
    104 104 * 函数功能           : 初始化DS1302.
    105 105 * 输    入         : 无
    106 106 * 输    出         : 无
    107 107 *******************************************************************************/
    108 108 
    109 109    
    110 110 void Ds1302Init()
    111 111 {
    112 112     u8 n;
    113 113     Ds1302Write(0x8E,0X00);         //禁止写保护,就是关闭写保护功能
    114 114     for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
    115 115     {
    116 116         Ds1302Write(WRITE_RTC_ADDR[n],time[n]);    
    117 117     }
    118 118     Ds1302Write(0x8E,0x80);         //打开写保护功能
    119 119 }
    120 120 
    121 121 /*******************************************************************************
    122 122 * 函 数 名         : Ds1302ReadTime
    123 123 * 函数功能           : 读取时钟信息
    124 124 * 输    入         : 无
    125 125 * 输    出         : 无
    126 126 *******************************************************************************/
    127 127 
    128 128 void Ds1302ReadTime()
    129 129 {
    130 130     u8 n;
    131 131     for (n=0; n<7; n++)//读取7个字节的时钟信号:分秒时日月周年
    132 132     {
    133 133         time[n] = Ds1302Read(READ_RTC_ADDR[n]);
    134 134     }
    135 135         
    136 136 }
    DS1302.c

    红外遥控代码

      1 #define _INFRARED_C
      2 #include"config.h"
      3 #include"Infrared.h"
      4 #include"Time.h"
      5 
      6 
      7 const u8 code IrCodeMap[][2] = {  //红外键码到标准PC键码的映射表
      8     {0x45,0x00}, {0x46,0x00}, {0x47,0x1B}, //开关->无  Mode->无   静音->ESC
      9     {0x44,0x00}, {0x40,0x25}, {0x43,0x27}, //播放->无  后退->向左 前进->向右
     10     {0x07,0x00}, {0x15,0x28}, {0x09,0x26}, // EQ->无   减号->向下 加号->向上
     11     {0x16, '0'}, {0x19,0x1B}, {0x0D,0x0D}, //'0'->'0'  箭头->ESC  U/SD->回车
     12     {0x0C, '1'}, {0x18, '2'}, {0x5E, '3'}, //'1'->'1'  '2'->'2'   '3'->'3'
     13     {0x08, '4'}, {0x1C, '5'}, {0x5A, '6'}, //'4'->'4'  '5'->'5'   '6'->'6'
     14     {0x42, '7'}, {0x52, '8'}, {0x4A, '9'}, //'7'->'7'  '6'->'8'   '9'->'9'
     15      };
     16 bit irflag=0;
     17 u8 ircode[4];
     18 
     19 void InfraredDriver()
     20 {
     21     u8 i;
     22     
     23     if (irflag)
     24     {
     25         irflag = 0;
     26         for (i=0; i<sizeof(IrCodeMap)/sizeof(IrCodeMap[0]); i++) //遍历映射表
     27         {
     28             if (ircode[2] == IrCodeMap[i][0])  //在表中找到当前接收的键码后,
     29             {                                  //用对应的映射码执行函数调度,
     30                 KeyAction(IrCodeMap[i][1]);    //直接调用按键动作函数即可。
     31                 break;
     32             }
     33         }
     34     }
     35 }
     36   
     37 
     38 
     39 void InitInfrared()
     40 {   
     41     IRIN=1;
     42     TMOD&=0x0F;
     43     TMOD|=0x10;
     44     TR1=0;
     45     ET1=0;
     46     IT0=1;
     47     EX0=1;
     48 }
     49 
     50  u16 GetHighTime()
     51  {
     52    TH1=0;
     53    TL1=0;
     54    TR1=1;
     55    while(IRIN)
     56    {
     57      if(TH1>=0x40)
     58      {
     59        break;
     60      }
     61    }
     62      TR1=0;
     63     return (TH1*256+TL1);
     64  }
     65 
     66  u16 GetLowTime()
     67 {
     68    TH1=0;
     69    TL1=0;
     70    TR1=1;
     71    while(!IRIN)
     72    {  if(TH1>=0x40)
     73      {
     74        break;
     75      }
     76    }
     77      TR1=0;
     78      return (TH1*256+TL1);
     79  }
     80 
     81 
     82  void outinterrupt() interrupt 0
     83 {
     84    u8 i,j;
     85    u8 byt;
     86    u16 time;
     87 
     88   time = GetLowTime();
     89   if((time<8500)||(time>9500))
     90   {
     91     IE0=0;
     92     return;
     93   }
     94 
     95   time=GetHighTime();
     96   if((time<=4000)||(time>=5000))
     97   {     IE0=0;
     98     return;
     99   }
    100 
    101   for(i=0;i<4;i++)
    102  {
    103    for(j=0;j<8;j++)
    104    {
    105      time=GetLowTime();
    106      if((time<340)||(time>780))
    107      {
    108         IE0=0;
    109         return;
    110      }  
    111        time=GetHighTime();
    112        if((time>=340)&&(time<=780))
    113         {
    114          byt>>=1;
    115         }  
    116        else if((time>=1460)&&(time<=1900))
    117        {
    118          byt>>=1;
    119          byt|=0x80;
    120        }
    121        else 
    122       {
    123         IE0=0;
    124         return;
    125       }
    126       }
    127     ircode[i]=byt;
    128  }
    129    irflag=1;
    130    IE0=0;
    131  }       
    132   
    Infrared.c

    显示层代码

      1 #define _TIME_C
      2 #include"config.h"
      3 #include"OLED.h"
      4 #include"DS1302.h"
      5 #include"main.h"
      6 #include"Time.h"
      7 #include"LedBuzzer.h"
      8 
      9 u8 xingqi[7][6]={"星期1","星期2","星期3","星期4",
     10              "星期5","星期6","星期7"};
     11 
     12 bit staMute = 0;
     13 
     14 u8 AlarmHour = 0x07;
     15 u8 AlarmMin = 0x30;
     16 
     17 u8 SetIndex = 0;
     18 
     19 u8 pdata SetAlarmHour;
     20 u8 pdata SetAlarmHour;    //闹钟小时数设置缓冲
     21 u8 pdata SetAlarmMin;     //闹钟分钟数设置缓冲
     22 u8 pdata SetTime[7];
     23               
     24 u8 str1[11], str3[9], *str2;
     25 
     26 
     27  void RefreshTime()
     28  {
     29                 Ds1302ReadTime();        
     30                 str3[0]=(time[4] >> 4) + '0'; 
     31                 str3[1]=(time[4]&0x0f)+'0';
     32                 str3[2]=':';
     33                 str3[3]=(time[5]>>4)+'0';
     34                 str3[4]=(time[5]&0x0f)+'0';
     35                 str3[5]=':';
     36                 str3[6]=(time[6]>>4)+'0';
     37                 str3[7]= (time[6]&0x0f)+'0';
     38                 str3[8]='';              
     39                 str2 =xingqi[(time[3]&0x0f)-1];   
     40                 OLED_P8x16Str(30,2,(u8*)str3);
     41                 OLED_P14x16Str(88,0,(u8*)str2);   
     42  }    
     43 
     44 void RefreshDate(u8 ops)
     45  {
     46       u8 backup=0;
     47 
     48     if((backup!=time[2])||ops!=0)
     49     {
     50                    str1[0] = '2';  
     51                 str1[1] = '0';
     52                 str1[2] = (time[0] >> 4) + '0';  
     53                 str1[3] = (time[0]&0x0f) + '0';  
     54                 str1[4] = '/';  
     55                 str1[5] = (time[1] >> 4) + '0';  
     56                 str1[6] = (time[1]&0x0F) + '0';
     57                 str1[7] = '/';
     58                 str1[8] = (time[2] >> 4) + '0';  
     59                 str1[9] = (time[2]&0x0F) + '0';
     60                 str1[10] = '';
     61             OLED_P8x16Str(0, 0, (u8*)str1);
     62             backup=time[2];
     63   }
     64 }
     65 
     66  void RefreshAlarm()
     67 {
     68   u8 pdata str[6];
     69 
     70     OLED6x8Showstr(0,7,"Alarm ");
     71     str[0] = (AlarmHour >> 4) + '0';   //闹钟小时数
     72     str[1] = (AlarmHour & 0x0F) + '0';
     73     str[2] = ':';                      //分隔符
     74     str[3] = (AlarmMin >> 4) + '0';    //闹钟分钟数
     75     str[4] = (AlarmMin & 0x0F) + '0';
     76     str[5] = ''; 
     77      OLED6x8Showstr(40,7,(u8*)str);
     78  }
     79  
     80  
     81    void AlarmMonitor()
     82  {
     83     if((time[4]==AlarmHour)&&(time[5]==AlarmMin))
     84     {
     85       if (!staMute)  //检查是否静音
     86             staBuzzer = ~staBuzzer;  //实现蜂鸣器断续鸣叫
     87         else
     88             staBuzzer = 0;
     89     }
     90     else
     91     {
     92         staMute = 0;
     93         staBuzzer = 0;
     94     }
     95  }                 
     96 
     97  void ShowSetTime()
     98   {
     99     u8 pdata   str4[6];
    100     str1[0]  = (SetTime[0]>>4) + '0';  //2位数年份
    101     str1[1]  = (SetTime[0] & 0x0F) + '0';
    102     str1[2]  = '/';
    103     str1[3]  = (SetTime[1] >> 4) + '0';   //月份
    104     str1[4]  = (SetTime[1] & 0x0F) + '0';
    105     str1[5]  = '/';
    106     str1[6]  = (SetTime[2] >> 4) + '0';   //日期
    107     str1[7]  = (SetTime[2] & 0x0F) + '0';
    108     str1[8] =  '';
    109     str2 =xingqi[(SetTime[3]&0x0f)-1]; //星期  
    110    
    111     str4[0] = (SetTime[4] >> 4) + '0';  //小时
    112     str4[1] = (SetTime[4] & 0x0F) + '0';
    113     str4[2] = ':';
    114     str4[3] = (SetTime[5] >> 4) + '0';   //分钟
    115     str4[4] = (SetTime[5] & 0x0F) + '0';
    116     str4[5] = '';
    117 
    118     OLED_P8x16Str(0,0,"Set Data Time");
    119     OLED_P8x16Str(0, 2, (u8*)str1);
    120        OLED_P14x16Str(88,2,(u8*)str2);
    121     OLED6x8Showstr(30,7,(u8*)str4);
    122  }
    123      
    124 void ShowSetAlarm()
    125  {  u8 pdata str[8];
    126       str[0] = (SetAlarmHour >> 4) + '0';   //小时
    127     str[1] = (SetAlarmHour & 0x0F) + '0';
    128     str[2] = ':';
    129     str[3] = (SetAlarmMin >> 4) + '0';    //分钟
    130     str[4] = (SetAlarmMin & 0x0F) + '0';
    131     str[5] = '';
    132    OLED_P8x16Str(0,0,"Set Alarm");
    133    OLED_P8x16Str(45,2,(u8*)str);
    134   }
    135 
    136   void CancelCurSet()
    137  {
    138        staSystem = E_NORMAL;
    139     OledInit();
    140     RefreshTime();   //刷新当前时间
    141     RefreshDate(1);  //立即刷新日期显示
    142     RefreshAlarm();
    143 }
    144 
    145 void SetRealTime()
    146 {
    147   u8 n;
    148     Ds1302Write(0x8E,0X00);         //禁止写保护,就是关闭写保护功能
    149     for (n=0; n<7; n++)//写入7个字节的时钟信号:分秒时日月周年
    150     {
    151         Ds1302Write(WRITE_RTC_ADDR[n],SetTime[n]);    
    152     }
    153     Ds1302Write(0x8E,0x80);         //打开写保护功能
    154 }
    155 
    156  void SetRightShift()
    157 {
    158     if (staSystem == E_SET_TIME)
    159     {
    160         switch (SetIndex)
    161         {
    162             case 0: SetIndex=1;   break;
    163             case 1: SetIndex=2;   break;
    164             case 2: SetIndex=3;   break;
    165             case 3: SetIndex=4;   break;
    166             case 4: SetIndex=5;   break;
    167             case 5: SetIndex=6;   break;
    168             case 6: SetIndex=7;   break;
    169             case 7: SetIndex=8;   break;
    170             case 8: SetIndex=9;   break;
    171             case 9: SetIndex=10;  break;
    172             default: SetIndex=0;  break;
    173         }
    174     }
    175     else if (staSystem == E_SET_ALARM)
    176     {
    177         switch (SetIndex)
    178         {
    179             case 0: SetIndex=1;   break;
    180             case 1: SetIndex=2;   break;
    181             case 2: SetIndex=3;   break;
    182             default: SetIndex=0;  break;
    183         }
    184     }
    185 } 
    186 
    187 void SetLeftShift()
    188 {
    189     if (staSystem == E_SET_TIME)
    190     {
    191         switch (SetIndex)
    192         {
    193             case 0: SetIndex=10;  break;
    194             case 1: SetIndex=0;   break;
    195             case 2: SetIndex=1;   break;
    196             case 3: SetIndex=2;   break;
    197             case 4: SetIndex=3;   break;
    198             case 5: SetIndex=4;   break;
    199             case 6: SetIndex=5;   break;
    200             case 7: SetIndex=6;   break;
    201             case 8: SetIndex=7;   break;
    202             case 9: SetIndex=8;   break;
    203             default: SetIndex=9;  break;
    204         }
    205     }
    206 
    207     else if (staSystem == E_SET_ALARM)
    208     {
    209         switch (SetIndex)
    210         {
    211             case 0: SetIndex=3;   break;
    212             case 1: SetIndex=0;   break;
    213             case 2: SetIndex=1;   break;
    214             default: SetIndex=2;  break;
    215         }
    216     }
    217 } 
    218 void InputSetNumber(u8 ascii)
    219 {
    220     u8 num;
    221     
    222     num = ascii - '0';
    223     if (num <= 9)  //只响应0~9的数字
    224     {
    225         if (staSystem == E_SET_TIME)
    226         {
    227             switch (SetIndex)
    228             {
    229                 case 0: SetTime[0] = (SetTime[0]&0x0F)|(num<<4);
    230                         OLED_8x16C(0, 2 ,ascii);  break;      //年份高位数字
    231                 case 1: SetTime[0] = (SetTime[0]&0xF0)|(num);
    232                         OLED_8x16C(8, 2, ascii);  break;      //年份低位数字
    233                 case 2: SetTime[1] = (SetTime[1]&0x0F)|(num<<4);
    234                         OLED_8x16C(24, 2, ascii);  break;      //月份高位数字
    235                 case 3: SetTime[1] = (SetTime[1]&0xF0)|(num);
    236                         OLED_8x16C(32, 2, ascii);  break;      //月份低位数字
    237                 case 4: SetTime[2] = (SetTime[2]&0x0F)|(num<<4);
    238                         OLED_8x16C(48, 2, ascii);  break;      //日期高位数字
    239                 case 5: SetTime[2] = (SetTime[2]&0xF0)|(num);
    240                         OLED_8x16C(56, 2, ascii);  break;      //日期低位数字
    241                 case 6: SetTime[3] = (SetTime[3]&0xF0)|(num);
    242                         OLED_P14x16Str(88,2,(u8*)(xingqi[(SetTime[3]&0x0f)-1]));  break;      //星期数字
    243                 case 7: SetTime[4] = (SetTime[4]&0x0F)|(num<<4);
    244                         OLED_6x8C(30,7, ascii);  break;      //小时高位数字
    245                 case 8: SetTime[4] = (SetTime[4]&0xF0)|(num);
    246                         OLED_6x8C(36,7, ascii);  break;      //小时低位数字
    247                 case 9: SetTime[5]= (SetTime[5]&0x0F)|(num<<4);
    248                         OLED_6x8C(48,7, ascii);  break;      //分钟高位数字
    249                 default:SetTime[5] = (SetTime[5]&0xF0)|(num);
    250                         OLED_6x8C(54,7, ascii);  break;      //分钟低位数字
    251             }
    252             SetRightShift();  //完成该位设置后自动右移
    253         }
    254         else if (staSystem == E_SET_ALARM)
    255         {
    256             switch (SetIndex)
    257             {
    258                 case 0: SetAlarmHour = (SetAlarmHour&0x0F) | (num<<4);
    259                         OLED_8x16C(45,2, ascii); break;      //小时高位数字
    260                 case 1: SetAlarmHour = (SetAlarmHour&0xF0) | (num);
    261                         OLED_8x16C(53,2, ascii); break;      //小时低位数字
    262                 case 2: SetAlarmMin = (SetAlarmMin&0x0F) | (num<<4);
    263                         OLED_8x16C(69,2, ascii); break;      //分钟高位数字
    264                 default:SetAlarmMin = (SetAlarmMin&0xF0) | (num);
    265                         OLED_8x16C(77,2, ascii); break;      //分钟低位数字
    266             }
    267             SetRightShift();  //完成该位设置后自动右移
    268         }
    269     }
    270 }
    271 
    272 
    273 void SwitchSystemSta()
    274 {      
    275     if (staSystem == E_NORMAL)  
    276     {
    277         staSystem = E_SET_TIME;
    278         SetTime[0]  = time[0];  //当前时间拷贝到时间设置缓冲区中
    279         SetTime[1]  = time[1];
    280         SetTime[2]  = time[2];
    281         SetTime[3]  = time[3];
    282         SetTime[4]  = time[4];
    283         SetTime[5]  = time[5];
    284         SetTime[6]  = time[6];
    285         OledInit();  //液晶清屏
    286         ShowSetTime(); //显示设置时间
    287         SetIndex = 255;    
    288         SetRightShift();
    289           
    290     }
    291     else if (staSystem == E_SET_TIME)  //时间设置切换到闹钟设置
    292     {     
    293         staSystem = E_SET_ALARM;
    294         SetTime[6] = 0;          //秒清零,即当设置时间后从0秒开始走时 
    295         SetRealTime();    //设定时间写入实时时钟
    296         SetAlarmHour = AlarmHour; //当前闹钟值拷贝到设置缓冲区
    297         SetAlarmMin  = AlarmMin;
    298         OledInit();  //液晶清屏
    299         ShowSetAlarm();    //显示设置闹钟
    300         SetIndex = 255;    //与接下来的右移一起将光标设在最左边的位置上
    301         SetRightShift();
    302     }
    303     else  //闹钟设置切换会正常运行
    304     {
    305         staSystem = E_NORMAL;
    306         AlarmHour = SetAlarmHour;  //设定的闹钟值写入闹钟时间
    307         AlarmMin  = SetAlarmMin;
    308         OledInit();  //液晶清屏
    309         RefreshTime();   //刷新当前时间
    310         RefreshDate(1);  //立即刷新日期显示
    311         RefreshAlarm();  //闹钟设定值显示
    312     }
    313 }
    314 
    315 /* 按键动作函数,根据键码执行相应的操作,keycode-按键键码 */
    316 void KeyAction(u8 keycode)
    317 {
    318     if  ((keycode>='0') && (keycode<='9'))  //数字键输入当前位设定值
    319     {
    320         InputSetNumber(keycode);
    321     }
    322     else if (keycode == 0x25)  //向左键,向左切换设置位
    323     {
    324         SetLeftShift();
    325     }
    326     else if (keycode == 0x27)  //向右键,向右切换设置位
    327     {
    328         SetRightShift();
    329     }
    330     else if (keycode == 0x0D)  //回车键,切换运行状态/保存设置
    331     {
    332         SwitchSystemSta();
    333     }
    334     else if (keycode == 0x1B)  //Esc键,静音/取消当前设置
    335     {
    336         if (staSystem == E_NORMAL) //处于正常运行状态时闹铃静音
    337         {
    338             staMute = 1;
    339         }
    340         else                       //处于设置状态时退出设置
    341         {
    342             CancelCurSet();
    343         }
    344     }
    345 }     
    Time.c

    主函数代码

     1 #define _MAIN_C
     2 #include"config.h"         //此文件中定义了单片机的一些特殊功能寄存器
     3 #include"DS18B20.h"    
     4 #include"OLED.h"
     5 #include"main.h"
     6 #include"DS1302.h"
     7 #include"LedBuzzer.h"
     8 #include"Infrared.h"
     9 #include"Time.h"
    10 
    11 
    12 //bit flag2s = 0;     //2s定时标志位
    13 bit flag200ms = 0;  //200ms定时标志
    14 u8 T0RH = 0;    //T0重载值的高字节
    15 u8 T0RL = 0;    //T0重载值的低字节
    16 enum eStaSystem staSystem = E_NORMAL;
    17  void main()
    18 {
    19     EA = 1;
    20     ConfigTimer0(1);         
    21     InitLed();           
    22     Ds1302Init();
    23     InitInfrared();
    24     OledInit();
    25 
    26     RefreshTime();
    27     RefreshDate(1);
    28     RefreshAlarm();
    29     while(1)
    30   {
    31       InfraredDriver();
    32     if(staSystem==E_NORMAL)
    33     {
    34       if(flag200ms)
    35       {
    36          flag200ms=0;
    37          RefreshTime();
    38          AlarmMonitor();
    39            RefreshDate(0);
    40       }
    41     }
    42    }             
    43 }
    44  
    45 void ConfigTimer0(u16 ms)
    46 {
    47     int32 tmp;
    48     
    49     tmp = (SYS_MCLK*ms)/1000; //计算所需的计数值
    50     tmp = 65536 - tmp;        //计算定时器重载值
    51     tmp = tmp + 33;           //补偿中断响应延时造成的误差   
    52     T0RH = (u8)(tmp>>8);   //定时器重载值拆分为高低字节
    53     T0RL = (u8)tmp;
    54     TMOD &= 0xF0;   //清零T0的控制位
    55     TMOD |= 0x01;   //配置T0为模式1
    56     TH0 = T0RH;     //加载T0重载值
    57     TL0 = T0RL;
    58     ET0 = 1;        //使能T0中断
    59     TR0 = 1;        //启动T0
    60 }
    61 /* T0中断服务函数,实现系统定时和按键扫描 */
    62 
    63 void InterruptTimer0() interrupt 1
    64 {
    65     
    66     static u8 tmr200ms = 0;
    67     
    68     TH0 = T0RH;  //重新加载重载值
    69     TL0 = T0RL;
    70     tmr200ms++;  //定时200ms
    71     if (tmr200ms >= 200)
    72     {
    73         tmr200ms = 0;
    74         flag200ms = 1;
    75     }
    76      
    77 }
    main.c

    蜂鸣器代码

     1 #define _LED_BUZZER_C
     2 #include"config.h"
     3 #include"LedBuzzer.h"
     4 
     5  bit staBuzzer=0;
     6 
     7 void InitLed()
     8 {  
     9   T2CON=0x00;
    10   RCAP2H=((65536-SYS_MCLK/1500)>>8);
    11   RCAP2L=(65536-SYS_MCLK/1500);
    12   TH2=RCAP2H;
    13   TL2=RCAP2L;
    14   ET2=1;
    15   PT2=1;
    16   TR2=1;
    17 }
    18 
    19 
    20 void interruptTime2() interrupt 5
    21 {
    22    
    23    TF2=0;
    24    
    25 
    26  if(staBuzzer == 1)
    27    BUZZER=~BUZZER;
    28  else
    29    BUZZER=1;
    30 
    31 }     
    Buff.c

    除引脚申明头文件,其他头文件就不附上啦

                                         

                  U/SD转换模式,RPT回到最初显示模式,|<<,>>|设置位向左向右移动,静音符号关闭闹铃,0~9输出时间设置数字。

                    欢迎大家指正(☆▽☆)

  • 相关阅读:
    orioledb pg 存储引擎
    nginx ngx_http_addition_module 模块openresty content_by_lua 不能生效的原因
    Windows下Erlang和RabbitMQ下载安装教程
    [建议收藏]缓存雪崩的处理办法
    【精选】Mysql BTree和B+Tree的结构?
    RabbitMQ用户和virtual hosts的添加以及授权
    程序员增加收入的几种方法
    基于redis的keys、scan删除ttl为1的key
    全面了解Nginx主要应用场景
    很佩服的一个Google佬,离职了。。
  • 原文地址:https://www.cnblogs.com/blogs-192-168-9-222/p/Elliesloveblog_210.html
Copyright © 2020-2023  润新知