|
|
View previous topic :: View next topic |
Author |
Message |
ccowley
Joined: 12 Sep 2009 Posts: 19
|
10 bit hardware PWM on a 12F683 |
Posted: Thu Sep 17, 2009 4:45 pm |
|
|
I have read everything I can find on the 10 bit PWM, but I still don't seem to be able to get it working properly. Admittedly I am new to programming in C. I have always programmed PICS with Pic Basic Pro and my transition has been somewhat slow. Pic Basic Pro doesn't support 10 bit PWM and I need it for a smooth fade-up/fade-down of an LED, so I thought this would be a simple project.
Any help for this slow C learner would be greatly appreciated!!!
The code I have been messing with is below. At this point I have just been trying to get it to switch between different brightness levels, not even fade, but it is not working.
PCW Compiler V. 4.099
Code: |
#include <12F683.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPUT //No Power Up Timer
#FUSES BROWNOUT //Reset when brownout detected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#use delay(clock=4000000)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,0,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
setup_comparator(NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_4MHZ);
// TODO: USER CODE!!
// int16 duty;
// duty=1000;
while (true)
{
set_pwm1_duty(1000L);
delay_ms (1000);
set_pwm1_duty(500L);
delay_ms (1000);
set_pwm1_duty (1L);
delay_ms (1000);
set_pwm1_duty (128L);
delay_ms (1000);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 17, 2009 5:08 pm |
|
|
Here's an example of 10-bit PWM mode. If you turn the trimpot, it
changes the PWM duty cycle.
Code: | #include <16F877.h>
#device adc=10
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//================================
void main()
{
int16 value;
setup_ccp1(CCP_PWM);
// Set the PWM frequency for 245 Hz (with a 4 MHz crystal)
setup_timer_2(T2_DIV_BY_16, 254, 1);
setup_port_a(AN0);
setup_adc(ADC_CLOCK_DIV_8); // Divisor for 4 MHz crystal
set_adc_channel(0);
delay_us(20);
while(1)
{
value = read_adc(); // 10-bit ADC result
set_pwm1_duty(value);
}
} |
|
|
|
ccowley
Joined: 12 Sep 2009 Posts: 19
|
Thanks for trying to help! |
Posted: Thu Sep 17, 2009 8:36 pm |
|
|
I appreciate the effort at helping me out! As I mentioned, I have read everything I could find on it already, so I have already looked at that code. I guess what I am really asking is, "What is wrong with my code, that it doesn't work?" I must be doing something wrong, but I am not sure what it is. I am still in the process of getting a grip on the C language.
Do you see anything wrong my code? I will continue to tinker around and try to solve the problem. I just thought someone might be able to quickly see what my coding problem was so that I wouldn't have to continue spending so much time with the trial and error route.
Thanks again, I do appreciate the effort! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 17, 2009 8:43 pm |
|
|
Most of the code in your program is unnecessary for PWM.
Look closely at the functions in my program, and compare the parameters
that I use to the ones that you use in the same functions. You will see
the problem. |
|
|
|
|
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
|