用按键控制LED灯的亮灭,当按键按下时,LED灯亮,当按键松开时,LED灯灭。
#include"msp430f6638.h"
unsigned char flag;
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop WDT
P4DIR &=~(BIT2);
P4DIR |= BIT4+BIT5+BIT6; // P4.4,P4.5,P4.6 set as output
P4OUT &=~(BIT4+BIT5+BIT6); memory按键是什么意思// set led off
P2IE |= BIT6; // enable P2.6 interrupt
P2IFG &= ~(BIT6); // clean interrupt flag
__enable_interrupt(); // enable interrupt
while(1)
{ if((P4IN & 0x04)==0)
{ P2IFG |= BIT6;}
else
{P2IFG &=~BIT6;}}
} // PORT2 interrupt service routine
#pragma vector=PORT2_VECTOR
__interrupt void port_2(void)
{P4OUT ^=(BIT4+BIT5+BIT6); // set led on
P2IFG &=~BIT6; // clean interrupt flag}
用按键控制LED灯的亮灭,当按键按下时,LED灯亮,当按键松开时,LED灯灭。(查询)
#include"msp430f6638.h"
void main(void)
{WDTCTL = WDTPW+WDTHOLD; // Stop WDT
//setting direction
P4DIR &= ~(BIT2); //setting IO for input
P4DIR |= BIT4+BIT5+BIT6; // P4.4,P4.5,P4.6 set as output
while (1)
{if ((P4IN & 0x04) == 0) //If key is pressed
{P4OUT |= BIT4+BIT5+BIT6; //led on}
else{P4OUT &=~(BIT4+BIT5+BIT6); // led off}}}
将ACLK配置为VLOCLK(约为10K),并将ACLK通过P1.0口输出
#include<msp430f6638.h>
void main(void)
{WDTCTL = WDTPW + WDTHOLD; //关看门狗
UCSCTL4 |= SELA_1; //将ACLK时钟源配置为VLO;
P1DIR |= BIT0;
P1SEL |= BIT0; //将ACLK通过P1.0输出
__bis_SR_register(LPM3_bits);//进入LPM3,SMCLK和MCLK停止,ACLK活动}
设ACLK = XT1 = 32768Hz,并通过P1.0输出。
#include <msp430f6638.h>
void main(void)
{WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT0; // ACLK set out to pin
P1SEL |= BIT0;
P5SEL |= BIT4+BIT5; // Select XT1
while(BAKCTL & LOCKIO) // Unlock XT1 pins for operation
BAKCTL &= ~(LOCKIO);
UCSCTL6 &= ~(XT1OFF); // XT1 On
UCSCTL6 |= XCAP_3; // Internal load cap
// Loop until XT1 fault flag is cleared
do
{UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
UCSCTL6 &= ~(XT1DRIVE_3); // reduce drive strength
UCSCTL4 |= SELA_0; // ACLK = LFTX1 (by default)
__bis_SR_register(LPM3_bits); // Enter LPM3
}
设ACLK = XT1 = 32768Hz,令SMCLK = XT2CLK,MCLK = DCO(默认) = 32 x ACLK = 1048576Hz,ACLK和SMCLK分别通过P1.0和P3.4输出。
#include <msp430f6638.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= BIT0; // ACLK set out to pins
P1SEL |= BIT0;
P3DIR |= BIT4; // SMCLK set out to pins
P3SEL |= BIT4;
while(BAKCTL & LOCKIO) // Unlock XT1 pins for operation
BAKCTL &= ~(LOCKIO);
P7SEL |= BIT2+BIT3; // Port select XT2
UCSCTL6 &= ~XT2OFF; // Set XT2 On
UCSCTL6 &= ~(XT1OFF); // XT1 On
UCSCTL6 |= XCAP_3; // Internal load cap
do
{UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
UCSCTL6 &= ~XT2DRIVE0; // Decrease XT2 Drive according to
// expected frequency
UCSCTL4 |= SELA_0 + SELS_5; // Select SMCLK, ACLK source andDCO source
while(1); // Loop in place
}
设ACLK = TACLK = LFXT1 = 32768, MCLK = SMCLK = DCOCLK = 32×ACLK = 1.048576Mhz,利用Timer_A输出周期为512 /32768 = 15.625ms、占空比分别为75%和25
%的PWM矩形波:
#include <msp430f6638.h>
void main(void)
{WDTCTL = WDTPW + WDTHOLD; // Stop WDT
while(BAKCTL & LOCKIO)
BAKCTL &= ~(LOCKIO); // Unlock XT1 pins for operation
UCSCTL6 &= ~(XT1OFF); // XT1 On
UCSCTL6 |= XCAP_3; // Internal load cap
// Loop until XT1 fault flag is cleared
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论