Kamis, 21 Juli 2011

RGB LED Controller Menggunakan ATMega 8535 / 16 dengan bahasa C++ (CodeVision AVR)

RGB LED merupakan sebuah LED yang terdiri dari 3 warna, di mana kombinasi dari ketiga warna tersebut dapat menghasilkan sebuah warna baru. Sebagai contoh, apabila wana biru dan merah digabung, maka akan menghasilkan warna ungu.
Untuk keterangan lebih lanjut mengenai RGB LED dapat dilihat di:

Di sini, saya bersama teman – teman mencoba membuat suatu proyek dengan menggunakan mikrokontroler ATMega 8535L / 16 untuk mengatur warna RGB LED.
Kit-nya kurang lebih seperti ini:


Untuk mengatur warna pada RGB LED, cukup dengan menekan angka – angka di keypad. Angka – angka yang dimasukkan tidak lain adalah persentase dari duty cycle lampu yang sedang dipilih (merah, hijau, biru). Dalam proyek ini, tanda * digunakan sebagai tombol delete dan tombol untuk memilih warna. Sedangkan tanda # merupakan tombol enter.



Skematik rangkaian dapat dilihat di bawah:



RGB LED


Keterangan:
  • JP1 terhubung ke ground.
  • JP2.1(warna hijau) terhubung ke PORT B.3
  • JP2.1(warna merah) terhubung ke PORT D.4
  • JP2.1(warna biru) terhubung ke PORT D.5
  • LCD terhubung ke PORT C
  • Keypad terhubung ke PORT A
Konfigurasi Timer / OCx:
  • Mode: Fast PWM top=FFh
  • Clock Source: System Clock
  • OCx output: Non-inverted PWM

Source Code

Chip type           : ATmega8535L
Program type        : Application
Clock frequency     : 11.059200 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*****************************************************
#include <mega8535.h>


// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <delay.h>
#include <stdlib.h>


