交通灯状态转换:
![]()
(其中黄灯会进行闪烁)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <msp430.h> typedef unsigned char uchar; typedef unsigned int uint; const uchar STATES[4] = { 0xCC, 0xD4, 0x78, 0xAC }; volatile uint i, j, current = 0; int main(void){ WDTCTL = WDTPW | WDTHOLD; P1DIR = 0xFF; P1OUT = 0xDB; while(1){ for(i = 10000; i > 0; i--) ; P1OUT = STATES[current]; if(current == 1){ for(j = 8; j > 0; j--){ for(i = 5000; i > 0; i--) ; P1OUT ^= 0x08; } } else if(current == 3){ for(j = 8; j > 0; j--){ for(i = 5000; i > 0; i--) ; P1OUT ^= 0x40; } } current = current == 3 ? 0 : current + 1; } }
|
效果:

(这里采用共阳极接法,引脚输出低电平时对应的发光二极管导通)