Sunday, August 15, 2021

ATMega16 Interfacing With LED

 

Overview

For most of novice microcontroller programmers, working with the LED could be fun and gaining more closer to coding. Chasing, randomizing and working with POV is quite interesting.

ATMega16 Interfacing With LED
Various color of 3mm of LED I use for my own

Taking back to a dozen of years that I started to program the microcontroller, there was nothing else rather than the popular PIC16F84A microcontroller from Arizona Microchip. Assembly language is a first choice of programming language. It’s free but the programmer need to know about the internal architecture, its peripherals, its related registers etc in dept.

Common used Assembly instructions are MOVE and ROTATE that work with the output port registers.



Programming in AVR GCC

AVR GCC has a number of operations that modify bits of any variable. A binary shift right and left operations could make a LED chasing program within a few lines of code.

A C array store a set of some identical variable in a single type of data. It’s broad and the output LED create various output patterns.

A C random function generate an unknown number within a range. It randomizes the output of LED.

Chasing LED Left

Binary shift left operation move the position of data with a specific position. For Example,

LED<<=1;

This statement shift the variable LED one position to the left.

 

Let see a simple example of chasing LED left using this operation. The program shift LED connects to port B one position within the duration of 100 mS. The original position is the LSB and ending with the MSB. After exceeding the MSB the data becomes 0. Then the program reset the LSB of data back to 1, and repeat the chasing operation.

  1. /*
  2.  * shiftLedPortB.c
  3.  *
  4.  * Created: 11/16/2020 6:46:41 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 16000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. unsigned char temp=0x01;
  16. /*Port B Output*/
  17. DDRB=0xFF;
  18. while (1)
  19. {
  20. PORTB=temp;
  21. _delay_ms(100);
  22. temp<<=1;
  23. if(temp==0) temp=0x01;
  24. }
  25. }

Without a development board, the user may install the hardware manually on a bread board using the corresponding schematic diagram.



ATMega16 Interfacing With LED
Schematic Diagram

Click here to download zip file of this working example.

I test this program using my development board.

ATMega16 Interfacing With LED
Program testing on my development board. LED scrolls from right to left.

Chasing LED Right

Similarly, we can shift the data to right direction using the binary shift right operation. Just like,



LED>>=1;

which mean the variable LED shift to the right one time. Let see another complement example, the LED shift to the right one time for every 100 milli second, repeatedly.

  1. /*
  2.  * shiftLedPortBRight.c
  3.  *
  4.  * Created: 11/25/2020 7:39:14 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 16000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. unsigned char temp=0x80;
  16. /*Port B Output*/
  17. DDRB=0xFF;
  18. while (1)
  19. {
  20. PORTB=temp;
  21. _delay_ms(100);
  22. temp>>=1;
  23. if(temp==0) temp=0x80;
  24. }
  25. }

The schematic diagram stays the same since we still use port B that already connected to LED on-board.



ATMega16 Interfacing With LED
Shifting LED right – A development board testing

Click here to download zip file of this working example.

Randomize The Output Port

With C rand() function we can create a random output from the port. The rand() function return a pseudo-random number between 0 and RAND_MAX.

Now let use this function to randomize the port B output. The output random sequence updates for every 100 ms.

  1. /*
  2.  * randomPortB.c
  3.  *
  4.  * Created: 11/25/2020 9:37:24 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 16000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. /*Port B Output*/
  16. DDRB=0xFF;
  17. while (1)
  18. {
  19. PORTB=rand();
  20. _delay_ms(100);
  21. }
  22. }

ATMega16 Interfacing With LED
Example Program – Randomize The Output Port – Running On Development Board

Click here to download zip file of this programming example.

LED Bar Graph Output

A bar graph LED display commonly found in modern mobile power bank charger, VU meter etc. Most of them are specialized, mixed function. There’s no need to program with a microcontroller.


Using a microcontroller output port, the programmer can create a sample of bar graph output. C array is very effective for holding a set of data with the same type but vary in content.

Let take a look at this sample of creating LED bar graph. The output graph increase for every 250 ms, and then rolls back to 0.

  1. /*
  2.  * barGraphPortB.c
  3.  *
  4.  * Created: 11/25/2020 10:49:29 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 16000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. unsigned char barGraph[9]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
  16. /*Port B Output*/
  17. DDRB=0xFF;
  18. while (1)
  19. {
  20. for(int i=0;i<9;i++){
  21. PORTB=barGraph[i];
  22. _delay_ms(250);
  23. }
  24. }
  25. }


The schematic diagram remains the same. LED bar graph connects to port b.

ATMega16 Interfacing With LED
LED Bar Graph Output – running on development board

Click here to download zip file of this example program. This video contains all working example in this post.

No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)