AVR MCU

Introduction to Atmel studio

I assume if you are here to do projects on Atmel studio using embedded c then you are familiar with Arduino IDE. If not then I would recommend you to get familiar with  Arduino IDE because it will build a basic foundation you need to get started. Arduino IDE will make it easy for you to relate variables, functions, etc. How to execute our first code on Atmel studio These are the steps you need to follow to run your first code. Open your Arduino IDe and copy the path I’ve shown below. For this, you need to scroll up. At the top, you will find it. Copy that path and paste it somewhere as I have pasted it in notepad++. You can see I’ve made a few changes in that path you need to do the same.  Now go to tools and open external tools. Here writes the name of the Board you are using as I am using Arduino Uno. Paste the commands in the command section and arguments in the arguments section after making the necessary changes and then click OK.  Now Go to file and create a New Project. Write the name of your program and select the compiler I’ve selected. After selecting the compiler press OK. Here you can see numerous microcontrollers. Now you need to select the one you are using. The microcontroller I am using is Atmega328p. Instead of searching, you can also write it in the search box. It is at the top right corner. Great! Your main.cpp file is open and nowhere you can start programming and making super cool projects. After writing your first program click on build -> build solution. Go to tools and select the External tool title you have saved in point 5). Here to run this code you just need to plug in your Arduino board and make a circuit for blinking LEDs, the same as you must have done while working on Arduino IDE. For reference, you can see the output. CODE #define F_CPU 16000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { DDRB|= (0B00100000); while (1) { PORTB=0B00100000; _delay_ms(1000); PORTB=0B11011111; _delay_ms(1000); } return(0); } /* This is just a demo code so that you can confirm that your code is running properly. For now, you don’t need to worry about the code or what is happening inside. I will share the link down below for the same. */ OUTPUT  

AVR MCU Embedded MCU's

ADC in AVR

