wuchy143
Member
Hi All,
I'm performing PWM to drive 5V LED's. Essentially the output of the chip is hooked up to a PFET which turns on/off a bank of ~LED's. The PWM on and off times are controlled by a pot which swings from 0--->5V. I read the A/D reading from the pot. From that reading I then load up Timer0, bang the port bit to turn off the PWM output. Then once that's done I load Timer0 again to achieve my On time.
Here is my code. Be gentle. I'm learning...haha j/k. Be rough I don't care. Just want to get better at this stuff.
The problem I am having is that when the LED's are stepping up in brightness just when they turn on my resoution stinks. It's very choppy from on brightness to the other levels. Currently each of my steps are 5uS but I want to make that smaller. That way if my steps are much smaller down at lower brightness it will give the human eye a much smoother change. I'm limited by the amount of steps I can have due to timer0 being a byte(256 steps).
Any guidance would be much appreciated. Thanks
I'm performing PWM to drive 5V LED's. Essentially the output of the chip is hooked up to a PFET which turns on/off a bank of ~LED's. The PWM on and off times are controlled by a pot which swings from 0--->5V. I read the A/D reading from the pot. From that reading I then load up Timer0, bang the port bit to turn off the PWM output. Then once that's done I load Timer0 again to achieve my On time.
Here is my code. Be gentle. I'm learning...haha j/k. Be rough I don't care. Just want to get better at this stuff.
Code:
void PWM (unsigned int On_Time);
void Read_AD_Pin(void);
void Gather_AD_Reading(void);
void Transform_AD_Reading(void);
unsigned int On_Time = 0x00;
unsigned int TMR0_MIRROR = 0x00;
unsigned int On_Time_Trans = 0x00;
unsigned int AD_10_Bit = 0x00;
void main() {
ADCON0 = 0b10000001; // a/d on
CMCON = 0x00; // Disable comparators
ANSEL = 0b00000001; //assigning digital i/o
TRISIO = 0b00000001; //ports are all outputs
OPTION_REG = 0b01011000;
T0IF_bit = 0; //clear timer0 overflow flag
for(;;)
{
Read_AD_Pin();
Gather_AD_Reading();
Transform_AD_Reading();
PWM(On_Time);
}
}
void PWM(unsigned int On_Time)
{
T0IF_bit = 0;
TMR0 = AD_10_Bit;
TMR0_MIRROR = 0xFF - AD_10_Bit;
while(T0IF_bit != 1)
{
GP2_bit = 0;
}
T0IF_bit = 0;
TMR0 = TMR0_MIRROR;
while(T0IF_bit != 1)
{
GP2_bit = 1;
}
T0IF_bit = 0;
}
void Read_AD_Pin(void)
{
NOT_DONE_bit = 1;
while(NOT_DONE_bit = 1)
{;}
}
void Gather_AD_Reading(void)
{
AD_10_Bit = (unsigned int)ADRESH;
AD_10_Bit <<= 8;
AD_10_Bit |=(unsigned int)ADRESL;
}
void Transform_AD_Reading(void)
{
AD_10_Bit = AD_10_Bit/4;
}
The problem I am having is that when the LED's are stepping up in brightness just when they turn on my resoution stinks. It's very choppy from on brightness to the other levels. Currently each of my steps are 5uS but I want to make that smaller. That way if my steps are much smaller down at lower brightness it will give the human eye a much smoother change. I'm limited by the amount of steps I can have due to timer0 being a byte(256 steps).
Any guidance would be much appreciated. Thanks