#include "msp430x44x.h " //------------------------------------------------------------- //MSP-Test44x Demo ROW matrix key ,LCD dispaly the key value // // MSP430F449 // ----------------- // /|\| XIN|- // | | | key // LCD --|RST XOUT|- // ---------- | | | | // | COM0|<---|COM0 P1.0|------>|1 2 3 * | // | . | . | . . | . |4 5 6 0 | // | COM3|<---|COM3 . | . |7 8 9 # | // | S0 |<---|S0 . | . | | // | . | . | P1.7|------>| | // | S14|<---|S14 | // // Yang Rui // Lierda, Inc // February 2004 // Built with IAR Embedded Workbench Version: 1.26B //--------------------------------------------------------- //-----flag byte define------ //--------------------------------------------------------- #define key_press 0x2 //--------------------------------------------------------- //-----vaule define---------- //--------------------------------------------------------- static unsigned char flag; //flag register static unsigned char key_buff; //key vaule buffer static unsigned char sec; //second buffer static unsigned char min; //minuter buffer static unsigned char hour; //hour buffer const unsigned char lcd_table[12]={ 0x7b, //*"0"*// 0x12, //*"1"*// 0x4f, //*"2"*// 0x1f, //*"3"*// //lcd display table 0x36, //*"4"*// 0x3d, //*"5"*// 0x7d, //*"6"*// 0x13, //*"7"*// 0x7f, //*"8"*// 0x3f, //*"9"*// 0x6d, //*"e"*// 0x65, //*"f"*// }; const unsigned char key_table[12]={ 0x0b, //*(key_vaule) 00(key_num) 0x09, //9 01 0x08, //8 02 0x07, //7 03 0x00, //0 04 0x06, //6 05 //key vaule table 0x05, //5 06 0x04, //4 07 0x0a, //# 08 0x03, //3 09 0x02, //2 0a 0x01, //1 0b }; void rtc(void); void display_rtc(void); void display_dec(int i,int j); void lcd_display (void); void scan_key_deal(void); //-------------------------------------------------- //-----------main PROGRAM----------------------------- //-------------------------------------------------- void main(void) { int i; WDTCTL = WDTPW + WDTHOLD; BTCTL=BTSSEL+BT_ADLY_250+BTFRFQ1; //BT 0.25 S interrupt IE2|=BTIE; P5SEL=0xfc; //enable lcd display LCDCTL=LCDON+LCD4MUX+LCDP2; for (i=0; i<8; ++i) // clear lcd LCDMEM[i] = 0x00; _EINT(); //*mainloop*// for(;;) { _BIS_SR(LPM3_bits); //in LPM3 _NOP(); } } // Basic Timer interrupt service routine interrupt[BASICTIMER_VECTOR] void basic_timer(void) { rtc(); //RTC modulate scan_key_deal(); //scan the queue key if(flag) //if key press,show the key value { flag=flag<0x6f?flag+1:0; lcd_display(); } else display_rtc(); //not ,display the rtc } //-------------------------------------------------------- //---------RTC PROGRAM------------------------------------ //-------------------------------------------------------- void rtc (void) { if (++sec < 240) return; sec = 0; if (++min < 60) return; min = 0; if (++hour < 24) return; hour = 0; } //--------------------------------------------------------- //---------RTC DISPLAY PROGRAM----------------------------- //--------------------------------------------------------- void display_rtc(void) { int i; i=sec; i=i>>2; display_dec(0,i); i=min; display_dec(2,i); i=hour; display_dec(4,i); LCDMEM[6]=0x00; } //--------------------------------------------------------- //-------- RTC LCD DISPLAY PROGRAM------------------------- //--------------------------------------------------------- void display_dec(int i,int j) { LCDMEM[i]=lcd_table[j%10]+0x80; LCDMEM[i+1]=lcd_table[j/10]; } //--------------------------------------------------------- //-------- KEY DISPLAY PROGRAM----------------------------- //--------------------------------------------------------- void lcd_display (void) { int i; for (i=0;i<7;i++) LCDMEM[i]=lcd_table[key_buff&0x0f]; } //--------------------------------------------------------- //------- SCAN KEY PROGRAM--------------------------------- //--------------------------------------------------------- void scan_key_deal(void) { unsigned char counter; unsigned char buffer; unsigned char com_byte; unsigned char key_num; P1DIR=0x0f; com_byte=0xff; P1OUT&=~0x08; P1OUT&=~0x04; P1OUT&=~0x02; buffer=P1IN; if(0xf0!=(buffer&0xf0)) //judge if the key was press { P1OUT|=0x08; P1OUT|=0x04; P1OUT|=0x02; com_byte=0xff; key_num=0x00; com_byte=0x02; for(counter=0x03;counter>0;counter--) //get the key num { P1OUT&=~com_byte; //scan the row if(!(P1IN&BIT4)) break; key_num++; if(!(P1IN&BIT5)) break; key_num++; if(!(P1IN&BIT6)) break; key_num++; if(!(P1IN&BIT7)) break; key_num++; com_byte=com_byte<<1; } flag|=key_press; buffer=P1IN; buffer=P1IN; key_buff=key_table[key_num]; //get the key value return; } // flag&=~key_press; }