// Declare your global variables here
unsigned char key1,key2,key3,key4;
unsigned char a[1],b[1];
int x,y,z,n,j,o,red,green,blue;
unsigned char keypad();
void main(void)
{
// Declare your local variables here


// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=P State6=P State5=P State4=1 State3=1 State2=1 State1=1 State0=0 
PORTA=0xFE;
DDRA=0x1F;


// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTB=0x00;
DDRB=0xFF;


// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;


// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTD=0x00;
DDRD=0xFF;


// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=FFh
// OC0 output: Non-Inverted PWM
TCCR0=0x69;
TCNT0=0x00;
OCR0=0x00;


// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 11059.200 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1;
TCCR1B=0x09;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;


// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;


// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;


// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;


// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;


// LCD module initialization
lcd_init(16);
lcd_gotoxy(4,0);
lcd_putsf("RGB  LED");
lcd_gotoxy(2,1);
lcd_putsf("Please Wait");
delay_ms(500);
lcd_gotoxy(13,1);
lcd_putsf(".");
delay_ms(500);
lcd_gotoxy(14,1);
lcd_putsf(".");
delay_ms(500);
lcd_gotoxy(15,1);
lcd_putsf(".");
delay_ms(500);
lcd_clear();
j=1;  


while (1)
      {
      // Place your code here
      if(j==1)
      {
       lcd_gotoxy(0,0);
       lcd_putsf("Red  :");
       o=1;
      }
      else if(j==2)
      {
       lcd_gotoxy(0,0);
       lcd_putsf("Green:");
       o=2;
      }
      else if(j==3)
      {
       lcd_gotoxy(0,0);
       lcd_putsf("Blue :");
       o=3;
      }   
      if(key1!='0' && key1!='1' && key1!='*')
      {
       key1=keypad();
       lcd_gotoxy(0,1);
       lcd_putchar(key1);
       delay_ms(500);
      }
      else if(key1=='0')
      {
       if(key2!='0' && key2!='1' && key2!='2' && key2!='3' && key2!='4' && key2!='5' && key2!='6' && key2!='7' && key2!='8' && key2!='9' && key2!='*')
       {
        key2=keypad();
        lcd_gotoxy(1,1);
        lcd_putchar(key2);
        delay_ms(500);
       }
       else if(key2=='*')
       {
        lcd_gotoxy(1,1);
        lcd_putsf(" ");
        if(j==1)
        {
         j=1;
         key1='';                          
         key2='';
        }
        else if(j==2)
        {
         j=2;
         key1='';
         key2='';
        }
        else if(j==3)
        {
         j=3;
         key1='';
         key2='';
        } 
       }  
       else
       {
        if(key3!='0' && key3!='1' && key3!='2' && key3!='3' && key3!='4' && key3!='5' && key3!='6' && key3!='7' && key3!='8' && key3!='9' && key3!='*')
        {
         key3=keypad();
         lcd_gotoxy(2,1);
         lcd_putchar(key3);
         delay_ms(500);
        }
        else if(key3=='*')
        {
         lcd_gotoxy(2,1);
         lcd_putsf(" ");
         if(j==1)
         {
          j=1;
          key2='';
          key3='';
         }
         else if(j==2)
         {
          j=2;
          key2='';
          key3='';
         }
         else if(j==3)
         {
          j=3;
          key2='';
          key3='';
         } 
        }   
        else
        {
         if(key4!='#' && key4!='*')
         {
          key4=keypad();
          lcd_gotoxy(3,1);
          lcd_putchar(key4);
         }
         else if(key4=='*')
         {
          lcd_gotoxy(3,1);
          lcd_putsf(" ");
          if(j==1)
          {
           j=1;
           key3='';
           key4='';
          }
          else if(j==2)
          {
           j=2;
           key3='';
           key4='';
          }
          else if(j==3)
          {
           j=3;
           key3='';
           key4='';
          } 
         }
         else
         {
          a[0]=key2;
          b[0]=key3;
          x=atoi(a);
          y=atoi(b);
          z=(10*x)+y;
          n=z*0.001*255;
          if(o==1)
          {
           red=n;
           j=2;
           key1='';
           key2='';
           key3='';
           key4='';
           lcd_clear();
          }
          else if(o==2)
          {
           green=n;
           j=3;
           key1='';
           key2='';
           key3='';
           key4='';
           lcd_clear();
          }
          else if(o==3)
          {
           blue=n;
           j=1;
           key1='';
           key2='';
           key3='';
           key4='';
           lcd_clear();
          }   
         }
        }    
       } 
      }
      else if(key1=='*')
      {
       if(j==3)
       {
        j=1;
        key1='';
       }
       else
       {
        j+=1;
        key1='';
       }
      }   
      else
      {
       lcd_gotoxy(1,1);
       lcd_putsf("00");
       delay_ms(500);
       n=255;
       if(o==1)
          {
           red=n;
           j=2;
           key1='';
           key2='';
           key3='';
           key4='';
           lcd_clear();
          }
          else if(o==2)
          {
           green=n;
           j=3;
           key1='';
           key2='';
           key3='';
           key4='';
           lcd_clear();
          }
          else if(o==3)
          {
           blue=n;
           j=1;
           key1='';
           key2='';
           key3='';
           key4='';
           lcd_clear();
          }   
      }
        
      OCR0=green;
      OCR1A=blue;
      OCR1B=red; 
      };
}
unsigned char keypad()
{
PORTA=0b11111100;
if(PINA.5==0) return('*');
if(PINA.6==0) return('2');
if(PINA.7==0) return('3');


PORTA=0b11111010;
if(PINA.5==0) return('1');
if(PINA.6==0) return('5');
if(PINA.7==0) return('6');


PORTA=0b11110110;
if(PINA.5==0) return('4');
if(PINA.6==0) return('8');
if(PINA.7==0) return('9');


PORTA=0b11101110;
if(PINA.5==0) return('7');
if(PINA.6==0) return('0');
if(PINA.7==0) return('#');
}



Tidak ada komentar:

Posting Komentar