AVRStudio C programming with Arduino RevC.pdf

July 17, 2019 | Author: Eduardo Garcia Breijo | Category: Arduino, Eletrônicos Digital, Bens Manufaturados, Equipamento de Escritório, Arquitetura de Computadores
Share Embed Donate


Short Description

Download AVRStudio C programming with Arduino RevC.pdf...

Description

Brief Introduction to programming AVR devices in C using AVR studio (Rev C) (www.avmicrotech.com)

Prepared by A.V.Microtech  Noida (India) www.avmicrotech.com

This document is to get you started with programming Atmel devices using WinAVR. From authors experience, the supporting documents with AVRstudio, WinAVR and Atmel datasheets are sufficient to achieving any level of expertise. ex pertise. The internet and online forums are also good source of information.

Introduction Atmel Atmega328 is versatile 28 Pin PDIP P DIP microcontroller. Loaded with many features it has  become a favorite microcontroller to learn embedded C programming. A bootloader can be burnt in the Atmega328 to help us download the code (hex files) directly from PC without the need of in-circuit programmer. Please note the difference, many Atmel microcontroller like Atmega32u4 and ARM microcontroller come with bootloader bu rnt during manufacturing process, however the Atmega328 is blank when new. We will introduce hardware and software to get started with Atmega328. As a prerequisite you must have the following. 1) AVR Studio. 2) WinAVR. 3) Atmega328 USB board like Bootboard. These requirements are explained in more details below:-

Software packages You will need functional installation of the following packa ges. 1) AVR studio Source Atmel Website :Website :- Installs as ICON Sreenshot of AVR studio. It comes with a good set of documentation found under Help

www.avmicrotech.com

2) WinAVR Source sourceforge.net The package includes:AVR-GCC free GNU c-compiler AVRdude free AVR flash downloader To check successful install Type “avr -gcc” at command prom pt and you get (avr-gcc: (avr-gcc: no input files) files) Type “avrdude” at command prompt and you get a list of avrdude flags. The avrdude documents can be found at C:\winAVR-YYYYMMDD\doc\avrdude\avrdude.pdf (Note: You may have to configure c onfigure AVR Studio to compile C program)

Atmega328 Bootboard Atmega328 bootboard is a versatile board that has Atmega328 loaded with bootloader. It has  jumper setting to convert it into and ArduinoISP programmer and burn bootloader into other Atmega328 as with any Atmel programmer. The details are available at the end of this document.

Bootloader :- Microcontrollers like Atmega328p have a portion of flash that can be programmed with bootloader code to auto download hex program into the main flash (0000 onwards). The  bootloader behave as though you have an in-circuit programmer between your PC and the Atmega328 board.

Upon hardware reset program counter jumps to the start of bootloader. The bootloader waits for approx 2 sec for the PC (infact avrdude) to establish communication and if no sync character www.avmicrotech.com

received in 2 sec, it jumps to 0000 to start executing the code. The bootloader that boot board comes programmed with can only communicate with avrdude. Fuses :- From the Atmega328 datasheet you can see that it has configurable fuses which decide on various runtime configuration. For example you can run it from internal clock or various types of external clocks. Please do not change fuses when operating from bootloader.

Programming Environment :- The bootboard works with Arduino IDE and with a simple customization, seamlessly with AVR studio.

The Arduino  environment (IDE) controls DTR signal and with boards hav ing the DTR tied to reset pin of atmega328, it is easy to bring the device into bootloader when required.

AVRstudio  by default uses STK500. Hence to download data we must come out from AVR studio and activate avrdude to help us download data into atmega328. For this we will write a batchfile in the default sub folder of the project directory. The same directory that AVRstudio keeps the newly created Hex file. The batchfile content are as below

avrdude -P com30 com30 -b  -b 57600 -p m328p -c arduino -F -e -U flash:w:firstcblinky flash:w:firstcblinky.hex .hex

Shown in the picture is default sub default  sub folder of our project “firstcblinky”

www.avmicrotech.com

Writing your first C program using AVRStudio

This chapter will walk you through the details of setting up and running your firstCblinky program and later writing a batch file to download it to your Atmega328 board. Please click AVR studio icon and click Create new project. Please select the location that will host your projects.

After naming your files click Next. Click AVR simulator and select the correct Device (Atmega328P). Click Next.

www.avmicrotech.com

Expand the Source Files on left and click your C file. Copy paste your C code. Click Build and select Rebuild All.

Downloading the code . Now we will write a batch file that will activate AVRdude and download the newly created Hex file to the Atmega328.

