專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

AVR單片機(jī)LED實(shí)驗(yàn)

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2010年09月04日   【字體:

  早兩天在網(wǎng)上買了套AVR128開(kāi)發(fā)板,功能著實(shí)強(qiáng)大,在線硬件仿真超級(jí)爽。網(wǎng)站的工程師很好說(shuō)話,有問(wèn)題發(fā)條短信過(guò)去,馬上從哈爾濱打電話過(guò)來(lái)給解決。AVR端口定義有點(diǎn)麻煩,剛從51過(guò)來(lái),好不習(xí)慣,不過(guò),會(huì)了C程序,基本上都沒(méi)什么問(wèn)題。

這是LED逐個(gè)點(diǎn)亮程序。采用WIN-GCC編譯器。

#include <string.h>
#include <stdio.h>
#define  F_CPU 7372800  /* 單片機(jī)主頻為7.3728MHz,用于延時(shí)子程序 */
#include <util/delay.h>
#include <avr/io.h>

#define delay_us(x)     _delay_us(x)    //AVR GCC延時(shí)函數(shù) x(us)
#define delay_ms(x)     _delay_ms(x)    //AVR GCC延時(shí)函數(shù) x(ms)


void main()
{   
    PORTA=0xff;     //關(guān)閉數(shù)碼管端口,以免數(shù)碼管出現(xiàn)亂碼
    PORTB=0xfe;     //設(shè)定PB端口的輸出值
    DDRB=0xFF;      //定義PB口為輸出方式 
    unsigned char i;
 while(1)
 {   
    i=PORTB;
       for(i=0;i<8;i++)
   {  
    PORTB=PORTB<<1;
    delay_ms(500);
    }
    PORTB=0xff;
    delay_ms(500);
 }
}
關(guān)閉窗口

相關(guān)文章