|
|
View previous topic :: View next topic |
Author |
Message |
younder
Joined: 24 Jan 2013 Posts: 53 Location: Brazil
|
Help with Output Compare module - dsPIC30F4012 |
Posted: Sun Jul 24, 2016 5:07 pm |
|
|
Hi Guys,
I'm having a hard time trying to get OC1 & OC2 pins at low level while mcu is powering up. I'm using Output Compare module in Pulse-Width Modulation Mode OCM<2:0> set to '110' (PWM without Fault Protection Input).
I'm setting OCM<2:0> to '000' (Output compare channel disabled) before power is off, but the pins still high for a short period of time right after power is on.
All the other output pins are at low state when powering on, only OC1 & 2 aren't.
Below is how I'm setting the pwm module:
Code: |
//MCU Setup & Fuses declaration
#include <30F4012.h>
#device ADC=10
#fuses FRC_PLL16,NOPROTECT,NOWRT,MCLR,BORRES,PUT64,NOCKSFSM,BORV27,NODEBUG,NOWDT
#use i2c(MASTER, SCL=PIN_E5, SDA=PIN_E4, FAST, FORCE_SW, stream=Bus1) //Software selectable I2C
#use pwm(OUTPUT=PIN_D0, TIMER=3, FREQUENCY=1000, STREAM=pwm1)
#build (stack=256) // 128,256,384 or 512 depending how big the code is
.
.
.
//Inside main loop
pwm_set_frequency(pwm1,Temp0); //in Hz
pwm_set_duty_percent(pwm1,SP_Pulse_Width_Pwm[1]); //in tenths %
pwm_on(pwm1);
.
.
.
//Before Power is off:
pwm_off(pwm1);
|
CCS V5.061
Any help would be very appreciated!
Hugo _________________ Hugo Silva |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Sun Jul 24, 2016 7:43 pm |
|
|
don't use it but... what does the datasheet say about the default state of the pins? it's not uncommon for a pin to be the 'wrong' state on powerup for MY applications.....
perhaps if it's a multiuse pin the 'other' peripheral has control on powerup ??
just tossing ideas out for you
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Jul 25, 2016 1:01 am |
|
|
Except for specific things like ECCP pins, which can in some cases be programmed in the fuses, all PIC pins wake up set as inputs.
So the other pins being 'low', is just because there is nothing connected to them, that pulls them up.....
There are peripherals that can cause problems (like the comparator), but on this pin the only higher priority use, is the ICD, which hopefully doesn't apply...
This is why on anything where initial level is important, you have to have the external hardware designed to pull the pin to the required idle level. :(
However there are then two CCS 'caveats'. Generally when using standard_io, CCS sets the TRIS, before setting the output register. This gives normally two instructions with the pin driven, and the level will depend on the contents of the output register (normally 1's). Also in your case, the code will try to setup the PWM, in the 'hidden' code at the start (since you use #USE PWM), before your code is reached. This could easily be selecting an unwanted mode momentarily....
If you look at ex_pwm_pcd.c, you will see an example of how to control the output_compare module directly. Use this, instead of #USE PWM
Then at the start of your main, put zero into the output latch for portD.
Code: |
//MCU Setup & Fuses declaration
#include <30F4012.h>
#device ADC=10
#fuses FRC_PLL16,NOPROTECT,NOWRT,MCLR,BORRES,PUT64,NOCKSFSM,BORV27,NODEBUG,NOWDT
#use i2c(MASTER, SCL=PIN_E5, SDA=PIN_E4, FAST, FORCE_SW, stream=Bus1) //Software selectable I2C
#build (stack=256) // 128,256,384 or 512 depending how big the code is
#byte lat_d=getenv("sfr:LATD") //only four bits used, so byte is big enough
.
.
.
//At start of main code
lat_d=0; //ensure output latch is clear
//Then setup the PWM with:
//first the timer
//You don't tell us your clock rate, so I can't put in the values for divisor
//and prescaler
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_y , 0xxxxx);
//these need to be calculated to give the PWM frequency
//the example shows how.
//then the OC
set_pwm_duty(0); //set this _before_ enabling the module
//now setup the module
setup_compare(1, COMPARE_PWM | COMPARE_TIMER3);
//Then in your loop, set the pwm output
set_pwm_duty(1, something);
|
Now the key points are the:
1) You ensure there is no random output from the output latch.
2) You set the output 'duty cycle' register to zero, before programming the PWM (this won't be happening with the #use code).
Does your frequency have to change in the loop?. Unusual for a PWM. Doing this brings the risk again of unexpected results. Remember when you change the frequency, the _old_ duty values will be in the register. When you reprogram the timer, you can/will get narrow/wide pulses, unless you set the duty to zero before the change. Think about it.... |
|
|
|
|
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
|