Author Topic: Pic24 pwm  (Read 6870 times)

0 Members and 1 Guest are viewing this topic.

Offline meerwetenTopic starter

  • Contributor
  • Posts: 42
  • Country: be
    • Greendigit Electronics
Pic24 pwm
« on: March 26, 2014, 09:16:25 pm »
i'm trying to get a pwm working on my pic24fj256bg106
from what i understand i need to map this to a sertain RPn pin, but it seems thats not working for me (or i made a mistake somewhere else ofc)

i can't find whats wrong with my coding (using mikroC pro for pic24, not mplab)
 
Code: [Select]
  #define led LATE5_bit

  void InitMain() {
  //ADPCFG = 0xFFFF;                           // Configure AN pins as digital I/O
  TRISB = 0x0000;                            // configureall ports as Output

  TRISD = 0x0000;
  TRISF = 0x0000;
  TRISG = 0x0000;
  TRISE = 0x0000;
  LATE5_bit = 0x00;
  //TRISC = 0x0000;
  Unlock_IOLOCK();
  PPS_Mapping_NoLock(0, _OUTPUT, _OC1);   // Sets pin 0 to be Output
  Lock_IOLOCK();

}
     int current_duty,maxi,chanel;
void main() {
     InitMain();
     current_duty  = 5000; //16 bit pwm
     chanel = 1;
     PWM_Init(100,chanel,0,0);
     PWM_Start(chanel); //Start pwm1
     maxi = 65536;

     PWM_Set_Duty(maxi,  chanel);            // Set current duty for PWM1
     while(1){
              while(current_duty < maxi){
                                 PWM_Set_Duty(current_duty,chanel);
                                 current_duty++;
                                 led = !led;
                                 delay_ms(100);
              }
              current_duty = 0;
     }

}
« Last Edit: March 28, 2014, 03:35:21 pm by meerweten »
Meerweten, Want meten is Weten
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Pic24 pwm
« Reply #1 on: March 26, 2014, 10:58:08 pm »
Quote
i can't find whats wrong with my coding

Really? Where is the main loop?
================================
https://dannyelectronics.wordpress.com/
 

Offline lewis

  • Frequent Contributor
  • **
  • Posts: 704
  • Country: gb
  • Nullius in verba
Re: Pic24 pwm
« Reply #2 on: March 26, 2014, 11:06:39 pm »
Really? Where is the main loop?

Why would you need a loop? And what would go in it?

meerweten:

Have a look in MikroC's help documentation (which is very good). You may need to call PWM1_Init(...) first:

Code: [Select]
//this is from MikroC for PIC, I don't have the 16-bit version installed on this machine.
//PIC24 should be similar

void PWM1_Init(long freq);

// For Enhanced Mid-Range Core 145x/150x MCUs
void PWM1_Init(unsigned long PWM_Freq, unsigned int timer_prescaler);



I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.
 

Offline senso

  • Frequent Contributor
  • **
  • Posts: 951
  • Country: pt
    • My AVR tutorials
Re: Pic24 pwm
« Reply #3 on: March 26, 2014, 11:55:45 pm »
Without a main loop the PC will just increase until it overflows and starts over at 0x0000, this is the best scenario assuming that an erased flash will contain what will be executed as nops, worst case, it will be random junk and you can't understand what the micro is doing.
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: Pic24 pwm
« Reply #4 on: March 27, 2014, 12:29:36 am »
Without a main loop the PC will just increase until it overflows and starts over at 0x0000, this is the best scenario assuming that an erased flash will contain what will be executed as nops, worst case, it will be random junk and you can't understand what the micro is doing.

I hope the code that calls main doesn't allow that to happen. main will end and return to c0 or whatever it's nowadays and hopefully it puts the processor to sleep after returning from main.

Edit: apparently on a PIC it doesn't go to sleep, it calls the main initialization (setting all the constant values etc) and then calls main, so it re-initializes and runs the code over and over including re-initializing the static variable values.


« Last Edit: March 27, 2014, 12:36:43 am by miguelvp »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Pic24 pwm
« Reply #5 on: March 27, 2014, 12:56:03 am »
Quote
And what would go in it?

Nothing. Absolutely nothing.

And that's the point of it.
================================
https://dannyelectronics.wordpress.com/
 

Offline miguelvp

  • Super Contributor
  • ***
  • Posts: 5550
  • Country: us
Re: Pic24 pwm
« Reply #6 on: March 27, 2014, 01:17:49 am »
end main on his compiler just loops forever when the end is reached

Code: [Select]
L_end_main:
0x002C        0xD7FF              BRA         $+0
 

Offline lewis

  • Frequent Contributor
  • **
  • Posts: 704
  • Country: gb
  • Nullius in verba
Re: Pic24 pwm
« Reply #7 on: March 27, 2014, 09:04:40 am »

Nothing. Absolutely nothing.

And that's the point of it.

It doesn't take a vast leap of logic to realise the code the OP posted was a snippet. And yes, the compiler generates a 'goto $' equivalent at the end of main(). It does not overflow back to 0x00. We know this because if it did, that's a loop! And we haven't defined any loops in the C source.
« Last Edit: March 27, 2014, 09:08:40 am by lewis »
I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1653
  • Country: nl
