Posted on Leave a comment

How to make ATmega GPIO work as Input or Output

AVR Input Output Port programming means you have to program its GPIO pins.

GPIO – General Purpose Input and Output

The GPIO is a very important peripheral of the microcontroller. Using the GPIO we can use the same exact pin for Input or Output.

To make the PIN input or output we need to set a bit in the data direction register DDR

Now lets take the example of ATmega16. Atmega 16 has 4 ports.
-PORT A
-PORT B
-PORT C
-PORT D

Each PORT has 8 pin
Each pin can be addressed individually
PortA.1 = Pin 1 of Port A
PA.1 = Pin 1 of Port A

To make a PIN input/output
DDRx = 1 // make the pin output
DDRx = 0 // make the pin input

Here x is to be replaced by port number
DDRA = DDR for port A


To Set the value of the pin we use
PORTx = 1 // set all the pins of the port to HIGH
PORTx = 0// Set all the pins of the port to LOW

To Read the value of the pin we use
PINx
usage:
int read_in;
DDRx = 0;
PORTx = 1; // This will enable the internal Pull up
read_in = PINx; // read_in variable will store the value in PINx

If the internal pull-up is not enabled than then value of the pin will always be in a floating state and it will not tell the accurate result.
When the internal pull-up is not enabled then the external pull has to be enabled by the user.

Posted on Leave a comment

Analog Modulation based Transmitter using ATmega16a

Analog Modulation is the very basic form of modulation that can be produced using very basic components.

The ATmega16a is used to generate a square wave. The frequency can be generated by carefully adjusting the 8-bit PWM Timer.

Using the above method a Carrier Signal is generated.

You can hear the carrier pulses on AM Radio. The reception is full of noise and interference.

The range of such signals is not good. Since the EM wave has very low power.

The circuit is given below.

AM transmitter using ATmega16a

AM Transmitter circuit

/*
 * AM Transmitter.c
 *
 * Created: 11/10/2021 9:59:04 PM
 * Author : abhay
 */ 
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>


void delay1(){
	_delay_ms(20);
}

int main ()
{
	
	TCCR0 = (1 << WGM01) | (1 << COM00) | (1 << CS00);
		DDRB|=(1<<PB3);  /*set OC0 pin as output*/
	
	while (1)
	{
		OCR0 = 4;	// 1600 KHz
		
		delay1();    //This delay will create pulse of carrier frequency

		OCR0 = 12;	//  615 kHz

		delay1();    //This delay will create pulse of carrier frequency



	}
}

PWM signal is being produced by an 8bit timer of the microcontroller.

The formula used for the value of OCR0

Where

f clk_I/O = 16000000

N = 1

OCRn = (1,2,3 … 255)

Frequency calculated using the formula

The Antenna Used for AM Transmission in this setup is of very small length. Its range and quality get improved if you touch the antenna with your bare hands.

Posted on Leave a comment

Memory Space in ATmega16A

To effectively program AVR based microcontroller you must have knowledge about its memory space. Since there is not just one memory space. There are three different address spaces. They are:

  1. Data memory (SRAM)
    Here all your the intermediate results are stored and all the run time calculation get carried in this memory space.
  2. Program memory
    It is the memory where your application code is stored. And also it stores the constants. It could be divided into two sections by setting appropriate fuse bits. The two section will be:
    1. Application Section
    2. Boot Section
  3. EEPROM
    This is the memory where you can save the run time data such as configuration options, intermediate result for future processing. But it is very limited in size. So proper managment had to be taken care. Since it limits the size of certain applications.

Out of the three memories present in the ATmega16a, only the SRAM is volatile.

Size and address of memory space

  1. Data Memory
    1024 Bytes (Starting Address: 0x0060 – Last Address: 0x045F)
    96 Address which contain two section General purpose Register and I/O Registers.
    General Purpose Register ( Starting Address: 0x0000 – Last address: 0x001F)
    I/O register (Starting Address: 0x0020 – Last address: 0x005F)
  2. Program Memory
    Flash Type memory organised into 8000 memory location each pointing to a 16 bit wide data.
    Starting Address: 0x0000
    Last Address: 0x1FFF

NOTE: Care must be taken while Read/Write operations of EEPROM. It is very sensitive to operating conditions. Variations outside the tolerance limits could result in corruption of data in the worst case total loss of stored data. It is highly recommended that a stable power source must be used if performing frequent EEPROM operations.

Posted on Leave a comment

Arm Based Microcontrollers

At the time of writing this article ARM-based microcontroller means the microcontroller which uses a 32-bit RISC processor design from ARM holdings.

Presently ARM Cortex-M series of processor cores are being integrated into microcontrollers.

The ARM-Cortex M family comprises the following processors.

  • Cortex-M0
  • Cortex-M0+
  • Cortex-M1
  • Cortex-M3
  • Cortex-M4
  • Cortex-M7
  • Cortex-M23
  • Cortex-M33
  • Cortex-M35P
  • Cortex-M55

The Cortex-M4 / M7 / M33 / M35P / M55 cores also have a hardware-based floating-point unit. The addition of a Floating point unit adds the capabilities for digital signal processing.

To see a list of ARM-based microcontrollers.

