|
|
View previous topic :: View next topic |
Author |
Message |
brood
Joined: 23 Jan 2007 Posts: 12
|
Pwm on Pic18f24k22 |
Posted: Mon Oct 24, 2011 10:38 am |
|
|
Hi All,
I'm having problems with the pwm to work the pic. It seems that I need to specify the timer the pwm has to use according to the datasheet eg. timer2, 4 or 6 and I cant seem to find how to set that. The compiler version I'm using is 4.114
My code is attached below:
Code: |
#include <18F24k22.h>
#device ADC=10
#fuses INTRC_IO,NOPROTECT,NOBROWNOUT,NOWDT,PUT,NOSTVREN,NOFCMEN,NOMCLR,NOIESO
#use delay(clock=64000000)
void main()
{
setup_oscillator(OSC_64MHZ|OSC_NORMAL);
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_timer_2(T2_DIV_BY_4, 0x8c, 1);
SET_PWM1_DUTY(300);
// main loop
for (;;)
{
}
}
|
Thanks in advance |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 24, 2011 11:28 am |
|
|
Add an upper-case 'L' to the end of the parameter. Now you will probably
see PWM output:
Quote: |
SET_PWM1_DUTY(300L);
|
This post has links to explain the reason for this:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1
See the links on 8-bit vs. 10-bit PWM mode. |
|
|
brood
Joined: 23 Jan 2007 Posts: 12
|
|
Posted: Tue Oct 25, 2011 3:14 am |
|
|
Hi PCM,
I've tried that but it doesn't work. Please see the following taken from the datasheet:
Quote: |
14.3.2 SETUP FOR PWM OPERATION
The following steps should be taken when configuring the CCP module for standard PWM operation:
1. Disable the CCPx pin output driver by setting the associated TRIS bit.
2. Select the 8-bit TimerX resource, (Timer2,Timer4 or Timer6) to be used for PWM generation by setting the CxTSEL<1:0> bits in the CCPTMRSx register. (1)
3. Load the PRx register for the selected TimerX with the PWM period value.
4. Configure the CCP module for the PWM mode by loading the CCPxCON register with the appropriate values.
5. Load the CCPRxL register and the DCxB<1:0> bits of the CCPxCON register, with the PWM duty cycle value
|
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 25, 2011 2:09 pm |
|
|
Vs. 4.114 is very buggy with the 18F24K22 and the whole 18FxxK22
family. To fix it, two built-in functions have to be replaced with macros
that work. Try the program below. I don't have your exact PIC, but I
tested it with a PIC in the same family and it works.
Code: |
#include <18F45K22.h>
#device ADC=10
#fuses INTRC_IO,NOPROTECT,NOBROWNOUT,NOWDT,PUT,NOSTVREN,NOFCMEN,NOMCLR,NOIESO
#use delay(clock=64000000)
//----------------------------------------------------
// This macro replaces the CCS built-in function setup_timer_2():
#byte T2CON = 0xFBA
#byte PR2 = 0xFBB
#define setup_timer_2(t2_mode, t2_period, t2_ps) \
T2CON = (t2_mode | ((t2_ps -1) << 3)); \
PR2 = (t2_period);
//----------------------------------------------------
// This macro replaces the CCS function for setup_ccp1():
#byte CCP1CON = 0xFBD
#byte PWM1CON = 0xFB7
#byte ECCP1AS = 0xFB6
#byte PSTR1CON = 0xFB9
#define setup_ccp1(mode) \
output_low(PIN_C2); \
CCP1CON = make8(mode, 0); \
PWM1CON = 0; \
ECCP1AS = make8(mode, 2); \
if(make8(mode, 3) == 0x00) \
PSTR1CON = 1;
//=========================================
void main()
{
setup_oscillator(OSC_64MHZ|OSC_NORMAL);
setup_ccp1(CCP_PWM);
setup_comparator(NC_NC_NC_NC);
setup_timer_2(T2_DIV_BY_4, 0x8c, 1);
SET_PWM1_DUTY(300L);
// main loop
for (;;)
{
}
} |
|
|
|
brood
Joined: 23 Jan 2007 Posts: 12
|
|
Posted: Wed Oct 26, 2011 5:25 am |
|
|
I got it to work using the following code:
Quote: |
#include <18F24k22.h>
#device ADC=10
#fuses INTRC_IO,NOPROTECT,BROWNOUT,BORV45,NOWDT,PUT,NOSTVREN,NOFCMEN,MCLR,NOIESO
#use delay(clock=64000000)
#byte PWM_TMR_SELECT = 0xF49
void main()
{
setup_oscillator(OSC_64MHZ|OSC_NORMAL);
PWM_TMR_SELECT = 0x49; // uses timer 2 for pwm
setup_ccp1(CCP_PWM); // setup pwm module
setup_comparator(NC_NC_NC_NC);
setup_timer_4(T4_DIV_BY_4, 0x8c, 1); // ~ 20Khz pwm 8c ~64khz ff
SET_PWM1_DUTY(300);
// main loop
for (;;)
{
}
}
|
Is the other timers also affected as I will be using them later on in the program? |
|
|
|
|
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
|