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  

Embedded Basics Embedded MCU's Miscellaneous

Introduction to the I2C

I2C communication stands for inter-integrated circuits. I2C contains the best features of SPI & UART. It is a synchronous communication protocol. It transmits data serially. It is widely used in microcontrollers, IoT, sensors, displays, and EEPROMs, etc. I2C can be single master multiple slaves and multiple master multiple or single slave. In this blog, we will get to know about I2C in depth. Highlights of I2C I2C works with 2 wires only for communication. SDA [Serial Data]: It is used by the Master and Slave for the transmission or receiving bits. SCL [Serial Clock]: It is used to carry clock signals. I2C can communicate with 2 different methods. Slave selection protocol uses a 7-bit slave address. I2C doesn’t have any fixed length of data transfer. Confirmation after transferring every byte. Full-duplex. How does I2C work? Start Condition To initiate the communication, the master keeps the SCL line high and pulls the SDA line low. If we have more than one master then the one who pulls the SDA line low first will send the data first and if the SDA line is already low then the other masters can not send the data. Addressing Master sends the same address to all the slaves. The slave then compares its own address with this address. If the address doesn’t match with the slave, the slave doesn’t do anything if it does match then the slave sends an acknowledge bit to the master. Read/Write Bit With the address, master sends the read/write bit to all the slaves where 0 indicates a write and 1 indicates a read. If the master wants to read data from the slave then it sends the read bit. If it wants to write then it sends a write bit. ACK/NACK Bit Each frame in a message is followed by an ack/nack bit. If an address frame was successfully received, an ACK bit is returned to the Master from the slaves. THE DATA FRAME When the acknowledge bit is received by the master, the master sends the data frame to the slave which is of 8 bits. After every data frame the slave sents an acknowledge bit to confirm that the data frame has been received successfully. Stop Condition To generate the stop condition master pulls the SCL from high to low and SDA line from low to high. Applications of I2C Communicating with multiple micro-controller. Accessing low-speed DACs[digital to analog converter] and ADCs[analog and digital converter]. Accessing real-time clocks. Reading hardware sensors. Previous Next

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!!

Embedded MCU's Nordic SoC

Introduction of Nordiac NRF5xx SoC\’s

