In this example we connect an RGB led to PORT C 0 – 2 of our PIC16F877A. Again we are using the Ql 200 PIC development board.
The RGB LED is a common anode type, so to light an individual LED you need to make the appropriate port pin low. In the examples below we will firstly switch various LEDs on individually , we will then loop through the various colours.
So for example 1 to make the red LED light we simply need to make Port C.0 go low and all other PORT C bits high, in this case we send a FE to PORT C, in binary this is 1111110. Similarily to switch the green LED on only we would send 0xFD, this converts to 11111101 in binary.
Lets look at the breadboard layout and the schematic
Breadboard layout
pic16f877 and rgb led breadboard
Schematic
pic16f877 and rgb led schematic
Code Examples
In the first example we light the red, green and blue LEDs individually
void main() { CMCON = 0x07; // To turn off comparators ADCON1 = 0x06; // To turn off adc TRISC = 0x00; // Sets all pins in PORTB as output PORTC = 0xFF; // Set RB0 to high 00000001 do // To set infinite loop { //RED on and off Delay_ms(250); // 300 mili seconds delay PORTC = 0xFE; Delay_ms(250); // 350 mili seconds delay PORTC = 0xFF; //GREEN on and off Delay_ms(250); // 300 mili seconds delay PORTC = 0xFD; Delay_ms(250); // 350 mili seconds delay PORTC = 0xFF; //BLUE on and off Delay_ms(250); // 300 mili seconds delay PORTC = 0xFB; Delay_ms(250); // 350 mili seconds delay PORTC = 0xFF; }while(1); // To set infinite loop }
In this example we loop through all available colours
void main() { CMCON = 0x07; // To turn off comparators ADCON1 = 0x06; // To turn off adc TRISC = 0x00; // Sets all pins in PORTB as output PORTC = 0xFF; // Set RB0 to high 00000001 do // To set infinite loop { PORTC = PORTC - 0x01; // Delay_ms(250); if(PORTC == 0xF8) //11111000 = R,G,B on { PORTC = 0xFF; //LEDS off Delay_ms(500); } }while(1); // To set infinite loop }
Links
PIC16F877 20pcs
PIC16F877 MQFP44 MICROCHIP FREE SHIPPING
Mini System PIC Development Board + Microchip PIC16F877 PIC16F877A+ USB Cable
Microcontroller Programmer Kit Development Board for PIC microcontroller evaluation QL200