风筝
发表于: 2018-7-10 09:26:22 | 显示全部楼层

实现闪烁LED功能通常是快速入门电子产品的首要步骤。在本篇文章中,我们将一个LED指示灯与ATmega32微控制器进行连接,该控制器属于AVR系列。我们将以0.5秒的速率闪烁LED。


需要的硬件和软件

●    ATmega32A微控制器

●    电源(5v)

●    AVR-ISP Programmer

●    100uF电容

●    LED指示灯

●    220Ω电阻

●    Atmel Studio 6.1

●    Progisp或flash magic


电路和工作说明

LED-Interfacing-with-AVR.gif


上图展示了LED指示灯与AVR微控制器进行连接的电路图,这里不需要连接外部晶振。因为ATMEGA默认使用内部的1MHz振荡器。只有在需要时钟精度的情况下,作为高​​精度计数的应用,才会使用外部晶振。


此处连接电阻以限制流过LED的电流。请注意,AVR控制器在端口不能提供超过30mA的电流。

Blinking-LED-with-ATmega32.jpg


编程说明

ATmega32微控制器闪烁LED的程序如下所示。代码中的注释解释了各行的作用。

  1. #include <avr/io.h> //header to enable data flow control over pins

  2. #define F_CPU 1000000 //telling controller crystal frequency
  3. #include <util/delay.h> //header to enable delay function in program

  4. int main(void)

  5. {

  6.      DDRD = 0xFF; // ( or 0b1111 1111)

  7.     while(1)    、

  8.     {

  9.         PORTD = 0xFF; // all pins of PORTD are said to provide 5v output or told to pull high (LED ON)

  10.         _delay_ms(220); //delay for 200ms
  11.        _delay_ms(220); );

  12.        PORTD = 0x00; // all pins of PORTD are said to provide ground at output or pull down
  13.       
  14.        _delay_ms(220); );
  15.        _delay_ms(220); );

  16.     }

  17. }
复制代码

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 32 | 回复: 41



手机版|

GMT+8, 2024-4-16 22:51 , Processed in 0.242951 second(s), 9 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表