Sunday, 21 June 2015

TOGGLING LED in ATMega168PB Xplained Mini Evaluation Kit

TOGGLING LED in ATMega168PB Xplained Mini Evaluation Kit

Hello readers today I will introduce you with one of the latest low cost Atmel Microcontroller based development board. The name of the development board is ATMEL ATMEGA168PB XPLAINED MINI. I got this board few days back and implemented few of the applications that I used to develop on other platforms using different microcontroller. ATMEL has done great work in bringing a new microcontroller based development board that is Arduino compatible and cost less than 10$, isn’t it great. The development board is based on microcontroller ATMega168PB which can be called as the heart of the entire development board but there is one more thing which makes it even more special and that is the presence of In Circuit Debugger which is a requirement of today’s Embedded designers so that the task of debugging can be performed in real time on the actual h/w. A microcontroller based development board costing less than 10$ and with the ICD (In Circuit Debugger) is a very good choice for developing microcontroller based applications. 

Fig.1 below shows pictorial view of the ATMega168PB Xplained Mini Evaluation Kit from ATMEL:-

Fig.1 ATMega168PB Xplained Mini Evaluation Kit

The top view showing different pins on board is shown below in Fig.2-

Fig.2 Top view of the evaluation kit showing different pins

The following breakout I/O port pins are visible onboard:-

PORTD pins PD0 to PD7 (8 pins)
PORTC pins PC0 to PC5 (6 pins)
PORTB pins PB0 to PB5 (6 pins)

However microcontroller ATMEGA168PB has PORTC pins PC0 to PC6 (pin number 23 to 29), PORTD pins PD0 to PD7 (pin number 30, 31, 32, 1, 2, 9, 10, 11) PORTB pins PB0 to PB7 (pin number 12 to 17 and pin number 7 and 8) and PORTE pins PE0 to PE3 (pin number 3, 6, 19, 22).  So it’s clear that every pin of the microcontroller is not seen on the board and if some application demands the use of these left out pins then some arrangement must be made in order to bring these pin out connections from the microcontroller.

Apart from the I/O port pins there are some other pins that are available like AREF for setting the reference voltage for the A/D conversion and dedicated ISP connection pins are also available in order to program the microcontroller using external ISP programmer. Breakout pins for VCC and GND connections are also available for connections with the external peripheral devices. This is very useful because if the board has enough number of GND and VCC pins then it becomes easy to interface number of peripherals taking the supply voltage from the common VCC and having common GND. If there is not enough number of pins available for GND and VCC then the h/w designer either combines different supply voltage wires together or then route it to the common VCC pin or they make a separate breakout board for connections of VCC and GND pins. It’s good that the board has enough number of VCC and GND breakout pins on board.

It is a pint to note that some of the port pins have some special functionality besides functioning as I/O pins. The special features include ADC channels, SPI pins, I2C pins etc. Readers can refer to the detailed pin diagram shown in Fig.3 below for the same:-

Fig.3 ATMega168PB Microcontroller

ATMega168PB microcontroller has 16KB of flash memory, 512 bytes of EEPROM, 1KB RAM. The total pin count is 32. The detailed features are given below for the reference of the readers:-

The description of the features is extracted from the data sheet of the ATMega168PB microcontroller.

 After understanding the features and pin diagram of the ATMega168PB microcontrollers, let’s develop a code for blinking the led connected on board on pin number PB5. For developing the hex file I have used Atmel Studio software, again a great job from Atmel. The software can be used as an IDE (Integrated Development Environment) for developing applications on all the AVR architecture based microcontrollers and also for developing applications for the ARM based MCU’s from ATMEL. I will surely post a separate tutorial for the instructions on using the Atmel studio software for developing an application. The code given below is tested on the kit and it works fine in which led connected to port pin PB5 blinks after every 1 second. The comments are also given in the code for better understanding of the readers.

#include <avr/io.h>  // header file
#define F_CPU 8000000        // define the clock frequency as 8MHZ
#include <util/delay.h>  // header file so as to use _delay_ms() function in the code

int main(void)  // main function starts from here
            DDRB = (1<<5); // configure PB5 as o/p pin
            PORTB = (1<<5); // enable the pull up connected on PB5
                        PORTB ^= (1<<5); // toggle PB5 using xor operation
                        _delay_ms(1000); // give delay of 1000ms

Brief Explanation of the code-

First include the relevant header files in the program, I have included avr/io.h and util/delay.h header files. Header files are always included at the top of the program and have specific purpose. util/delay.h contains the definition of the delay function and in order to use the built in delay function (_delay_ms( ) ) this header file must be included in the code. Avr/io.h is included to use the I/O functionality of the microcontroller.

It is necessary to define the clock frequency in the code. It’s not a mandatory thing to do but a good practice that must be followed by the s/w designers. Here the definition of clock frequency is given by-
#define F_CPU 8000000 // Indicating the clock frequency of 8MHz.

In the main function two important registers are utilized ie.

DDRB (Data Direction Register of PORTB) and

 PORTB register

DDRB register is used for configuring the PORTB pins as i/p or o/p. If 1 is written to a particular position of DDRB register then that port pin is configured as o/p pin and if 0 is written then that particular port pin is configured as i/p pin.  Since the on board LED is connected to PORTB pin 5 and so the instruction for configuring it as o/p can be written as-

DDRB  = (1<<5) // configures PB5 as o/p pin
Weak pull ups are available internally on the port pins and they can be enabled. Here the pull up of PORTB pin PB5 is enabled by writing

PORTB = (1<<5) //enables pull up on PB5

In the infinite loop the blinking action is performed by utilizing the XOR operation. The instruction for toggling the port pin PB5 is given as-

PORTB ^= (1<<5); // toggle PB5 using xor operation

Note:   Logic 1 XOR Logic 1 is Logic 0
Logic 1 XOR Logic 0 is Logic 1.

The LED remains in a particular state for the time delay defined by _delay_ms( ) function. Here time delay of 1 second is specified using the instruction _delay_ms(1000) indicating 1000 ms delay. 

So reader’s that’s it with the LED blinking code, now download ATMEL Studio on your System, get ATMega168PB Xplained Mini Evaluation Kit and start experimenting.