- UID
- 800392
|
#include <hidef.h> /* common defines and macros */
#include <mc9s12xdp512.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"
#define uchar unsigned char
#define uint unsigned int
#define nop _asm(nop)
#define CS PORTA_PA0
#define SID PORTA_PA1
#define SCLK PORTA_PA2
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
nop;
}
void Delay(word ms)
{
word a;
while(ms)
{
a=1335;
while(a--);
ms--;
}
return;
}
/********************************************************************
函 数 名:Write_8bits
入口参数:W_bits
出口参数:无
建立日期:
修改日期:
函数作用:负责串行输出8个bit位
说 明:
********************************************************************/
void Write_8bits(uint a)
{
char i,d;
for(i=0;i<8;i++)
{
CS=1;
SCLK=0;
delay_nus(600);
d=a&0x80;
if(d)
SID=1;
else
SID=0;
a<<=1;
delay_nus(600);
SCLK=1;
CS=0;
}
}
/*******************************************************************
函 数 名:W_1byte
入口参数:RW、RS、W_data
出口参数:无
建立日期:
修改日期:
函数作用:写一个字节的数据到12864液晶,包括指令和数据
说 明:RW=1,从液晶读数据到MCU;RW=0,写一个数据到液晶;
(一般RW都设为0,即只向液晶写数据,不读数据)
RS=1,写入的是数据;RS=0,写入的是指令;
一般模式:RW=0,RS=1;写数据
RW=0,RS=0;写指令
********************************************************************/
void W_1byte(uchar RW, uchar RS, uchar W_data)
{
uint H_data,L_data,S_ID = 0xf8; //11111RWRS0
if(RW == 0)
{
S_ID &=~ 0x04;
}
else //if(RW==1)
{
S_ID |= 0X04;
}
if(RS == 0)
{
S_ID &=~ 0x02;
}
else //if(RS==1)
{
S_ID |= 0X02;
}
H_data = W_data;
H_data &= 0xf0; //屏蔽低4位的数据
L_data = W_data; //xxxx0000格式
L_data &= 0x0f; //屏蔽高4位的数据
L_data <<= 4; //xxxx0000格式
CS = 1;
Write_8bits(S_ID);
Write_8bits(H_data); //发送H_data
Write_8bits(L_data); //发送L_data
CS = 0;
}
void setbusclock(void)
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR=1;
REFDV=1; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz;
nop; //BUS CLOCK=16M
nop;
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
void LCD_Init(void)
{
uchar cmd;
cmd=0x30; //功能设置 8位数据,基本指令
W_1byte(0,0,cmd);
Delay(1);
cmd=0x0C; //显示状态 ON,游标OFF,反白OFF
W_1byte(0,0,cmd); //写指令
Delay(1);
cmd=0x02;
W_1byte(0,0,cmd);
Delay(1);
cmd=0x01; //清除显示
W_1byte(0,0,cmd); //写指令
Delay(1);
cmd=0x80; //设置DDRAM地址
W_1byte(0,0,cmd); //写指令
Delay(1);
//延时
}
void Port_Init(void)
{
DDRA = 0xff;
PORTA= 0x00;
SCLK=0;
CS=0;
Delay(10);
}
void Char_Set_XY(uchar x, uchar y, char *p)
{
if(x == 0)
{
W_1byte(0,0,(0x80+y));
}
if(x == 1)
{
W_1byte(0,0,(0x90+y));
}
if(x == 2)
{
W_1byte(0,0,(0x88+y));
}
if(x == 3)
{
W_1byte(0,0,(0x98+y));
}
while(*p != 0)
{
W_1byte(0,1,*p++);
} Delay(1);
}
void main(void)
{
/* put your own code here */
EnableInterrupts;
setbusclock();
Port_Init();
LCD_Init(); //清除显示
for(;;)
{
Char_Set_XY(0,0,"FreescaleS12");
Char_Set_XY(1,1,"matlab");
Char_Set_XY(2,0,"Please wait!");
Char_Set_XY(3,4,"Ver 1.0");
}
} /* wait forever */
/* please make sure that you never leave this function */
串行发送 复位时不能整屏快速显示 只能逐行显出
void Write_8bits(uint a)
{
char i,d;
for(i=0;i<8;i++)
{
CS=1;
SCLK=0;
delay_nus(600);
d=a&0x80;
if(d)
SID=1;
else
SID=0;
a<<=1;
delay_nus(600);
SCLK=1;
CS=0;
}
}
此处延时减小马上变成乱码
如何解决呢 大家帮忙忙看看呢 谢谢! |
|