资源描述:
《用按键控制LED灯的亮.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、用按键控制LED灯的亮灭,当按键按下时,LED灯亮,当按键松开时,LED灯灭。#include"msp430f6638.h"unsignedcharflag;voidmain(void){WDTCTL=WDTPW+WDTHOLD;//StopWDTP4DIR&=~(BIT2);P4DIR
2、=BIT4+BIT5+BIT6;//P4.4,P4.5,P4.6setasoutputP4OUT&=~(BIT4+BIT5+BIT6);//setledoffP2IE
3、=BIT6;//enableP2.6interruptP2IFG&=~(BI
4、T6);//cleaninterruptflag__enable_interrupt();//enableinterruptwhile(1){if((P4IN&0x04)==0){P2IFG
5、=BIT6;}else{P2IFG&=~BIT6;}}}//PORT2interruptserviceroutine#pragmavector=PORT2_VECTOR__interruptvoidport_2(void){P4OUT^=(BIT4+BIT5+BIT6);//setledonP2IFG&=~BIT6;//cleaninter
6、ruptflag}用按键控制LED灯的亮灭,当按键按下时,LED灯亮,当按键松开时,LED灯灭。(查询)#include"msp430f6638.h"voidmain(void){WDTCTL=WDTPW+WDTHOLD;//StopWDT//settingdirectionP4DIR&=~(BIT2);//settingIOforinputP4DIR
7、=BIT4+BIT5+BIT6;//P4.4,P4.5,P4.6setasoutputwhile(1){if((P4IN&0x04)==0)//Ifkeyispressed{P4
8、OUT
9、=BIT4+BIT5+BIT6;//ledon}else{P4OUT&=~(BIT4+BIT5+BIT6);//ledoff}}}将ACLK配置为VLOCLK(约为10K),并将ACLK通过P1.0口输出#includevoidmain(void){WDTCTL=WDTPW+WDTHOLD;//关看门狗UCSCTL4
10、=SELA_1;//将ACLK时钟源配置为VLO;P1DIR
11、=BIT0;P1SEL
12、=BIT0;//将ACLK通过P1.0输出__bis_SR_register(LPM3_b
13、its);//进入LPM3,SMCLK和MCLK停止,ACLK活动}设ACLK=XT1=32768Hz,并通过P1.0输出。#includevoidmain(void){WDTCTL=WDTPW+WDTHOLD;//StopwatchdogtimerP1DIR
14、=BIT0;//ACLKsetouttopinP1SEL
15、=BIT0;P5SEL
16、=BIT4+BIT5;//SelectXT1while(BAKCTL&LOCKIO)//UnlockXT1pinsforoperationBAKCTL&=~(L
17、OCKIO);UCSCTL6&=~(XT1OFF);//XT1OnUCSCTL6
18、=XCAP_3;//Internalloadcap//LoopuntilXT1faultflagiscleareddo{UCSCTL7&=~(XT2OFFG+XT1LFOFFG+DCOFFG);SFRIFG1&=~OFIFG;//Clearfaultflags}while(SFRIFG1&OFIFG);//TestoscillatorfaultflagUCSCTL6&=~(XT1DRIVE_3);//reducedrivestrengthUCSCT
19、L4
20、=SELA_0;//ACLK=LFTX1(bydefault)__bis_SR_register(LPM3_bits);//EnterLPM3}设ACLK=XT1=32768Hz,令SMCLK=XT2CLK,MCLK=DCO(默认)=32xACLK=1048576Hz,ACLK和SMCLK分别通过P1.0和P3.4输出。#includevoidmain(void){WDTCTL=WDTPW+WDTHOLD;//StopWDTP1DIR
21、=BIT0;//ACLKsetouttopinsP1SEL
22、
23、=BIT0;P3DIR
24、=BIT4;//SMCLKsetouttopinsP3SEL
25、=BIT4;while(BAKCTL&LOCKIO)//UnlockXT1pinsforoperationBAKCTL&=~(LOCKIO);P7SEL
26、=BIT2+BIT3