CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

10 bit hardware PWM on a 12F683

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ccowley



Joined: 12 Sep 2009
Posts: 19

View user's profile Send private message

10 bit hardware PWM on a 12F683
PostPosted: Thu Sep 17, 2009 4:45 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 17, 2009 5:08 pm     Reply with quote

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

View user's profile Send private message

Thanks for trying to help!
PostPosted: Thu Sep 17, 2009 8:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 17, 2009 8:43 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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