Hello friends.
I want to post an small contribution. It is an small rgb strip led controller with a PIC19F1933.
How the strip works?
Very simple, it has the three colors are pwm signals, it seems that are represented in rgb format but playing with it I have found that it is in cmyk format.
My code:
#include <16F1933.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOMCLR
#use delay(internal=2000000)
void draw_raw_color(unsigned int16 cyan, unsigned int16 magenta, unsigned int16 yellow);
void draw_color_in_cmyk(float cyan, float magenta, float yellow);
void draw_color_in_rgb(unsigned int red, unsigned int green, unsigned int blue);
void draw_raw_color(unsigned int16 cyan, unsigned int16 magenta, unsigned int16 yellow) {
set_pwm4_duty(cyan);
set_pwm1_duty(magenta);
set_pwm2_duty(yellow);
}
void draw_color_in_rgb(unsigned int red, unsigned int green, unsigned int blue) {
//Convert from RGB to CMYK
float r_prima = (float) red / (float) 255;
float g_prima = (float) green / (float) 255;
float b_prima = (float) blue / (float) 255;
unsigned int16 cyan = (1 - r_prima) * 1023;
unsigned int16 magenta = (1 - g_prima) * 1023;
unsigned int16 yellow = (1 - b_prima) * 1023;
//Set the colors
draw_raw_color(cyan, magenta, yellow);
}
void draw_color_in_cmyk(float cyan, float magenta, float yellow) {
unsigned int16 pwm_cyan = cyan * 1023;
unsigned int16 pwm_magenta = magenta * 1023;
unsigned int16 pwm_yellow = yellow * 1023;
draw_raw_color(pwm_cyan, pwm_magenta, pwm_yellow);
}
void main()
{
//122Hz frequency timer 2
setup_timer_2(T2_DIV_BY_16,255,1);
//no internal lcd
setup_lcd(LCD_DISABLED);
//Set all ports output
set_tris_a(0);
set_tris_b(0);
set_tris_c(0);
set_tris_e(0);
//Setup the PWMs
setup_ccp1(CCP_PWM | CCP_TIMER2); //Magenta
setup_ccp2(CCP_PWM | CCP_TIMER2); //Yellow
setup_ccp4(CCP_PWM | CCP_TIMER2); //Cyan
while(TRUE)
{
//draw the any color you want
draw_color_in_rgb(0, 0, 255); //Blue
delay_ms(2000);
draw_raw_color(1023, 1023, 1023); //Power OFF
delay_ms(2000);
draw_color_in_cmyk(1, 0, 1); //Green
delay_ms(2000);
}
}
The first part, this:
#include <16F1933.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOMCLR
#use delay(internal=2000000)
void draw_raw_color(unsigned int16 cyan, unsigned int16 magenta, unsigned int16 yellow);
void draw_color_in_cmyk(float cyan, float magenta, float yellow);
void draw_color_in_rgb(unsigned int red, unsigned int green, unsigned int blue);
is the pic configuration and the functions declaration
The first function draw_raw_color:
void draw_raw_color(unsigned int16 cyan, unsigned int16 magenta, unsigned int16 yellow) {
set_pwm4_duty(cyan);
set_pwm1_duty(magenta);
set_pwm2_duty(yellow);
}
Puts the color in the PWM range to the PWM
The second is the function draw_rgb_color:
void draw_color_in_rgb(unsigned int red, unsigned int green, unsigned int blue) {
//Convert from RGB to CMYK
float r_prima = (float) red / (float) 255;
float g_prima = (float) green / (float) 255;
float b_prima = (float) blue / (float) 255;
unsigned int16 cyan = (1 - r_prima) * 1023;
unsigned int16 magenta = (1 - g_prima) * 1023;
unsigned int16 yellow = (1 - b_prima) * 1023;
//Set the colors
draw_raw_color(cyan, magenta, yellow);
}
Draws the colors in RGB, here is where the mathematics start:
The CMYK colours are a range between 0 and 1, usually we use percents, of cyan, magenta, and yellow and a key, where I haven't set it here but is 0.Unlikely CMYK, RGB colors are in range of 0 to 255, to convert to this to CMYK and later push to the PWM, we need to know the percent of the color in the rgb scales,i.e. :
The color (89, 100, 83) has a (89 /255 ) * 100 % of red color, (100 /255 ) * 100 % of green color and (83 /255 ) * 100 % of blue color.
In this program to save operations and memory I only do the division.Now we have the RGB percents, we have to convert to CMYK percents, substracting to 1 the result of the above operation and later multiply by 1023 to get the color in pwm range and push to the PWM
After explaining this, Here we have cmyk function,wich is very easy, we have the color in cmyk float, so we only need to multiply that to 1023 to convert to PWM range:
void draw_color_in_cmyk(float cyan, float magenta, float yellow) {
unsigned int16 pwm_cyan = cyan * 1023;
unsigned int16 pwm_magenta = magenta * 1023;
unsigned int16 pwm_yellow = yellow * 1023;
draw_raw_color(pwm_cyan, pwm_magenta, pwm_yellow);
}
And the main function, where I have setup the PWM and the program loop:
void main()
{
//122Hz frequency timer 2
setup_timer_2(T2_DIV_BY_16,255,1);
//no internal lcd
setup_lcd(LCD_DISABLED);
//Set all ports output
set_tris_a(0);
set_tris_b(0);
set_tris_c(0);
set_tris_e(0);
//Setup the PWMs
setup_ccp1(CCP_PWM | CCP_TIMER2); //Magenta
setup_ccp2(CCP_PWM | CCP_TIMER2); //Yellow
setup_ccp4(CCP_PWM | CCP_TIMER2); //Cyan
while(TRUE)
{
//draw the any color you want
draw_color_in_rgb(0, 0, 255); //Blue
delay_ms(2000);
draw_raw_color(1023, 1023, 1023); //Power OFF
delay_ms(2000);
draw_color_in_cmyk(1, 0, 1); //Green
delay_ms(2000);
}
}
Here we set all the output to out, setup the pwm and draw blue color, wait 2 seconds, power off, wait 2 seconds and draw green and wait 2 seconds to start.
I attach a link of a video of a prototype working and the code
Thanks for watching this, and if you have some questions or want to say something to me ask me here