Please locate your project folder that was created in the location selected. A default  sub folder is created by AVRstudio during the project creation step. Open default  sub folder to see its contents.

www.avmicrotech.com

Create a batch file called Bootloader

Copy paste the following single line batch command.

avrdude -P com30 -b 57600 -p m328p -c arduino -F -e -U flash:w:firstcblinky.hex pause

(You can create a txt document, update it and rename it as “Bootloader.bat”. The comm Port and file name has to be edited)

www.avmicrotech.com

Before clicking the Bootloader.bat to down load the code kindly check the following. 1) Edit batch file to update:A)Select the correct com port that your USB board shows up as avrdude -P com1 -b 57600 - p m328p……………. (go to Control Panel/System/Device manager  and check under ports (com & LPT) to see the new com port againstr USB serial Port)

B)Change the Hex file name to point to the correct one. ………….-F -e -U flash:w:firstcblinky.hex

www.avmicrotech.com

C programming Introduction:- AVR Libc user manual (avr-libc-user-manual.pdf) carries a great deal of information on programming AVR devices u sing GNU GCC.

A typical C program is as shown below:******************************************************************************************************

#define F_CPU 16000000

#include #include #include #include #include #include #include #include #include #include



//function declaration void PortInit(void); int main(void) { PortInit(); uint8_t b; double d; b = 0b01010101;

// Main entry into the program

//(8 bit variable just like int a;)

while(1) // Loop to run continuously { d = 5000; while(d>0){d = d-1;} PORTB = b;

d = 5000; while(d>0){d = d-1;} PORTB = ~b;

} } void PortInit(void) { DDRB = 7; //Set port B

www.avmicrotech.com

}

****************************************************************************************************

int main(void) is the main entry point and is a must for every WinAVR C program. While(1) is a non terminating loop to keep running untill the reset is pressed. Port registers are addressed direct (sfr_defs.h). The AVR registers from R1 to R32 are ho wever not available. In similar way some of the useful instructions like SETB and CLRB also seems to have  been lost but are offered in more useful way through a mechanism like PORTB |= _BV(PB1) for sbi (sfr,bit) PORTB &= ~(_BV(PB1)) for cbi(sfr,bit)

Another C program showing the usage of Interrupts. #define F_CPU 16000000 #include #include #include #include #include #include #include #include #include #include



//#define DDRD _SFR_IO8(0x11)

void PortInit(void); void TimerInit(void); int main(void) { PortInit(); TimerInit(); uint8_t b;//(8 bit variable just like int a;) //double d; b = 0b01010101; while(1)

www.avmicrotech.com

{ if ((PINB & 1) == 1){ b = ~b; //b = b & 0b11111110; PORTB = b & 0b11111110; } }

} void PortInit(void) { DDRB = 0b00001111; } ISR(TIMER1_OVF_vect) { TCNT1H = 0xF0; TCNT1L = 0x00; PORTB |= _BV(0); } void TimerInit(void) { //TCCR1A TCCR1B = 0b00000101; TIMSK1 |= _BV(0); sei();

}

www.avmicrotech.com

A direct control of 16x2 LCD display. The LCD controller is HD44780 from Hitachi

//RS--4

R/W--5

E--6

#include #define F_CPU 16000000 #include #include #include #include #include #include #define E

PORTD4

#define RS PORTD5 #define RW PORTD6 void PortInit(void); void e(int a); void rs(int a); void rw(int a); void wrt(char a, int b); void lcd_init(void); void wrtb(char a, int c); int main (void) { // set PORTD for output uint8_t a, b, c; char z; PortInit(); a = 0b10101010; z = 'A'; c = 'A'; b = 0b00000100; wrt(b, 1); b = 0b00001001; wrt(b, 1); _delay_ms(5);

www.avmicrotech.com

b = 0b00000100; wrt(b, 1); b = 0b00001001; wrt(b, 1); _delay_ms(5); while(1) { _delay_ms(100); PORTC = a; _delay_ms(100); PORTC = ~a; _delay_ms(100); PORTC = a; _delay_ms(100); PORTC = ~a; _delay_ms(100); PORTC = a; _delay_ms(100); PORTC = ~a; _delay_ms(1000); } } void PortInit(void) { DDRC = 0xFF; DDRB = 0xFF; DDRD = 0xFF; lcd_init(); } void wrt(char a, int b) { rs(b); rw(0); e(1); PORTB = a; e(0); } void wrtb(char a, int c) { int b; b = (a>>4);

www.avmicrotech.com

wrt( b, c); b = (a); wrt(b, c); } void e(int a) { if (a == 1){ PORTD |= (1
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF