自己制作的STC的自动冷启动下载器,虽然是51单片机这种老掉牙的东西,业余的电子爱好,折腾一下,以前买的那种模块,识别率不高,经常识别不到。面板用OLED作显示,8838铝合金壳体(T12电烙铁套装壳子),TYPEC接口,手机数据线即可。
主要电路原理:CH340G负责USB转串口,TLP109光耦把串口数据隔离,并把数据分为2路,一路给下载芯片,另一路给主控芯片,4个继电器负责输出供电切换和冷启动断电,目标下载板只有小电流时可以直接借用USB5V,如果有大电容可以切换到外置DC口供电,这样不影响USB。主控芯片使用12C5A60S2,对串口数据进行解析,面板显示和开关以及继电器这些。板上有主控芯片的冷启动开关,不需要借助其它的下载模块,直接本机就能自己给自己刷程序,只是需要手动冷启动。OLED显示板使用1.3寸SSD1306芯片,查手册时才发现这屏虽然可以接受5V的TTL电平,但是会影响寿命,所以增加了TXS0102电平转换芯片。程序的核心原理是ISP软件下载时会主动打开串口,主控芯片只需要在串口打开时扫描是否有0X7F下载数据流,识别到连续的一定数量的7F就进行冷启动断电1次,重新上电以后不再进行7F扫描,直到程序下载完成串口断开连接才会回到初始状态,串口波特率支持2400-4800-9600,ISP下载软件默认最低是2400,扫描7F时进行波特率切换,几个波特率轮番查询都没有7F,那就识别为单纯的串口连接,不进行冷启动断电,不能一直查询,避免串口调试中的7F触发冷启动,更高的波特率感觉不太必要,程序中也没写更多
hex文件.rar
(5.31 KB, 下载次数: 0)
原理图 PCB.rar
(6.51 MB, 下载次数: 0)
- #include <STC12C5A60S2.H>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit SDA=P1^5;
- sbit SCL=P1^4;
- sbit k3=P2^7;
- sbit J1=P2^5;
- sbit J2=P2^4;
- sbit J3=P2^6;
- sbit J4=P2^3;
- sbit led=P0^3;
- sbit k1=P0^6;
- sbit RTS=P3^5;
- sbit k2=P0^7;
- uchar t,t1; //定时器计数
- bit bt; //波特率修改标志
- uchar code STC[3][60]={
- {0x00,0x00,0x00,0x00,0x00,0xC0,0xF0,0xF8,0x78,0x18,0x1C,0x0C,0x0C,0x18,0x38,0x70,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x9C,0xFC,0xFC,0x7C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xF0,0x78,0x38,0x1C,0x0C,0x0C,0x1C,0x38,0x70,0x60,0x00},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0x3E,0x78,0xF0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xF0,0xFE,0xFF,0x1F,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFC,0xFF,0x1F,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
- {0x00,0x0F,0x1E,0x3C,0x78,0x70,0x70,0x30,0x38,0x1E,0x0F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x7C,0x7F,0x0F,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x1F,0x3E,0x38,0x70,0x60,0x60,0x70,0x38,0x1C,0x0C,0x00,0x00,0x00,0x00},
- }; //STC
- uchar code leng2424[3][24]={
- {0x00,0x00,0x30,0x70,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0xC0,0xF0,0xFC,0x3C,0x3C,0xF4,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00},
- {0x00,0x00,0x00,0x80,0xE0,0xF9,0x79,0x1C,0x6C,0x6E,0x67,0x63,0x61,0x62,0x67,0x7F,0x7C,0xE9,0xE3,0xE7,0x67,0x2E,0x0E,0x04},
- {0x00,0x0C,0x1E,0x1F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x06,0x0E,0x1C,0x3C,0x3C,0x7F,0x67,0x43,0x01,0x00,0x00,0x00,0x00},
- };//冷
- uchar code qi2424[3][24]={
- {0x00,0x00,0x00,0x00,0x00,0xF0,0xF0,0x30,0x30,0x30,0x30,0x34,0x3E,0x3E,0x3C,0x30,0x30,0x30,0xF0,0xF0,0xF0,0x00,0x00,0x00},
- {0x00,0x00,0x00,0x00,0xF0,0xFF,0xFF,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC7,0xC7,0xC7,0xC0,0x00,0x00},
- {0x00,0x20,0x70,0x7E,0x3F,0x0F,0x01,0x7F,0x7F,0x7F,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7F,0x7F,0x7F,0x00,0x00},
- };//启
- uchar code dong2424[3][24]={
- {0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x80,0x80,0x80,0x80,0xFC,0xFC,0x84,0x80,0x80,0x80,0x80,0x80},
- {0x00,0x04,0x04,0x04,0xE4,0xFC,0x7C,0x1C,0x44,0xE4,0xE4,0x84,0x05,0x01,0x01,0xFD,0xFF,0xFF,0x01,0x01,0xE1,0xFF,0xFF,0x00},
- {0x00,0x00,0x0E,0x0F,0x0F,0x05,0x04,0x06,0x06,0x07,0x07,0x6F,0x7E,0x78,0x3E,0x0F,0x07,0x60,0x60,0x60,0x7F,0x7F,0x3F,0x00},
- };//动
- uchar code xia2424[3][24]={
- {0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xF8,0xF8,0xF8,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x08,0x1C,0x3C,0x78,0xF0,0xE0,0xC0,0x80,0x00,0x00},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00},
- }; //下
- uchar code zai2424[3][24]={
- {0x00,0x80,0x90,0x90,0x90,0x90,0xFE,0xFE,0xFE,0x90,0x90,0x90,0x90,0x80,0xFE,0xFE,0x82,0x80,0x8C,0x9C,0x98,0x90,0x80,0x80},
- {0x00,0x00,0xC8,0xE8,0xF8,0xBC,0x9F,0xEF,0xEA,0xE8,0x88,0x88,0x88,0x08,0x1F,0xFF,0xFF,0x80,0xF0,0xFC,0x3C,0x0C,0x00,0x00},
- {0x00,0x08,0x18,0x18,0x18,0x08,0x08,0x7F,0x7F,0x7F,0x0C,0x4C,0x64,0x70,0x38,0x3D,0x1F,0x1F,0x3F,0x78,0x60,0x78,0x78,0x18},
- }; //载
- uchar code cun2424[3][24]={
- {0x00,0x00,0x00,0x20,0x20,0x20,0x20,0x20,0xE0,0xF8,0xFE,0x3E,0x24,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00},
- {0x00,0x40,0xC0,0xE0,0x70,0xFE,0xFE,0xFF,0x87,0x81,0x86,0x86,0x86,0x86,0x86,0xE6,0xF6,0xFE,0x9E,0x9E,0x8E,0x86,0x80,0x80},
- {0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x20,0x60,0x60,0x7F,0x7F,0x3F,0x00,0x00,0x00,0x00,0x00,0x00},
- }; //存
- uchar code chu2424[3][24]={
- {0x00,0x00,0x00,0xC0,0xF8,0x3C,0x04,0x00,0x10,0xF8,0x60,0x00,0x00,0x30,0x30,0xFC,0xFC,0x30,0x30,0xC0,0x70,0x30,0x00,0x00},
- {0x00,0x0C,0x0F,0x03,0xFF,0x00,0x06,0x06,0xFE,0xFE,0x00,0x80,0xC6,0xC6,0xE6,0x37,0x3F,0x2E,0x27,0x27,0xE6,0x06,0x06,0x00},
- {0x00,0x00,0x00,0x00,0x7F,0x00,0x00,0x00,0x1F,0x1F,0x0C,0x06,0x01,0x00,0x7F,0x13,0x13,0x13,0x13,0x13,0x7F,0x00,0x00,0x00},
- }; //储
- uchar code cuo2424[3][24]={
- {0x00,0x00,0xC0,0xE0,0xF8,0x7C,0x7C,0x64,0x60,0x60,0x60,0x60,0x60,0xFC,0xFC,0x60,0x60,0x60,0xFC,0xFC,0x60,0x60,0x60,0x00},
- {0x00,0x01,0x61,0x67,0xFE,0xFE,0xFE,0x66,0x66,0x60,0x04,0xE4,0xE4,0xE7,0x67,0x64,0x64,0x64,0x67,0xE7,0xE4,0xE4,0x04,0x04},
- {0x00,0x00,0x00,0x00,0x3F,0x3F,0x3F,0x1C,0x0E,0x06,0x00,0x7F,0x7F,0x7F,0x32,0x32,0x32,0x32,0x32,0x7F,0x7F,0x7F,0x00,0x00},
- };//错
- uchar code wu2424[3][24]={
- {0x00,0x00,0x00,0x0C,0x1C,0x3C,0x38,0x30,0x00,0xF8,0xF8,0xF8,0x88,0x88,0x88,0x88,0x88,0x88,0x88,0xF8,0xF8,0x00,0x00,0x00},
- {0x00,0x03,0x03,0x02,0xFE,0xFE,0xFE,0x00,0x88,0x89,0x89,0x89,0x89,0x89,0xF9,0xF9,0x99,0x89,0x89,0x89,0x89,0x88,0x88,0x80},
- {0x00,0x00,0x00,0x10,0x3F,0x3F,0x1F,0x4E,0x66,0x60,0x70,0x38,0x1E,0x1F,0x07,0x07,0x0F,0x1E,0x38,0x30,0x70,0x60,0x60,0x20},
- };//误
- uchar code ce2424[3][24]={
- {0x00,0x00,0x08,0x1C,0x18,0x10,0x00,0xF8,0xF8,0x18,0x98,0x98,0x18,0xF8,0xF8,0x00,0x00,0xE0,0x20,0x00,0xFC,0xFC,0x00,0x00},
- {0x00,0x00,0x03,0x06,0xCC,0xC4,0x00,0xFF,0xFF,0x00,0xFF,0xFF,0x00,0xFF,0xFF,0x00,0x00,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00},
- {0x00,0x30,0x3C,0x1E,0x03,0x00,0x00,0x60,0x38,0x1E,0x07,0x04,0x0E,0x1C,0x78,0x20,0x00,0x03,0x63,0x60,0x7F,0x3F,0x00,0x00},
- };// 测
- uchar code shi2424[3][24]={
- {0x00,0x00,0x08,0x1C,0x38,0x20,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xFC,0xFC,0x80,0x84,0x9C,0xB8,0x90,0x00,0x00},
- {0x00,0x06,0x06,0x06,0xFE,0xFE,0x00,0x00,0x18,0x18,0xF8,0xF8,0x18,0x18,0x00,0x1F,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00},
- {0x00,0x00,0x00,0x10,0x3F,0x1F,0x0C,0x06,0x32,0x10,0x1F,0x1F,0x08,0x0C,0x0C,0x00,0x03,0x0F,0x3C,0x70,0x60,0x7C,0x18,0x00},
- };// 试
- uchar code shu2424[3][24]={
- {0x00,0x00,0xC8,0xDC,0xF8,0xC0,0xC0,0xFC,0xC0,0xC0,0xF8,0xD8,0xC0,0x00,0xF0,0xFC,0x8C,0x80,0x80,0x80,0x80,0x80,0x00,0x00},
- {0x00,0x00,0x4C,0x4C,0x46,0xE3,0x71,0x5F,0x40,0x43,0xCE,0xD4,0x3C,0x0F,0x7F,0xF0,0x80,0x00,0xE0,0xFF,0x03,0x00,0x00,0x00},
- {0x00,0x00,0x40,0x60,0x63,0x27,0x34,0x1C,0x0C,0x0F,0x0B,0x58,0x68,0x60,0x30,0x19,0x0F,0x0F,0x1B,0x30,0x60,0x60,0x00,0x00},
- };// 数
- uchar code ju2424[3][24]={
- {0x00,0x80,0x80,0x80,0x80,0xFC,0x84,0x80,0x80,0x00,0xF8,0xF8,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0x98,0xF8,0x08,0x00,0x00},
- {0x00,0x40,0xC0,0x60,0x20,0xFF,0x10,0x18,0x08,0x00,0xFF,0x3F,0x11,0x11,0x11,0x11,0xFF,0xFF,0x11,0x11,0x11,0x11,0x10,0x00},
- {0x00,0x00,0x20,0x60,0x60,0x7F,0x00,0x00,0x30,0x3E,0x07,0x00,0x7F,0x7F,0x31,0x31,0x31,0x31,0x31,0x31,0x7F,0x7F,0x00,0x00},
- };// 据
- uchar code ou1616[2][16]={
- {0x00,0x00,0xFC,0xFC,0xE4,0xC4,0xF4,0xF4,0xE4,0xFE,0xDE,0xD2,0xD0,0x70,0x70,0x10},
- {0x00,0x00,0x3F,0x3F,0x2E,0x27,0x27,0x66,0x60,0x70,0x3F,0x0F,0x1F,0x78,0xE0,0xC0},
- };//欧
- uchar code yang1616[2][16]={
- {0x00,0x00,0xFC,0xFC,0xE4,0xFC,0x9C,0x00,0xFC,0xFC,0x84,0x84,0x84,0xFC,0xFC,0xFC},
- {0x00,0x00,0xFF,0xFF,0x18,0x1F,0x0F,0x06,0x7F,0x7F,0x10,0x10,0x10,0x7F,0x7F,0x7F},
- };//阳
- uchar code yun1616[2][16]={
- {0x00,0xC0,0xC0,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC4,0xC0,0xC0},
- {0x00,0x00,0x20,0x70,0x78,0x7C,0x2F,0x23,0x21,0x20,0x2C,0x3C,0x38,0x70,0x60,0x00},
- };//云
- uchar code tian1616[2][16]={
- {0x00,0xC0,0xC4,0xC4,0xC4,0xC4,0xC4,0xFC,0xFC,0xFC,0xC4,0xC4,0xC4,0xC4,0xC0,0xC0},
- {0x00,0x40,0x60,0x60,0x30,0x38,0x1E,0x0F,0x03,0x07,0x1E,0x18,0x30,0x60,0x60,0x40},
- }; //天
- uchar code mo1616[2][16]={
- {0x00,0x10,0x90,0xFE,0x90,0x10,0x04,0xE4,0xAE,0xAE,0xA4,0xAE,0xAE,0xE4,0x04,0x00},
- {0x00,0x06,0x03,0x7F,0x00,0x43,0x48,0x4B,0x2A,0x3A,0x0E,0x1A,0x2A,0x4B,0xC8,0x40},
- }; //模
- uchar code shi1616[2][16]={
- {0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x3E,0xFE,0x10,0x14,0x1C,0x10,0x10,0x00},
- {0x00,0x20,0x61,0x21,0x3F,0x3F,0x11,0x11,0x10,0x03,0x1E,0x30,0x60,0x40,0x70,0x00},
- };//式
- uchar code zi1616[2][16]={
- {0x00,0x00,0x00,0xE0,0xE0,0xA0,0xB8,0xBC,0xAC,0xA0,0xA0,0xA0,0xF0,0xF0,0x00,0x00},
- {0x00,0x00,0x00,0x7F,0x7F,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x7F,0x7F,0x00,0x00},
- };//自
- uchar code shou1616[2][16]={
- {0x00,0x20,0xA0,0xA0,0xB0,0xB0,0xB0,0xF0,0xF0,0x98,0x98,0xD8,0xD8,0x90,0x00,0x00},
- {0x02,0x02,0x02,0x02,0x22,0x22,0x62,0x7F,0x7F,0x02,0x02,0x02,0x03,0x03,0x03,0x02},
- };//手
- uchar code dong1616[2][16]={
- {0x00,0x80,0x90,0x90,0x90,0x90,0x98,0xD8,0xD0,0xC0,0xF8,0xF8,0x40,0x60,0xE0,0xC0},
- {0x00,0x10,0x18,0x1E,0x17,0x17,0x5D,0x78,0x30,0x1E,0x0F,0x21,0x60,0x60,0x7F,0x3F},
- };//动
- uchar code chuan1616[2][16]={
- {0x00,0x00,0x00,0x78,0x78,0x48,0x48,0xFE,0xFE,0xFE,0x48,0x48,0x78,0x78,0x00,0x00},
- {0x00,0x00,0x1F,0x1F,0x11,0x11,0x11,0x7F,0x7F,0x7F,0x11,0x11,0x11,0x1F,0x1F,0x00},
- };//串
- uchar code kou1616[2][16]={
- {0x00,0x00,0x00,0xF8,0xF8,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0xF8,0xF8,0xF8,0x00},
- {0x00,0x00,0x00,0x7F,0x7F,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7F,0x7F,0x7F,0x00},
- };//口
- uchar code kai1616[2][16]={
- {0x00,0x80,0x84,0x84,0x84,0xFC,0xFC,0xFC,0x84,0x84,0xFC,0xFC,0x84,0x84,0x84,0x80},
- {0x00,0x41,0x41,0x61,0x71,0x3F,0x1F,0x03,0x01,0x01,0x7F,0x7F,0x01,0x01,0x01,0x01},
- };//开
- uchar code qi1616[2][16]={
- {0x00,0x00,0x00,0xF8,0xF8,0xC8,0xC8,0xCA,0xCE,0xCE,0xC8,0xC8,0xF8,0xF8,0xF8,0x00},
- {0x00,0x60,0x78,0x3F,0x0F,0xFE,0xFE,0x22,0x22,0x22,0x22,0x22,0xFE,0xFE,0xFE,0x00},
- };//启
- uchar code guan1616[2][16]={
- {0x00,0x00,0x30,0x30,0x30,0x3E,0x3E,0xF8,0xF0,0xF8,0x3E,0x3E,0x30,0x30,0x30,0x00},
- {0x00,0x42,0x42,0x42,0x62,0x32,0x3A,0x1F,0x0F,0x1F,0x1A,0x32,0x72,0x62,0x62,0x02},
- };//关
- uchar code bi1616[2][16]={
- {0x00,0xF0,0xF0,0xF6,0x4E,0x4C,0x40,0x44,0xC4,0xF4,0xF4,0x44,0x44,0xFC,0xFC,0xFC},
- {0x00,0x7F,0x7F,0x7F,0x18,0x1C,0x0E,0x67,0x63,0x3F,0x3F,0x00,0x40,0x7F,0x7F,0x3F},
- };//闭
- uchar code gong1616[2][16]={
- {0x00,0x80,0xE0,0xFC,0xFE,0x26,0x20,0xFE,0xFE,0xFE,0x20,0xFE,0xFE,0x20,0x20,0x20},
- {0x00,0x00,0x00,0x7F,0x7F,0x46,0x66,0x7F,0x3F,0x07,0x06,0x1F,0x3F,0x76,0x66,0x46},
- };//供
- uchar code dian1616[2][16]={
- {0x00,0x00,0xF0,0xF0,0x90,0x90,0x90,0xFE,0xFE,0x90,0x90,0x90,0xF0,0xF0,0x00,0x00},
- {0x00,0x00,0x1F,0x1F,0x0C,0x0C,0x0C,0x7F,0x7F,0x4C,0x4C,0x4C,0x4F,0x4F,0x60,0x60},
- };//电
- uchar code U0816[2][8]={
- {0x00,0xF8,0xF8,0x00,0x00,0x00,0xF8,0xF8},
- {0x00,0x1F,0x3F,0x30,0x20,0x30,0x3F,0x1F},
- };//U
- uchar code S0816[2][8]={
- {0x00,0x70,0xF8,0xD8,0x88,0x38,0x38,0x30},
- {0x00,0x1C,0x3C,0x31,0x21,0x33,0x3F,0x1E},
- };//S
- uchar code B0816[2][8]={
- {0x00,0xF8,0xF8,0x88,0x88,0x98,0xF8,0x70},
- {0x00,0x3F,0x3F,0x31,0x31,0x31,0x1F,0x1E},
- };//B
- uchar code I0816[2][8]={
- {0x00,0x00,0x00,0xF8,0xF8,0xF8,0x00,0x00},
- {0x00,0x00,0x00,0x3F,0x3F,0x3F,0x00,0x00},
- };//I
- uchar code N0816[2][8]={
- {0x00,0xF8,0xF8,0xF0,0xC0,0x00,0xF8,0xF8},
- {0x00,0x3F,0x3F,0x01,0x07,0x3F,0x3F,0x3F},
- };//N
- uchar code T0816[2][8]={
- {0x00,0x08,0x08,0xF8,0xF8,0xF8,0x08,0x08},
- {0x00,0x00,0x00,0x3F,0x3F,0x3F,0x00,0x00},
- };//T
- uchar code num[16][16]={
- {0x00,0xE0,0xF0,0x18,0x08,0x18,0xF0,0xE0,0x00,0x0F,0x1F,0x30,0x20,0x30,0x1F,0x0F},/*"0",0*/
- {0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x3F,0x20,0x20},/*"1",1*/
- {0x00,0x70,0x78,0x08,0x08,0x08,0xF8,0xF0,0x00,0x30,0x38,0x2C,0x26,0x23,0x31,0x30},/*"2",2*/
- {0x00,0x30,0x38,0x08,0x08,0x88,0xF8,0x70,0x00,0x18,0x38,0x21,0x21,0x23,0x3E,0x1C},/*"3",3*/
- {0x00,0x00,0x80,0xC0,0x70,0xF8,0xF8,0x00,0x00,0x06,0x07,0x25,0x24,0x3F,0x3F,0x24},/*"4",4*/
- {0x00,0xF8,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x19,0x39,0x20,0x20,0x31,0x1F,0x0E},/*"5",5*/
- {0x00,0xE0,0xF0,0x98,0x88,0x98,0x90,0x00,0x00,0x0F,0x1F,0x31,0x20,0x20,0x3F,0x1F},/*"6",6*/
- {0x00,0x18,0x18,0x08,0x88,0xE8,0x78,0x18,0x00,0x00,0x00,0x3E,0x3F,0x01,0x00,0x00},/*"7",7*/
- {0x00,0x70,0xF8,0x88,0x08,0x88,0xF8,0x70,0x00,0x1C,0x3E,0x23,0x21,0x23,0x3E,0x1C},/*"8",8*/
- {0x00,0xF0,0xF8,0x08,0x08,0x18,0xF0,0xE0,0x00,0x01,0x13,0x32,0x22,0x33,0x1F,0x0F},/*"9",9*/
- {0x00,0x00,0xC0,0xF8,0xF8,0xE0,0x00,0x00,0x20,0x3C,0x3F,0x23,0x02,0x27,0x3F,0x38},/*"A",10*/
- {0x08,0xF8,0xF8,0x88,0x88,0xF8,0x70,0x00,0x20,0x3F,0x3F,0x20,0x20,0x31,0x1F,0x0E},/*"B",11*/
- {0xC0,0xF0,0x38,0x08,0x08,0x08,0x38,0x38,0x07,0x1F,0x38,0x20,0x20,0x30,0x18,0x08},/*"C",12*/
- {0x08,0xF8,0xF8,0x08,0x08,0x18,0xF0,0xE0,0x20,0x3F,0x3F,0x20,0x20,0x30,0x1F,0x0F},/*"D",13*/
- {0x08,0xF8,0xF8,0x88,0xE8,0xE8,0x18,0x10,0x20,0x3F,0x3F,0x20,0x23,0x23,0x38,0x18},/*"E",14*/
- {0x08,0xF8,0xF8,0x88,0xE8,0xE8,0x18,0x10,0x20,0x3F,0x3F,0x20,0x03,0x03,0x00,0x00},/*"F",15*/
- };
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=925;y>0;y--);
- }
- void init_uart( ) //2400bps@11.0592MHz
- {
- PCON &= 0x7F; //波特率不倍速
- SCON = 0x50; //8位数据,可变波特率
- AUXR &= 0xFB; //定时器时钟12T模式
- BRT = 0xF4; //设置定时重载值
- AUXR |= 0x01; //串口1使用独立波特率发射器为波特率发生器
- //AUXR |= 0x10; //启动独立波特率发射器
- }
- void timer0( ) //50毫秒 16位自动重载
- {
- AUXR &= 0x3F; //定时器时钟12T模式
- TMOD = 0x11; //设置定时器模式
- TL0 = 0x00; //设置定时初始值
- TH0 = 0x4C; //设置定时初始值
- TL1 = 0x00; //设置定时初始值
- TH1 = 0x4C; //设置定时初始值
- TF0 = 0; //清除TF0标志
- TF1 = 0; //清除TF1标志
- EA=1;
- ET0=1; //开中断
- ET1=1;
- }
- void IAP_CLOSE() //关闭IAP
- {
- IAP_CONTR = 0; //关闭IAP功能
- IAP_CMD = 0; //清除命令寄存器
- IAP_TRIG = 0; //清除触发寄存器
- IAP_ADDRH = 0x80; //将地址设置到非IAP区域
- IAP_ADDRL = 0;
- }
- uchar READ_IAP(uint addr) //读一个字节
- { //5A60S2有1kb存储空间,空间地址从0000H到03FFH,每512字节为一个扇区,共2个扇区
- uchar dat; //数据缓冲区
- IAP_CONTR = 0x83; //使能IAP,1000----,设置CPU等待时间(11.0592)---- 0011
- IAP_CMD =0X01; //设置IAP读取命令
- IAP_ADDRL = addr; //设置IAP低地址
- IAP_ADDRH = addr >> 8; //设置IAP高地址
- IAP_TRIG = 0x5a; //写触发命令(0x5a)
- IAP_TRIG = 0xa5; //写触发命令(0xa5)
- _nop_( ); //等待ISP/IAP/EEPROM操作完成
- _nop_( );
- _nop_( );
- dat = IAP_DATA; //读ISP/IAP/EEPROM数据
- IAP_CLOSE( ); //关闭IAP功能
- return dat; //返回
- }
- void WRITE_IAP(uint addr, uchar dat) //写入一个字节
- {
- IAP_CONTR =0x83; //使能IAP
- IAP_CMD =0X02; //设置IAP写入命令
- IAP_ADDRL = addr; //设置IAP低地址
- IAP_ADDRH = addr >> 8; //设置IAP高地址
- IAP_DATA = dat; //写ISP/IAP/EEPROM数据
- IAP_TRIG = 0x5a; //写触发命令(0x5a)
- IAP_TRIG = 0xa5; //写触发命令(0xa5)
- delay(1); //等待ISP/IAP/EEPROM操作完成 用不了1毫秒,几十微秒就可,有微秒函数可替换
- IAP_CLOSE( );
- }
- void DELETE_IAP(uint addr) //扇区擦除
- {
- IAP_CONTR = 0x83; //使能IAP
- IAP_CMD =0x03; //设置IAP命令
- IAP_ADDRL = addr; //设置IAP低地址
- IAP_ADDRH = addr >> 8; //设置IAP高地址
- IAP_TRIG = 0x5a; //写触发命令(0x5a)
- IAP_TRIG = 0xa5; //写触发命令(0xa5)
- delay(25); //等待ISP/IAP/EEPROM操作完成
- IAP_CLOSE( );
- }
- void start( )
- {
- SCL = 1;
- SDA = 1;
- SDA = 0;
- _nop_( );
- _nop_( );
- SCL = 0;
- _nop_( );
- _nop_( );
- }
- void stop( )
- {
- SCL=0;
- SDA=0;
- _nop_( );
- SCL=1;
- _nop_( );
- SDA=1;
- _nop_( );
- _nop_( );
- _nop_( );
- _nop_( );
- _nop_( );
- _nop_( );
- _nop_( );
- _nop_( );
- }
- void writebyte(uchar date) //写1个字节
- {
- uchar i;
- for(i=8;i>0;i--)
- {
- date<<=1;
- SDA=CY;
- SCL=1;
- _nop_( );
- _nop_( );
- SCL=0;
- _nop_( );
- _nop_( );
- }
- SDA=1;
- SCL=1;
- _nop_( );
- SCL=0;
- }
- void qingping( )
- {
- uchar x,y;
- for(y=0;y<8;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y);
- writebyte(0x00); //列地址低位
- writebyte(0x10); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<128;x++)
- {
- writebyte(0x00); //清屏
- }
- stop();
- }
- }
- void initoled( ) //初始化OLED
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xae); //--关闭oled面板
- writebyte(0x00); //---设置低列地址
- writebyte(0x10); //---设置高列地址
- writebyte(0x40); //--设置起始行地址设置映射RAM显示起始行 (0x00~0x3F)
- writebyte(0x81); //--设置对比度控制寄存器
- writebyte(0xFF); // 设置SEG输出电流亮度
- writebyte(0xa1); //--设置SEG/列映射 0xa0左右反置 0xa1正常
- writebyte(0xc8); //设置COM/行扫描方向 0xc0上下反置 0xc8正常
- writebyte(0xa6); //--设置正常显示
- writebyte(0xa8); //--设置多路复用率(1 to 64)
- writebyte(0x3f); //--1/64 duty
- writebyte(0xd3); //-设置显示偏移 移位映射RAM计数器 (0x00~0x3F)
- writebyte(0x00); //-不抵消
- writebyte(0xd5); //--设置显示时钟分频比/振荡器频率
- writebyte(0x80); //--设置分频比,将时钟设置为100帧/秒
- writebyte(0xd9); //--设定预充电时间
- writebyte(0xf1); //将预充电设置为15个时钟,放电设置为1个时钟
- writebyte(0xda); //--设置com引脚硬件配置
- writebyte(0x12);
- writebyte(0xdb); //--set vcomh
- writebyte(0x30); //设置VCOM取消选择级别
- writebyte(0x20); //-设置页面寻址模式 (0x00/0x01/0x02)
- writebyte(0x02); //
- writebyte(0x8d); //--设置电荷泵启用/禁用
- writebyte(0x14); //--set(0x10) 使能
- writebyte(0xa4); // 在屏幕上禁用整个显示 (0xa4/0xa5)
- writebyte(0xa6); // 禁用反转显示打开 (0xa6/a7)
- stop( );
- qingping( );
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xaf); //--打开oled面板 //先清屏再开显示,否则容易显示断电前上一次的图像
- stop( );
- }
- void display( )
- {
- uchar x,y,z;
- z=34; //要写入的列地址首位
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y); //b0代表第一页,最上面一档的8个像素,写0-1-2共3页
- writebyte(0x00+(z%16)); //列地址低位,占用60列,居中排列则左边第34个列地址开始写
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<60;x++)
- {
- writebyte(STC[y][x]);
- }
- stop( );
- }
- delay(200);
- z=4;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb3+y); //b3代表第4页,写4-5-6共3页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(leng2424[y][x]);
- }
- stop( );
- }
- delay(200);
- z=z+24;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb3+y); //b3代表第4页,写4-5-6共3页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(qi2424[y][x]);
- }
- stop( );
- }
- delay(200);
- z=z+24;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb3+y); //b3代表第4页,写4-5-6共3页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(dong2424[y][x]);
- }
- stop( );
- }
- delay(200);
- z=z+24;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb3+y); //b3代表第4页,写4-5-6共3页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(xia2424[y][x]);
- }
- stop( );
- }
- delay(200);
- z=z+24;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb3+y); //b3代表第4页,写4-5-6共3页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(zai2424[y][x]);
- }
- stop( );
- }
- delay(200);
- z=40;
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb7); //b7代表最后一页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<23;x++)
- {
- writebyte(0x30); //_____
- }
- stop( );
- delay(200);
- z=63;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb6+y); //b6 b7 代表最后2页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(ou1616[y][x]);
- }
- stop( );
- }
- delay(200);
- z=z+16;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb6+y); //b6 b7 代表最后2页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(yang1616[y][x]);
- }
- stop();
- }
- delay(200);
- z=z+16;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb6+y); //b6 b7 代表最后2页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(yun1616[y][x]);
- }
- stop();
- }
- delay(200);
- z=z+16;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb6+y); //b6 b7 代表最后2页
- writebyte(0x00+(z%16)); //列地址低位
- writebyte(0x10+(z/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(tian1616[y][x]);
- }
- stop();
- }
- }
- void displaymoshi(uchar page,uchar addr)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(mo1616[y][x]); //写数据
- }
- for(x=0;x<16;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- writebyte(shi1616[y][x]); //写数据
- }
- stop( );
- }
- }
- void dispcunchucuowu( )
- {
- uchar x,y;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb1+y);
- writebyte(0x00+(39%16)); //列地址低位
- writebyte(0x10+(39/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(cun2424[y][x]); //存
- }
- for(x=0;x<24;x++)
- {
- writebyte(chu2424[y][x]); //储
- }
- stop( );
- }
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb4+y);
- writebyte(0x00+(15%16)); //列地址低位
- writebyte(0x10+(15/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(cuo2424[y][x]); //错
- }
- for(x=0;x<24;x++)
- {
- writebyte(wu2424[y][x]); //误
- }
- for(x=0;x<24;x++)
- {
- writebyte(ce2424[y][x]); //测
- }
- for(x=0;x<24;x++)
- {
- writebyte(shi2424[y][x]); //试
- }
- stop( );
- }
- }
- void dispshujucuowu( )
- {
- uchar x,y;
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb1+y);
- writebyte(0x00+(39%16)); //列地址低位
- writebyte(0x10+(39/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(shu2424[y][x]); //数
- }
- for(x=0;x<24;x++)
- {
- writebyte(ju2424[y][x]); //据
- }
- stop( );
- }
- for(y=0;y<3;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb4+y);
- writebyte(0x00+(15%16)); //列地址低位
- writebyte(0x10+(15/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<24;x++)
- {
- writebyte(cun2424[y][x]); //存
- }
- for(x=0;x<24;x++)
- {
- writebyte(chu2424[y][x]); //储
- }
- for(x=0;x<24;x++)
- {
- writebyte(cuo2424[y][x]); //错
- }
- for(x=0;x<24;x++)
- {
- writebyte(wu2424[y][x]); //误
- }
- stop( );
- }
- }
- void displayshouzi(uchar page,uchar addr,bit m)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- if(m==0)
- {
- writebyte(zi1616[y][x]); //写数据
- }
- else
- {
- writebyte(shou1616[y][x]);
- }
- }
- for(x=0;x<16;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- writebyte(dong1616[y][x]); //写数据
- }
- stop( );
- }
- }
- void dispckou(uchar page,uchar addr)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(chuan1616[y][x]); //写数据
- }
- for(x=0;x<16;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- writebyte(kou1616[y][x]); //写数据
- }
- stop( );
- }
- }
- void dispkaiguan(uchar page,uchar addr,bit m)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- if(m==0)
- {
- writebyte(guan1616[y][x]); //写数据
- }
- else
- {
- writebyte(kai1616[y][x]);
- }
- }
- for(x=0;x<16;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- if(m==0)
- {
- writebyte(bi1616[y][x]); //写数据
- }
- else
- {
- writebyte(qi1616[y][x]);
- }
- }
- stop( );
- }
- }
- void dispvcc(uchar page,uchar addr,bit vcc)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<8;x++)
- {
- if(vcc==0)
- {
- writebyte(U0816[y][x]); //写数据
- }
- else
- {
- writebyte(I0816[y][x]);
- }
- }
- for(x=0;x<8;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- if(vcc==0)
- {
- writebyte(S0816[y][x]); //写数据
- }
- else
- {
- writebyte(N0816[y][x]);
- }
- }
- for(x=0;x<8;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- if(vcc==0)
- {
- writebyte(B0816[y][x]); //写数据
- }
- else
- {
- writebyte(T0816[y][x]);
- }
- }
- stop( );
- }
- }
- void dispgongdian(uchar page,uchar addr)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- writebyte(gong1616[y][x]); //写数据
- }
- for(x=0;x<16;x++) //1个字占用2页,先把所有的字的1页写完了,再写下1页的字
- {
- writebyte(dian1616[y][x]); //写数据
- }
- stop( );
- }
- }
- void dispnum2(uchar page,uchar addr,bit jz,uchar n) //写2位数字
- {
- uchar x,y,na,nb;
- if(jz==0) //jz=0代表10进制
- {
- na=n/10;
- nb=n%10;
- }
- else //jz=1代表16进制
- {
- na=n/16;
- nb=n%16;
- }
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<8;x++)
- {
- if(y==0)
- {
- writebyte(num[na][x]);
- }
- else
- {
- writebyte(num[na][x+8]);
- }
- }
- for(x=0;x<8;x++)
- {
- if(y==0)
- {
- writebyte(num[nb][x]);
- }
- else
- {
- writebyte(num[nb][x+8]);
- }
- }
- }
- }
- void disp_nn(uchar page,uchar addr)
- {
- uchar x,y;
- for(y=0;y<2;y++)
- {
- start();
- writebyte(0x78);
- writebyte(0x00); //写指令
- writebyte(0xb0+y+page); //b6 b7 代表最后2页
- writebyte(0x00+(addr%16)); //列地址低位
- writebyte(0x10+(addr/16)); //列地址高位
- stop();
- start();
- writebyte(0x78);
- writebyte(0x40); //写数据
- for(x=0;x<16;x++)
- {
- if(y==0)
- {
- writebyte(0x80); //写数据 ___
- }
- else
- {
- writebyte(0x01); //写数据 ___
- }
- }
- stop( );
- }
- }
- void main( )
- {
- bit key,ms,ck,dy,dc,xz,btx; //按键标志 模式状态 串口状态 供电选择 电源开/关 下载标志 7F检测失败
- uchar vdc,dat,j; //供电选择存储数据 串口接收数据 7F个数
- uint tt; //长按/短按计数
- uchar btln,btl; //波特率扫描次数 波特率
- delay(100);
- initoled( );
- init_uart( );
- timer0( );
- led=0;
- display( );
- delay(1500);
- led=1;
- qingping( );
- vdc=READ_IAP(0x0000); //读出供电存储数据
- if(vdc==0x01||vdc==0x10)
- {
- if(vdc==0x01)
- {
- dy=0; //读出的数据是01则是USB供电
- }
- else
- {
- dy=1; //读出的数据是10则是INT供电
- }
- }
- else //既不是01也不是10的话,则是数据错误,可能是新的主控MCU,应该重新写入数据
- {
- DELETE_IAP(0x0000); //擦除扇区
- WRITE_IAP(0X0000,0x01); //重新写入供电存储数据,默认是USB供电
- vdc=READ_IAP(0x0000); //重新读出数据
- dispshujucuowu( ); //报错
- delay(1500);
- qingping( );
- }
- displaymoshi(0,16);
- displayshouzi(0,80,ms);
- dispckou(4,16);
- dispvcc(2,0,dy);
- dispgongdian(2,24);
- dispkaiguan(2,80,dc);
- dispkaiguan(4,80,ck);
- disp_nn(6,75);
- disp_nn(6,110);
- disp_nn(6,0);
- disp_nn(6,16);
- delay(500);
- if(dy==1)
- {
- J3=0; //启动INT供电继电器GND
- J1=0;
- dc=1;
- dispkaiguan(2,80,dc);
- }
- else
- {
- J4=0; //启动USB供电继电器GND
- J2=0;
- dc=1;
- dispkaiguan(2,80,dc);
- }
- while(1)
- {
- if(ck==0) //串口标志关闭时,扫描串口是否打开
- {
- if(RTS==0)
- {
- delay(1);
- if(RTS==0)
- {
- ck=1;
- dispkaiguan(4,80,ck); //识别到串口打开,更新显示
- led=0;
- }
- }
- }
- if(ck)
- {
- if(xz==0) //没有出现下载标志时,定时调整波特率用以检测7F,下载标志出现时就不能再调整波特率了
- { //2400-4800-9600-2400-4800-9600,共测试3种常用波特率,循环2次仍未检测到7F就不再检测
- if(btln==0&&bt==0) //波特率切换计数 波特率设置标志
- {
- btl=2400; //从2400开始扫描
- AUXR&=0XEF; //停止独立波特率发生器
- dispnum2(6,0,0,24);
- dispnum2(6,16,0,0);
- BRT = 0xF4; //设置定时重载值
- AUXR |= 0x10; //启动独立波特率发生器
- bt=1; //修改波特率标志,防止多次修改
- btln++;
- TR0=1; //启动定时器计时
- }
- if(btln==1&&bt==0)
- {
- btl=4800; //波特率4800
- AUXR&=0XEF; //停止独立波特率发生器
- dispnum2(6,0,0,48);
- BRT = 0xFA; //设置定时重载值
- AUXR |= 0x10; //启动独立波特率发生器
- bt=1; //修改波特率标志,防止多次修改
- btln++;
- TR0=1; //启动定时器计时
- }
- if(btln==2&&bt==0)
- {
- btl=9600; //波特率9600
- AUXR&=0XEF; //停止独立波特率发生器
- dispnum2(6,0,0,96);
- BRT = 0xFD; //设置定时重载值
- AUXR |= 0x10; //启动独立波特率发生器
- bt=1; //修改波特率标志,防止多次修改
- btln++;
- TR0=1; //启动定时器计时
- }
- if(btln==3&&bt==0)
- {
- btl=2400; //从2400开始扫描
- AUXR&=0XEF; //停止独立波特率发生器
- dispnum2(6,0,0,24);
- dispnum2(6,16,0,0);
- BRT = 0xF4; //设置定时重载值
- AUXR |= 0x10; //启动独立波特率发生器
- bt=1; //修改波特率标志,防止多次修改
- btln++;
- TR0=1; //启动定时器计时
- }
- if(btln==4&&bt==0)
- {
- btl=4800; //波特率4800
- AUXR&=0XEF; //停止独立波特率发生器
- dispnum2(6,0,0,48);
- BRT = 0xFA; //设置定时重载值
- AUXR |= 0x10; //启动独立波特率发生器
- bt=1; //修改波特率标志,防止多次修改
- btln++;
- TR0=1; //启动定时器计时
- }
- if(btln==5&&bt==0)
- {
- btl=9600; //波特率9600
- AUXR&=0XEF; //停止独立波特率发生器
- dispnum2(6,0,0,96);
- BRT = 0xFD; //设置定时重载值
- AUXR |= 0x10; //启动独立波特率发生器
- bt=1; //修改波特率标志,防止多次修改
- btln++;
- TR0=1; //启动定时器计时
- }
- if(btln==6&&bt==0)
- {
- btx=1; //2轮波特率切换仍未出现xz标志,7F检测失败
- }
- }
- if(RTS==1) //扫描串口是否关闭
- {
- delay(1);
- if(RTS==1)
- {
- ck=0;
- dispkaiguan(4,80,ck); //识别到串口关闭,更新显示
- disp_nn(6,75);
- disp_nn(6,110);
- disp_nn(6,0);
- disp_nn(6,16); //________
- led=1;
- j=0; //清除7F次数记录
- xz=0; //清除下载标志
- TR1=0;
- TL1 = 0x00; //设置定时初始值
- TH1 = 0x4C; //设置定时初始值
- t1=0; //清除定时器1计数
- t=0; //清除定时器0计数
- bt=0; //清除波特率设置标志
- btln=0; //清除波特率设置次数
- btx=0; //清除7F检测失败标志
- delay(2); //等待数毫秒,清除下载机上传的数据
- RI=0;
- }
- }
- if(RI&&btx==0&&xz==0) //扫描是否有数据,且没有7F检测失败标志 下载标志也没有被激活
- {
- dat=SBUF;
- RI=0;
- dispnum2(6,75,1,dat); //数据显示
- if(dat==0x7f)
- {
- j++;
- dispnum2(6,110,0,j);
- if(j>34) //连续35个7F,识别为下载数据流
- {
- j=34;
- xz=1; //设立下载标志
- TR0=0; //关闭定时器0
- TL0 = 0x00; //恢复定时初始值以便下次使用
- TH0 = 0x4C;
- if(ms==0) //自动模式才对继电器控制,手动模式不予处理
- {
- if(dy) //电源选择为INT
- {
- if(dc) //如果供电在开启状态,先断电再给电实现冷启动
- {
- J1=1;
- dc=0;
- dispkaiguan(2,80,dc);
- delay(1500);
- J1=0;
- dc=1;
- dispkaiguan(2,80,dc);
- TR1=1; //启动定时器1;
- }
- else //如果供电在关闭状态,直接给电即可实现冷启动
- {
- J1=0;
- dc=1;
- dispkaiguan(2,80,dc);
- TR1=1;
- }
- }
- else //电源选择为USB
- {
- if(dc)
- {
- J2=1;
- dc=0;
- dispkaiguan(2,80,dc);
- delay(1500);
- J2=0;
- dc=1;
- dispkaiguan(2,80,dc);
- TR1=1; //启动定时器1;
- }
- else
- {
- J2=0;
- dc=1;
- dispkaiguan(2,80,dc);
- TR1=1; //启动定时器1;
- }
- }
- }
- }
- }
- else
- {
- j=0;
- dispnum2(6,110,0,j);
- }
- }
- }
- if(k1==0&&key==0)
- {
- delay(5);
- if(k1==0)
- {
- key=1;
- tt=1000;
- while(tt>0&&k1==0)
- {
- tt--;
- delay(1);
- }
- if(tt==0) //1S以上为长按
- {
- ms=~ms; //长按为模式切换
- displayshouzi(0,80,ms);
- }
- else //1S以内为短按 电源开/关
- {
- dc=~dc;
- if(dy==1) //选择为INT
- {
- J1=~dc; //继电器与dc状态相反
- }
- else
- {
- J2=~dc;
- }
- dispkaiguan(2,80,dc);
- }
- }
- }
- if(k2==0&&key==0)
- {
- delay(5);
- if(k2==0)
- {
- key=1;
- J1=J2=J3=J4=1; //关闭所有供电继电器
- dispkaiguan(2,80,0); //电源显示状态为关闭
- dy=~dy;
- dispvcc(2,0,dy);
- DELETE_IAP(0x0000);
- if(dy==0)
- {
- WRITE_IAP(0X0000,0x01); //写入USB供电存储数据
- J4=0;
- }
- else
- {
- WRITE_IAP(0X0000,0x10); //写入INT供电存储数据
- J3=0;
- }
- if(dc==1) //电源状态为开启时,恢复继电器
- {
- delay(500);
- if(dy==1) //电源选择为INT
- {
- J1=0; //开启继电器1
- }
- else //电源选择为USB
- {
- J2=0; //开启继电器2
- }
- dispkaiguan(2,80,dc); //电源显示状态为关闭
- }
- }
- }
- if(k3==0&&key==0) //备用按键
- {
- delay(5);
- if(k3==0)
- {
- qingping( );
- dispcunchucuowu( );
- DELETE_IAP(0x0000); //擦除扇区
- WRITE_IAP(0X0000,0xFB);
- while(1);
- }
- }
- if(key)
- {
- if(k1==1&&k2==1&&k3==1)
- {
- delay(5);
- if(k1==1&&k2==1&&k3==1)
- {
- key=0;
- }
- }
- }
- }
- }
- void timeraa( ) interrupt 1
- {
- TL1 = 0x00; //设置定时初始值
- TH1 = 0x4C; //设置定时初始值
- t++;
- if(t==20)
- {
- t=0;
- TR0=0; //关闭定时器
- bt=0;
- }
- }
- void timerbb( ) interrupt 3
- {
- TL1 = 0x00; //设置定时初始值
- TH1 = 0x4C; //设置定时初始值
- t1++;
- if(t1==4)
- {
- t1=0;
- led=~led;
- }
- }
复制代码
|