MSP430G2553 模擬交通燈
阿新 • • 發佈:2020-07-27
交通燈狀態轉換:
(其中黃燈會進行閃爍)
#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; } } //兩種情況合併 // if(current == 1 || current == 3){ // // uchar bit_of_yellow_light = current == 1 ? 0x08 : 0x40; // // for(j = 8; j > 0; j--){ // for(i = 5000; i > 0; i--) // ; // P1OUT ^= bit_of_yellow_light; // } // } current = current == 3 ? 0 : current + 1; } }
效果
注意:這裡採用共陽極接法,引腳輸出低電平時對應的發光二極體導通