Re: Pic24 pwm
« Reply #8 on: March 27, 2014, 10:14:54 am »
Just always place while(1) on your code. Heck, I even have programs with 2 while(1) loops, just in case the first fails. :scared:
Some compilers may run through code memory (makes no sense). Sensible compilers will treat main() as any other function from the init code, and after main has ended (which it should never do btw), reset the software by jumping back to zero.

Anyway, as per standard debugging on MCU's "this pin doesn't work":
- Check your TRIS register settings (all outputs seems to be OK)
- Check your analog functions (analog by default, overrides digital functons).
- Check your re mappable I/O ( something like "PPS_Mapping_NoLock(0, _OUTPUT, _OC1);" makes no sense - where is the remappable pin number? Is it 0? Your comment says 5)
- Check your timer/PWM configuration. (Did you initialize before start?)

I don't have/use mikroC, so I don't know their library and how much it takes care of it's own, but I suspect you need to check your analog functions, PPS, and timer initialization.
« Last Edit: March 27, 2014, 10:16:30 am by hans »
 

Offline meerwetenTopic starter

  • Contributor
  • Posts: 42
  • Country: be
    • Greendigit Electronics
Re: Pic24 pwm
« Reply #9 on: March 27, 2014, 10:33:16 am »
- Check your re mappable I/O ( something like "PPS_Mapping_NoLock(0, _OUTPUT, _OC1);" makes no sense - where is the remappable pin number? Is it 0? Your comment says 5)
- Check your timer/PWM configuration. (Did you initialize before start?)

yes i forgot to change my comment back to 0
Meerweten, Want meten is Weten
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Pic24 pwm
« Reply #10 on: March 27, 2014, 12:25:56 pm »
Quote
timer initialization

This particular chip is part of the 1xx family that has DEDICATED timers to the OC (and IC) modules - this effectively has added 5 - 9 timers to the chip. Really nice.

The pwm set-up is actually quite simple - if you follow the datasheet. You set up OCxCON1/2 - the key there is to select system clock and synchronize to itself, and then write to OCxR and OCxRS.

================================
https://dannyelectronics.wordpress.com/
 

Offline mazurov

  • Frequent Contributor
  • **
  • Posts: 524
  • Country: us
Re: Pic24 pwm
« Reply #11 on: March 27, 2014, 03:56:13 pm »
Pasted from the source I'm working on at the moment. Substitute serial with your function. To understand it better, take a look inside pps.h header

Code: [Select]

#include "pps.h"

...

//Set serial console pins
    PPSUnLock;
    PPSOutput( PPS_RP21, PPS_U2TX );    //hardcoded
    PPSInput( PPS_U2RX, PPS_RP26 );     //todo - make less cryptic
 
    PPSLock;
With sufficient thrust, pigs fly just fine - RFC1925
 

Offline meerwetenTopic starter

  • Contributor
  • Posts: 42
  • Country: be
    • Greendigit Electronics
Re: Pic24 pwm
« Reply #12 on: March 28, 2014, 03:38:44 pm »
so i added the main loop.
added the pwm init i forgot
but still can't start the pwm

timer: sould be initialized by the pwm function

so for my own sake tried a simple blinking led and that worsk so the chip is def working


Code: [Select]
#define led LATE5_bit

  void InitMain() {
  //ADPCFG = 0xFFFF;                           // Configure AN pins as digital I/O
  TRISB = 0x0000;                            // configureall ports as Output

  TRISD = 0x0000;
  TRISF = 0x0000;
  TRISG = 0x0000;
  TRISE = 0x0000;
  LATE5_bit = 0x00;
  //TRISC = 0x0000;
  Unlock_IOLOCK();
  PPS_Mapping_NoLock(0, _OUTPUT, _OC1);   // Sets pin 0 to be Output
  Lock_IOLOCK();

}
     int current_duty,maxi,chanel;
void main() {
     InitMain();
     current_duty  = 5000; //16 bit pwm
     chanel = 1;
     PWM_Init(100,chanel,0,0);
     PWM_Start(chanel); //Start pwm1
     maxi = 65536;

     PWM_Set_Duty(maxi,  chanel);            // Set current duty for PWM1
     while(1){
              while(current_duty < maxi){
                                 PWM_Set_Duty(current_duty,chanel);
                                 current_duty++;
                                 led = !led;
                                 delay_ms(100);
              }
              current_duty = 0;
     }

}
Meerweten, Want meten is Weten
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Pic24 pwm
« Reply #13 on: March 28, 2014, 08:01:41 pm »
I think people can only offer limited help as it is unlikely that many people have the same set-up as yours. So I would suggest that you go through the compiler manual and / or sample code and make sure that they are as much "similar" as possible.


Code: [Select]
              while(current_duty < maxi){
                                 PWM_Set_Duty(current_duty,chanel);
                                 current_duty++;
                                 led = !led;
                                 delay_ms(100);
              }
              current_duty = 0;

Do you observe led's timing?

That will at least tell you if the loop is running.

================================
https://dannyelectronics.wordpress.com/
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Pic24 pwm
« Reply #14 on: March 28, 2014, 08:05:01 pm »
Also, you didn't specify the datatype for current_duty and maxi.

With the wrong type,
Code: [Select]
maxi = 65536;
makes zero sense and may cause the comparison to fail. Thus the importance to see if led's being flipped at the right frequency.

Read a good C book for that.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf