|
|
View previous topic :: View next topic |
Author |
Message |
lxc032
Joined: 12 Jul 2011 Posts: 5
|
PWM signal program |
Posted: Fri Jul 29, 2011 10:37 am |
|
|
Hi everyone. I'm developing a PWM dimming program using PIC18F4431. There are two LED strings in my circuit. Everything is working except that when the duty cycle of one of the LED strings achieves 100%, the duty cycle of the other string will suddenly drop to 0%. The main body of my code is listed as follows.
Code: |
while(1)
{
set_adc_channel(0);
delay_us(20);
Vsense_adc_read_1 = read_adc();
delay_us(20);
Vsense_dimm_1 = (int32)(Vsense_adc_read_1*iADCgain_n16);
PWM_Duty1 = (int16)(Vsense_dimm_1);
PWM_Duty2 = (int16)(PWM_Duty1+3000);
PDC0L = make8(PWM_Duty1,0);
PDC0H = make8(PWM_Duty1,1);
PDC1L = make8(-PWM_Duty2,0);
PDC1H = make8(-PWM_Duty2,1);
}
|
I have no idea to figure out the problem. Please give me a help. Thank You~! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jul 29, 2011 12:22 pm |
|
|
Since it's not a complete program, no one here can compile, run and see why it fails so....
Add code to send the PWM values to either a local LCD or to a PC terminal program. Also send the ADC value as well.
That way you can easily debug when and why 'it goes funny'. |
|
|
lxc032
Joined: 12 Jul 2011 Posts: 5
|
|
Posted: Fri Jul 29, 2011 2:39 pm |
|
|
My code is listed as follows.
Code: |
#include <18f4331.h>
#device adc=10
#use delay(clock=4000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT,MCLR,BORV27,HPOL_HIGH,LPOL_HIGH,T1STANDARD
//================================================================
#byte PWMCON0=0xF6F
#byte PWMCON1=0xF6E
#byte PTCON0=0xF7F
#byte PTCON1=0xF7E
#byte PTPERL=0xF7B
#byte PTPERH=0xF7A
#byte PDC0L=0xF79
#byte PDC0H=0xF78
#byte PDC1L=0xF77
#byte PDC1H=0xF76
#byte PDC2L=0xF75
#byte PDC2H=0xF74
#byte TMR1H=0xFCF
#byte TMR1L=0xFCE
#byte DTCON=0xF6D
#byte PTMRL=0xF7D
#byte PTMRH=0xF7C
#byte OVDCOND=0xF6B
#byte OVDCONS=0xF6A
void main()
{
signed int16 PWM_Duty1=0;
signed int16 PWM_Duty2=0;
signed int16 Vsense_adc_read_1=0;
signed int16 Vsense_dimm_1=0;
signed int32 iADCgain_n16=20;
set_tris_b(0x0C);
//setup_ccp1(CCP_PWM);
PTCON0=0x00;
PTCON1=0x80;
PTPERL=0xFF;
PTPERH=0x0C;
DTCON=0x07;
PWMCON0=0x5D;
PWMCON1=0x00;
setup_adc_ports(sAN0);
setup_adc_ports(sAN1);
setup_adc_ports(sAN2);
setup_adc_ports(sAN3);
setup_adc(ADC_CLOCK_DIV_16);
//setup_timer_2(T2_DIV_BY_16,255,1);
while(1)
{
set_adc_channel(0);
delay_us(20);
Vsense_adc_read_1 = read_adc();
delay_us(20);
Vsense_dimm_1 = (int32)(Vsense_adc_read_1*iADCgain_n16);
PWM_Duty1 = (int16)(Vsense_dimm_1);
PWM_Duty2 = (int16)(PWM_Duty1+3000);
PDC0L = make8(PWM_Duty1,0);
PDC0H = make8(PWM_Duty1,1);
PDC1L = make8(-PWM_Duty2,0);
PDC1H = make8(-PWM_Duty2,1);
}
/*PWM_Duty1 = 3200;
PDC0L = make8(PWM_Duty1,0);
PDC0H = make8(PWM_Duty1,1);
*/
}
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|