摘要:我們知道Arduino開發(fā)板常用的芯片是Atmel公司生產(chǎn)的AVR微控制器系列。最常見的是ATmega328P,被廣泛用于Arduino Uno開發(fā)板。其他常用的AVR芯片包括ATmega2560和ATmega32U4。使用Arduino平臺開發(fā)AVR的單片機非常方便。Arduino IDE提供了一個非常簡潔、易于使用的開發(fā)環(huán)境,使編寫和上傳代碼變得簡單。它提供了一套簡化的函數(shù)庫和API,使開發(fā)者可以輕松地與ATmega328P的硬件進行交互,無需深入了解底層的寄存器操作。
一、使用Arduino點燈
使用ArduinoIDE開發(fā)AVR的ATmega328P單片機非常方便。在官網(wǎng)下載ArduinoID后,使用IDE自帶的實例就可以玩耍了。
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
這樣看起開非常簡單,這根學(xué)習(xí)STM32單片機完全不一樣,可能大家一聽你是玩Arduino的就覺得那玩意太垃圾了。其實想這個單片機也有高大上一點的玩法。
二、AVR單片機IO操作步驟
在玩AVR的ATmega328P之前先了解一下AVR單片機。AVR的IO端口是標準的雙向口,在復(fù)位時所有端口處于高阻態(tài),AVR的每個端口對應(yīng)三個寄存器,即DDRx,PORTx,PINx。在使用AVR單片機之前,一定要根據(jù)引腳功能對相應(yīng)的端口初始化,否則,端口很可能不能正常工作。當(dāng)單片機的引腳作為通用數(shù)字I/O口使用時,每個引腳都具有3個寄存器位:DDRxn、PORTxn、PINxn。
DDRxn | PORTxn | PINxn | I/O | 上拉電阻 | 說明 |
---|---|---|---|---|---|
0 | 0 | – | 輸入 | 無 | 高阻態(tài) |
0 | 1 | – | 輸入 | 有 | 帶上拉電阻 |
1 | 0 | – | 輸出 | 無 | 輸出低電平 |
1 | 1 | – | 輸出 | 無 | 輸出高電平 |
AVR單片機中對IO口進行操作之前需要進行相應(yīng)的初始化設(shè)置,其設(shè)置步驟如下:
- 1 通過方向寄存器DDRx設(shè)置相應(yīng)的端口為輸入或者輸出。
- 2 如果設(shè)置為輸出的話,把需要輸出的數(shù)據(jù)送往數(shù)據(jù)寄存器PORTx。如果設(shè)置為輸入的話,從輸入寄存器PINx中讀取外部的輸入值,同時可以通過設(shè)置PORTx來設(shè)置相應(yīng)的引腳是否需要上拉電阻。
三、使用Atmel Studio點燈
Atmel Studio是Atmel(現(xiàn)在是Microchip)公司推出的一款集成開發(fā)環(huán)境(IDE),專門用于嵌入式系統(tǒng)的開發(fā)。提供了豐富的工具和功能,以支持Atmel微控制器的編程、調(diào)試和部署。Atmel Studio支持多種編譯器,包括GCC和IAR編譯器,可用于生成最優(yōu)化的代碼。支持多種編程語言,包括C、C++和匯編語言。開發(fā)者可以根據(jù)自己的需求選擇最合適的編程語言來編寫他們的應(yīng)用程序。
首先在官網(wǎng)下載Atmel Studio的安裝包,由于Atmel被Microchip收購了,所以Atmel Studio也更名為Microchip Studio,它整合了Atmel Studio的功能,并擴展支持了更多Microchip微控制器系列,包括PIC和dsPIC系列。Atmel Studio和Microchip Studio都是用于微控制器應(yīng)用程序開發(fā)的IDE。Atmel Studio主要針對Atmel微控制器,而Microchip Studio則擴展了支持更多的Microchip微控制器系列。
下載之后傻瓜式安裝就可以了。安裝完成的界面如下,看著是不是夠很熟悉。Atmel Studio和Visual Studio在界面和功能上有很多相似之處,這是因為Atmel Studio是基于Visual Studio Shell開發(fā)的。它們的主要區(qū)別在于針對的目標平臺和硬件。Atmel Studio專注于Atmel微控制器的開發(fā),而Visual Studio是一個通用的開發(fā)環(huán)境,可用于各種應(yīng)用程序的開發(fā)。因此,如果你已經(jīng)熟悉Visual Studio,切換到Atmel Studio會相對容易,因為它們之間有很多共享的特性和工作流程。
3.1 新建工程
/*
* GccApplication1.c
*
* Created: 2023/5/18/星期三 14:48:19
* Author : LiuYao
* Board : Arduino Nano
*/
#ifndef F_CPU
#define F_CPU 10000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB =(1<<DDB5);
/* Replace with your application code */
while (1)
{
PORTB |=(1<<PORTB5);
_delay_ms(1000);
PORTB &= !(1<<PORTB5);
_delay_ms(1000);
}
return 0;
}
3.2 工具配置
Atmel Studio沒有原生支持Arduino的開發(fā),需要進行設(shè)置,本文使用的板子為ArduinoUNO,單片機型號為ATmega328P,注意在新建工程的時候要選對型號。
打開以后關(guān)鍵點是填寫下面三個選項,同時需要勾選Use Output window
。
- Title:Arduino UNO(隨意填寫)
- Command:D:\Software\Arduino\hardware\tools\avr\bin\avrdude.exe,這個路徑為ArduinoIDE中avrdude.exe的路徑,根據(jù)自己的路徑填寫。
- Arguments:這里填寫要注意串口號,我這里是COM7,根據(jù)自己板子識別填寫正確的COM號。
-C "D:\Software\Arduino\hardware\tools\avr\etc\avrdude.conf" -v -p atmega328p -c arduino -P COM7 -b 115200 -D -U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i
Arguments參數(shù)解釋如下:
Usage: avrdude.exe [options]
Options:
-p <partno> Required. Specify AVR device.
-b <baudrate> Override RS-232 baud rate.
-B <bitclock> Specify JTAG/STK500v2 bit clock period (us).
-C <config-file> Specify location of configuration file.
-c <programmer> Specify programmer type.
-D Disable auto erase for flash memory
-i <delay> ISP Clock Delay [in microseconds]
-P <port> Specify connection port.
-F Override invalid signature check.
-e Perform a chip erase.
-O Perform RC oscillator calibration (see AVR053).
-U <memtype>:r|w|v:<filename>[:format]
Memory operation specification.
Multiple -U options are allowed, each request
is performed in the order specified.
-n Do not write anything to the device.
-V Do not verify.
-u Disable safemode, default when running from a script.
-s Silent safemode operation, will not ask you if
fuses should be changed back.
-t Enter terminal mode.
-E <exitspec>[,<exitspec>] List programmer exit specifications.
-x <extended_param> Pass <extended_param> to programmer.
-y Count # erase cycles in EEPROM.
-Y <number> Initialize erase cycle # in EEPROM.
-v Verbose output. -v -v for more.
-q Quell progress output. -q -q for less.
-l logfile Use logfile rather than stderr for diagnostics.
-? Display this usage.
3.3 編譯和燒寫程序
首先編譯工程。
將Arduino UNO連接到電腦,去Tools中點擊剛設(shè)置的配置Arduino UNO
。
這個時候就可以把程序燒寫到Arduino中了,output窗口會輸出下面的內(nèi)容。
如果報錯,需要考慮一下上面提到的路徑是不是正確還有COM端口號。
注:通過Atmel Studio這個方式不能為Arduino Nano板子下載程序,因為Arduino Nano板子大多數(shù)用的是Old Bootloader,如果使用這種方式會下載失敗。
試驗現(xiàn)象
文章來源:http://www.zghlxwxcb.cn/news/detail-449316.html
下次有人問你玩啥單片機時候你要說:我不玩Arduino,我玩AVR的,和你的ARM一樣。文章來源地址http://www.zghlxwxcb.cn/news/detail-449316.html
到了這里,關(guān)于使用Atmel Studio開發(fā)Arduino的ATmega328P單片機的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!