View previous topic :: View next topic |
Author |
Message |
lxc032
Joined: 12 Jul 2011 Posts: 5
|
Problem of using PIC16F877A |
Posted: Tue Jul 12, 2011 10:17 am |
|
|
Dear All,
I'm doing a project about PWM DIMMING using PIC16F877A,
I have two problems.
(1) How can I generate a phase shift between two LED strings?
(2) Following is the code of my project. However, It does not work.
What is the problem?
Code: |
#include<16F877A.h>
int value;
void adc ();
{
setup_adc(ALL_ANALOG);
set_adc_channel(1)
delay_ms(3000)
value = read_adc()
}
void PWM_generation;
{
setup_ccp1(CCP_PWM);
if (value==2)
{
setup_timer_2(T2_DIVBY_4, 100, 0);
set_pwm1_duty(50);
delay_ms(200);
}
else
{
setup_timer_2(T2_DIVBY_4, 250, 0);
set_pwm1_duty(125);
delay_ms(300);
}
set_pwm1_duty(0);
}
void main;
{
adc;
PWM_generation;
}
end |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
PWM dimming |
Posted: Tue Jul 12, 2011 2:12 pm |
|
|
Hi,
Give us more detail about frequency, resolution etc.
A sketch of the expected PWM signals (as viewed on a 'scope) would help.
Mike Walne |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Jul 12, 2011 3:02 pm |
|
|
Some comments inline.
Code: |
#include<16F877A.h>
//No clock statement - delays won't work properly - no fuses...
int value;
void adc ();
{
setup_adc(ALL_ANALOG);
set_adc_channel(1)
delay_ms(3000)
value = read_adc()
}
void PWM_generation; //Not a function declaration - creates a void variable
{
setup_ccp1(CCP_PWM);
if (value==2)
{
setup_timer_2(T2_DIVBY_4, 100, 0); //Incorrect syntax for the divisor
set_pwm1_duty(50);
delay_ms(200);
}
else
{
setup_timer_2(T2_DIVBY_4, 250, 0);
set_pwm1_duty(125);
delay_ms(300);
}
set_pwm1_duty(0);
}
void main;
{
adc;
PWM_generation; // function calls = ()
//No loop, so code will execute once, and then die.
}
end //Er. Different language....
|
'value' is very unlikely to ever be '2'. Would need approximately 0.0488v (assuming a 5v PIC) on the analog input to get this.
Best Wishes |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Jul 12, 2011 3:42 pm |
|
|
Also:
Code: | set_adc_channel(1)
delay_ms(3000)
value = read_adc()
|
The delay should be more like delay_us(20); _________________ Google and Forum Search are some of your best tools!!!! |
|
|
|