mohikan
Joined: 15 Apr 2007 Posts: 1 Location: oradea, ro
|
|
Posted: Sun Apr 15, 2007 3:26 am |
|
|
I had a similar project, the LEDs were connected to port RA (RA2=LED Blue, RA3=LED Red, RA4=LED Green) each with its own resistor (RA4 needs pull-up), the software PWM is in timer2 ISR and it's from a Microchip MicroSolutions Newsletter.
Code: |
-------------------------------------------------------------------------------------
#include "RGBTest.h"
#INT_TIMER2
void TIMER2_isr()
{
#asm
movf pwm_counter,w
subwf pwm_duty_green,w
rlf output,f
movf pwm_counter,w
subwf pwm_duty_red,w
rlf output,f
movf pwm_counter,w
subwf pwm_duty_blue,w
rlf output,f
rlf output,f
rlf output,w
movwf PORTA
incf pwm_counter,f
clrf output
#endasm
}
#inline
void set_pwm_duty(ColorType color,int duty)
{
switch(color)
{
case RED:
{
pwm_duty_red=duty;
break;
}
case GREEN:
{
pwm_duty_green=duty;
break;
}
case BLUE:
{
pwm_duty_blue=duty;
break;
}
default:break;
}
}
void main()
{
set_tris_a(0x21);//00100001
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,0x4E,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_ccp1(CCP_OFF);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
printf("\n\rRGBTest v1.0 2006\n\r");
while(TRUE)
{
switch(getc())
{
case '7':
{
++pwm_duty_red;
set_pwm_duty(RED,pwm_duty_red);
break;
}
case '4':
{
--pwm_duty_red;
set_pwm_duty(RED,pwm_duty_red);
break;
}
case '8':
{
++pwm_duty_green;
set_pwm_duty(GREEN,pwm_duty_green);
break;
}
case '5':
{
--pwm_duty_green;
set_pwm_duty(GREEN,pwm_duty_green);
break;
}
case '9':
{
++pwm_duty_blue;
set_pwm_duty(BLUE,pwm_duty_blue);
break;
}
case '6':
{
--pwm_duty_blue;
set_pwm_duty(BLUE,pwm_duty_blue);
break;
}
default:break;
}
printf("> R:%03u G:%03u B:%03u\r",pwm_duty_red,pwm_duty_green,pwm_duty_blue);
}
}
-------------------------------------------------------------------------------------
#include <16F628A.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay(clock=4000000)
#use rs232(baud=19200,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=UART)
#use fast_io(a)
#define PORTA 0x05
typedef enum
{
RED,
GREEN,
BLUE
} ColorType;
unsigned int pwm_counter=0;
unsigned int pwm_duty_red=0;
unsigned int pwm_duty_green=0;
unsigned int pwm_duty_blue=0;
unsigned int output=0;
-------------------------------------------------------------------------------------
|
|
|