一、輸入/輸出端口GPIO編程一—(02)、控制LCD1602顯示~~廢話不多說了~~直接上圖~~然后是程序~~MDK1_2.c//------------------------------------------------------------------------------//這個當(dāng)然是主函數(shù)了#i
一—(02)、控制LCD1602顯示~~
然后是程序~~
?
MDK1_2.c
//------------------------------------------------------------------------------
//這個當(dāng)然是主函數(shù)了
#include“lpc210x.h”
#include“macroandconst.h”
#include“delay.h”
#include“l(fā)cdinit.h”
//------------------------------------------------------------------------------
//很讓人蛋疼~~這里貌似不能加上const~~
//怎么會出現(xiàn)這種情況呢~~等下次遇到了再解決~~
uchar title0[]=“Designed by ”;
uchar title1[]=“_Justin”;
//------------------------------------------------------------------------------
//端口初始化函數(shù)
void port_init()
{
PINSEL0=0X00000000;//所有引腳連接至GPIO
PINSEL1=0X00000000;
IODIR=0X000007FF;//設(shè)置為輸出~~當(dāng)然,如果你要讀LCD1602里面的數(shù)據(jù)~~那就有輸出有輸入唄~~
//自己再改動吧~~
}
int main()
{
port_init();
lcd_init();
lcd_display_string(0,0,title0,12);
lcd_display_string(1,7,title1,7);
while(1);
}
LcdInit.c
//------------------------------------------------------------------------------
//這里也包括了LCD的顯示函數(shù)
#include“l(fā)pc210x.h”
#include“macroandconst.h”
#include“delay.h”
#include“bit.h”
//------------------------------------------------------------------------------
//RS、RW、EN引腳高低電平的定義
//這里不一定非要這么寫,我也是因?yàn)橐郧坝眠^ATMEGA16,這樣寫方便了很多,但是這里由于IOSET、IOCLR的寄存器寫1有效,寫0無效
//所以根本不用擔(dān)心~~所以也是可以直接賦值的~~
//還有,這里有個很重要的問題就是! SET_BIT() 不管是把引腳拉高或者置低,都要用到
//你懂得~~只有在IOSET、IOCLR寄存器寫1才能達(dá)到置1和清零的作用
#define lcd_rs_1 SET_BIT(IOSET,8)//數(shù)據(jù)命令選擇
#define lcd_rs_0 SET_BIT(IOCLR,8)
#define lcd_rw_1 SET_BIT(IOSET,9)//讀寫命令選擇
#define lcd_rw_0 SET_BIT(IOCLR,9)
#define lcd_en_1 SET_BIT(IOSET,10)//使能信號
#define lcd_en_0 SET_BIT(IOCLR,10)
//------------------------------------------------------------------------------
#define busy(1《《7)//忙檢測
//------------------------------------------------------------------------------
//忙檢測函數(shù)
void lcd_check_busy()
{
IOCLR=0xff;//把數(shù)據(jù)口都拉低
IODIR=0x700;//這里設(shè)置RS、RW、E為輸出口,數(shù)據(jù)口為輸入口
lcd_rs_0;
lcd_rw_1;
while(IOPIN&busy) //IOPIN反映了外部環(huán)境對器件的影響,也就是檢測PB7是否為0。如果為0則中斷while循環(huán)~~
{
lcd_en_0;
delay_1ms();
lcd_en_1;
delay_1ms();
}
lcd_en_0;
IODIR=0x7ff;//設(shè)置RS、RW、E和數(shù)據(jù)口都為輸出口
}
//------------------------------------------------------------------------------
//LCD寫數(shù)據(jù)函數(shù)
void lcd_write_data(uchar dat)
{
lcd_check_busy();
lcd_rs_1;
lcd_rw_0;
IOCLR=0xff;//先要清零
IOSET=dat;//再送數(shù)據(jù)
lcd_en_1;
delay_1ms();
lcd_en_0;
}
//------------------------------------------------------------------------------
//LCD寫指令函數(shù)
//其中flag的作用是為0不進(jìn)行忙檢測,為非0則進(jìn)行忙檢測
void lcd_write_command(uchar com,uchar flag)
{
if(flag)
lcd_check_busy();
lcd_rs_0;
lcd_rw_0;
IOCLR=0xff;//先要清零,也就是將引腳都拉低
IOSET=com;//然后再送數(shù)據(jù)
lcd_en_1;
delay_1ms();//送一個延時
lcd_en_0;//鎖存
}
//------------------------------------------------------------------------------
//LCD初始化函數(shù)
void lcd_init()
{
lcd_write_command(0x38,0); //8位數(shù)據(jù)傳送,兩行顯示,5*7字形,不檢測忙信號
delay_nms(5);
lcd_write_command(0x38,0);
delay_nms(5);
lcd_write_command(0x38,0);
delay_nms(5);
lcd_write_command(0x38,1);//8位數(shù)據(jù)傳送,兩行顯示,5*7字形,檢測忙信號
lcd_write_command(0x08,1);//關(guān)閉顯示,檢測忙信號
lcd_write_command(0x01,1);//清屏,檢測忙信號delay_1ms();
lcd_write_command(0x06,1);//顯示光標(biāo)右移設(shè)置,檢測忙信號
lcd_write_command(0x0c,1);//顯示屏打開,光標(biāo)不顯示,不閃爍,檢測忙信號
}
//------------------------------------------------------------------------------
//LCD顯示一個字節(jié)函數(shù)
voidlcd_display_char(uchar row,uchar add,uchar dat)
{
switch(row)
{
case 0:
lcd_write_command(0x80+add,1);
lcd_write_data(dat);
break;
//------------------------------------------------------------------------------
case 1:
lcd_write_command(0x80+0x40+add,1);
lcd_write_data(dat);
break;
default:
break;
}
}
//------------------------------------------------------------------------------
//LCD顯示多個字節(jié)函數(shù)
voidlcd_display_string(uchar row,uchar add,uchar *s,uchar num)
{
uchar i;
switch(row)
{
case 0:
lcd_write_command(0x80+add,1);
for(i=0;i{
lcd_write_data(s[i]);
}
break;
//------------------------------------------------------------------------------
case 1:
lcd_write_command(0x80+0x40+add,1);
for(i=0;i{
lcd_write_data(s[i]);
}
break;
default:
break;
}
}
.h文件不用發(fā)了~~肯定會~~
然后就是
Delay.c
//------------------------------------------------------------------------------
//測試軟件延時和寫位運(yùn)算的~~
#include“l(fā)pc210x.h”
#include“macroandconst.h”
//------------------------------------------------------------------------------
#define xtal 12//晶振的頻率,單位MHz,是可以改變的~~
//------------------------------------------------------------------------------
//延時1ms函數(shù)
void delay_1ms()
{
uint32 i;
for(i=1;i《(uint32)(xtal*143-2);i++)
;
}
//------------------------------------------------------------------------------
//延時nms函數(shù)
void delay_nms(uint32 n)
{
uint32 i=0;
while(i{
delay_1ms();
i++;
}
}
順便把位操作的問價(jià)發(fā)一下吧~~
感覺還是挺有用的~~
bit.h
#ifndef__bit_h
#define__bit_h
//------------------------------------------------------------------------------
#include“l(fā)pc210x.h”
//------------------------------------------------------------------------------
#define CPL_BIT(x,y)(x^=(1《#define CLR_BIT(x,y)(x&=~(1《#define SET_BIT(x,y)(x|=(1《#define GET_BIT(x,y)(x&(1《#endif
評論
查看更多