View previous topic :: View next topic |
Author |
Message |
drkome
Joined: 26 Dec 2024 Posts: 7
|
DSPIC30F4011 PWM signal generation |
Posted: Wed Jan 01, 2025 4:09 am |
|
|
Hello everyone, I am using DSPIC30F4011 series processor. I am trying to use motor control pwm module. I looked at many code examples and functions on the internet. I tried to program it at register level by reading datasheet. But I could not produce pwm signal. When I examine the legs with oscilloscope, I assume that PWML1 leg is at HIGH. This means that I have activated pwm but pwm signal is not generated. There is no duty cycle and period. Only at HIGH.
Code: |
#include<30F4011.h>
#FUSES PROTECT
#use delay(internal = 30Mhz)
int* PTCON = ((volatile int *) 0x01C0);
int* PTMR = ((volatile int *) 0x01C2);
int* PTPER = ((volatile int *) 0x01C4);
int* PWMCON1 = ((volatile int *) 0x01C8);
int* PDC1 = ((volatile int *) 0x01D6);
int* TRISD = ((volatile int *) 0x02D2);
int* PORTD = ((volatile int *) 0x02D4);
void main (){
TRISD = 0b0000;
*PTPER = 0x0000;
*PORTD = 0b0100;
*PTMR = 0x0000;
*PWMCON1 = 0x00ff;
*PTPER = 0xff00;
*PDC1 = 0x0000;
*PTCON = 0b1000000000000000;
*PDC1 = 0x0F05;
}
|
I am making a mistake somewhere in the code I wrote above. I am new to this. There is definitely something I am missing. I guess I am not noticing because I am new. Thank you. (Note: I changed the PDC1 value many times. It never remained constant in my attempts.) Thanks for answer. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
Posted: Wed Jan 01, 2025 9:52 am |
|
|
What version of CCS compiler and why are you enabling the PROTECT fuse? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19588
|
|
Posted: Wed Jan 01, 2025 10:24 am |
|
|
First thing stop using PROTECT. .There have been hundreds of posts about
this. Understand with this set, even a single byte change in your code
requires a full chip erase. Result more chip lives are used up, and this
takes a little more time. Silly. Only set this when you have your code
working.
Now, better really to use the compiler features, rather than working this
way. What you post means having create a pointer variable for each register,
and this means space used up, and time as well. Just use the compilers
built in ability to have virtual variables that directly access the registers:
Code: |
#include<30F4011.h>
#use delay(internal = 30Mhz)
#word PTCON = getenv("SFR:FTCON")
#wprd PTMR = getenv("SFR:PTMR")
#word PTPER = getenv("SFR:PTPER")
#word PWMCON1 = getenv("SFR:PWMCON1")
#word PDC1 = getenv("SFR:PDC1")
#word TRISD = getenv("SFR:TRISD")
#word PORTD = getenv("SFR:PORTD")
void main (void)
{
TRISD = 0b0000;
PTPER = 0x0000;
PORTD = 0b0100;
PTMR = 0x0000;
PWMCON1 = 0x00ff;
PTPER = 0xff00;
PDC1 = 0x0000;
PTCON = 0b1000000000000000;
PDC1 = 0x0F05;
}
|
This creates virtual variables, that you can read and write just like
normal variables. You can use the fixed addresses as you show, but
I'm letting the compiler work out 'where' these variables actually are.
These variables can be #byte or #word.
However the reason it did not work, was that you wrote directly to
TRISD, not to what it pointed to. So the TRIS was not being set..... |
|
|
drkome
Joined: 26 Dec 2024 Posts: 7
|
DSPIC30F4011 PWM signal generation |
Posted: Wed Jan 01, 2025 10:45 am |
|
|
Thanks for reply,
First I dont know version. I searched and I didnt find version. But I will.
Second Someone wrote to teach me this because I just learned it and it stayed in the project #fuses project. I will delete this macro. TRISD and PORTD is working. I tried with one led and it is worked. I havent any problem about them. I have problem with PWM. I looked datasheet for registers about PWM motor controll. I looked forums and everywhere. I used functions. I can enable pwm. How can I understand I enable pwm. I want to explain this. I looked PWML1 And it was HIGH while I was looking with osiloscope. But This havent any pwm signal. It just was HIGH. Do I need to set dead time ? I dont touch dead time registers. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19588
|
|
Posted: Wed Jan 01, 2025 11:48 am |
|
|
In what you posted, you were not setting the TRIS. If you had it working
elsewhere you probably did correctly have the * in front.
However the reason ithe PWM does nothing, is that the chip is asleep.
Your code runs off the end, and the chip stops, so no PWM output. |
|
|
drkome
Joined: 26 Dec 2024 Posts: 7
|
DSPIC30F4011 PWM signal generation |
Posted: Wed Jan 01, 2025 12:14 pm |
|
|
I will follow your advice and try it in the future. Thank you very much for your help. If I get a result tomorrow, I will update here. |
|
|
drkome
Joined: 26 Dec 2024 Posts: 7
|
DSPIC30F4011 PWM signal generation |
Posted: Fri Jan 03, 2025 1:53 am |
|
|
I managed to create a pwm signal with these codes.
Code: |
void main() {
output_d(0b0100);
setup_motor_pwm(1,MPWM_FREE_RUN,1,0,7000);
set_motor_unit(1,1, MPWM_ENABLE_H | MPWM_ENABLE_L,0,0);
set_motor_pwm_duty(1,1,6600);
set_motor_unit(1,2,MPWM_INDEPENDENT | MPWM_ENABLE_H | MPWM_ENABLE_L,0,0);
set_motor_pwm_duty(1,2,6518);
set_motor_unit(1,3,MPWM_INDEPENDENT | MPWM_ENABLE_H | MPWM_ENABLE_L,0,0);
set_motor_pwm_duty(1,3,6518);
while(1);
}
|
But if I delete the while loop. PWM signal is not generated. I wonder if the system goes to sleep mode when the main function is finished. (I still haven't been able to generate a PWM signal by programming the registers. I'm confused if I should activate a timer or a bit. I looked at the datasheet and other topics in the forum but couldn't.) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19588
|
|
Posted: Fri Jan 03, 2025 4:17 am |
|
|
Of course it does. That is what I told you.
Key to understand is that a normal C program is running under an
operating system. If you drop off the end of this, you return to the OS.
If however you write a core program on the PC like the boot code, it
becomes your responsibility to handle what happens when the routine
exits. The same applies on the PIC. You are writing the whole program.
There is nothing for the code to drop 'out' to. Your code needs to remain
running all the time. If you let it drop off the end, the operation stops.
The compiler does have an option of what to put here, but the default
is to go to sleep. |
|
|
|