整理lcd1602.docx
《整理lcd1602.docx》由会员分享,可在线阅读,更多相关《整理lcd1602.docx(32页珍藏版)》请在冰点文库上搜索。
整理lcd1602
MSP430G2553实现LCD1602的字符显示,而且是半字节的,用的是P2口的高四位。
/*********************************************************************
*
*LCD1602显示之高四位相连的方法
*
*描述:
4线数据宽度,操作Lcd1602
*在LCD1602屏幕上第一行显示 Hello!
LCD1602
*第二行显示 MSP430G2553
*硬件电路:
MSP430g2553
*硬件连接:
*MSP430与LCD连接信息
*LCD1602,4位接口,即使用D4-D7数据口,D0-D3不接入MCU
*PIN1-->地
*PIN2-->VCC(一定要接+5V)
*PIN3-->仿真时悬空,实际电路2K电阻-->地(一定要接好,否则没有任何显示)
*PIN4-->RS-->P1.6
*PIN5-->R/W-->GND
*PIN6-->EN-->P1.7
*PIN7-->D0不接
*PIN8-->D1不接
*PIN9-->D2不接
*PIN10-->D3不接
*PIN11-->D4-->P2.4
*PIN12-->D5-->P2.5
*PIN13-->D6-->P2.6
*PIN14-->D7-->P2.7
*PIN15-->VCC
*PIN16-->地
*调试器:
MSP430FET全系列JTAG仿真器
*调试软件:
CCS5.1.1编译
*****************************************************************/
#include
#include
#include
/*****************************************************
端口定义
****************************************************/
#defineLCD_EN_PORTP1OUT//以下2个要设为同一个口
#defineLCD_EN_DDRP1DIR
#defineLCD_RS_PORTP1OUT//以下2个要设为同一个口
#defineLCD_RS_DDRP1DIR
#defineLCD_DATA_PORTP2OUT//以下3个要设为同一个口
#defineLCD_DATA_DDRP2DIR//一定要用高4位
#defineLCD_RSBIT6
#defineLCD_ENBIT7
#defineLCD_DATABIT7|BIT6|BIT5|BIT4//4位数据线连接模式
/***************************************************
预定义函数
**************************************************/
voidLCD_init(void);
voidLCD_init_first(void);
voidLCD_en_write1(void);//上升沿使能
voidLCD_en_write2(void);//下降沿使能
voidLCD_write_command(unsignedcharcommand);
voidLCD_write_data(unsignedchardata);
voidLCD_set_xy(unsignedcharx,unsignedchary);
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s);
voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedchardata);
voiddelay_1ms(void);
voiddelay_nus(unsignedintn);
voiddelay_nms(unsignedintn);
unsignedcharLCDBuf1[]={"Hello!
LCD1602"};//第一行要显示的内容
unsignedcharLCDBuf2[]={"MSP430G2553"};//第二行要显示的内容
/********************************************
主函数
*******************************************/
voidmain()
{
WDTCTL=WDTPW+WDTHOLD;//关闭看门狗
LCD_init_first();
LCD_init();
delay_nms(100);
LCD_write_string(0,0,LCDBuf1);
delay_nms(10);
LCD_write_string(0,1,LCDBuf2);
}
/********************************************
LCD液晶操作函数
*******************************************/
voidLCD_init_first(void)//LCD1602液晶初始化函数(热启动)
{
delay_nms(500);
LCD_DATA_DDR|=LCD_DATA;//数据口方向为输出
LCD_EN_DDR|=LCD_EN;//设置EN方向为输出
LCD_RS_DDR|=LCD_RS;//设置RS方向为输出
delay_nms(50);
LCD_write_command(0x30);
delay_nms(50);
LCD_write_command(0x30);
delay_nms(5);
LCD_write_command(0x30);
delay_nms(500);
}
/*****************************************
*
*LCD1602液晶初始化函数
*
****************************************/
voidLCD_init(void)
{
delay_nms(500);
LCD_DATA_DDR|=LCD_DATA;//数据口方向为输出
LCD_EN_DDR|=LCD_EN;//设置EN方向为输出
LCD_RS_DDR|=LCD_RS;//设置RS方向为输出
delay_nms(500);
LCD_write_command(0x28);//4位数据接口
delay_nms(50);
LCD_write_command(0x28);//4位数据接口
delay_nms(50);
LCD_write_command(0x28);//4位数据接口
delay_nms(50);
LCD_en_write2();
delay_nms(50);
LCD_write_command(0x28);//4位数据接口
delay_nms(500);
LCD_write_command(0x01);//清屏
LCD_write_command(0x0c);//显示开,关光标,不闪烁
LCD_write_command(0x06);//设定输入方式,增量不移位
delay_nms(50);
}
/*****************************************
*
*液晶使能上升沿
*
****************************************/
voidLCD_en_write1(void)
{
LCD_EN_PORT&=~LCD_EN;
delay_nus(10);
LCD_EN_PORT|=LCD_EN;
}
/*****************************************
*
*液晶使能下降沿
*
****************************************/
voidLCD_en_write2(void)
{
LCD_EN_PORT|=LCD_EN;
delay_nus(10);
LCD_EN_PORT&=~LCD_EN;
}
/*****************************************
*
*写指令函数
*
****************************************/
voidLCD_write_command(unsignedcharcommand)
{
delay_nus(16);
P2SEL=0x00;
LCD_RS_PORT&=~LCD_RS;//RS=0
LCD_en_write1();
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=command&0xf0;//写高四位
delay_nus(16);
LCD_en_write2();
command=command<<4;//低四位移到高四位
LCD_en_write1();
LCD_DATA_PORT&=0x0f;//清高四位
LCD_DATA_PORT|=command&0xf0;//写低四位
LCD_en_write2();
}
/*****************************************
*
*写数据函数
*
****************************************/
voidLCD_write_data(unsignedchardata)
{
delay_nus(16);
P2SEL=0x00;
LCD_RS_PORT|=LCD_RS;//RS=1
LCD_en_write1();//E上升沿
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=data&0xf0;//写高四位
delay_nus(16);
LCD_en_write2();
data=data<<4;//低四位移到高四位
LCD_en_write1();
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=data&0xf0;//写低四位
LCD_en_write2();
}
/*****************************************
*
*写地址函数
*
****************************************/
voidLCD_set_xy(unsignedcharx,unsignedchary)
{
unsignedcharaddress;
if(y==0)address=0x80+x;
elseaddress=0xc0+x;
LCD_write_command(address);
}
/*****************************************
*
*LCD在任意位置写字符串,列x=0~15,行y=0,1
*
****************************************/
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s)
{
LCD_set_xy(X,Y);//写地址
while(*s)//写显示字符
{
LCD_write_data(*s);
s++;
}
}
/*****************************************
*
*LCD在任意位置写字符,列x=0~15,行y=0,1
*
****************************************/
voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedchardata)
{
LCD_set_xy(X,Y);//写地址
LCD_write_data(data);
}
/*****************************************
*
*1us延时函数
*
****************************************/
voiddelay_1us(void)
{
asm("nop");
}
/*****************************************
*
*Nus延时函数
*
****************************************/
voiddelay_nus(unsignedintn)
{
unsignedinti;
for(i=0;idelay_1us();
}
/*****************************************
*
*1ms延时函数
*
****************************************/
voiddelay_1ms(void)
{
unsignedinti;
for(i=0;i<1140;i++);
}
/*****************************************
*
*Nms延时函数
*
****************************************/
voiddelay_nms(unsignedintn)
{
unsignedinti=0;
for(i=0;idelay_1ms();
}
msp430g2553制作的简易电子钟
芯片用的是g2553,LCD1602显示,整分的时候显示温度。
直接放代码了哈。
/******************************************************************
FileName:
Launchpadg2553LCD1602液晶测试程序
Frequency:
1.000000MHz
Time:
2011/08/1318:
16
ModifyTime:
2012/04/29
Author:
Fuwei
******************************************************************/
#include"msp430g2553.h"
#defineucharunsignedchar
#defineuintunsignedint
externvoidInitialize_LCD();
externvoidLCD_ShowString(ucharx,uchary,char*str);
externvoidLCD_ShowChar(ucharx,uchary,charc);
charhour=20,minute=6,second=0;
charhour1,hour0,minute1,minute0,second1,second0;
longtemp;
longIntDegC;
charTemp2,Temp1,Temp0;
intmain(void)
{
//Stopwatchdogtimertopreventtimeoutreset
WDTCTL=WDTPW+WDTHOLD;
//液晶显示端口初始化
P2DIR|=0x07;
TACTL=TASSEL_1+MC_2+TAIE;//ACLK,contmode,interrupt
_EINT();
for(;;)
{
ADC10CTL1=INCH_10+ADC10SC;//TempSensorADC10CLK
ADC10CTL0=SREF_1+ADC10SHT_3+REFON+ADC10ON;
__delay_cycles(5);//WaitforReftosettle
ADC10CTL0|=ENC+ADC10SC;//Sampandconvertstart
__delay_cycles(100);//Waitforconversion
ADC10CTL0&=~ENC;//DisableADCconversion
ADC10CTL0&=~(REFON+ADC10ON);//RefandADC10off
//oC=((A10/1024)*1500mV)-986mV)*1/3.55mV=A10*423/1024-278
temp=ADC10MEM;
IntDegC=((temp-673)*4230)/1024;
Temp2=IntDegC%1000/100;
Temp1=IntDegC%100/10;
Temp0=IntDegC%10;
__delay_cycles(1000000);//delay1second
}
}
//Timer_A3InterruptVector(TA0IV)handler
#pragmavector=TIMER0_A1_VECTOR
__interruptvoidTimer_A(void)
{
switch(TA0IV)
{
case2:
break;
case4:
break;
case10:
second+=2;
if((hour>=23)&&(minute>=59)&&(second>=59))
hour=0;
if((minute>=59)&&(second>=59))
{
minute=0;
hour++;
}
if(second>=59)
{
second=0;
minute++;
Initialize_LCD();
LCD_ShowString(0,0,"Temperature:
");
LCD_ShowString(8,1,".'C");
LCD_ShowChar(6,1,Temp2+'0');
LCD_ShowChar(7,1,Temp1+'0');
LCD_ShowChar(9,1,Temp0+'0');
}
else
{
second1=second/10;
second0=second%10;
minute1=minute/10;
minute0=minute%10;
hour1=hour/10;
hour0=hour%10;
Initialize_LCD();
LCD_ShowString(0,0,"Time:
");
LCD_ShowString(4,1,":
:
");
LCD_ShowChar(2,1,hour1+'0');
LCD_ShowChar(3,1,hour0+'0');
LCD_ShowChar(5,1,minute1+'0');
LCD_ShowChar(6,1,minute0+'0');
LCD_ShowChar(8,1,second1+'0');
LCD_ShowChar(9,1,second0+'0');
}
break;
}
}
MSP430G2553一个数码管实现按键加1功能
#include
unsignedinti=0,j;
unsignedcharseg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//关闭看门狗
P2DIR|=0xff;
P2SEL&=~(0xC0);
P1DIR&=~BIT3;
P1REN|=BIT3;
P1IE|=BIT3;//设置P1.3可以中断
P1IES=BIT3;//设置P1.3为下降沿中断
_EINT();
_BIS_SR(LPM0_bits+GIE);//进入低功耗睡眠,打开总中断开关
}
unsignedcharp1keyj(void)//判键子程序
{
unsignedcharx;
x=(~P1IN&0X08);//P1.3接有按键
return(x);//有按键返回非全0
}
#pragmavector=PORT1_VECTOR//中断服务程序:
__interruptvoidp1int(void)
{
if(p1keyj())
P2OUT=seg[i++];
for(j=8000;j>0;j--)
if(i>9)
i=0;
}
单通道AD转换并通过1602显示的程序
/*********************************************************************
*
*
*****************************************************************/
#include
#include
#include
/**********************************************