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

Issue With 12F1572 PWM Setup

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



Joined: 10 Mar 2014
Posts: 3

View user's profile Send private message

Issue With 12F1572 PWM Setup
PostPosted: Mon Mar 10, 2014 1:32 pm     Reply with quote

Hi Everyone! First time poster, long time browser. I've just installed MPLAB X after a very long hiatus and I'm trying to get back into using CCS.

Has anyone had a chance to play with setting up the three hardware PWMs on the 12F1572? I'm having a bad time sorting out what to do as I've only ever utilized timer2 and the ccp modules to generate my PWM outputs.

Below is my code. I'm not getting output from any of the PWM channels.

Code:

#include <12F1572.h>
#use fast_io(A)
#fuses INTRC_IO,NOPROTECT,NOWDT,NOMCLR,PUT,BROWNOUT
#use delay(clock=8000000)                     

#define GP0              PIN_A0    //  PWM 2
#define GP1              PIN_A1    //  PWM 1
#define GP2              PIN_A2    //  PWM 3

void main(void) {

set_tris_a(0b00000000);   
   
setup_comparator(NC_NC);
setup_dac(DAC_OFF)
setup_adc(ADC_OFF);   ;
set_adc_channel(NO_ANALOGS);   
setup_adc_ports(NO_ANALOGS);
setup_cwg(CWG_DISABLED, 0, 0, 0);

setup_pwm1(PWM_ENABLED | PWM_STANDARD | PWM_CLK_FOSC | PWM_CLK_DIV_BY_4 | PWM_PWM1_A1, PWM_DUTY_INT_ENABLED);
setup_pwm2(PWM_ENABLED | PWM_STANDARD | PWM_CLK_FOSC | PWM_CLK_DIV_BY_4 | PWM_PWM2_A0, PWM_DUTY_INT_ENABLED);
setup_pwm3(PWM_ENABLED | PWM_STANDARD | PWM_CLK_FOSC | PWM_CLK_DIV_BY_4 | PWM_OUTPUT, PWM_DUTY_INT_ENABLED);

 set_pwm1_duty(50);
 set_pwm2_duty(50);
 set_pwm3_duty(50);

 while(1){
 //stuff goes here
 }

}  //end of main



EDIT: My MPLAB version is 2.05, My CCS Plugin Version is 1.57, and my CCS PCM version is 5.021


Last edited by Eclectrical on Mon Mar 10, 2014 1:51 pm; edited 2 times in total
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Mar 10, 2014 1:34 pm     Reply with quote

first obvious question..
Have you got the '1Hz flashing LED' program to run correctly ?

I ask because a lot of people have had problems with X.

hth
jay
Eclectrical



Joined: 10 Mar 2014
Posts: 3

View user's profile Send private message

PostPosted: Mon Mar 10, 2014 1:46 pm     Reply with quote

Is that a specific test program I should compile and run?

I've verified that I'm compiling correctly and that I have control of the pins just by doing something like this in my while(1):

Code:

output_low(GP0);
output_low(GP1);
output_low(GP2);
delay_ms(500);
output_high(GP0);
output_high(GP1);
output_high(GP2);
delay_ms(500);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 10, 2014 3:42 pm     Reply with quote

Your code doesn't call these functions, and I believe they're required to
setup the PWM in the 12F1572:
Code:
set_pwm1_period();
set_pwm1_phase();


I would just concentrate on getting PWM channel 1. Once you get that
running, then worry about the other channels.
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 5:24 am     Reply with quote

good news , you've confirmed the PIC is alive and well, with your version of the 1Hz LED test. It's not a CCS example, rather one you write,compile,run to test the PIC hardware, PCB, basic setup config, etc. If it runs then you KNOW the PIC is getting power, and can diddle the I/O bits.
The next 'test program' is the 'Hello World' one, which will check out the serial communications to say a PC terminal program,It will confirm you've got hardware(MAX232) and wiring OK and the PC program configured correctly.
These two simple basic tests are necessary to eliminate 99% of the 'it don't work' problems often posted here where complex programs are posted and the real problem is a low level hardware or config issue.

bad news. while I don't use that PIC , I agree it's probably in the 'setup config' of the PWM peripheral.CCS may have an example( though not that specific PIC) that uses the PWM? Maybe google that PIC, PWM ,setup and C what hits you get.You can't be the only one using it !!!

hth
jay
Eclectrical



Joined: 10 Mar 2014
Posts: 3

View user's profile Send private message

PostPosted: Tue Mar 11, 2014 9:43 am     Reply with quote

Thank you very much for your help PCM programmer and temtronic! Messing with the channels one at a time and adding in the phase and period functions did the trick.

I think this is a relatively new part; I couldn't find much at all about it after a good googling. I'll post my functioning setup below just in case there are any other wayward travelers searching for a solution.


Code:
#include <12F1572.h>
#use fast_io(A)
#fuses INTRC_IO,NOPROTECT,NOWDT,NOMCLR,PUT,BROWNOUT
#use delay(clock=8000000)                     

#define GP0              PIN_A0    //  PWM 2
#define GP1              PIN_A1    //  PWM 1
#define GP2              PIN_A2    //  PWM 3

void main(void) {

set_tris_a(0b00000000);     
setup_comparator(NC_NC);
setup_dac(DAC_OFF)
setup_adc(ADC_OFF);   ;
set_adc_channel(NO_ANALOGS);   
setup_adc_ports(NO_ANALOGS);
setup_cwg(CWG_DISABLED, 0, 0, 0);


//-------------------------OSC Frequency
//PWM Freq  = --------------------------------------
//-------------(Period + 1) * (Timer Prescaler) * 4

setup_pwm1(PWM_ENABLED | PWM_STANDARD | PWM_CLK_FOSC | PWM_CLK_DIV_BY_2 | PWM_OUTPUT, PWM_DUTY_INT_ENABLED);
setup_pwm2(PWM_ENABLED | PWM_STANDARD | PWM_CLK_FOSC | PWM_CLK_DIV_BY_2 | PWM_OUTPUT, PWM_DUTY_INT_ENABLED);
setup_pwm3(PWM_ENABLED | PWM_STANDARD | PWM_CLK_FOSC | PWM_CLK_DIV_BY_2 | PWM_OUTPUT, PWM_DUTY_INT_ENABLED);

set_pwm1_period(511);
set_pwm2_period(511); 
set_pwm3_period(511); 

set_pwm1_phase(0);
set_pwm2_phase(0);
set_pwm3_phase(0);

set_pwm1_duty(256);  //50%
set_pwm2_duty(256);  //50%
set_pwm3_duty(256);  //50%

 while(1)
 {
 //stuff goes here
 }

}  //end of main
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