Home AVRAVR Code ATMEGA 16 and RGB LED examples

ATMEGA 16 and RGB LED examples

A couple of simple examples, we used an RGB LED breakout. This was a common anode type, so to switch an LED on you need to switch the appropriate port pin low.

 

Schematic

atmega and rgb led

atmega and rgb led

Code

These examples were written in mikroC PRO for AVR

Example 1 : Cycle through Red, green and blue colours


void main() {
DDRB = 0xFF;           // Set direction to be output

do {
PORTB = 0xFF;        // Turn OFF diodes on PORTB
Delay_ms(200);      // 1 second delay
PORTB = 0xFE;
Delay_ms(200);      // 1 second delay
PORTB = 0xFD;
Delay_ms(200);      // 1 second delay
PORTB = 0xFB;
Delay_ms(100);      // 1 second delay
} while(1);            // Endless loop
}

Example 2 : Cycle through all colours


void main()
{

DDRB = 0xFF;
PORTB = 0xFF; // Set RB0 to high 00000001
do // To set infinite loop
{

PORTB = PORTB - 0x01; //
Delay_ms(250);
if(PORTB == 0xF8) //11111000 = R,G,B on
{
PORTB = 0xFF; //LEDS off
Delay_ms(500);
}
}while(1); // To set infinite loop
}

Links
ATMEGA16/ATmega32 AVR Minimum System Board + USB ISP USBasp Programmer

ATMEGA16 AVR microcontroller

ATMEGA16 Minimum System Board

You may also like