Table of Contents Overview on Nordic IoT Chips Nordic Semiconductor is a semiconductor MNC, which designs & develops a wide range of SoCs supporting IoT protocols. These SoC’s have all functionalities of Microcontroller(MCU) based on ARM cortex Processors, plus they embed 2.4ghz RF transceivers into the chips using RF-CMOS technology for making IoT chips. NRF52 and NRF51 series of SoC’s are widely accepted across the industries where there is a need for Low Power/Ultra Low Power consumption IoT solutions. Nordic Semiconductor NRF5xxx SoC is ideal for Ultralow power and cost-effective short-range wireless solutions. Typical applications for NRF5xxx SoC are: IoT(Internet of Things) devices Wearables Smart Home Wireless Mobile Phone Accessories PC Peripherals Beacons Bluetooth-enabled consumers NRF51 & NRF52 Series have all hardware support of MCU peripherals like ADC, SPI, I2C, UART, GPIO, TIMERS, etc, and 2.4ghz RF transceivers for hardware support of IoT protocols into the chip. Thus NRF5xxx series of SoC can be used for Embedded devices applications(Using MCU peripherals like UART, SPI, I2C, TIMERS, ADC, RTC, USB, etc.)and IoT applications( using protocols like BLE, ZigBee,  Thread, ANT, etc.) simultaneously. NRF5x Series supports the following short-range IoT protocols: BLE THREAD ZigBee ANT Near Field Communication  NRF Proprietary IoT protocols: ESBP & Gazell. Nordic Semiconductor also provides a full suite of IoT stacks to support of above IoT protocols. Nordic Semiconductor has named them Softdevices. To get to know about IoT protocols supported by Nordic chips you can refer to this. Nordic Semiconductor provides a wide range of development hardware from development kits to USB dongles for fast prototyping and hobbyists to play with NRF5x IoT Soc.  How to get started??  We will be using Nordic NRF52840 – DK, which is the official development board for the NRF52840 SoC. Now when I first got the NF52840 DK, it is very different from Arduino, STM, ESP boards. It has lots and lots of chips on it and quite fascinating to explore what all hardware is on it for what purposes. nRF52840-Development Kit At the same time, it is very confusing for me also on how to start with these IoT chips as they have so many features and functionalities on them. From MCU peripherals to IoT stacks, Crypto engines, Power management, analog features, and many more. nRF52 and nRF51 Chips So what I decided at first will start from exploring MCU peripherals, understanding peripheral driver header, and source files: HAL & LL. Subsequently will get to know about the Development environment and SDK of NRF chips. Starting from basics at first and then will move to IoT protocols, in that also we will first start from BLE and then to other IoT protocols. Those who don’t want to explore NRF52840 MCU peripherals can directly start from the BLE part of NRF5x chips. At first, we will make some application code using NRF5x MCU peripherals like GPIO, UART, SPI, I2C, TIMERS and etc. Will understand the HAL APIs provided by Nordic SDK for these peripherals. Will then interface different sensors and modules for making embedded devices. Then will understand the supported IoT protocols architecture and stacks. Now to get started with any MCU or SoC the first couple of things we have to understand are: Its Development Software: Software Development KIT(SDK), Integrated Development Environment (IDE), Debuggers, Environment setup for cross-compilation from our laptop/desktop to the designated MCU processor. Then we have to gather & understand the development board of the corresponding semiconductor chip. As development boards are the fastest and easiest way to get started with semiconductor chips. We will be using NRF52840- Development Board in this series and all the blogs and videos will be on it only.  Development Software for Nordic SoC’s Toolchain setup for Nordic SoC’s At first, we need an IDE. IDE provides a complete solution and one-stop environment for firmware development on microcontrollers. So Nordic Semiconductor provides 2 IDE for the development: Seger Embedded Studio and Arm® Keil µVision. The one which is recommended and preferred to use is Seger Embedded Studio (SES). So, in this tutorial series, we are going to use Seger Embedded Studio (SES). SES is a professional IDE that is highly recommended and used in the industry. SES is free to use for Nordic chips and its installation is pretty easy, can be downloaded from this link. Seger Embedded Studio Nordic chips are programmed and debugged using J-Links. Nordic NRF52840-DK has an onboard Jlink debugger and programmer. So we don’t have to care much about the hardware side of the debugger, but we have to install some software packs for using the Onboard J-Link debugger. We will install the Segger J-link Software and save it along with the same directory as that of SES. So that all that we have to set up for the toolchain of Nordic Chips, successfully installed the IDE and Debugger pack for Developing for Firmware on Nordic chips. At first, when we open the Segger Embedded Studio, it will open with a Hello world example as shown below. Segger Embedded Studio first time open page. How to get started with Seger Embedded Studio for NRF52840 nRF5 Series: Developing with SEGGER Embedded Studio (nordicsemi.com) Software Development Kit for Nordic SoC’s Once we have set up the toolchain, we will now explore and understand the SDK provided by Nordic. SDK includes the building blocks for developing applications. This includes the framework, peripheral libraries, Source & Header files of drivers, RF stacks, example codes for various applications, bootloaders and etc. Every MCU vendor provides the SDK, which is written in C/C++ languages to get started. One will write the source code in SES IDE using SDK provided by Nordic. Now as we navigate to the Nordic Semiconductor website under the Development Software Section, we will see there are a number of SDK packs which are listed: nRF 5 SDK nRF SDK for Mesh/Zigbee/Thread nRF Connect SDK nRF MDK Softdevices Overview of Nordiac SDK’s At first, it is very confusing for me & can’t figure out which SDK is for what

Stay Updated With Us

Error: Contact form not found.