mplab x ide 搭配xc8 ,给PIC16F1787芯片驱动12864LCD液晶屏显示一串数字结果只亮屏没有任何显示,求教各位是何原因?
程序如下:
#include <xc.h>
// CONFIG1
#pragma config FOSC = XT // Oscillator Selection (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable bit (Vcap functionality is disabled on RA6.)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF // Low Power Brown-Out Reset Enable Bit (Low power brown-out is disabled)
#pragma config LVP = ON
#define rs RC0 //定义LCD的数据/命令控制口
#define rw RC1 //定义LCD的读/写控制口
#define e RC2 //定义LCD的使能口
#define psb RB0 //定义通信方式控制口
#define rst RB1 //定义复位口
#define nop() asm("nop") //
const unsigned char TAB1A[ ] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned int lcd_x; //定义LCD页地址寄存器
unsigned int lcd_y; //定义LCD列地址寄存器
bit busy; //定义LCD忙标志位
void adc(); //ad端口初始化
void getad(); //ad转换
void keyscan(); //按键扫描
void init(); //单片机端口初始化
void lcd_init(); //lcd初始化
void send_d(unsigned char x); //申明送一字节数据函数
void send_i(unsigned char x); //申明送一字节控制命令函数
void chk_busy(); //申明检测LCD是否工作繁忙函数
void delayms(unsigned int n); //申明ms 延时函数
void delay(unsigned int m); //申明延时函数
void writelcd(const unsigned char *pt); //声明LCD写字符串函数
void main()
{
init(); //I/O初始化
while (1)
{
lcd_init(); //lcd初始化
delay(100);
send_i(0x81);
writelcd(TAB1A); //写字符串
delayms(100);
}
}
void writelcd( const unsigned char *pt) //写字符串
{
while(*pt)
send_d(*pt++);
}
void init() //I/0口初始化
{
TRISA = 0X00;
ANSELA = 0X00;
LATA = 0X10;
TRISB = 0X00;
ANSELB = 0X00;
TRISD = 0X00;
ANSELD = 0X00;
}
void lcd_init() //LCD初始化
{
nop();
delay(40);
psb = 1; //选择并行通信
send_i(0x30); //8位接口,基本指令
delay(100);
send_i(0x01); //清屏显示
delay(100);
send_i(0x0C); //显示开,关光标,不闪烁
delay(100);
send_i(0x06); // 光标从右向左加1位移动
}
void send_d(unsigned char x) //发送一字节数据
{
chk_busy();
rs = 1;
rw = 0;
delay(1);
PORTD = x;
e = 1;
delay(1);
e = 0;
}
void send_i(unsigned char x) //发送一字节指令
{
chk_busy();
rs = 0;
rw = 0;
delay(1);
PORTD = x;
e = 1;
delay(1);
e = 0;
}
void chk_busy() //忙检测函数
{
busy = 1;
TRISD = 0XFF;
rs = 0;
rw = 1;
while (busy)
{
nop();nop();nop();
e = 1;
nop();nop();nop();
if (!RD7) busy = 0;
nop();nop();nop();
e = 0;
}
e = 0;
LATD = 0X00;
}
void delayms(unsigned int n) //延时函数
{
unsigned int i,j;
for (i = 0;i < n;i++)
for (j=0;j<2000;j++);
}
void delay(unsigned int m ) //延时函数
{
unsigned int i,j;
for (i = 0;i < m;i++)
for (j=0;j<10;j++);
} |