资讯详情

avr单片机串行四线驱动1602液晶

用串口的方式来驱动1602液晶的的优点很明显,就是串行能节约单片机有限的io口,本程序仅用四线就完成了液晶的驱动 示范程序很简单,不用多注释应该都能看懂。作为演示用途,其中有些长时间延时没有没有使用定时器,在多任务系统中当然要用定时中断来代替了。 这就是电路,细心的朋友会发现实物图中有几个贴片的阻容件,秘密就在这里,利用的记忆效应,把并行的数据转为串行。 关于本制作的更多图解请打开:http://www.ruidan.com/bbs/dpj-20365-1.html // Drive a 1602 with 2 wire //=================================================== //ICC- application builder : 2010-10-3 19:30:02 // Target : M16 // Crystal: 4.0000Mhz

#include <iom16v.h> #include <macros.h>

#define Set_E PORTB|=2 #define Clr_E PORTB&=~2 #define Set_D PORTB|=1 #define Clr_D PORTB&=~1 #define Set_xy(y,x) Send(0,(y<<6)|(x&15)|0x80)

//=================================================== void init_devices(void) { CLI(); //disable all interrupts B = 0x03; CR = 0x00; GICR = 0x00; (); //re-enable interrupts }

//=================================================== void Delay(unsigned int i) { while(i--); }

//=================================================== void Send(unsigned char RS, unsigned char dat) { unsigned char i; for (i = 2; i > 0; i--) { if (dat & 0x80) Set_D; else Clr_D; Delay(10608);//14520us if (RS) Set_E; if (dat & 0x40) Set_D; else Clr_D; Delay(462); //660us if (dat & 0x20) Set_D; else Clr_D; Delay(18); //30us Set_E; if (dat & 0x10) Set_D; else Clr_D; _NOP(); //0.5us < t < 1.36us Clr_E; dat <<= 4; } }

//=================================================== void init_1602(void) { unsigned char i = 3; Clr_D; Clr_E; Delay(10608); do{ Clr_D; Delay(462); Set_D; Set_E; Delay(18); if (i == 0) Clr_D; _NOP();_NOP();_NOP(); Clr_E; }while(i--); Send(0,0x28); Send(0,0x01); Send(0,0x0f); }

//=================================================== void Send_S(unsigned char *p) { while(*p) Send(1,*p++); }

//=================================================== void main(void) { unsigned char i; init_devices(); init_1602(); Set_xy(0,2); Send_S("Hello world!"); Set_xy(1,3); Send_S("I'm COWBOY."); for (i=0;i<255;i++) Delay(10000); Send(0,0x01); Set_xy(0,3); Send_S("Welcome to"); Set_xy(1,1); Send_S(www.ruidan.com); while(1); }

-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台