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.
Various color of 3mm of LED I use for my own |
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.
/* * shiftLedPortB.c * * Created: 11/16/2020 6:46:41 PM * Author : Admin */ #include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> int main(void) { unsigned char temp=0x01; /*Port B Output*/ DDRB=0xFF; while (1) { PORTB=temp; _delay_ms(100); temp<<=1; if(temp==0) temp=0x01; } }
Without a development board, the user may install the hardware manually on a bread board using the corresponding schematic diagram.
Schematic Diagram |
Click here to download zip file of this working example.
I test this program using my development board.
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.
/* * shiftLedPortBRight.c * * Created: 11/25/2020 7:39:14 PM * Author : admin */ #include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> int main(void) { unsigned char temp=0x80; /*Port B Output*/ DDRB=0xFF; while (1) { PORTB=temp; _delay_ms(100); temp>>=1; if(temp==0) temp=0x80; } }
The schematic diagram stays the same since we still use port B that already connected to LED on-board.
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.
/* * randomPortB.c * * Created: 11/25/2020 9:37:24 PM * Author : admin */ #include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> int main(void) { /*Port B Output*/ DDRB=0xFF; while (1) { PORTB=rand(); _delay_ms(100); } }
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.
/* * barGraphPortB.c * * Created: 11/25/2020 10:49:29 PM * Author : admin */ #include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> int main(void) { unsigned char barGraph[9]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF}; /*Port B Output*/ DDRB=0xFF; while (1) { for(int i=0;i<9;i++){ PORTB=barGraph[i]; _delay_ms(250); } } }
The schematic diagram remains the same. LED bar graph connects to port b.
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