Table of Contents Introduction to Analog To Digital Conversion (ADC) Microcontrollers as we know of today are digital devices that is they only understand digital signals but our world is not digital and produces many analog signals as well. Here comes the role of ADC. Analog to Digital converter as the name suggests is used to convert any analog signal to digital so that microcontroller can read that signal. AVR microcontroller have inbuilt ADC on all bits of PORTA. In this blog we will be discussing about all the registers and bits necessary to use ADC on AVR. ADC on AVR Now let us use this ADC of AVR and convert analog signals to digital and read them. Block Diagram for using ADC on AVR: Let us discuss these steps in detail to understand use of ADC in AVR STEP – I : Basic Setup 1) Setting up pre-scalar for ADC frequency We know that digital signal are at fixed moment or instances of time while analog are continuous in time. So when we convert analog signal to digital signal we need to decide that on how many instances of time we need to take value of signal so that we can create a digital counterpart of signal. In the image above the green line depicts analog signal and blue lines make up digital signal. Now we need to decide that for ADC how many blue lines we need. The no. of blue lines in a second is called sampling frequency. AVR has a counter which counts from 0 till 65536 and turns back to 0 and counts again. We will tell AVR that every time you count 16 times then put 1 blue line of digital signal according to analog signal. This thing is known as pre-scalar. If we set up a pre-scalar of 8 means we say to AVR that after every 8 times you count add 1 reading of digital signal. Preferred and generally used pre-scalar value is 16. ADCSRA i.e. ADC Control and Status Register A has bits 2,1,0 which decide the prescalar for ADC. You can refer table below which is taken from datasheet to select value for ADC pre-scalar: Lets see code for setting 16 bit pre-scalar. For 16 bit pre-scalar we need to only set bit ADPS2, so we will set 1 to ADPS2 bit in ADCSRA register. ADCSRA |= (1<<ADPS2); 2) Setting up resolution for ADC Resolution is nothing but a technical term for maximum value that we will be taking up as digital value. As we convert analog signal to digital we convert it to numbers where a number will represent the maximum analog input value i.e. if we set max input to 5v then digital value of all inputs equal to greater than 5v will be that number. Similarly a number is assigned to minimum value and the range between those numbers represent the values between minimum to maximum input voltage set. Now 8bit resolution means 0-255. The question comes up how? It has a simple answers. 8 bit resolution means that the converted value will have 8 binary digits. So now minimum value of 8 digit binary number is 00000000 which is 0 in decimal and highest value is 11111111 which is 255 in decimal hence the 8 bit range is 0 – 255. Here 255 represent 5V (Maximum voltage input) and 0 represents 0V. This makes up 256 digits for 0 volt hence we can calculate: 5V/256 = 0.0195V This implies that every number represents 0.0195 V. So 1 in digital value is equal to 1*0.0195V = 0.0195V. Similarly 25 means 25*0.0195V = 0.4875 V and so on… Similarly 10 bit has 10 digits so range becomes from 0(0000000000) – 1023(1111111111). And now if we select maximum output voltage to be 5V then 1023 means 5V but for every count precision increases, so: 5V/1024 = 0.00488V Hence every count represent 0.00488V and as shown above now 25 count will mean 25*0.00488 = 0.122V. Enough theory!!! Now lets see how to write code and select the resolution (range) we need for our ADC. AVR has a 10bit ADC. We can obtain both 8 bit values and 10 bit values from ADC for AVR. To set resolution we need to use ADMUX register of our microcontroller. ADMUX stand for ADC Multiplexer selection register. For 8 bit mode, set ADLAR bit of ADC to 1. ADMUX |= (1<<ADLAR); For 10 bit mode, clear ADLAR bit i.e. make it 0. ADMUX &= ~(1<<ADLAR); 3) Setting up ADC reference voltage Reference voltage is the voltage level which will be considered as maximum voltage by our ADC and at any voltage level equal to or greater than reference voltage, we will get the maximum value after conversion. The maximum value that we discussed above is same ad known as reference voltage as ADC takes this voltage as reference to work. Reference voltage can be set using ADMUX register. Bit 7 and bit 6 decide the reference that we are going to use according to the following table fetched from the datasheet. a) AREF(00) – We give the reference voltage to AREF pin, i.e. PIN 32. b) AVCC(01) – The reference voltage is considered as power connected to VCC. AVCC is Analog VCC which is optional to give for better ADC functionality. If you leave it empty then also nothing happens. It also suggest to add an external capacitor between AREF and GND so that there is no noise in ADC due to AREF pin. c) Internal 2.56V(11) – This provides a fixed internal reference voltage of 2.56V to ADC. It also has suggestion of adding a capacitor in similar way as above so that there is no noise from AREF Pin. For AREF mode, clear REFS0 and REFS1 i.e. make them both 0 ADMUX &= ~(1<<REFS0); ADMUX &= ~(1<<REFS1); For AVCC mode, set REFS0 ADMUX |= (1<<REFS0); For Internal 2.56V, set both REFS0 and REFS1 i.e. make them both 1 ADMUX |= (1<<REFS0);

AVR MCU Embedded MCU's

Starting Up with AVR

