<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于AVR單片機(jī)I/O演示C語言程序

          基于AVR單片機(jī)I/O演示C語言程序

          作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
          //程序流程:全亮->全滅->PD隔一步進(jìn)->全亮->全滅->PD隔二步進(jìn)->全亮->全滅->PB全亮->pb0置位->pb0清零->PB0反轉(zhuǎn)->全滅->(循環(huán))

          // Target : M8
          // Crystal: 11.059Mhz

          #include
          #include

          //起始全亮
          void port_init(void)
          {
          PORTB = 0x00;
          DDRB = 0xFF;
          PORTC = 0x00;
          DDRC = 0x7F;
          PORTD = 0x00;
          DDRD = 0xFF;
          }

          //延時(shí)函數(shù),大約1ms;
          void delay(char tim)
          {
          unsigned int i,j;
          for(i=0;i for(j=0;j<10000;j++);
          }

          //led全亮
          void led_on(void)
          {
          PORTB = 0x00;
          PORTC = 0x00;
          PORTD = 0x00;

          delay(5000);
          }
          //led全滅
          void led_off(void)
          {
          PORTB = 0xFF;
          PORTC = 0xFF;
          PORTD = 0xFF;

          delay(5000);
          }
          //PB隔1步進(jìn)
          void pd_1(void)
          {
          char i;
          for (i = 0; i < 8; i++)
          {
          PORTB = ~(1 << (i));//位操作結(jié)合移位操作
          delay(5000);
          }
          }
          //PB隔2步進(jìn)
          void pd_2(void)
          {
          char i;
          for (i = 0; i < 8; i+=2)
          {
          PORTB = ~(1 << (i));//位操作結(jié)合移位操作
          delay(5000);
          }
          }

          //call this routine to initialise all peripherals
          void init_devices(void)
          {
          //stop errant interrupts until set up
          CLI(); //disable all interrupts
          port_init();

          MCUCR = 0x00;
          GICR = 0x00;
          TIMSK = 0x00; //timer interrupt sources
          SEI(); //re-enable interrupts
          //all peripherals are now initialised
          }

          //
          void main(void)
          {
          init_devices();

          while(1)
          {
          led_on();
          led_off();

          led_on();
          led_off();


          pd_1();

          led_on();
          led_off();


          pd_2();

          led_on();
          led_off();


          //賦值(會(huì)給所有的位以特定值),使pb0為0,led亮;
          PORTB = 0xFE;
          delay(5000);
          //置位(不影響其他位),使pb0為1,led滅;
          PORTB |= 0x01;
          delay(5000);
          //清零(不影響其他位),使pb0為0,led亮;
          PORTB &= ~0x01;
          delay(5000);
          //反轉(zhuǎn)(不影響其他位),使pb0為1,led滅;
          PORTB ^= 0x01;
          delay(5000);
          }
          }


          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();