Keil.com has a great list that has almost all the major microcontrollers out in the market and is supported by the Keil IDE.

https://www.keil.com/dd/chips//arm.htm
Posted on Leave a comment

Compile AVR Code in Raspberry Pi

I have recorded a video showing the steps to compile AVR code.

In this video, you will see how to compile AVR led blinking code for the atmega32 microcontroller is being compiled using AVR-GCC in raspberry pi 3 b+.

Step 1. : Create a file and put your avr c code in that file. Save that file with a “.c” extension

Step 2. : Run the following commands one by one

avr-gcc -g -0s -mmcu=atmega32 -c led.c
avr-gcc -g -mmcu=atmega32 -o led.elf led.o
avr-objcopy -j .text -j .data -o ihex led.elf led.hex

Step 3. : The step 2 will create a hex file. which can be uploaded to the microcontroller with the help of a programmer.

Posted on Leave a comment

How to install AVR-GCC Compiler in raspberry pi

In this video, you will see that how avr compiler can be installed in raspberry pi model 3 b+.

For steps on how to install avrdude in raspberry pi you can go here.

http://www.exasub.com/component/integrated-circuit/microcontroller/avr/using-usbasp-v2-0-avr-programmer-with-raspberry-pi-3/

AVR compiler and avrdude together on raspberry pi make it a development environment for embedded systems involving Avr microcontroller.

Posted on Leave a comment

Using USBASP V2.0 AVR Programmer with Raspberry Pi 3

Raspberry pi or Rpi can be used for programming certain AVR microcontrollers. I have made a video showing you the steps.

Step 1: you have to install AVRDUDE

sudo apt-get install avrdude -y

Step 2: You can check if the avrdude is installed properly or not using

avrdude

step 3: connect your USBASP v2.0 to your microcontroller

avrdude -c usbasp -p m32

Microcontroller Programmed:

Atmega32, atmega32a, Atmega16, Atmega16a,

Posted on Leave a comment

How to add a new .h header file in STM32 Cube IDE

  1. Click on [arrow] besides Core in your project explorer
  2. Right Click on “Inc” folder and point your mouse at the “NEW” menu item.
  3. Select “header file” option
  4. A new Header file dialog box will appear
  5. Fill in the name of the header file in “Header file” with a dot h extension
  6. Click on finish
  7. Now open your main.c file and add your custom header file after USER CODE BEGIN Incudes
Posted on Leave a comment

STM32L476vg ARM Cortex M4F Architecture

It uses ARM v7E-M architecture.

It a Harvard based architecture with two distinct buses for data and memory.

It has all the instruction set of M0, M1 and M3 .

It also has an additional feature set to support Floating-point Arithmetic. IEEE754 standard in single precision and double precision.

The following points are from the programming model.

There are three modes of operations:

  1. Thumb State
    1. Thread mode: Privileged
    2. Thread mode: Unprivileged
  2. Debug mode

Two thread mode are given to support a operating system. Kernel software run in priviledged mode and the user application software runs in unprivledged mode.

Unprivileged mode has some restriction on memory access.

Privileged mode has full access to system resources.

If an operating system is running and a user application needs to access the Privileged resources it has to generate an exception/interrupt and then the interrupt will be taken by the handler and put the system in privilege mode.

You can switch from privilege mode to unprivileged mode by setting nPRIV and SPSEL bit in the CONTROL register.

Just like all the other processors ARM Cotex M4 has registers and pointer registers.

The major difference is the use of two different stack pointer registers.

  1. Main Stack Pointer (MSP)
  2. Process Stack Pointer (PSP)

If application is running in privileged mode than main stack pointer will be used. And if the application is working in unprivileged the process stack pointer will be used.

General Purpose Registers:

R0 – R12 – General Purpose Register

R13 – Stack Pointer (SP) {MSP and PSP}

R14 – Link Register (LR)

R15 – program counter (PC)

Special registers:

  • xPSR – {APSR, EPSR, IPSR}
  • FAULTMASK
  • BASEPERI
  • PRIMASK
  • CONTROL
floating point registers of arm cortex m4f

There are 32 FPU registers from s0 to s31.

They group together to form a single 64-bit register. which are from D0 to D15

There is a Floating Point Status and Control Register (FPSCR).

Posted on Leave a comment

ARM Processor Based Microcontrollers from ST

There are lot of ARM based microcontroller offered by ST.

They use ARM Cortex M processor with ST peripheral such as GPIO, ADC etc.

They mostly fall into these following groups:

  1. ARM Cortex M0
  2. ARM Cortex M3
  3. ARM Cortex M4
  4. ARM Cortex M33
  5. ARM Cortex M7
    which is also known as M4F as it has an FPU unit.

Then there is ST classification :

  • High performance
    Higher clock speed, Has almost everything included from that segment.
  • Mainstream
    Balanced between Low Power and High Performance.
  • Low Power
    Clock speed reduced to a limit, Has additional hardware for switching off individual peripherals.
  • Wireless
    Has cortex M0+, Peripherals support for radio

The widely available stm32f1 microcontroller also known as the blue pill has ARM cortex M3.

STM32L4 has an ARM Cortex M4F.