Table of Contents Introduction AVR stands for a Alf and Vegard’s RISC processor and is a microcontroller. Microcontroller is like a mini computer or a processor dedicated for a particular task. We can code AVR using C, C++ or Assembly language. AVR PINOUT Brief description of AVR pins Pin 1 – Pin 8 (PB0 – PB7): General Input / Output pins used as GPIOs i.e. General purpose Input-Output. Pin 9: It is a reset pin and resets the microcontroller. The bar above the RESET represents that it is active low i.e. when the reset pin is grounded then it resets the microcontroller. Pin 10, Pin 11 (Power Supply): These pins are used to give power to the microcontroller. We give a positive line to the VCC (Pin 10) and ground to GND (Pin 11). Generally, 5V is given as input. Pin 12, Pin 13 (Crystal Oscillator) : These pins are used to connect a crystal oscillator with AVR to give it an external clock. Pin 14 – Pin 21 (PD0 – PD7) : General Input / Output pins used as GPIOs i.e General purpose Input Output. Pin 22 – Pin 29 (PC0 – PC7) : General Input / Output pins used as GPIOs i.e General purpose Input Output. Pin 30 (AVCC) : It is used to provide power to Analog to Digital Converter of AVR. Also provides immunity to noise while conversion. Pin 31 (GND) : It is a common ground provided to be used. Pin 32 (AREF) : It is used to give a reference voltage to Analog to digital converter of AVR. Pin 33 – Pin 40 (PA0 – PA7) : These pins are used to take analog input and have Analog to Digital converter. For eg: we can sense temperature and convert from values 0-255 (8 bit) or 10 bit values. Programming AVR For any microcontroller to perform any action we need to put some code i.e. a set of instructions in it which it can follow. We write code in computer and then transfer the code to microcontroller using specific toolchain. For AVR MCU,  we can write code using different IDE like Atmel Studio, Arduino IDE, VsCode in Embedded-C language,  C and C++ for hardware purposes. Some SDK used for writing code of AVR are ATMEL STUDIO IDE, Arduino IDE, WinAVR etc. All of the mention SDK are recommended where Atmel Studio is a bit heavy software with loads of features for high performance laptops whereas WinAVR is a really light software to achieve the main motive of AVR coding with some basic and few extra features. WinAVR is recommended for people with laptops having low configurations. WinAVR is a pretty simple tool and can be installed easily. Here we will be discussing on how to install and get Atmel Studio ready for coding of AVR. Setting Up Atmel Studio You should refer to Video 6 – Video 9 of the following playlist created by Gettobyte for detailed tutorial on Atmel Studio. AVRDude AVRDude is a tool or so called utility to download/upload/manipulate the contents of ROM and EEPROM of AVR microcontrollers using your PC and uses ISP i.e. in system programming technique. It is a command line tool and has a huge list of commands which can be referred from its documentation or directly found from the internet as required. Link of Documentation is provided : http://download-mirror.savannah.gnu.org/releases/avrdude/avrdude-doc-6.3.pdf. For quick reference you can also use command: ‘avrdude help’ in command line. To use avrdude, you need to first download and install it which can be done from the provided link: http://download.savannah.gnu.org/releases/avrdude/avrdude-6.3-mingw32.zip. After downloading follow the below steps: 1) Copy the address of folder containing unzipped files 2) Type Env in search bar in start 3) Select ‘Edit the system environment variables’ 4) A dialog box will pop up. Click on environment variables in bottom right corner. 5) Select ‘Path’ in ‘System Variables’. 6) Click on edit button below system variables box. 7) Click on ‘New’ 8) Paste the address that you copied. 9) Click ‘Ok’ on all the dialog boxes. Now you are AVRDude is setup and ready to use. Applications and use of AVR AVR consist of multiple input output pins, ADC, oscillator, timer, interrupts and many more features hence useful in integrating multiple sensors and actuators (Devices that takes signal from microcontroller to perform some action in real world). This lead to use of AVR in variety of applications like home automation, automobiles, medical devices etc. AVR comes in small size with less memory as well as big size with huge memory size and more processing power and features to cover from most simple to much complex applications. Conclusion Congratulations, now your pc is completely ready to code AVR microcontroller. In further blog series of AVR we will dive deep into concepts of programming the AVR and understand different concepts of AVR programming, AVR microcontroller and electronics itself so stay tuned!!

AVR MCU Embedded MCU's

Getting started with AVR and Arduino

