proteus仿真四个外部中断按键 1602液晶.docx
《proteus仿真四个外部中断按键 1602液晶.docx》由会员分享,可在线阅读,更多相关《proteus仿真四个外部中断按键 1602液晶.docx(9页珍藏版)》请在冰豆网上搜索。
proteus仿真四个外部中断按键1602液晶
Proteus仿真外部中断,共4个按键,控制1602液晶显示,基于MSP430F249
程序1(库文件KU.h):
/**************************************************//****
//延时设置~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//***********************************************************
#defineCPU_F((double)4000000)////主时钟的频率4MHz
#definedelay_us(x)__delay_cycles((long)(CPU_F*(double)x/1000000.0))
#definedelay_ms(x)__delay_cycles((long)(CPU_F*(double)x/1000.0))
#defineRS0P3OUT&=~BIT0
#defineRS1P3OUT|=BIT0
#defineRW0P3OUT&=~BIT1
#defineRW1P3OUT|=BIT1
#defineEN0P3OUT&=~BIT2
#defineEN1P3OUT|=BIT2
//***************************************************//
//时钟设置
//************************************************//
//驱动1602**************************
voidLCD_write_com(unsignedcharcom)
{
RS0;
RW0;
EN0;
P1OUT=com;
delay_ms
(1);
EN1;
delay_ms
(1);
EN0;
}
voidLCD_write_data(unsignedchardata)
{
RS1;
RW0;
EN0;
P1OUT=data;
delay_ms
(1);
EN1;
delay_ms
(1);
EN0;
}
voidLCD_clear(void)
{
LCD_write_com(0x01);
delay_ms
(1);
}
voidLCD_write_str(unsignedcharx,unsignedchary,unsignedchar*s)
{
if(y==0)
{
LCD_write_com(0x80+x);
}
else
{
LCD_write_com(0xC0+x);
}
while(*s)
{
LCD_write_data(*s);
s++;
}
}
voidLCD_write_char(unsignedcharx,unsignedchary,unsignedchardata)
{
if(y==0)
{
LCD_write_com(0x80+x);
}
else
{
LCD_write_com(0xC0+x);
}
LCD_write_data(data);
}
voidLCD_init(void)
{
EN0;
delay_ms
(1);
LCD_write_com(0x38);
delay_ms
(1);
LCD_write_com(0x0C);
delay_ms
(1);
LCD_write_com(0x06);
delay_ms
(1);
LCD_write_com(0x01);
delay_ms
(1);
}
程序2:
主文件
#include
#include"KU.h"
unsignedcharA1;
unsignedcharA2;
unsignedcharB1;
unsignedcharB2;
unsignedcharMENU;
#pragmavector=PORT2_VECTOR
__interruptvoidP21_IRQ(void)
{
switch(P2IFG&0x0f)
{
case0x01:
//菜单
if(MENU==0)
{
A1=0;
A2=0;
B1=0x00;
B2=0;
LCD_clear();
LCD_write_str(0,0,"menu:
");
LCD_write_char(0x0e,1,0x30+A1);
LCD_write_char(0x0f,1,0x30+A2);
P2IFG=0x00;
MENU=1;
}
else
{
MENU=0;
P2IFG=0x00;
}
break;
case0x02:
//置数
if(MENU==1)
{
if((B1&0x01)==0x00)
{
if(A1==9)
{
A1=0;
}
else
{
A1++;
}
}
if((B1&0x01)==0x01)
{
if(A2==9)
{
A2=0;
}
else
{
A2++;
}
}
LCD_write_char(0x0e,1,0x30+A1);
LCD_write_char(0x0f,1,0x30+A2);
}
P2IFG=0x00;
break;
case0x04:
//移位
if(MENU==1)
{
B1=~B1;
LCD_write_char(0x0e,1,0x30+A1);
LCD_write_char(0x0f,1,0x30+A2);
}
P2IFG=0x00;
break;
case0x08:
//确认
if(MENU==1)
{
if((A1==2)&(A2==5))
{
LCD_clear();
LCD_write_str(0,0,"lmnop:
");
}
}
MENU=2;
P2IFG=0x00;
break;
}
}
intmain(void)
{
WDTCTL=WDTPW+WDTHOLD;
P1SEL=0x00;
P1DIR=0xFF;//P1液晶数据
P3SEL=0x00;
P3DIR|=BIT0+BIT1+BIT2;//P3液晶控制
delay_ms(100);
P2SEL=0x00;//P1普通IO功能
P2DIR=0xf0;//P1。
0~P13。
3输入模式,外部电路已接上拉电阻
P2IE=0x0f;//开启P1低四位中断
P2IES=0x01;//下降沿触发中断
P2IFG=0x00;
_EINT();
LCD_init();
LCD_clear();
while
(1)
{
if(MENU==0)
{
LCD_clear();
LCD_write_str(0,0,"Thenumber:
");
LCD_write_str(0X08,1,"1234.5");
delay_ms(500);
}
}
}