標題:
12864m 51單片機串行C程序
[打印本頁]
作者:
huxiao
時間:
2015-1-24 16:02
標題:
12864m 51單片機串行C程序
大家可以做個實驗,本人正在做12864m的萬年歷,做好后給大家發(fā)上來,準備用串行通訊,這樣可以少焊8根線,我是個大懶蛋,哈哈。
#include <reg51.h>
#include <string.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
//====================================================================
//函數(shù)聲明
void Delay(uint ms); //延時子程序
void W_1byte(uchar RW, uchar RS, uchar W_data);
void Write_8bits(uint W_bits);
void LCD_Init(void);
sbit CS=P2^0; //CS=RS
sbit SID=P2^1; //RW=SID
sbit SCLK=P2^2; //E=SCLK
sbit PSB=P2^3; //E=SCLK
//===================================================================
code uchar mynew1[]={"歡迎你使用本產(chǎn)品"};
code uchar mynew2[]={"努力學(xué)習(xí)成為高手"};
code uchar mynew3[]={"為你提供技術(shù)支持"};
code uchar mynew4[]={" QQ:15910380 "};
void main()
{
uchar i=0;
SP = 0X60;
CS = 0;
SCLK = 0;
SID = 0;
PSB=0;
LCD_Init();
while(1)
{
W_1byte(0,0,0x80);
_nop_();
for(i=0;mynew1[i]!='\0';i++)
{
W_1byte(0,1,mynew1[i]);
}
W_1byte(0,0,0x90);
for(i=0;mynew2[i]!='\0';i++)
{
W_1byte(0,1,mynew2[i]);
}
W_1byte(0,0,0x88);
for(i=0;mynew3[i]!='\0';i++)
{
W_1byte(0,1,mynew3[i]);
}
W_1byte(0,0,0x98);
for(i=0;mynew4[i]!='\0';i++)
{
W_1byte(0,1,mynew4[i]);
}
}
}
/******************************************************************/
void LCD_Init(void)
{
uchar cmd;
cmd=0x30; //功能設(shè)置 8位數(shù)據(jù),基本指令
W_1byte(0,0,cmd);
Delay(2);
cmd=0x0C; //顯示狀態(tài) ON,游標OFF,反白OFF
W_1byte(0,0,cmd); //寫指令
Delay(2);
cmd=0x01; //清除顯示
W_1byte(0,0,cmd); //寫指令
Delay(2);
cmd=0x02; //地址歸位
W_1byte(0,0,cmd); //寫指令
Delay(2);
cmd=0x80; //設(shè)置DDRAM地址
W_1byte(0,0,cmd); //寫指令
Delay(2); //延時
}
/*******************************************************************
函數(shù)作用:寫一個字節(jié)的數(shù)據(jù)到12864液晶,包括指令和數(shù)據(jù)
說 明:RW=1,從液晶讀數(shù)據(jù)到MCU;RW=0,寫一個數(shù)據(jù)到液晶;
(一般RW都設(shè)為0,即只向液晶寫數(shù)據(jù),不讀數(shù)據(jù))
RS="1",寫入的是數(shù)據(jù);RS=0,寫入的是指令;
一般模式:RW=0,RS=1;寫數(shù)據(jù)
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位的數(shù)據(jù)
L_data = W_data; //xxxx0000格式
L_data &= 0x0f; //屏蔽高4位的數(shù)據(jù)
L_data <<= 4; //xxxx0000格式
CS = 1;
Write_8bits(S_ID); //發(fā)送S_ID
Write_8bits(H_data); //發(fā)送H_data
Write_8bits(L_data); //發(fā)送L_data
CS = 0;
}
/********************************************************************
函數(shù)作用:負責(zé)串行輸出8個bit位
********************************************************************/
void Write_8bits(uint W_bits)
{
uint i,Temp_data;
for(i=0; i<8; i++)
{
Temp_data = W_bits;
Temp_data <<= i;
if((Temp_data&0x80)==0) //bit7 is zero
{
SID = 0;
_nop_();
SCLK = 1;
_nop_();
_nop_();
SCLK = 0;
_nop_();
SID = 0;
}
else //bit7 is one
{
SID = 1;
_nop_();
SCLK = 1;
_nop_();
_nop_();
SCLK = 0;
_nop_();
SID = 0;
}
}
}
/********************************************************************
函數(shù)作用:毫秒級的延時程序,當(dāng)晶振為12Mhz時,xtal=12;
********************************************************************/
void Delay(uint ms)
{
uint i;
while(ms--)
{
for(i=1;i<(uint)(8*143-2);i++)
;
}
}
復(fù)制代碼
作者:
danhm
時間:
2015-1-29 21:43
先搜藏了,謝謝分享
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1