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 support@ccsinfo.com

PWM PIC12LF1822

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



Joined: 10 Aug 2011
Posts: 8
Location: Italia

View user's profile Send private message

PWM PIC12LF1822
PostPosted: Wed Apr 15, 2015 4:22 pm     Reply with quote

Hi Everyone,

I'm using a PIC12LF1822 and I need to have the hardware PWM working on PIN A5.
The PWM frequency I would like to have as an output should have a frequency of 400 Hz.
I'm using an internal oscillator at a frequency of 4 MHz.
The compiler version is 4.138.13.14.

I've try different test for this but the system still doesn't work.
I've follow what the data sheet Microchip suggest to do to setup correctly the PWM but nothing happens.

Following you can find my .h and .c
Code:

#include <12LF1822.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES WDT_SW                   //No Watch Dog Timer, enabled in Software
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES PLL_SW                   //4X HW PLL disabled, 4X PLL enabled/disabled in software
#FUSES NOSTVREN                 //Stack full/underflow will not cause reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG

#use delay(clock=4000000)

// INTCON Register      ******************************************
#byte INTCON = getenv("SFR:INTCON")
#bit IOCIF = INTCON.0
#bit INTF = INTCON.1
#bit TMR0IF = INTCON.2
#bit IOCIE = INTCON.3
#bit INTE = INTCON.4
#bit TMR0IE = INTCON.5
#bit PEIE = INTCON.6
#bit GIE = INTCON.7

// PIE1 Register      ******************************************
#byte PIE1 = getenv("SFR:PIE1")
#bit TMR1IE = PIE1.0
#bit TMR2IE = PIE1.1
#bit CCP1IE = PIE1.2
#bit SSP1IE = PIE1.3
#bit TXIE = PIE1.4
#bit RCIE = PIE1.5
#bit ADIE = PIE1.6
#bit TMR1GIE = PIE1.7

// TRISA Register      ******************************************
#byte TRISA = getenv("SFR:TRISA")
#bit TRISA0 = TRISA.0
#bit TRISA1 = TRISA.1
#bit TRISA2 = TRISA.2
#bit TRISA3 = TRISA.3
#bit TRISA4 = TRISA.4
#bit TRISA5 = TRISA.5

// PORTA Register      ******************************************
#byte PORTA = getenv("SFR:PORTA")
#bit RA0 = PORTA.0
#bit RA1 = PORTA.1
#bit RA2 = PORTA.2
#bit RA3 = PORTA.3
#bit RA4 = PORTA.4
#bit RA5 = PORTA.5

// APFCON Register      ******************************************
#byte APFCON = getenv("SFR:APFCON")
#bit CCP1SEL = APFCON.0
#bit P1BSEL = APFCON.1
#bit TXCKSEL = APFCON.2
#bit T1GSEL = APFCON.3
#bit SSSEL = APFCON.5
#bit SDOSEL = APFCON.6
#bit RXDTSEL = APFCON.7

// PR2 Register         ******************************************
#byte PR2 = getenv("SFR:PR2")

// T2CON Register      ******************************************
#byte T2CON = getenv("SFR:T2CON")
#bit T2CKPS0 = T2CON.0
#bit T2CKPS1 = T2CON.1
#bit TMR2ON = T2CON.2
#bit T2OUTPS0 = T2CON.3
#bit T2OUTPS1 = T2CON.4
#bit T2OUTPS2 = T2CON.5
#bit T2OUTPS3 = T2CON.6

// CCPR1L Register      ******************************************
#byte CCPR1L = getenv("SFR:CCPR1L")

// CCP1CON Register      ******************************************
#byte CCP1CON = getenv("SFR:CCPR1L")
#bit CCP1M0 = CCP1CON.0
#bit CCP1M1 = CCP1CON.1
#bit CCP1M2 = CCP1CON.2
#bit CCP1M3 = CCP1CON.3
#bit DC1B0 = CCP1CON.4
#bit DC1B1 = CCP1CON.5
#bit P1M0 = CCP1CON.6
#bit P1M1 = CCP1CON.7

// PIR1 Register      ******************************************
#byte PIR1 = getenv("SFR:PIR1")
#bit TMR1IF = PIR1.0
#bit TMR2IF = PIR1.1
#bit CCP1IF = PIR1.2
#bit SSP1IF = PIR1.3
#bit TXIF = PIR1.4
#bit RCIF = PIR1.5
#bit ADIF = PIR1.6
#bit TMR1GIF = PIR1.7


Code:

#include <main.h>

#int_TIMER2
void  TIMER2_isr(void)
{
//output_toggle(PIN_A5);
}

