#include "msp430x14x.h"
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define P55 5
#define P56 6
#define P57 7
#define RS_CLR P5OUT &= ~(1 << P55) //RS置低
#define RS_SET P5OUT |= (1 << P55) //RS置高
#define RW_CLR P5OUT &= ~(1 << P56) //RW置低
#define RW_SET P5OUT |= (1 << P56) //RW置高
#define EN_CLR P5OUT &= ~(1 << P57) //E置低
#define EN_SET P5OUT |= (1 << P57) //E置高
#define DataPort P4OUT //P4口为数据口
#define uchar unsigned char
#define uint unsigned int
unsigned char x=0;
unsigned char table[2];
unsigned char table1[]="jiulishi:";
unsigned int number;
//======================写指令=========================//
void write_com(uchar com)
{
RS_CLR; //RS置低
EN_CLR; //En置低
DataPort=com;
delay_ms(1);
EN_SET; //E置高
delay_ms(2);
EN_CLR;
}
//======================写数据==========================//
void write_data(uchar data)
{
RS_SET; //RS置高
EN_CLR;
DataPort=data;
delay_ms(1);
EN_SET;
delay_ms(2);
EN_CLR;
}
//=======================================================//
//add1为1602显示的行选择切值为1或2),add2为行的位选择,shu为要显示的数;
void write_shu(uchar add1,uchar add2, uchar shu)
{
uchar shi,ge;
shi=shu/10;
ge=shu%10;
switch(add1)
{
case 1:{
write_com(0x80+add2);
write_data(0x30+shi);//注此处若要显示3为可再加write_data(0x30+bai);
write_data(0x30+ge);
}
break;
case 2:{
write_com(0x80+0x40+add2);
write_data(0x30+shi);
write_data(0x30+ge);
}
break;
}
}
//========================1602初始化================================//
void init()
{
P4SEL = 0x00;
P4DIR = 0xFF;
P5SEL = 0x00;
P5DIR|= BIT5 + BIT6 + BIT7; //控制口设置为输出模式
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void INIT()
{
P3SEL|=0X30;
P3DIR|=0X10;
P3DIR&=~0X20;
UTCTL0|=SSEL0;
UCTL0|=CHAR;
UBR10=0X00;
UBR00=0X03;
UMCTL0=0X4A;
ME1=UTXE0+URXE0;
UCTL0&=~SWRST;
IE1|=URXIE0;
IFG1&=~UTXIFG0;
}
void main( void )
{
unsigned char Thousand, Hundred, Decade, Unit,x;
WDTCTL = WDTPW + WDTHOLD;
init();
INIT();
_EINT();
write_com(0x80);
for(x=0;x<9;x++)
write_data(table1[x]);
write_com(0x80+0x40+6);
write_data('m');
write_data('m');
while(1)
{
while(!UTXIFG0);
TXBUF0=0X55;
number=table[0]*256+table[1];
if(number > 6000)
number = 0;
Thousand = number/1000;
Hundred = number%1000/100;
Decade = number%100/10;
Unit = number%10;
write_com(0x80+0x40+2);
write_data(0x30+Thousand);
write_data(0x30+Hundred);
write_data(0x30+Decade);
write_data(0x30+Unit);
}
}
#pragma vector=USART0RX_VECTOR
__interrupt void uarss(void)
{
table[x++]=RXBUF0;
if(x>sizeof table-1)
x=0;
/*table[x]=RXBUF0;
x++;
if(x==2)
x=0;
*/
}