找回密码
 立即注册
查看: 25252|回复: 0

使用ATmega32微控制器实现LED闪烁

[复制链接]

864

主题

285

回帖

3695

积分

版主

积分
3695
QQ
发表于 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. }
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|YiBoard一板网 ( 冀ICP备18020117号 )

GMT+8, 2025-10-30 05:58 , Processed in 0.123478 second(s), 3 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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