void main()
{
// Setup APFCON Register      ******************************************
CCP1SEL = 1;      // CCP1/P1A function is on RA5 PWM_DIMMING   
P1BSEL = 1;         // P1B function is on RA4
TXCKSEL = 0;      // TX/CK function is on RA0
T1GSEL = 1;         // T1G function is on RA3
SSSEL = 0;          // SS function is on RA3 (Slave Select SPI)
SDOSEL = 0;         // SDO function is on RA0 (Serial Data Out SPI)
RXDTSEL = 0;      // RX/DT function is on RA1

// Setup TRISA Register         ******************************************
TRISA0 = 1;         // ICSP DAT (not used)
TRISA1 = 0;         // SCL
TRISA2 = 1;         // SDA
TRISA3 = 1;         // VPP (not used)
TRISA4 = 1;         // INT_ACC
TRISA5 = 1;         // PWM_DIMMING

// Setup PWM frequency      ******************************************
PR2 = 155;         // Frequency 400Hz

// Setup PWM mode         ******************************************
CCP1M0 = 0;         // 1100 = PWM mode: P1A, P1C active-high; P1B, P1D active-high
CCP1M1 = 0;
CCP1M2 = 1;
CCP1M3 = 1;
P1M0 = 0;
P1M1 = 0;

// Setup PWM duty cycle    ******************************************
CCPR1L = 128;
DC1B0 = 0;
DC1B1 = 0;

// Clear interrupt Timer2 for PWM      ******************************************
TMR2IF = 0;

// Setup Timer2 Prescaler      ******************************************
T2CKPS0 = 0;
T2CKPS1 = 1;      // 10 = Prescaler is 16

delay_ms(100);

// Enable Timer2         ******************************************
TMR2ON = 1;

TRISA5 = 0;         // PWM_DIMMING



   while(TRUE)
   {

   }

}

Can someone help me find what's wrong?
Thank you in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 15, 2015 8:17 pm     Reply with quote

Quote:

I need to have the hardware PWM working on PIN A5.
The PWM frequency I would like to have as an output should have a
frequency of 400 Hz.

The easiest way to do this, if you have a modern version of the compiler,
is as shown below. The compiler should accept "OUTPUT_PIN=PIN_A5"
and program the APFCON and the TRIS for that, but it doesn't work. So
I had to add that part manually at the start of main(). This was tested
with compiler vs. 5.044 and it was tested in hardware.
Code:

#include <12LF1822.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)

#use pwm(CCP1, FREQUENCY=400, DUTY=50, OUTPUT=PIN_A5)

#byte APFCON = getenv("SFR:APFCON")
#bit CCP1SEL = APFCON.0

//======================
void main()     
{
CCP1SEL = 1;  // CCP1 output on Pin A5
output_low(PIN_A5);  // Configure Pin A5 as an output pin

while(TRUE);
}
 



Quote:
The compiler version is 4.138.13.14.

That's not the compiler version. Look at the top of your .LST file to find
the version number. The .LST file will be in your project directory after
you compile the source file with no errors. Example of version numbers:
http://www.ccsinfo.com/devices.php?page=versioninfo
mancio76



Joined: 10 Aug 2011
Posts: 8
Location: Italia

View user's profile Send private message

PostPosted: Thu Apr 16, 2015 12:52 am     Reply with quote

Hi,รน
thank you for the answer.
I've checked the compiler version:
"CCS PCM C Compiler, Version 4.138"

It's not so fresh but I've developed a long term project on this one and I wont change (for the moment) to the new 5.xx.

As you can know, with this kind of compiler version, the directive #use PWM it's not managed.

Before to arrive at managing the single register value, I've made experiments with the functions and directives provided by the compiler but nothing happens.

Can you help me a little more?

Thank you in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 16, 2015 1:23 am     Reply with quote

This should work with your version:
Code:

#include <12LF1822.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)

//======================
void main()     
{
setup_ccp1(CCP_PWM | CCP_P1A_A5);     // Output on pin A5
setup_timer_2(T2_DIV_BY_16, 155, 1);  // 400 Hz
set_pwm1_duty(78);                    // 50% duty cycle

while(TRUE);
}
 
mancio76



Joined: 10 Aug 2011
Posts: 8
Location: Italia

View user's profile Send private message

PostPosted: Thu Apr 16, 2015 9:52 am     Reply with quote

Thank you, it works!!
Great!!
MACDAVI



Joined: 20 Feb 2018
Posts: 1

View user's profile Send private message

P1B - Modification
PostPosted: Tue Feb 20, 2018 2:47 pm     Reply with quote

PCM programmer wrote:
This should work with your version:
Code:

#include <12LF1822.h>
#fuses INTRC_IO, NOWDT
#use delay(clock=4M)

//======================
void main()     
{
setup_ccp1(CCP_PWM | CCP_P1A_A5);     // Output on pin A5
setup_timer_2(T2_DIV_BY_16, 155, 1);  // 400 Hz
set_pwm1_duty(78);                    // 50% duty cycle

while(TRUE);
}
 


Hello everyone, I am using this microcontroller and would like to check how to change the PWM output to the P1B pin 7 of the microcontroller.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: P1B - Modification
PostPosted: Wed Feb 21, 2018 12:17 am     Reply with quote

MACDAVI wrote:

I am using this microcontroller and would like to check how to
change the PWM output to the P1B pin 7 of the microcontroller.

If you have a modern version of the CCS compiler, the pin selection
can be done with the setup_ccp1() function. Look in the 12LF1822.h file
in the "CCP Functions" section. It lists a constant parameter that will
select Pin A0 for P1B, as shown in bold below:
Quote:
setup_ccp1(CCP_PWM | CCP_P1B_A0 );
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