MSP430学习笔记1-I/O端口
开发版型号:SEED—MSP430F5529A
开发工具 :CCS 5.4v
以下内容是CCS中I/O端口操作的一些简单的demo,现整理如下:
有关MSP430F5529芯片I/O口相关资料,可参考《MSP430x5xx and MSP430x6xx Family User's Guide (Rev. M)——Chapter 11 Digital I/O Module》
1:P1.0 端口翻转
1 #include <msp430.h> 2 3 int main(void) 4 { 5 volatile unsigned int i; 6 7 WDTCTL = WDTPW+WDTHOLD; // Stop WDT 8 P1DIR |= BIT0; // P1.0 set as output 9 10 while(1) // continuous loop 11 { 12 P1OUT ^= BIT0; // XOR P1.0 13 for(i=50000;i>0;i--); // Delay 14 } 15 }
注意:为了不影响控制寄存器中其他位的状态,将P1.0端口设置为输出状态选择使用
1 P1DIR |= BIT0; // P1.0 set as output
而不是
1 P1DIR = BIT0; // P1.0 set as output NO!
2:一个简单的if选择
1 #include <msp430.h> 2 3 int main(void) 4 { 5 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer 6 P1DIR |= BIT0; // Set P1.0 to output direction 7 8 while (1) // Test P1.4 9 { 10 if (P1IN & BIT4) 11 P1OUT |= BIT0; // if P1.4 set, set P1.0 12 else 13 P1OUT &= ~BIT0; // else reset 14 } 15 }
3:P1.4的一个下降沿产生中断,引起P1.0 翻转
1 #include <msp430.h> 2 3 int main(void) 4 { 5 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer 6 P1DIR |= BIT0; // Set P1.0 to output direction 7 P1REN |= BIT4; // Enable P1.4 internal resistance 8 P1OUT |= BIT4; // Set P1.4 as pull-Up resistance 9 P1IES |= BIT4; // P1.4 Hi/Lo edge 10 P1IFG &= ~BIT4; // P1.4 IFG cleared 11 P1IE |= BIT4; // P1.4 interrupt enabled 12 13 __bis_SR_register(LPM4_bits + GIE); // Enter LPM4 w/interrupt 14 __no_operation(); // For debugger 15 } 16 17 // Port 1 interrupt service routine 18 #pragma vector=PORT1_VECTOR 19 __interrupt void Port_1(void) 20 { 21 P1OUT ^= BIT0; // P1.0 = toggle 22 P1IFG &= ~BIT4; // P1.4 IFG cleared 23 }
4:P1.4上升沿产生中断后使芯片进入LPM(Low Power Model),之后在主程序中翻转P1.0,并改变P1.4的中断触发方式(翻转)
1 #include <msp430.h> 2 3 int main(void) 4 { 5 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer 6 P1DIR |= BIT0; // Set P1.0 to output direction 7 P1OUT &= ~BIT0; 8 P1REN |= BIT4; // Enable P1.4 internal resistance 9 P1OUT |= BIT4; // Set P1.4 as pull-Up resistance 10 P1IES &= ~BIT4; // P1.4 Lo/Hi edge 11 P1IFG &= ~BIT4; // P1.4 IFG cleared 12 P1IE |= BIT4; // P1.4 interrupt enabled 13 14 while(1) 15 { 16 __bis_SR_register(LPM4_bits + GIE); // Enter LPM4 w/interrupt 17 __no_operation(); // For debugger 18 P1OUT ^= BIT0; // P1.0 = toggle 19 P1IES ^= BIT4; // Toggle between H-L and L-H transition triggers 20 P1IE |= BIT4; // Enable port interrupt 21 } 22 } 23 24 // Port 1 interrupt service routine 25 #pragma vector=PORT1_VECTOR 26 __interrupt void Port_1(void) 27 { 28 P1IFG &= ~BIT4; // Clear P1.4 IFG 29 P1IE &= ~ BIT4; // Clear P1.4 IE 30 _BIC_SR_IRQ(LPM4_bits); // Exit LPM4 31 }
5:对端口PA(P1+P2)进行写操作
1 #include <msp430.h> 2 3 int main(void) 4 { 5 WDTCTL = WDTPW+WDTHOLD; // Stop WDT 6 7 PADIR |= 0xFFFF; // PA.x output 8 PAOUT = 0x1234; // Write to Port A 9 10 __bis_SR_register(LPM4_bits + GIE); // Enter LPM4 w/interrupts enabled 11 __no_operation(); // Place BREAKPOINT HERE and check register PAOUT, P1OUT and P2OUT 12 }
说明
1:关于 __bis_SR_register(LPM4_bits + GIE)
为了使单片机进入中断,在主函数中必须添加一句__bis_SR_register(LPM4_bits + GIE)"__bis_SR_register (LPM4_bits + GIE)(means go to sleep mode Low_Power_Mode_4 and with General_Interrupt_Enabled.);其中LPM4_bits可以根据实际情况而改变,而GIE是系统中断使能位,必须置高才有效;而这也是其低能耗的一个重要体现。
2:关于__no_operation();
Description
__no_operation inserts a NOP opcode into the code stream.
__no_operation is an intrinsic function and produces inline code.
From:this
3:关于__no_operation() and _delay_cycles()
_delay_cycles(xxx) is an intrinsic function that creates code that takes exactly xxx cycles to execute. For a single cycle delay, it would be identical to __no_operation(). You can do a quick test in either IAR or CCS with different values of xxx to see the actual code being created by the __delay_cycles() function. For example:
__delay_cycles(1) creates NOP
__delay_cycles(2) creates JMP #Next_Address_In_Flash
__delay_cycles(1) creates RLAM.W #3,R15
....If you only need a single cycle delay, you can stick with __no_operation().
From: this
相关宏定义(in msp430f5529.h)
1 /************************************************************ 2 * STANDARD BITS 3 ************************************************************/ 4 5 #define BIT0 (0x0001) 6 #define BIT1 (0x0002) 7 #define BIT2 (0x0004) 8 #define BIT3 (0x0008) 9 #define BIT4 (0x0010) 10 #define BIT5 (0x0020) 11 #define BIT6 (0x0040) 12 #define BIT7 (0x0080) 13 #define BIT8 (0x0100) 14 #define BIT9 (0x0200) 15 #define BITA (0x0400) 16 #define BITB (0x0800) 17 #define BITC (0x1000) 18 #define BITD (0x2000) 19 #define BITE (0x4000) 20 #define BITF (0x8000)
Steven Meng
2013.5.22