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 Question

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



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PWM Question
PostPosted: Wed Feb 21, 2007 4:57 pm     Reply with quote

Hi,

I need to obtain the max frecuency in PWM, I see a answer from PCM Programmer, and say:

http://www.ccsinfo.com/forum/viewtopic.php?t=17993

Code:
The frequency calculation goes like this:
Código:

                      Crystal Frequency
PWM Freq  = -----------------------------------------
                  (PR2 + 1) * (Timer2 Prescaler) * 4

The Timer2 prescaler can have values of 1, 4, or 16.

PR2 can have a value from 0 to 255.

So the lowest PWM frequency for your application would be:
Código:

         24 MHz                24,000,000
 -----------------------  =  ---------------  = 1465 Hz     
 (255 + 1) * 16 * 4             16,384


Here is a test program for this:
(I don't have a 16c745 to test, so I'm putting in the
likely fuse values that I think you would use).

Código:
#include <16c745.h>
#fuses H4, NOWDT, NOPROTECT, PUT    // H4 = use x4 PLL mode
#use Delay(clock=24000000)

main()
{
output_low(PIN_C2);    // Set CCP1 output low
setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 255, 1);   // Setup for 1465 Hz     
set_pwm1_duty(128);                    //  50% duty cycle

while(1);         // Prevent PIC from going to sleep
}



My PIC18F4620 work to 25Mhz, and for this the equation is:

25000000/0+1*1*4=6,25Mhz

for this my code is:

Code:
    #use fast_io (C)
    #use fast_io (D)
    set_tris_c( 0 );
    set_tris_d( 0 );
   
    setup_ccp1(CCP_PWM);
    set_pwm1_duty(1);
    setup_timer_2(T2_DIV_BY_1,0,1);


But not work!!, the max frecuency is if I replace the Period 0 by 1, 3.125Mhz.

Why not work Period in Timer2 equal to 0??????

Thank you very much!

Best Regards,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 21, 2007 7:56 pm     Reply with quote

Here is a test program that shows how to get the maximum PWM
frequency. The key point is that your duty cycle value should be
an 'int16' value, in order to get any resolution at all, when your PR2
value is very low.

The program below has a PWM frequency of 5 Mhz. It has five steps
available for the duty cycle: 0%, 25%, 50%, 75%, and 100%.
When you increase the PWM frequency up to a very high value, the
tradeoff is that you have very low resolution (only five steps).

The program below will put out PWM pulses for each duty cycle
for one second each. Then it repeats.
Code:

#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=======================
void main()
{
int16 duty;   

setup_timer_2(T2_DIV_BY_1, 0,1);
setup_ccp1(CCP_PWM);

while(1)
  {
   duty = 0;    // 0% duty cycle         
   set_pwm1_duty(duty);
   delay_ms(1000);

   duty = 1;    // 25% duty cycle
   set_pwm1_duty(duty);
   delay_ms(1000);

   duty = 2;    // 50% duty cycle
   set_pwm1_duty(duty);
   delay_ms(1000);
 
   duty = 3;    // 75% duty cycle
   set_pwm1_duty(duty);
   delay_ms(1000);

   duty = 4;    // 100% duty cycle
   set_pwm1_duty(duty);
   delay_ms(1000);
  }

}
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Thu Feb 22, 2007 7:45 am     Reply with quote

Dear PCM Programmer!

I test your code and not work, I use 18F4620 to 25Mhz. This is the .LST. What is wrong????????

Code:
CCS PCH C Compiler, Version 4.025, 37009               22-Feb-07 10:41

               Filename: C:\Clock Generate\test.lst

               ROM used: 394 bytes (1%)
                         Largest free fragment is 65142
               RAM used: 8 (0%) at main() level
                         9 (0%) worst case
               Stack:    1 locations

