MSP430单片机综合实例(proteus)

最近学了一段时间的msp430单片机,感觉这款低功耗的单片机很有意思,虽然还没有彻底弄明白它的具体操作,但是一些简单的程序已经能够实现了,下面是我写的一个MSP430比较综合一点的程序,和大家分享一下;

主程序:

#include<msp430x24x.h> #include"liushui.h" #include"juzhen.h" #define uchar unsigned char #define uint unsigned int #define rs_0 P6OUT&=~BIT4; #define rs_1 P6OUT|=BIT4; #define rw_0 P6OUT&=~BIT5; #define rw_1 P6OUT|=BIT5; #define e_0 P6OUT&=~BIT6 #define e_1 P6OUT|=BIT6; #define wei1_0 P6OUT&=~BIT0; #define wei1_1 P6OUT|=BIT0; #define wei2_1 P6OUT|=BIT1; #define wei3_0 P6OUT&=~BIT2; #define wei3_1 P6OUT|=BIT2; #define wei4_0 P6OUT&=~BIT3; #define wei4_1 P6OUT|=BIT3; uint j,t,count1,count2,count3,count4=99; uchar temp1,temp2; const uchar table1[]={" shizhong dabao "}; const uchar table2[]="0123456789"; const uchar table3[]=" 15:45:00 "; void delay2(uint x) { uint y; while(x--) for(y=120;y>0;y--); } void w_com(uchar com) { rs_0; P2OUT=com; delay2(1); e_1; e_0; } void w_dat(uchar dat) { rs_1; P2OUT=dat; delay2(1); e_1; e_0; } void init_1602() { w_com(0x38); w_com(0x0c); w_com(0x06); w_com(0x01); w_com(0x80); } void keyscan2() { if(P1IN==0xfe) { delay2(1); while(P1IN==0xfe); count2++; if(count2==60) count2=0; temp1=count2/10; temp2=count2; w_com(0x86+0x40); w_dat(table2[temp1]); delay2(5); w_com(0x87+0x40); w_dat(table2[temp2]); } if(P1IN==0xfd) { delay2(1); while(P1IN==0xfd); count3++; if(count3==24) count3=0; temp1=count3/10; temp2=count3; w_com(0x83+0x40); w_dat(table2[temp1]); delay2(5); w_com(0x84+0x40); w_dat(table2[temp2]); } } void shuma() { temp1=count4/10; temp2=count4; wei1_0; P5OUT=count5; delay2(1); wei1_1; wei3_0; P5OUT=table[temp1]; delay2(1); wei3_1; wei4_0; P5OUT=table[temp2]; delay2(1); wei4_1; } void main(void) { WDTCTL=WDTPW+WDTHOLD; CCTL0=CCIE; CCR0=1000; TACTL|=TASSEL_2+ID_3+MC_1; _EINT();//开中断 P5DIR=0xff; P4DIR=0xff; P4OUT=0xff; P6DIR=0xff; P2DIR=0xff; P2OUT=0xff; P3DIR=0xff; P1DIR=0xff; P1OUT=0xff; P1DIR=0x00; wei2_1; init_1602(); for(j=0;j<16;j++) { w_dat(table1[j]); } w_com(0x80+0x40); for(j=0;j<12;j++) { w_dat(table3[j]); } while(1) { liushui(); //shuma(); } } #pragma vector = TIMERA0_VECTOR __interrupt void Timer_A (void) { t++; keyscan2(); keyscan(); shuma(); if(t==100) { t=0; count4--; if(count4==0) count4=99; count1++; if(count1==60) { count1=0; count2++; if(count2==60) { count2=0; count3++; if(count3==24) count3=0; temp1=count3/10; temp2=count3; w_com(0x83+0x40); w_dat(table2[temp1]); delay(5); w_com(0x84+0x40); w_dat(table2[temp2]); } temp1=count2/10; temp2=count2; w_com(0x86+0x40); w_dat(table2[temp1]); delay(5); w_com(0x87+0x40); w_dat(table2[temp2]); } temp1=count1/10; temp2=count1; w_com(0x89+0x40); w_dat(table2[temp1]); delay(5); w_com(0x8a+0x40); w_dat(table2[temp2]); } } 键盘程序:

#include<msp430x24x.h> #define uchar unsigned char #define uint unsigned int uint i; const uchar table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; void keyscan() { P4DIR=0x0f; uchar temp; P4OUT=0xfe; temp=P4IN; if(temp!=0xfe) { switch(temp) { case 0xee:i=0; break; case 0xde:i=1; break; case 0xbe:i=2; break; case 0x7e:i=3; break; } } P4OUT=0xfd; temp=P4IN; if(temp!=0xfd) { switch(temp) { case 0xed:i=4; break; case 0xdd:i=5; break; case 0xbd:i=6; break; case 0x7d:i=7; break; } } P4OUT=0xfb; temp=P4IN; if(temp!=0xfb) { switch(temp) { case 0xeb:i=8; break; case 0xdb:i=9; break; case 0xbb:i=10; break; case 0x7b:i=11; break; } } P4OUT=0xf7; temp=P4IN; if(temp!=0xf7) { switch(temp) { case 0xe7:i=12; break; case 0xd7:i=13; break; case 0xb7:i=14; break; case 0x77:i=15; break; } } }

流水灯程序:

#include <msp430x24x.h> #define uchar unsigned char #define uint unsigned int void delay(uint x) { uint y; while(x--) for(y=120;y>0;y--); } void liushui() { uint i,j; uchar temp; temp=0x01; for(j=0;j<5;j++) { for(i=0;i<8;i++) { P1OUT=~(temp<<i); delay(200); } } P1OUT=0x00; for(i=0;i<6;i++) { P1OUT=~P1OUT; delay(200); } temp=0x80; for(j=0;j<5;j++) { for(i=0;i<8;i++) { P1OUT=~(temp>>i); delay(200); } } P1OUT=0x00; for(i=0;i<6;i++) { P1OUT=~P1OUT; delay(200); } }


经验分享 程序员 微信小程序 职场和发展