用IIC接口同时驱动OLED和16位AD实例

2020-02-07 09:36发布

近日做的,试验电路。两个从机在同一IIC口上,以地址区分。MCP3425的资料可从MCHP官网取得。SSD1306驱动OLED的资料可从网络下载,初始化命令序列来自OLED模块供应商的资料。原OLED模块给的是SPI口,为取得从机响应信号而改为IIC。

电路简单,只贴照片。其中屏上数字是MCP3425读数。 (原文件名:110928_mcp3425_oled.jpg)

程序。字库也来自供应商资料。屏幕显示的小字用的8*6字库,大字用的16*8字库。ourdev_683743F5CKAO.rar(文件大小:6K) (原文件名:110928_oled_mcp3425.rar)

源程序原来的扩展名是.C,所以只能传压缩后的。现在补充上字符版本:

/*-------------------------------------------------------------------
编译器:MCC18 V3.40
日期:2011 0928            
版本:V1.0
龙丘110918串行12864oled模块,ssd1306芯片
8*6码表,1608ascii码表,保留原开发者信息
mcp3425与OLED共用iic接口
-------------------------------------------------------------------*/
#include <P18f26j13.h>
#include <stdlib.h>

#define Key PORTCbits.RC2
#define SA0 LATBbits.LATB3                //OLED模块IIC 地址选择指示
#define ORESET LATBbits.LATB0                //oled清0

//**********************外部引用函数******************************
void main (void);
void InterruptHandlerHigh (void);
void LCD_WrCmd(unsigned char);
void LCD_WrDat(unsigned char);
void LCD_Init(void);        
void LCD_Set_Pos(unsigned char, unsigned char);
void LCD_Fill(unsigned char);
void LCD_P6x8Str(unsigned char,unsigned char,unsigned char*);
void LCD_PutPixel(unsigned char,unsigned char);
void LCD_P8x16Str(unsigned char,unsigned char,unsigned char*);
void iic_start(void);
void iic_rstart(void);
void iic_stop(void);
void iic_write(unsigned char);
unsigned char iic_read(unsigned char);

#define X_WIDTH 128
#define Y_WIDTH 64
//======================================================
// 128X64I液晶底层驱动[8X16]字体库
// 设计者: powerint
// 描  述: [8X16]西文字符的字模数据 (纵向取模,字节倒序)
// !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~
//======================================================
rom unsigned char F8X16[][16] =
{
  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,0x02,0x02,0x04,0x00,0x00,0x00,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
};

