綠燈做成呼吸燈, 紅燈好像沒有PWM不過也在呼吸燈滅的時候讓它閃一下吧, 至於S2就是免消抖點紅燈吧,順便學一下中斷使用。
代碼如下:
- #include "msp430.h"
- const unsigned char curve[] = {
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 2, 2, 2, 2, 2,
- 2, 2, 3, 3, 3, 3, 3, 3,
- 4, 4, 4, 4, 4, 5, 5, 5,
- 5, 6, 6, 6, 6, 7, 7, 7,
- 8, 8, 8, 8, 9, 9, 9, 10,
- 10, 10, 11, 11, 11, 12, 12, 13,
- 13, 13, 14, 14, 15, 15, 15, 16,
- 16, 17, 17, 18, 18, 18, 19, 19,
- 20, 20, 21, 21, 22, 22, 23, 23,
- 24, 24, 25, 25, 26, 26, 27, 27,
- 28, 29, 29, 30, 30, 31, 31, 32,
- 33, 33, 34, 34, 35, 36, 36, 37,
- 38, 38, 39, 39, 40, 41, 41, 42,
- 43, 43, 44, 45, 46, 46, 47, 48,
- 48, 49, 50, 50, 51, 52, 53, 53,
- 54, 55, 56, 56, 57, 58, 59, 59,
- 60, 61, 62, 62, 63, 64, 65, 66,
- 66, 67, 68, 69, 70, 70, 71, 72,
- 73, 74, 75, 75, 76, 77, 78, 79,
- 80, 80, 81, 82, 83, 84, 85, 86,
- 87, 87, 88, 89, 90, 91, 92, 93,
- 94, 95, 95, 96, 97, 98, 99, 100,
- 101, 102, 103, 104, 105, 106, 106, 107,
- 108, 109, 110, 111, 112, 113, 114, 115,
- 116, 117, 118, 119, 120, 121, 122, 122,
- 123, 124, 125, 126, 127, 128, 129, 130,
- 131, 132, 133, 134, 135, 136, 137, 138,
- 139, 140, 141, 142, 143, 144, 145, 146,
- 147, 148, 149, 150, 151, 152, 153, 154,
- 155, 156
- };
- int pos = 0;
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD;
- DCOCTL= 0;
- BCSCTL1= CALBC1_1MHZ;
- DCOCTL= CALDCO_1MHZ;
- BCSCTL2 |= DIVS_3;
- P1DIR |= BIT6 | BIT0;
- P1REN |= BIT3;
- P1IE |= BIT3;
- P1SEL |= BIT6;
- TACCR0 = 625;
- TACTL = TASSEL_2 | MC_1;
- TACCTL1 = OUTMOD_7 | CCIE;
- TACCR1 = 0;
- __low_power_mode_0();
- return 0;
- }
- #pragma vector = PORT1_VECTOR
- __interrupt void p1_isr(void)
- {
- if(P1IFG & BIT3)
- {
- P1OUT |= BIT0;
- while (~P1IN & BIT3);
- P1IFG &= ~BIT3;
- }
- }
- #pragma vector=TIMER0_A1_VECTOR
- __interrupt void t0a1_isr(void)
- {
- int new_ccr1 = 1;
-
- TACCTL1 &= ~CCIFG;
- if (pos < 500) {
- new_ccr1 = curve[pos++ >> 1];
- } else if (pos < 1000) {
- new_ccr1 = curve[(999 - pos++) >> 1];
- } else {
- pos = 0;
- P1OUT ^= BIT0;
- }
-
- if (P1IN & BIT0) {
- for (int i = 3000; i>0; i--);
- P1OUT ^= BIT0;
- } else {
- while (TAR <= new_ccr1);
- TACCR1 = new_ccr1;
- }
- }