代码:
#include <xc.h>
#include "OLED.h"
//CONFIG1L芯片配置字
#pragma config RETEN = OFF //超低功耗稳压器禁止
#pragma config INTOSCSEL = HIGH //休眠期间 LF-INTOSC 于高功耗模式
#pragma config SOSCSEL = DIG //SOSC配置为( SCLKI)模式;使能 RC0 和 RC1 数字I/O
#pragma config XINST = OFF //扩展指令集使能位,禁止指令集扩展和变址寻址模式 (传统模式)
// CONFIG1H
#pragma config FOSC = HS1 //4 MHz - 16 MHz 外部晶振设置HS中等功耗振荡器,
#pragma config PLLCFG = ON //使能PLL x4功能,8M晶振经4倍频变为32MHz系统时钟
#pragma config FCMEN = OFF //禁止故障保护时钟监视器
#pragma config IESO = OFF //禁止双速启动
// CONFIG2L
#pragma config PWRTEN = OFF //禁止上电延时
#pragma config BOREN = SBORDIS //由硬件使能欠压复位
#pragma config BORV = 3 //设置电压为1.8v欠压复位
#pragma config BORPWR = ZPBORMV //选择ZPBORMV而不是BORMV
// CONFIG2H
#pragma config WDTEN = OFF //关看门狗
//#pragma config WDTEN = SWDTDIS //由硬件使能WDT,禁止SWDTEN位
#pragma config WDTPS = 256 //看门狗后分频1:256,溢出时间1.024s
// CONFIG3H
#pragma config CANMX = PORTB //CAN通信引脚设置到 RB2 和 RB3
#pragma config MSSPMSK = MSK7 //使能 7 位地址掩码模式
#pragma config MCLRE = ON //使能MCLR外部复位功能
// CONFIG4L
#pragma config STVREN = ON // 堆栈满/下溢复位
#pragma config BBSIZ = BB2K // 配置引导区大小为2K
// CONFIG5L
#pragma config CP0 = OFF //00800-01FFF代码不加密
#pragma config CP1 = OFF //02000-03FFF代码不加密
#pragma config CP2 = OFF //04000-05FFF代码不加密
#pragma config CP3 = OFF //06000-07FFF代码不加密
// CONFIG5H
#pragma config CPB = OFF //引导区代码不加密
#pragma config CPD = OFF //EEPROM数据不不加密
// CONFIG6L
#pragma config WRT0 = OFF // 00800-01FFF 写保护位不使能
#pragma config WRT1 = OFF // 02000-03FFF 写保护位不使能
#pragma config WRT2 = OFF // 04000-05FFF 写保护位不使能
#pragma config WRT3 = OFF // 06000-07FFF 写保护位不使能
// CONFIG6H
#pragma config WRTC = OFF //配置寄存器不受写保护
#pragma config WRTB = OFF //引导区不受写保护
#pragma config WRTD = OFF //EEPROM数据不受写保护
// CONFIG7L
#pragma config EBTR0 = OFF //00800-01FFF表读保护位不使能
#pragma config EBTR1 = OFF //02000-03FFF表读保护位不使能
#pragma config EBTR2 = OFF //04000-05FFF表读保护位不使能
#pragma config EBTR3 = OFF //06000-07FFF表读保护位不使能
// CONFIG7H
#pragma config EBTRB = OFF //引导区不受写保护
#define uint unsigned int
#define u8 unsigned char
#define LED1 LATDbits.LATD4 //定义LED1端口
#define LED2 LATEbits.LATE1 //定义LED2端口
#define uchar unsigned char
#define uint unsigned int
#define READ 0X80 //只包括前3个控制BIT--下同
#define EWEN 0X00
#define ERASE 0Xc0
#define WRITE 0X40
#define ERAL 0X00
#define WRAL 0X00
#define EWDS 0X00
#define cs LATCbits.LATC4
#define ck LATCbits.LATC5
#define di1 LATCbits.LATC6
#define d0 PORTCbits.RC7
#define readcode 0x0c
#define writecode 0x0a
//MS延时函数
void MS_delay(uint MS)
{
uint x,y;
for(y=MS;y>0;y--)
{
for(x=796;x>0;x--);
}
}
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
/*****************************************************
函数功能:延时若干毫秒
入口参数:n
***************************************************/
void delaynms(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
void delay(uchar n)
{
int i;
for(i=0;i<n;i++)
;
}
uchar readdata(uchar add)
{
uchar read_data,i;
cs=1;
delay(4);
di1=1; // 1
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
di1=1; //0
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
for(i=0;i<8;i++)
{
if(add&0x80)
di1=1;
else
di1=0;
delay(4);
ck=1;
delay(4);
add<<=1;
ck=0;
delay(4);
}
for(i=0;i<8;i++)
{
read_data<<=1;
ck=1;
delay(4);
ck=0;
delay(4);
//d0=1;
if(d0)
read_data|=1;
delay(4);
}
cs=0;
return(read_data);
}
void writedata(uchar add,uchar write_data)
{
uchar i;
cs=1;
delay(4);
di1=1; // 1
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
di1=0; //0
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
for(i=0;i<8;i++)
{
if(add&0x80)
di1=1;
else
di1=0;
delay(4);
ck=1;
delay(4);
add<<=1;
ck=0;
delay(4);
}
for(i=0;i<8;i++)
{
if(write_data&0x80)
di1=1;
else
di1=0;
delay(4);
ck=1;
delay(4);
write_data<<=1;
ck=0;
delay(4);
}
cs=0;
}
void EW_enable(void)
{
uchar i;
cs=1;
delay(4);
di1=1; //1
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
di1=0; //0
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
di1=0; //0
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
di1=1; //1
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
di1=1; //1
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
for(i=0;i<5;i++)
{
di1=1; //1
delay(4);
ck=1;
delay(4);
ck=0;
delay(4);
}
delay(4);
cs=0;
}
void main(void)
{
uint l;
uchar address;
uchar bai,shi,ge,i,j,k;
ANCON0bits.ANSEL6=0; //RE1(AN6)配置为数字IO
ANCON1=0x00; //将部分带模拟输入功能的引脚设置为数字IO
//输出使用LAT寄存器,输入使用PORT寄存器
TRISDbits.TRISD4=0; //PORD4配置为输出
TRISEbits.TRISE1=0; //PORE1配置为输出
TRISC=0X80;
LCD_Init();
LCD_CLS();
EW_enable();
writedata(0x80,55);
for(l=0;l<10000;l++);
writedata(0x81,66);
for(l=0;l<10000;l++);
writedata(0x82,196);
for(l=0;l<10000;l++);
i=readdata(1);
//i=READ1(address); /*把地址为address的内容读出并存放在dat中*/
bai=i/100+48;
shi=i%100/10+48;
ge=i%10+48;
LCD_P8x16Char(0,0,bai);
LCD_P8x16Char(8,0,shi);
LCD_P8x16Char(16,0,ge);
address=2;
j=readdata(2); /*把地址为address的内容读出并存放在dat中*/
bai=j/100+48;
shi=j%100/10+48;
ge=j%10+48;
LCD_P8x16Char(0,2,bai);
LCD_P8x16Char(8,2,shi);
LCD_P8x16Char(16,2,ge);
address=0;
k=0;
k=readdata(0); /*把地址为address的内容读出并存放在dat中*/
//nop;
//nop;
//EW_disable();
bai=k/100+48;
shi=k%100/10+48;
ge=k%10+48;
LCD_P8x16Char(0,4,bai);
LCD_P8x16Char(8,4,shi);
LCD_P8x16Char(16,4,ge);
while(1)
{
LED1=0; LED2=1; //点亮LED1
MS_delay(500);
LED1=1; LED2=0; //点亮LED2
MS_delay(500);
}
}
效果图:
|