*
00000:  GOTO   002C
.................... #include <18F4620>
.................... //////// Standard Header file for the PIC18F4620 device ////////////////
.................... #device PIC18F4620
.................... #list
.................... 
.................... #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
.................... #use delay(clock=25000000)
00004:  CLRF   FEA
00006:  MOVLW  09
00008:  MOVWF  FE9
0000A:  MOVF   FEF,W
0000C:  BZ    002A
0000E:  MOVLW  08
00010:  MOVWF  01
00012:  CLRF   00
00014:  DECFSZ 00,F
00016:  BRA    0014
00018:  DECFSZ 01,F
0001A:  BRA    0012
0001C:  MOVLW  19
0001E:  MOVWF  00
00020:  DECFSZ 00,F
00022:  BRA    0020
00024:  BRA    0026
00026:  DECFSZ FEF,F
00028:  BRA    000E
0002A:  RETLW  00
.................... //#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
.................... 
.................... //=======================
.................... void main()
.................... {
0002C:  CLRF   FF8
0002E:  BCF    FD0.7
00030:  CLRF   FEA
00032:  CLRF   FE9
00034:  MOVF   FC1,W
00036:  ANDLW  C0
00038:  IORLW  0F
0003A:  MOVWF  FC1
0003C:  MOVLW  07
0003E:  MOVWF  FB4
.................... int16 duty;   
.................... 
.................... setup_timer_2(T2_DIV_BY_1, 0,1);
00040:  MOVLW  00
00042:  IORLW  04
00044:  MOVWF  FCA
00046:  MOVLW  00
00048:  MOVWF  FCB
.................... setup_ccp1(CCP_PWM);
0004A:  MOVLW  B7
0004C:  ANDWF  FB1,F
0004E:  BCF    F94.2
00050:  BCF    F8B.2
00052:  MOVLW  0C
00054:  MOVWF  FBD
00056:  CLRF   FB7
00058:  BTFSS  FCA.2
0005A:  BRA    0062
0005C:  BCF    F9E.1
0005E:  BTFSS  F9E.1
00060:  BRA    005E
00062:  BCF    F94.2
00064:  BCF    F8B.2
.................... 
.................... while(1)
....................   {
....................    duty = 0;    // 0% duty cycle         
00066:  CLRF   07
00068:  CLRF   06
....................    set_pwm1_duty(duty);
0006A:  MOVFF  07,02
0006E:  MOVFF  06,01
00072:  RRCF   02,F
00074:  RRCF   01,F
00076:  RRCF   02,F
00078:  RRCF   01,F
0007A:  RRCF   02,F
0007C:  MOVFF  01,FBE
00080:  RRCF   02,F
00082:  RRCF   02,W
00084:  ANDLW  30
00086:  MOVWF  00
00088:  MOVF   FBD,W
0008A:  ANDLW  CF
0008C:  IORWF  00,W
0008E:  MOVWF  FBD
....................    delay_ms(5000);
00090:  MOVLW  14
00092:  MOVWF  08
00094:  MOVLW  FA
00096:  MOVWF  09
00098:  RCALL  0004
0009A:  DECFSZ 08,F
0009C:  BRA    0094
.................... 
....................    duty = 1;    // 25% duty cycle
0009E:  CLRF   07
000A0:  MOVLW  01
000A2:  MOVWF  06
....................    set_pwm1_duty(duty);
000A4:  MOVFF  07,02
000A8:  MOVFF  06,01
000AC:  RRCF   02,F
000AE:  RRCF   01,F
000B0:  RRCF   02,F
000B2:  RRCF   01,F
000B4:  RRCF   02,F
000B6:  MOVFF  01,FBE
000BA:  RRCF   02,F
000BC:  RRCF   02,W
000BE:  ANDLW  30
000C0:  MOVWF  00
000C2:  MOVF   FBD,W
000C4:  ANDLW  CF
000C6:  IORWF  00,W
000C8:  MOVWF  FBD
....................    delay_ms(5000);
000CA:  MOVLW  14
000CC:  MOVWF  08
000CE:  MOVLW  FA
000D0:  MOVWF  09
000D2:  RCALL  0004
000D4:  DECFSZ 08,F
000D6:  BRA    00CE
.................... 
....................    duty = 2;    // 50% duty cycle
000D8:  CLRF   07
000DA:  MOVLW  02
000DC:  MOVWF  06
....................    set_pwm1_duty(duty);
000DE:  MOVFF  07,02
000E2:  MOVFF  06,01
000E6:  RRCF   02,F
000E8:  RRCF   01,F
000EA:  RRCF   02,F
000EC:  RRCF   01,F
000EE:  RRCF   02,F
000F0:  MOVFF  01,FBE
000F4:  RRCF   02,F
000F6:  RRCF   02,W
000F8:  ANDLW  30
000FA:  MOVWF  00
000FC:  MOVF   FBD,W
000FE:  ANDLW  CF
00100:  IORWF  00,W
00102:  MOVWF  FBD
....................    delay_ms(5000);
00104:  MOVLW  14
00106:  MOVWF  08
00108:  MOVLW  FA
0010A:  MOVWF  09
0010C:  RCALL  0004
0010E:  DECFSZ 08,F
00110:  BRA    0108
....................   
....................    duty = 3;    // 75% duty cycle
00112:  CLRF   07
00114:  MOVLW  03
00116:  MOVWF  06
....................    set_pwm1_duty(duty);
00118:  MOVFF  07,02
0011C:  MOVFF  06,01
00120:  RRCF   02,F
00122:  RRCF   01,F
00124:  RRCF   02,F
00126:  RRCF   01,F
00128:  RRCF   02,F
0012A:  MOVFF  01,FBE
0012E:  RRCF   02,F
00130:  RRCF   02,W
00132:  ANDLW  30
00134:  MOVWF  00
00136:  MOVF   FBD,W
00138:  ANDLW  CF
0013A:  IORWF  00,W
0013C:  MOVWF  FBD
....................    delay_ms(5000);
0013E:  MOVLW  14
00140:  MOVWF  08
00142:  MOVLW  FA
00144:  MOVWF  09
00146:  RCALL  0004
00148:  DECFSZ 08,F
0014A:  BRA    0142
.................... 
....................    duty = 4;    // 100% duty cycle
0014C:  CLRF   07
0014E:  MOVLW  04
00150:  MOVWF  06
....................    set_pwm1_duty(duty);
00152:  MOVFF  07,02
00156:  MOVFF  06,01
0015A:  RRCF   02,F
0015C:  RRCF   01,F
0015E:  RRCF   02,F
00160:  RRCF   01,F
00162:  RRCF   02,F
00164:  MOVFF  01,FBE
00168:  RRCF   02,F
0016A:  RRCF   02,W
0016C:  ANDLW  30
0016E:  MOVWF  00
00170:  MOVF   FBD,W
00172:  ANDLW  CF
00174:  IORWF  00,W
00176:  MOVWF  FBD
....................    delay_ms(5000);
00178:  MOVLW  14
0017A:  MOVWF  08
0017C:  MOVLW  FA
0017E:  MOVWF  09
00180:  RCALL  0004
00182:  DECFSZ 08,F
00184:  BRA    017C
....................   }
00186:  BRA    0066
.................... 
.................... } 
00188:  SLEEP

