Tri-colour LED adalah LED yang mempunyai tiga keluaran warna. Tri-colour LED boleh bernyala dengan warna merah, hijau ataupun biru iaitu warna-warna asas RGB. Tri-colour LED seolah-olah menggabungkan tiga biji LED dan mempunyai empat kaki. Salah satu kakinya adalah common.
Gambar di atas adalah salah satu tri-colour LED common anode. Common anode bermaksud, kaki common di sambungkan ke 5V dan tri-colour LED akan menyala apabila mana-mana kaki lain bersambung ke 0V. Setiap kaki boleh di kawal kecerahannya menggunakan PWM dan gabungan warna-warna akan menghasilkan warna lain seperti kod warna di bawah.
Colours | HEX Code | ||
R | G | B | |
BLACK | 00 | 00 | 00 |
RED | FF | 00 | 00 |
GREEN | 00 | 80 | 00 |
BLUE | 00 | 00 | FF |
YELLOW | FF | FF | 00 |
PINK | FF | C0 | CB |
ORANGE | FF | A5 | 00 |
PURPLE | 80 | 00 | 80 |
BROWN | A5 | 2A | 2A |
WHITE | FF | FF | FF |
Rujukan: http://en.wikipedia.org/wiki/Web_colors
Berikut adalah videonya.
PIC Source Code
/*
Project: Tri Colour LED Lesson
PIC (Crystal Frequency):
PIC16F877A (20MHz), PIC16F887 (20MHz), PIC16F1939 (Internal 32MHz)
PIC18F4520 (10MHz), PIC18F4550 (10MHz), PIC18F46K22 (Internal 16MHz)
Compiler: HI-TECH C for PIC10/12/16 V9.83, PIC18 V9.80
Last Modified: 20 November 2013
Website: http://shahrulnizam.com
*/
#define ADC
#define HD44780
#define TIMER0
#if defined (_16F877A) || (_16F887)
#define RED PORTBbits.RB0
#define GREEN PORTBbits.RB1
#define BLUE PORTBbits.RB2
#elif defined (_16F1939) || (_18F4520) || (_18F4550) || (_18F46K22)
#define RED LATBbits.LATB0
#define GREEN LATBbits.LATB1
#define BLUE LATBbits.LATB2
#endif
#include "LibraryHardware.h"
#include "LibraryPeripheral.h"
#include "LibraryUniversalDisplay.h"
#include "LibraryMath.h"
char counter,red,green,blue;
static void interrupt isr(void)
{
if(TMR0IF==1){
TMR0IF=0;
counter++;
if(counter==255){
counter=0;
RED=0;
GREEN=0;
BLUE=0;}
if(counter==red) RED=1;
if(counter==green) GREEN=1;
if(counter==blue) BLUE=1;
}
}
void setup()
{
initialize();
TRISA=0b00000111;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;
timer0(8);
enable_tmr0();
BUZZER=1;
adc_init(0);
adc_init(1);
adc_init(2);
lcd_init();
lcd_goto(0,0);
lcd_string(" TRI COLOUR LED EX ");
}
void loop()
{
red=map(adc_read(0),0,1023,0,255);
green=map(adc_read(1),0,1023,0,255);
blue=map(adc_read(2),0,1023,0,255);
lcd_goto(0,1);
lcd_string(" RED = ");
lcd_number(red,HEX,2);
lcd_string(" ");
lcd_goto(0,2);
lcd_string(" GREEN = ");
lcd_number(green,HEX,2);
lcd_string(" ");
lcd_goto(0,3);
lcd_string(" BLUE = ");
lcd_number(blue,HEX,2);
lcd_string(" ");
}
Hi,
Nak tanya sikit tentang timer0 interrupt PWM code.
Jika red = 200, bermakna duty cycle nya ialah 200?
tetapi,
dari counter = 0-199, RED = 0,
dari counter = 200-255, RED = 1.
duty cycle RED hanya 55? duty cycle macam jadi inverse?
LimSH, bergantung kpd common anode atau cathode. Yg kat atas common anode, ketika 0 akan menyala, ketika 1 akan padam.
OIC ! betul pun , tak terfikir dengan hardware!
Hi,
just to ask, this one involve the ADC right ?
vick, yes this example involve ADC to control the brightness.
Does this involve PWM ?
The brightness use PWM, but the code not use PWM Module. Just use Timer0 Interrupt to generate Low Frequency PWM.
salam..kalau nak buat untuk 2 warna bleh x
w/salam youngteen, 2 warna pun boleh.