Home Category Child Category Part I – Untangling the wires: On your journey of exploring the different areas of embedded systems and microcontroller programming, you must’ve come across the term ‘AVR’. This blog (or series of blogs) will try to demystify all the terminologies. Prerequisites Before we proceed, this blog assumes that you already have a basic overview and understanding of what microcontrollers are, what their basic functionalities are, and how they differ from microprocessors. Knowledge of the C/CPP programming language would be beneficial. Table of Contents Introduction Arduino vs AVR The family of AVR microcontrollers Keywords Further reading References Introduction The technical definition of what AVR is, is: The AVR microcontroller is a family of mostly 8-bit microcontrollers developed by Atmel (now Microchip), that follow the Harvard RISC architecture. Let\’s try to break all this technical jargon down. Harvard architecture is a type of architecture where the instructions (program code) and data are stored in different parts of memory. This is in contrast to Von Neumann\’s architecture where no such distinction is made. The following illustration might help you understand the concept a little better. Now coming to the other piece of jargon that might’ve thrown you off RISC. It stands for Reduced Instruction Set-Computer. It is a computer architecture philosophy that generally follows the idea of having smaller and atomic instruction sizes when compared to its counterpart CISC (Complex Instruction Set-Computer), which has relatively bigger instructions. We won\’t be diving into this as this is a topic for another day. Arduino vs AVR Before we move any further let\’s clear one big misconception out of the way. People tend to confuse the terms Arduino and AVR quite often. Although it wouldn\’t be fair to compare these two (it would be like comparing an engine to a car), the following table clearly explains what are the differences between the two. In this article when we use the word “Arduino”, we will be referring to the Arduino platform which includes all the hardware and software made by the company.   Arduino   AVR 1. Is hardware and software platform which includes, but is not limited to: the Arduino IDE, Arduino Uno board, and the Arduino programming language 1. Is a family of microcontrollers developed in 1996 by Atmel. It is only a piece of hardware. 2. The Arduino Uno is a board that is built around the ATMega328, which is an AVR microcontroller. Apart from the microcontroller itself, it has other components such as a USB2.0 cable and a PCB on top of which the components such as resistors and capacitors are wired together. 2. Is a microcontroller that requires additional passive and active components (clock, LEDs, etc.) to be wired up by the end-user for it to work. 3. Products are developed and maintained by Arduino (company) 3. Products are developed and maintained by Microchip Technology Inc 4. An Arduino Uno board:Notice the various components such as voltage regulators, connectors, switches, LEDs that are already soldered onto the board.The rectangular black IC (Integrated Circuit)/chip on the bottom right-hand corner of the board is the engine that drives the car (board), ie, the ATmega328, which is an AVR microcontroller 4. The ATmega328P, an AVR microcontroller: It doesn’t come packaged with any components, which have to be attached by the vendor/programmer themselves Difference Between Arduino and AVR The family of AVR microcontrollers Microchip offers a wide selection of microcontrollers to choose from, each meeting a different set of requirements. They can broadly be divided into three categories. Series Package size FlashSize  Operating Frequency Example ATtiny(TinyAVR) 8-32 pins 0.5kB – 32kB 1.6MHz-32Mhz ATtiny85 ATMega(megaAVR) 28-100 pins 4kB – 256kB 1.6MHz-32Mhz ATMega328 ATxmegaXMEGA 44-100pins 16kB – 256kB 1.6MHz-32Mhz ATXmega128 AVR MCU Family Series Differences The above table isn’t exhaustive as there are many other families of microcontrollers such as the 32-bit AVR32 and the newly released AVR Dx series. The family of microcontrollers you end up choosing, totally depends on the scope of your project. Looking for a very small compact and lower power microcontroller which can automate a basic task, like toggling an LED-based on an input switch? Then go for the ATtiny family. Looking to control a servo motor that is connected to an IR sensor? You definitely won\’t go wrong with the ATmega series. Still, looking for more firepower for controlling multiple sensors? The ATxmega series has got your back. In the upcoming blog, we shall deep dive into one of the most widely loved and used microcontrollers – the humble yet mighty ATmega328. Till next time! Ciao! Further Reading http://ce.sharif.edu/~pourmohammadi/AVR%20Microcontroller%20and%20Embedded%20Systems/AVR%20Microcontroller%20and%20Embedded%20Systems.pdf https://en.wikipedia.org/wiki/AVR_microcontrollers http://www.avrbeginners.net/ References https://www.microchip.com/en-us/product/ATmega328P Intro Video on Device Drivers and Application Code for AVR(Arduino Boards) – YouTube

Stay Updated With Us

Error: Contact form not found.