Configuration Fuses:
   Word  1: 0200   HS NOIESO NOFCMEN RESERVED
   Word  2: 1E1E   BROWNOUT NOWDT BORV21 PUT WDT32768
   Word  3: 8300   CCP2C1 PBADEN NOLPT1OSC MCLR RESERVED
   Word  4: 0081   STVREN NODEBUG NOLVP NOXINST RESERVED
   Word  5: C00F   NOPROTECT NOCPD NOCPB
   Word  6: E00F   NOWRT NOWRTD NOWRTC NOWRTB
   Word  7: 400F   NOEBTR NOEBTRB
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 22, 2007 3:14 pm     Reply with quote

I don't have an 18F4620. I do have an 18F4550 and I compiled the test
program for it with PCH vs. 4.025. It works OK. I'm using a 20 MHz crystal.

Do you know that your PIC works ? Have you tried making a small
test program to blink an LED ?
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Thu Feb 22, 2007 3:44 pm     Reply with quote

I connect my scope to pin C2 and not see 6.25Mhz...

How to test your program????

Thank you very much
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 22, 2007 3:47 pm     Reply with quote

I tested it with my oscilloscope.

I repeat:
Do you know for sure that your PIC works ?
Have you run any other program with it ?
Have you tested it with an LED blink program ?
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Thu Feb 22, 2007 3:54 pm     Reply with quote

sorry,

My Pic work ok, I test this code

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,1,1);
set_pwm1_duty(4L);

and I see 3.1256Mhz in my scope.

I am using a CCS Ethernet Board for this test.

Any idea???
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 22, 2007 4:38 pm     Reply with quote

Some revisions of the 18F4620 have a bug with the PWM. It won't
run at the highest PWM frequency. This is the reason for your problem.

Download the Errata for the A3 and A4 silicon from this web page:
http://www.microchip.com/stellent/idcplgidcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010304
Read this section:
Quote:

8. Module ECCP and CCP

Clearing the PR2 register to select the fastest
period may also result in the output(s) remaining
at a logic low output level.


The B4 silicon errata does not list this bug, so I assume this means
that Microchip fixed it for that revision.
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 7:32 am     Reply with quote

Dear PCM,

You are right!!!, I test with another model and now it work!!!!!!

Thank you very much!!!!
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