|
1黑幣
程序如下,cs腳我接的是vcc,用的是5v電壓的12864,之前懷疑是因?yàn)橐_只有3.3v才不顯示的,但是之后加了緩沖器升到5v還是沒有顯示,程序檢查半天也沒看到錯(cuò)誤
//12864液晶串行顯示測(cè)試程序
//P1.4模擬SID(接第5腳),P1.5模擬SCLK(接第6腳)
//4腳(CS信號(hào))接高電平選通,15腳(PSB信號(hào))接地選擇串行方式
//17腳(RST信號(hào))根據(jù)注釋提示懸空
#include <MSP430g2231.h>
void int_port(void) //管腳初始化
{
// P1SEL&=~(BIT4+BIT5); //P1.4模擬SID,設(shè)置為i/o口輸出方向
P1DIR|=(BIT4+BIT5);
}
void delay(unsigned int t) //延時(shí)函數(shù)
{ //粗略延時(shí),滿足時(shí)序要求
unsigned int i,j;
for(i=0; i<t; i++)
for(j=0; j<10; j++);
}
void sendbyte(unsigned char zdata) //數(shù)據(jù)傳送函數(shù)
{
P1OUT&=~BIT5;
unsigned char code_seg7;
unsigned char i;
code_seg7=zdata;
for(i=0; i<8; i++)
{
P1OUT&=~BIT5;
if((code_seg7<<i)&0x80)
P1OUT|=BIT4; //SID為1
else
P1OUT&=~BIT4; //SID為0
delay(20);
P1OUT|=BIT5;//產(chǎn)生時(shí)鐘信號(hào)上沿
delay(20);
P1OUT&=~BIT5; //產(chǎn)生時(shí)鐘信號(hào)下沿
}
}
void write_com(unsigned char cmdcode) //寫命令函數(shù)
{ //串口控制格式(11111AB0)
//A數(shù)據(jù)方向控制,A=H時(shí)讀,A=L時(shí)寫
//B數(shù)據(jù)類型選擇,B=H時(shí)為顯示數(shù)據(jù),B=L時(shí)為命令
sendbyte(0xf8); //MCU向LCD發(fā)命令
sendbyte(cmdcode & 0xf0); //發(fā)高四位數(shù)據(jù)(數(shù)據(jù)格式D7D6D5D4_0000)
sendbyte((cmdcode << 4) & 0xf0);//發(fā)低四位數(shù)據(jù)(數(shù)據(jù)格式D3D2D1D0_0000)
delay(20); //延時(shí)等待
}
void write_data(unsigned char Dispdata)//寫數(shù)據(jù)函數(shù)
{
sendbyte(0xfa); //MCU向LCD發(fā)數(shù)據(jù)
sendbyte(Dispdata & 0xf0); //發(fā)高四位數(shù)據(jù)
sendbyte((Dispdata << 4) & 0xf0);//發(fā)低四位數(shù)據(jù)
delay(20);
}
void lcdinit() //LCD初始化
{
delay(20000); //復(fù)位等待(內(nèi)部自帶上電復(fù)位電路),時(shí)間較長(zhǎng)
write_com(0x30); //功能設(shè)定:基本指令集操作
delay(500); //延時(shí)等待
write_com(0x02);
delay(500);
write_com(0x0c); //整體顯示,關(guān)游標(biāo)
delay(500);
write_com(0x01);
delay(500);
write_com(0x06); //屏幕清零
delay(500);
write_com(0x80);
}
void write_pos(unsigned char x,unsigned char y)
{
unsigned char pos;
if(x==1)
x=0x80;
else if(x==2)
x=0x90;
else if(x==3)
x=0x88;
else if(x==4)
x=0x98;
pos=x+y-1;
write_com(pos);
}
void print_string(unsigned char x,unsigned char y,unsigned char *s) //發(fā)送字符串
{
unsigned char i;
lcdinit();
write_pos(x,y);
for(i=0;*(s+i)!='\0';i++)
write_data(s);
}
void Test()
{
print_string(1,1,"頻率計(jì):");
delay(50);
write_pos(2,7);
write_data('H');
write_data('z');
delay(50);
}
void main()//正確結(jié)果:屏幕顯示:德州儀器MSP430(第一行) 頻率: 1234HZ(第二行)
{
WDTCTL=WDTPW+WDTHOLD; //關(guān)閉看門狗
int_port(); //端口初始化
lcdinit(); //LCD初始化
Test(); //測(cè)試
while(1); //CPU空轉(zhuǎn)
}
|
|