|
|
View previous topic :: View next topic |
Author |
Message |
DB_LXP
Joined: 15 Feb 2021 Posts: 1
|
Setup timer for PWM pin on dsPIC33EP64GS806 not working |
Posted: Mon Feb 15, 2021 11:43 am |
|
|
Hi All,
I am trying to setup one of my PWM pins on a dsPIC33EP64GS806. I have followed the example code in "ex_pwm_pcd.c" to setup the timer and compare functions. When I set a value for the period, I am not getting the desired frequency. For example, when I set a period of 25000 which will give me 100Hz (OSC/2*8*25000) I am only getting 38Hz. I have tried this with different TMR_DIV_BY values: 1, 8 and 64, and I get the following frequencies respectively: 305Hz, 38Hz and 4.8Hz. This is very mind boggling to me because I have also tried using the #use pwm() function and that works! So I am inclined to say that it is not a hardware problem, but possibly a setup problem. I could really use some guidance in solving this issue. Any help would be greatly appreciated. Thanks!
Code: |
#include <33EP64GS806.h>
#device ADC=10
#fuses NOPROTECT, NOWDT
#fuses PR_PLL
#fuses XT
#fuses ICSP2
#FUSES CKSNOFSM
#pin_select OC1 = PIN_C3
#use delay(clock=40M, crystal=10M)
void main() {
/* Timer 2 setup */
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,25000);
setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER2);
/* End Timer 2 setup */
set_pwm_duty(1,20000);
}
|
Here is my list file as well:
CCS PCD C Compiler, Version 5.100, xxxxx 15-Feb-21 10:30
Filename: C:temp\test.lst
ROM used: 1184 bytes (3%)
Largest free fragment is 43744
RAM used: 129 (2%) at main() level
129 (2%) worst case
Stack used: 2 locations
Stack size: 128
*
0000: GOTO 426
.................... #include <33EP64GS806.h>
.................... //////////// Standard Header file for the DSPIC33EP64GS806 device ////////////////
.................... ///////////////////////////////////////////////////////////////////////////
.................... //// (C) Copyright 1996, 2020 Custom Computer Services ////
.................... //// This source code may only be used by licensed users of the CCS C ////
.................... //// compiler. This source code may only be distributed to other ////
.................... //// licensed users of the CCS C compiler. No other use, reproduction ////
.................... //// or distribution is permitted without written permission. ////
.................... //// Derivative programs created using this software in object code ////
.................... //// form are not restricted in any way. ////
.................... ///////////////////////////////////////////////////////////////////////////
.................... #device DSPIC33EP64GS806
....................
.................... #list
....................
.................... #device ADC=10
....................
.................... #fuses NOPROTECT, NOWDT
.................... #fuses PR_PLL
.................... #fuses XT
.................... #fuses ICSP2
.................... #FUSES CKSNOFSM
.................... #pin_select OC1 = PIN_C3
.................... #use delay(clock=40M, crystal=10M)
*
0412: CP0 W0
0414: BTSC.B 42.1
0416: BRA 424
0418: REPEAT #E1B
041A: NOP
041C: REPEAT #3FFE
041E: NOP
0420: DEC W0,W0
0422: BRA NZ,418
0424: RETURN
.................... void set_port_direction();
....................
.................... void main() {
0426: MOV #2F80,W15
0428: MOV #2FFF,W0
042A: MOV W0,20
042C: NOP
042E: BSET.B 8C1.7
0430: DISI #6
0432: MOV #742,W1
0434: MOV #46,W2
0436: MOV #57,W3
0438: MOV.B W2L,[W1]
043A: MOV.B W3L,[W1]
043C: BCLR.B 742.6
043E: MOV #1000,W0
0440: MOV W0,67E
0442: DISI #6
0444: MOV #742,W1
0446: MOV #46,W2
0448: MOV #57,W3
044A: MOV.B W2L,[W1]
044C: MOV.B W3L,[W1]
044E: BSET.B 742.6
0450: MOV #1E,W4
0452: MOV W4,746
0454: MOV #40,W4
0456: MOV W4,744
0458: DISI #E
045A: MOV #103,W0
045C: MOV #743,W1
045E: MOV #78,W2
0460: MOV #9A,W3
0462: MOV.B W2L,[W1]
0464: MOV.B W3L,[W1]
0466: MOV.B W0L,743
0468: LSR W0,#8,W0
046A: MOV #742,W1
046C: MOV #46,W2
046E: MOV #57,W3
0470: MOV.B W2L,[W1]
0472: MOV.B W3L,[W1]
0474: MOV.B W0L,[W1]
0476: CLR E0E
0478: CLR E1E
047A: CLR E2E
047C: CLR E3E
.................... set_port_direction();
047E: CALL 400
.................... /* Timer 2 setup */
.................... // Timer 2 is for PWM fan control
.................... setup_timer3(TMR_INTERNAL | TMR_DIV_BY_64,25000);
0482: CLR 112
0484: MOV #61A8,W4
0486: MOV W4,10E
0488: MOV #8020,W4
048A: MOV W4,112
.................... setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER3);
048C: MOV #406,W4
048E: MOV W4,900
0490: CLR 902
.................... /* End Timer 2 setup */
....................
.................... set_pwm_duty(1,10000);
0492: MOV #2710,W4
0494: MOV W4,906
....................
.................... while(1) {
....................
.................... delay_ms(100);
0496: MOV #64,W0
0498: CALL 412
049C: BRA 496
.................... }
....................
.................... }
....................
049E: PWRSAV #0
....................
.................... void set_port_direction() {
.................... // Set port direction here to get the fastest performance possible
.................... // Input is a 1, output is a 0
....................
.................... set_tris_a(0b0000000000000100);
*
0400: MOV #4,W4
0402: MOV W4,E00
.................... // 0b0000000000000001
.................... // A0: 820V_Contactor_Drive_Output
.................... // A1: Aux_Battery_12V_Control_Output
.................... // A2: Contactor_Drive_Relay_2_Sense_Input
.................... // A3: Zone_1_Fan_Control
.................... // A4: Zone_2_Fan_Control
.................... // A5: Unimplemented
.................... // A6: Unimplemented
.................... // A7: Unimplemented
.................... // A8: Unimplemented
.................... // A9: Unimplemented
.................... // A10: Unimplemented
.................... // A11: Unimplemented
.................... // A12: Unimplemented
.................... // A13: Unimplemented
.................... // A14: Unimplemented
.................... // A15: Unimplemented
....................
....................
.................... // Unleash pack controller port direction
.................... // 0b0000000000000001
.................... // A0: MCU_CAN_Rx input (C1RX)
.................... // A1: MCU_CAN_Tx output (C1TX)
.................... // A2: MCU_Curtis_CAN_Tx output (C2TX)
.................... // A3: POSITIVE_CONTACTOR_DRIVER_CONTROL output
.................... // A4: NEGATIVE_CONTACTOR_DRIVER_CONTROL output
.................... // A5: Unimplemented
.................... // A6: Unimplemented
.................... // A7: Unimplemented
.................... // A8: Unimplemented
.................... // A9: Unimplemented
.................... // A10: Unimplemented
.................... // A11: Unimplemented
.................... // A12: Unimplemented
.................... // A13: Unimplemented
.................... // A14: Unimplemented
.................... // A15: Unimplemented
....................
.................... /* output_low(POSITIVE_CONTACTOR_DRIVER_CONTROL);
.................... output_low(NEGATIVE_CONTACTOR_DRIVER_CONTROL); */
....................
.................... set_tris_b(0b1010101100000000);
0404: MOV #AB00,W4
0406: MOV W4,E10
....................
.................... // B0:
.................... // B1:
.................... // B2:
.................... // B3:
.................... // B4: Zone_3_Fan_Control
.................... // B5:
.................... // B6: Zone_7_Fan_Control
.................... // B7: Zone_6_Fan_Control
.................... // B8: aux_sense_615V_Input
.................... // B9: I_SENSE_ADC_CHANNEL_Input
.................... // B10:
.................... // B11: Interlock_In_Sense_Input
.................... // B12: MCU_CAN_Tx output (C1TX)
.................... // B13: MCU_CAN_Rx input (C1RX)
.................... // B14:
.................... // B15: aux_sense_410V_Input
....................
....................
.................... // Unleash pack controller port direction
.................... // 0b0000001010000011
.................... // B0: MCU_Curtis_CAN_Rx (C2RX)
.................... // B1: OSC1 input
.................... // B2: OSC2 output
.................... // B3: PGD, otherwise not used (output low)
.................... // B4: PGC, otherwise not used (output low)
.................... // B5: Unused
.................... // B6: Unused
.................... // B7: J1772_CP_TO_MASTER_CONTROLLER input
.................... // B8: unused
.................... // B9: Accelerometer X-axis analog input
.................... // B10: Unimplemented
.................... // B11: Unused
.................... // B12: KEYSWITCH_CONTROL output
.................... // B13: FORWARD_SWITCH_CONTROL output
.................... // B14: REVERSE_SWITCH_CONTROL output
.................... // B15: Unused
....................
.................... /* output_low(PIN_B3);
.................... output_low(PIN_B4);
.................... output_low(PIN_B5);
.................... output_low(PIN_B6);
.................... output_low(PIN_B8);
.................... output_low(PIN_B11);
.................... output_low(KEYSWITCH_CONTROL);
.................... output_low(FORWARD_SWITCH_CONTROL);
.................... output_low(REVERSE_SWITCH_CONTROL);
.................... output_low(PIN_B15); */
....................
.................... set_tris_c(0b0000011000000000);
0408: MOV #600,W4
040A: MOV W4,E20
.................... // 0b0010010001000011
.................... // C0: Safety_Interlock_Control_Output
.................... // C1:
.................... // C2: Zone_5_Fan_Control
.................... // C3: Fan_12V_PWM_Control_Output
.................... // C4:
.................... // C5:
.................... // C6:
.................... // C7: SDA
.................... // C8: SCL
.................... // C9: aux_sense_820V_Input
.................... // C10: Contactor_Drive_Relay_1_Sense_Input
.................... // C11:
.................... // C12: 615V_Contactor_Drive_Output
.................... // C13: Zone_10_Fan_Control
.................... // C14: Zone_4_Fan_Control
.................... // C15:
....................
....................
.................... // Unleash pack controller port direction
.................... // 0b0010010001000011
.................... // C0: NEGATIVE_CONTACTOR_SENSE input
.................... // C1: Accelerometer Z-axis analog input
.................... // C2: Unused
.................... // C3: Unused
.................... // C4: J1772_CP_DRIVER_FROM_MASTER_CONTROLLER output
.................... // C5: Unused
.................... // C6: I_Sense analog input
.................... // C7: SCL output
.................... // C8: SDA output
.................... // C9: Unused
.................... // C10: Accelerometer Y-axis analog input
.................... // C11: Unimplemented
.................... // C12: ACCELEROMETER_SELF_TEST output
.................... // C13: POSITIVE_CONTACTOR_SENSE input
.................... // C14: Unimplemented
.................... // C15: Unimplemented
....................
.................... /* output_low(PIN_C2);
.................... output_low(PIN_C3);
.................... output_low(J1772_CP_DRIVER_FROM_MASTER_CONTROLLER);
.................... output_low(PIN_C5);
.................... output_low(PIN_C9);
.................... output_low(ACCELEROMETER_SELF_TEST); */
....................
.................... set_tris_d(0b0010000011000000);
040C: MOV #20C0,W4
040E: MOV W4,E30
0410: RETURN
.................... // D0:
.................... // D1:
.................... // D2:
.................... // D3: 1025V_Contactor_Drive_Output
.................... // D4:
.................... // D5:
.................... // D6: Contactor_Drive_Relay_3_Sense_Input
.................... // D7: Sense_12V_Grid_Connected_Supply_Input
.................... // D8:
.................... // D9: Zone_9_Fan_Control
.................... // D10: Negative_Contactor_Drive_Control
.................... // D11:
.................... // D12: 410V_Contactor_Drive_Output
.................... // D13: Sense_12V_Aux_Battery_Input
.................... // D14: Zone_8_Fan_Control
.................... // D15:
....................
.................... }
Configuration Fuses:
Word 1L: FFFF NOWRTB NOBSS NOBS NOWRT NOPROTECT NOWPCFG NOPROTECT_CFG NOAIVT
H: 0000
Word 2L: FFFF
H: 0000
Word 3L: FFFF
H: 0000
Word 4L: FFFF
H: 0000
Word 5L: FFFF
H: 0000
Word 6L: 7FFF
H: 0000
Word 7L: FFF8 FRC IESO
H: 0000
Word 8L: FF7A HS OSCIO IOL1WAY CKSNOFSM PLLWAIT
H: 0000
Word 9L: FF9F WPOSTS16 WPRES128 NOWDT WINDIS WDTWIN_25%
H: 0000
Word 10L: FFFF
H: 0000
Word 11L: FFDE ICSP2 NOJTAG NODEBUG NOBTSWP
H: 0000
Word 12L: FFFF PWMLOCK NOALTI2C1 NOALTI2C2 NODBCC
H: 0000
Word 13L: FFFF ALTWREG1=NO ALTWREG2=NO
H: 0000
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Tue Feb 16, 2021 3:52 am |
|
|
Two things wrong with what you post:
Code: |
#include <33EP64GS806.h>
#device ADC=10
#device ICD=2 //Correct syntax to select the ICSP2 port.
#fuses NOPROTECT, NOWDT
#fuses PR_PLL
#fuses XT
//#fuses ICSP2 //not supported on your compiler
#FUSES CKSNOFSM
#pin_select OC1 = PIN_C3
#use delay(clock=40M, crystal=10M)
void main() {
/* Timer 2 setup */
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,25000);
setup_compare(1, COMPARE_PWM_EDGE | COMPARE_TIMER2);
/* End Timer 2 setup */
set_pwm_duty(1,20000);
//You need to keep the chip awake otherwise the PWM won't happen...
while (TRUE)
delay_cycles(1);
}
|
Now what you posted, would not compile. The compiler would complain
about the ICSP setting, and with this fixed, as soon as it ran, it'd go
straight to sleep, so there would be no PWM output actually happening.
So, presumably what you were actually testing was not this code.
This is doubly supported, since this code will not generate the listing you
actually show. This has a delay at the end preventing the sleep.
Not a good start....
Now, the time will not be quite what you expect, since the PR2 value is
'timebase-1', so it'll actually count to 25001. However it should give
very nearly the time you expect. Suggests to me, that in fact the CPU
is not running at the speed you think it is. When you say you tested with
the #USE PWM, double check that the fuse/clock settings you used for
this. Were the same as you are currently trying to use, and what frequency
you actually see. It actually looks as if the chip is running at about 14MHz,
not the 40MHz you are telling it you are running at.
Double check by doing a simple delay/loop code like:
Code: |
#include <33EP64GS806.h>
#device ADC=10
#device ICD=2 //Correct syntax to select the ICSP2 port.
#fuses NOPROTECT, NOWDT
#fuses PR_PLL
#fuses XT
//#fuses ICSP2 //not supported on your compiler
#FUSES CKSNOFSM
#use delay(clock=40M, crystal=10M)
void main() {
//You need to keep the chip awake otherwise the PWM won't happen...
while (TRUE)
{
delay_ms(1);
output_toggle(PIN_C3);
}
}
|
If this does not generate a 500Hz output, you know that the problem is
the clock setup. |
|
|
|
|
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
|