金牌会员
- 积分
- 2048
- 威望
- 1302
- 贡献
- 390
- 兑换币
- 8
- 注册时间
- 2011-2-25
- 在线时间
- 178 小时
|
说明:这是调试液晶模块的程序,在51上可以显示,但用XS128就不能显示出来,麻烦各位高手帮忙看看,急啊!!
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define uint unsigned int
#define uchar unsigned char
#define comm 0
#define dat 1
#define rs PORTB_PB1 //H=data; L=command;//RS为并行的指令/数据选择信号;串行的片选信号
#define rw PORTB_PB3 //H=read; L=write;
#define e PORTB_PB5 //input enable;
#define psb PORTB_PB6 //H=并口; L=串口;
#define rst PORTB_PB7 //Reset Signal 低电平有效
#define busy PORTA_PA7 //读忙碌位
/*数字的ASCII码*/
uchar shuzi_table[]={0x30+0,0x30+1,0x30+2,0x30+3,0x30+4,0x30+5,0x30+6,0x30+7,0x30+8,0x30+9};
void wr_lcd (uchar dat_comm,uchar content);
void chk_busy (void);
void delay (uint us);
void Disp_SZ(uchar adr,word shuzi);
uchar tab1[]={"脉冲频率:"};
uchar tab2[]={"HZ"};
void SetBusCLK_16M(void)
{
CLKSEL=0X00; // disengage PLL to system
PLLCTL_PLLON=1; // turn on PLL
SYNR=0x00 | 0x01; // VCOFRQ[7:6];SYNDIV[5:0]
// fVCO= 2*fOSC*(SYNDIV + 1)/(REFDIV + 1)
// fPLL= fVCO/(2 × POSTDIV)
// fBUS= fPLL/2
// VCOCLK Frequency Ranges VCOFRQ[7:6]
// 32MHz <= fVCO <= 48MHz 00
// 48MHz < fVCO <= 80MHz 01
// Reserved 10
// 80MHz < fVCO <= 120MHz 11
REFDV=0x80 | 0x01; // REFFRQ[7:6];REFDIV[5:0]
// fREF=fOSC/(REFDIV + 1)
// REFCLK Frequency Ranges REFFRQ[7:6]
// 1MHz <= fREF <= 2MHz 00
// 2MHz < fREF <= 6MHz 01
// 6MHz < fREF <= 12MHz 10
// fREF > 12MHz 11
// pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
POSTDIV=0x00; // 4:0, fPLL= fVCO/(2xPOSTDIV)
// If POSTDIV = $00 then fPLL is identical to fVCO (divide by one).
_asm(nop); // BUS CLOCK=16M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
/*******IO初始化*******/
void initIOBoutput(void)//IO口初始化,B口为输出
{
DDRB=0XFF;
PORTB=0XFF;
}
void initIOAoutput(void)//IO口初始化,A口为输出
{
DDRA=0XFF;
PORTA=0XFF;
}
/*------------------初始化-----------------*/
void init_lcd(void)
{
rst=1;
psb=1;
wr_lcd (comm,0x30); /*30---基本指令动作*/
wr_lcd (comm,0x01); /*清屏,地址指针指向00H*/
wr_lcd (comm,0x06); /*光标的移动方向*/
wr_lcd (comm,0x0c); /*开显示,关游标*/
}
/*-----------上半屏显示汉字或字符------------*/ //上半行即为第一行和第二行,其中第一行的起始位置是0x80,第二行的起始位置是0x90
void chn_disp1 (uchar *chn)
{
uchar i,j;
wr_lcd (comm,0x30);
wr_lcd (comm,0x80);
j=0;
for (i=0;i<10;i++) //10是字符的数
wr_lcd (dat,chn[j*16+i]);
}
void chn_disp2 (uchar *chn)
{
uchar i,j;
wr_lcd (comm,0x30);
wr_lcd (comm,0x8E);
j=0;
for (i=0;i<2;i++)
wr_lcd (dat,chn[j*16+i]);
}
/************显示一个4位的十进制数字*****************/
void Disp_SZ(uchar adr,word shuzi)
{
word tmp1,tmp2,tmp3,tmp4;
tmp4 = shuzi%10000/1000;
tmp3 = shuzi%1000/100;
tmp2 = shuzi%100/10;
tmp1 = shuzi%10;
wr_lcd (comm,0x30);
wr_lcd (comm,adr);
if(tmp4==0)
{
wr_lcd (comm,0x30);
if(tmp3==0)
{
wr_lcd (comm,0x30);
if(tmp2==0)
{
wr_lcd (dat,0x30);
wr_lcd (dat,shuzi_table[tmp1]);
}
else
{
wr_lcd (dat,shuzi_table[tmp2]);
wr_lcd (dat,shuzi_table[tmp1]);
}
}
else
{
wr_lcd (dat,shuzi_table[tmp3]);
wr_lcd (dat,shuzi_table[tmp2]);
wr_lcd (dat,shuzi_table[tmp1]);
}
}
else
{
wr_lcd (dat,shuzi_table[tmp4]);
wr_lcd (dat,shuzi_table[tmp3]);
wr_lcd (dat,shuzi_table[tmp2]);
wr_lcd (dat,shuzi_table[tmp1]);
}
}
/*--------------清DDRAM------------------*/
void clrram (void)
{
wr_lcd (comm,0x30);
wr_lcd (comm,0x01);
}
/*********读忙操作***********/
void chk_busy (void)
{
PORTA=0xff;//??
rs=0;
rw=1;
e =1;
while(busy==1);
e =0;// e的改变是由其时序图决定的
}
/*------------------写指令或写数据---------------------*/
void wr_lcd (uchar dat_comm,uchar content)
{
chk_busy ();
if(dat_comm)
{
rs=1; //data
rw=0; //write 写指令
}
else
{
rs=0; //command
rw=0; //write 发送指令
}
PORTA=content; //output data or comm
e=1;
delay (100);
e=0; //e的决定是有其时序图决定的
}
void delay (uint us) //delay time
{
while(us--);
}
void delay1 (uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<15;j++)
delay (1);
}
/*********主函数*****/
void main()
{
word a =99;
/* put your own code here */
// DisableInterrupts;
SetBusCLK_16M();
initIOBoutput();
initIOAoutput();
init_lcd();
// EnableInterrupts;
for(;;)
{
// PORTB_PB0=~PORTB_PB0;
delay1 (4000);
chn_disp1 (tab1);
delay1 (4000);
Disp_SZ(0x8A,a);
delay1 (4000);
chn_disp2 (tab2);
delay1 (4000);
clrram();
a++;
// _FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
} |
|