zoukankan      html  css  js  c++  java
  • MSP430G2553 模拟交通灯

    交通灯状态转换:

    (其中黄灯会进行闪烁)

    #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;
        }
    }
    

      

    效果

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

  • 相关阅读:
    Beta冲刺置顶随笔
    Beta总结
    用户试用与调查报告
    Beta冲刺第七天
    Beta冲刺第六天
    Beta冲刺第五天
    Beta冲刺第四天
    Beta冲刺第三天
    Beta冲刺第二天
    爬虫基本操作
  • 原文地址:https://www.cnblogs.com/yl-xy/p/13384612.html
Copyright © 2011-2022 走看看