从winAVR开始

时间:2013-04-03 17:01:04

标签: c controllers avr notepad

我有20多年的编程从pascal 7开始到delphi。我想开始使用C编程微控制器,大多数电子工具包推荐的工具是带程序员笔记本的winAVR。我已经安装了该软件,并希望开始编译代码,我至少可以说是丢失了,并且找不到任何简单的文档来让我自己进入可以开始测试代码的轨道。谁能提供一些好的入门材料?

1 个答案:

答案 0 :(得分:0)

而对于PC来说,通常的第一个程序是"Hello, World!",在嵌入式世界中(一个缺少显示,至少从一开始,相当于blinky led:你将一个LED连接到一些输出引脚你的处理器(don't forget the current-limiting resistor!:你需要一个与LED串联的电阻),你让LED闪烁。你可以找到很多用于AVR的闪烁LED,但我们可以在这里写一个:

// The next define tells delay.h what your CPU speed is, assuming 1Mhz
#define F_CPU 1000000UL
#include <util/delay.h>

main() {
   while(1) { // loop forever
      DDRB = 0xFF;     // Set the direction of all pins 
                       // on port B to OUTPUT (can change to some other port)
      PORTb = 0xFF;    // Set all pins on port B high (can change to some other port)
      _delay_ms(1000); // Wait one second;
      PORTb = 0x00;    // Set all pins on port B low (can change to some other port)
      _delay_ms(1000); // Wait one second;
   }
}

它应该在WinAVR上编译,并正确加载。将PORTBDDRB更改为您想要的其他端口。请注意,此程序会更改该端口上的所有引脚:因此,如果您的端口B有8个引脚,则所有引脚都会闪烁连接到它们的引脚。不要忘记限流电阻,并且LED是方向性的:它们仅在以一种方式插入时才起作用,而不是另一种方式。