rom unsigned char F6x8[][6] =
{
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   // sp
    { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },   // !
    { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
    { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #
    { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $
    { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },   // %
    { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },   // &
    { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },   // '
    { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (
    { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )
    { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *
    { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +
    { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 },   // ,
    { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },   // -
    { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },   // .
    { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },   // /
    { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0
    { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1
    { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2
    { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3
    { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4
    { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5
    { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6
    { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7
    { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8
    { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9
    { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },   // :
    { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;
    { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },   // <
    { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },   // =
    { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },   // >
    { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?
    { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E },   // @
    { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C },   // A
    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B
    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C
    { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D
    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E
    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F
    { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G

    { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H
    { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I
    { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J
    { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K
    { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L
    { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M
    { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N
    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O
    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P
    { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q
    { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R
    { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },   // S
    { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T
    { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U
    { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V
    { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W
    { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },   // X
    { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y
    { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z
    { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [
    { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // 55
    { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]
    { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^
    { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },   // _
    { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },   // '
    { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },   // a
    { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b
    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },   // c
    { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F },   // d
    { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },   // e
    { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f
    { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C },   // g
    { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h
    { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i
    { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 },   // j
    { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k
    { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l
    { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m
    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n
    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },   // o
    { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 },   // p
    { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC },   // q
    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r
    { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },   // s
    { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t
    { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u
    { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v
    { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w
    { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },   // x
    { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C },   // y
    { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 },   // z
    { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }    // horiz lines
};
rom unsigned char *ascii_rom_ptr;
unsigned char Rtimehi[4]={0x0,0x09,0x1,0x54},Rtimelo[4]={0x11,0x11,0x15,0x30};
unsigned int ackcount=0;
unsigned char tttry[]={'T','h','i','s',' ','i','s',' ','a',' ','t','e','s','t',};
static char second_ascii[9]={0,0,0,0,0,0,0,0,0};
char binary_second;

unsigned char mcp3425_test;

//***************主函数*****************
void main()
{
unsigned char i,j;
//*************************RTCC初始化*******************
T1GCON=0;
T1CON=0X8B;                                //允许tmr1的振荡器
RTCCFG=0X07;
RTCCAL=0X0;                                //校准值
PADCFG1=0X02;                        //输出秒时钟
//PADCFG1=0X0;                        //不输出秒时钟
_asm  movlw 0x55 _endasm
_asm movwf EECON2,0 _endasm
_asm movlw 0xAA _endasm
_asm  movwf EECON2,0 _endasm
_asm  bsf RTCCFG,5,1 _endasm        //开启写入
RTCCFGbits.RTCEN=1;
//********************OLED初始化*******************
TRISBbits.TRISB0=0;                //oled reset on this pin
TRISBbits.TRISB3=0;                //oled data/command selecting on this pin
TRISCbits.TRISC3=1;                //SSP1SCL,IIC模块要求
TRISCbits.TRISC4=1;                //SSP1SDA,IIC模块要求
PIE1bits.SSP1IE=0;
ODCON3bits.SPI1OD=1;        //spi1集电极开路打开

//以下按照iic模式初始化
SSP1STAT=0X80;                //100k/1M标准速率模式
SSP1CON1=0X28;                //IIC主模式,FOSC/(4 * (SSPxADD + 1))时钟,IIC允许
SSP1CON2=0;                        //清除所有通信标示
SSP1ADD=79;                        //8m 4Xpll 100k速率
IPR1bits.SSP1IP=1;        //高优先级
LCD_Init();

//****************读rtcc****************
RTCCFG|=0x03;                //置指针为0b11
while (RTCCFGbits.RTCSYNC);        //等待不忙
for (i=0;i<4;i++)        //读
{
Rtimelo=RTCVALL;
Rtimehi=RTCVALH;
}
//****************TIMERS*******************
T0CON=0X84;                                //定时器, 分频
INTCONbits.T0IE=1;

T5CON=0X3F;                                //1/4Fosc,1:8 prescale,T1OSC, 16 bits WR,enabled
T5GCON=0;                                //no gate
PIE5bits.TMR5IE=1;

INTCONbits.PEIE=1;
INTCONbits.GIE=1;

//////////////////写MCP3425//////////////////////
iic_start();
iic_write(0xD2);                //ADRRESS OF MCP3425
if (SSP1CON2bits.ACKSTAT)
second_ascii[8]+=1;                        //如果oled iic不响应则监测+1
iic_write(0xf7);                //MCP3425连续读,8X,14位
if (SSP1CON2bits.ACKSTAT)
second_ascii[8]+=1;                        //如果oled iic不响应则监测+1
iic_stop();

SA0=0;                                                //OLED的iic地址选为0
LCD_P8x16Str(2,0,tttry);
LCD_P6x8Str(8,2,tttry);
LCD_Set_Pos(0,5);

//**************************主循环************************
while(1)
{
RTCCFG|=0x03;                //置指针为0b11
Rtimelo[0]=RTCVALL;                //读年
Rtimehi[0]=RTCVALH;                //读空
Rtimelo[1]=RTCVALL;                //读日
Rtimehi[1]=RTCVALH;                //读月
Rtimelo[2]=RTCVALL;                //读小时
Rtimehi[2]=RTCVALH;                //读星期
Rtimelo[3]=RTCVALL;                //读秒
Rtimehi[3]=RTCVALH;                //读分
//////////////////读MCP3425//////////////////////
iic_start();
iic_write(0xD3);                //ADRRESS OF MCP3425
if (SSP1CON2bits.ACKSTAT)
second_ascii[8]+=1;                        //如果oled iic不响应则监测+1
second_ascii[5]=iic_read(0);        //收前2字节,给ack
second_ascii[6]=iic_read(0);
second_ascii[7]=iic_read(1);        //读,给NACK
iic_stop();
mcp3425_test=second_ascii[6];                        //读到的内容test
second_ascii[0]=mcp3425_test%1000/100+48;                //mcp3425读数低位转换为三位ascii码
second_ascii[1]=mcp3425_test%100/10+48;
second_ascii[2]=mcp3425_test%10+48;

LCD_P8x16Str(0,5,second_ascii);

//到达MAIN最后部分

}
}

//----------------------------------------------------------------------------
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh                 //jump to interrupt routine
  _endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh

void
InterruptHandlerHigh ()
{
/*
  if (PIR1bits.SSP1IF)
          PIR1bits.SSP1IF=0;
  else
*/
  if (INTCONbits.TMR0IF)
    {                                         //check for TMR0 overflow
      INTCONbits.TMR0IF = 0;        //clear interrupt flag
TMR0H=0X20;                                                        //加快点划速率
TMR0L=0;

   }
  else if (PIR3bits.TMR4IF)
        {
        PIR3bits.TMR4IF=0;                                //清除中断标志
        }
  else if (PIR1bits.TMR2IF)
        {
    PIR1bits.TMR2IF = 0;                //clear interrupt flag
        }
  else if (PIR5bits.TMR5IF)
        {
    PIR5bits.TMR5IF = 0;                //clear interrupt flag
        }
  else if (PIR1bits.ADIF)
        {
    PIR1bits.ADIF = 0;                        //clear interrupt flag
        }
else if (PIR1bits.TMR1IF)
        {
    PIR1bits.TMR1IF = 0;                //clear interrupt flag
        }
}

/////////////////iic子程序//////////////
void iic_start()
{
SSP1CON2bits.SEN = 1;                //SET SEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
}
void iic_rstart()                        //再启动
{
SSP1CON2bits.RSEN = 1;                //SET RSEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
}
void iic_stop()
{
SSP1CON2bits.PEN = 1;                //SET PEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
}
void iic_write(unsigned char thebyte)
{
SSP1BUF = thebyte;                //put in the byte
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
}
unsigned char iic_read(unsigned char NotACK)
{
unsigned char gotiicbyte;
SSP1CON2bits.ACKDT=NotACK;        //字节接收结束后,该位为0则继续接收,为1则停止接收
SSP1CON2bits.RCEN = 1;                //SET RCEN
while (SSP1CON2bits.RCEN);                //WAIT receiving ends
SSP1CON2bits.ACKEN=1;
while (SSP1CON2bits.ACKEN);                //WAIT SLAVE GOT THE ACK/NACK FLAG
gotiicbyte=SSP1BUF;                        //读到的内容
return gotiicbyte;
}

///////////OLED子程序///////////////////
void LCD_Set_Pos(unsigned char x, unsigned char y)
{
  LCD_WrCmd(0xb0+y);
  LCD_WrCmd(((x&0xf0)>>4)|0x10);
  LCD_WrCmd((x&0x0f)|0x01);
}
void LCD_WrDat(unsigned char data)
{
SA0=1;                                                //改变地址
SSP1CON2bits.SEN = 1;                //SET SEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1BUF = 0x7a;                //ADRRESS OF the oled
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1BUF = 0X40;                //控制字,c0=0,D/C=1,and six 0's
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1BUF = data;
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1CON2bits.PEN = 1;                //SET PEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
}
void LCD_WrCmd(unsigned char cmd)
{
SA0=0;                                                //改变地址为低
SSP1CON2bits.SEN = 1;                //SET SEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1BUF = 0x78;                //ADRRESS OF the oled
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
if (SSP1CON2bits.ACKSTAT)
ackcount+=1;                        //如果oled iic不响应则+1
SSP1BUF = 0X80;                //控制字,c0=1,D/C=0,and six 0's
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1BUF = cmd;
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
SSP1CON2bits.PEN = 1;                //SET PEN
while (!PIR1bits.SSP1IF);                //WAIT ACK FROM SLAVE
PIR1bits.SSP1IF =0;                        //CLEAR FLAG
}
void LCD_Init(void)        
{
unsigned char i,j;
SA0=0;                                //按电平定义OLED iic地址
ORESET=0;                        //OLED RESET
for (i=0;i<60;i++)        //delay
for (j=0;j<60;j++);
ORESET=1;                        //OLED RESET end
for (i=0;i<200;i++)        //delay
for (j=0;j<100;j++);
//以下初始化序列来自龙丘110918开发的51例程
iic_start();
iic_write(0x78);                //ADRRESS OF the oled
if (SSP1CON2bits.ACKSTAT)
ackcount+=1;                        //如果oled iic不响应则+1
iic_write(0X80);                //控制字,c0=1,D/C=0,and six 0's
iic_write(0XAE);                //关闭显示
iic_write(0X00);                //set low column address
iic_write(0X10);                //set high column address
iic_write(0X40);                //set start line address
iic_write(0X81);                //set contrast control register
iic_write(0Xdf);                // Current Brightness
iic_write(0Xa1);                //Set SEG/Column Mapping
iic_write(0Xc8);                //COM/Row Scan Direction
iic_write(0Xa6);                //oled 正常显示
iic_write(0Xa8);                //oled 驱动路数
iic_write(0X3f);                //oled 1/64
iic_write(0Xd3);                //set display offset
iic_write(0X00);                //not offset
iic_write(0Xd5);                //set display clock divide ratio/oscillator frequency
iic_write(0X80);                //set divide ratio, Set Clock as 100 Frames/Sec.该句发两遍,因为0x80恰巧是一个SSD1306保留字
iic_write(0X80);                //set divide ratio, Set Clock as 100 Frames/Sec
iic_write(0Xd9);                //oled Set Pre-charge Period
iic_write(0Xf1);                //oled 15:1
iic_write(0Xda);                //set com pins hardware configuration
iic_write(0X12);                //配置字
iic_write(0Xdb);                //oled VCOMH Deselect
iic_write(0X40);                //VCOM Deselect Level
iic_write(0X20);                //Set Page Addressing Mode
iic_write(0X02);                //页式
iic_write(0X8d);                //set Charge Pump enable/disable
iic_write(0X14);                //set(0x10) disable
iic_write(0Xa4);                //Disable Entire Display On (0xa4/0xa5)
iic_write(0Xa6);                //Disable Inverse Display On (0xa6/a7)
iic_write(0XAF);                //显示开
iic_stop();
LCD_Fill(0x00);  //初始清屏
LCD_Set_Pos(0,0);  
}
void LCD_Fill(unsigned char bmp_data)
{
        unsigned char y,x;
        for(y=0;y<8;y++)
        {
                LCD_WrCmd(0xb0+y);
                LCD_WrCmd(0x01);
                LCD_WrCmd(0x10);
                for(x=0;x<X_WIDTH;x++)
                LCD_WrDat(bmp_data);
        }
}
//==============================================================
//函数名:LCD_P6x8Str(byte x,byte y,byte *p)
//功能描述:写入一组标准ASCII字符串
//参数:显示的位置(x,y),y为页范围0~7,要显示的字符串
//返回:无
//==============================================================  
void LCD_P6x8Str(unsigned char x,unsigned char y,unsigned char ch[])
{
  unsigned char c=0,i=0,j=0;      
  while (ch[j]!='')
  {   
    c =ch[j]-32;
    if(x>126){x=0;y++;}
    LCD_Set_Pos(x,y);   
          ascii_rom_ptr=  &F6x8[c][0];  
          for(i=0;i<6;i++)
        {     
          LCD_WrDat(*ascii_rom_ptr);
        ascii_rom_ptr++;
        }
          x+=6;
          j++;
  }
}
void LCD_P8x16Str(unsigned char x,unsigned char y,unsigned char ch[])
{
  unsigned char c=0,i=0,j=0;
  while (ch[j]!='')
  {   
    c =ch[j]-32;
    if(x>120){x=0;y++;}
    LCD_Set_Pos(x,y);   
          ascii_rom_ptr=  &F8X16[c][0];  
          for(i=0;i<8;i++)     
            LCD_WrDat(*ascii_rom_ptr++);
          LCD_Set_Pos(x,y+1);   
          ascii_rom_ptr=  &F8X16[c][8];  
          for(i=0;i<8;i++)     
            LCD_WrDat(*ascii_rom_ptr++);  
          x+=8;
          j++;
  }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。