Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PWM Prescaler and overflow
21-05-2013, 11:48 PM,
#1
PWM Prescaler and overflow
Hey guys,
I just wanna say first, I think the pinguino project is a really sweet idea in getting into PIC32 controllers, while its nowhere near as simple as arduino, the cost difference IMO justifies it over things like the Maple etc.

Now I do actually have a Maple which I do all of my processor speed dependent things on and its nice because the HardwareTimer library helps a lot on changing parameters.

What i'm wondering is: (I have the pinguino micro from Olimex)
How do I change the PWM prescaler and Overflow?

Assuming its ran off the 8MHz clock, I'd assume that the maximum range is set by the timer overflow and the prescaler sets the speed mainly.

For example, I want an 8 bit PWM output on pin 11, so as far as I know, thats timer 3. I'd like to know how to set the overflow of the timer to 255 and the prescaler to 1:1 (fastest PWM speed), if in future, I want to change these, I also want to know where i'd look.

Also, after using the Olimex interrupt code, I'd also like to understand the formula for calculating interrupt rate from the values used for T1CONSET and PR1.

If I can manage the above, i'll happily post a few tutorials creating audio using the pinguino!
Reply
24-05-2013, 01:51 AM,
#2
RE: PWM Prescaler and overflow
Bump?
Reply
24-05-2013, 09:12 AM,
#3
RE: PWM Prescaler and overflow
It'll be in the Microchip site in the data sheets. They're free to download and very detailed.

Note that the chip has a "pin 11" and so does this project but they're different so make clear which you mean as you read datasheets.

John
Reply
24-05-2013, 09:38 AM,
#4
RE: PWM Prescaler and overflow
Hi,
PWM resolution = f(PWM frequency), this means the higher the frequency, the lower the resolution.
Example of calculation in datasheet :
   
Take a look at p32/include/pinguino/core/pwm.c to learn how to configure, TxCON, PRx and OCxCON registers.
By the way, did you try PWM.setFrequency(), PWM.setDutyCycle() or PWM.setPercentDutyCycle() functions ?
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
24-05-2013, 12:45 PM,
#5
RE: PWM Prescaler and overflow
Thanks for the replies guys!

I've had a look through the PWM source code and managed to force a prescaler of 1 and overflow of 255 (or period I think its called, either way, I modified the PR3 register) which would be ample for my needs. I was trying to follow the tutorial found here:
http://umassamherstm5.org/tech-tutorials...orials/pwm

Now obviously I know the above is for the pic32mx220, and i'm using the mx440 (pinguino micro) but the code seems the same either way, just a few changes on what registers are used etc.

The main question i'm wondering is how I can change which pin the output compare goes to? In the above sketch, he uses PPS which I don't think works in the IDE as it always comes up with errors for me.

On a related note, how come I can't use standard asm within the IDE, I was trying to set up an interrupt from scratch the other day (not using the olimex method) and it just wouldn't let me do it at all.

The code header looked like:
void __ISR(8, ipl4) Tmr2Int(void)

Where the interrupt is based off Timer 2, putting that directly into pinguino didn't allow it to successfully compile.

Cheers,
Reply
26-05-2013, 10:47 PM,
#6
RE: PWM Prescaler and overflow
Anyone know the reason for this?! Sad
Reply
27-05-2013, 12:12 AM,
#7
RE: PWM Prescaler and overflow
Why not just read through this project's code and figure out what it does then use it.

John
Reply
28-05-2013, 03:46 PM,
#8
RE: PWM Prescaler and overflow
Well thats what I need help with! The code i'm using is:

Code:
    // Connect the wrapper to vector 4 (Timer1 overflow)
    void ISR_wrapper_vector_4(void) __attribute__ ((section (".vector_4")));

    // The wrapper calls the "Tmr1Interrupt" function
    // each time an interrupt occurs
    void ISR_wrapper_vector_4(void) { Tmr1Interrupt(); }

    //We declare the function as an interrupt routine
    void Tmr1Interrupt(void) __attribute__ ((interrupt));

// This code will be executed each time an interrupt occurs
    void Tmr1Interrupt(void)
    {
    PORTBSET = 2;
    PORTBCLR = 2;
    IFS0CLR=0x10;
}

void setup() {
    IntConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);    // interrupt mode (interrupt.c)
    T1CON=0;                // reset timer 1 configuration
    TMR1=0;                    // reset timer 1 counter register
    PR1=800;            // define the period; combined with the prescaler equals ~ 4 interrupts for 1s
    IPC1SET=0x7;        // select interrupt priority and sub-priority
    IFS0CLR=0x10;        // clear interrupt flag
    IEC0SET=0x10;        // enable timer 1 interrupt
    T1CON=0x8000;     // turn TIMER1 ON
    TRISB = 0b0000;
    }

void loop() {
}

Now this produces a 50kHz interrupt which makes sense, 20MHz Peripheral clock with a prescaler of 0 and overflow of 800 (20e6/800 = 50e3), the only part I need help understanding is why:

void ISR_wrapper_vector_4(void) __attribute__ ((section (".vector_4")));

// The wrapper calls the "Tmr1Interrupt" function
// each time an interrupt occurs
void ISR_wrapper_vector_4(void) { Tmr1Interrupt(); }

//We declare the function as an interrupt routine
void Tmr1Interrupt(void) __attribute__ ((interrupt));

// This code will be executed each time an interrupt occurs
void Tmr1Interrupt(void)

this section is needed, as opposed to just writing:

void __ISR(4, ipl4) Tmr1Interrupt(void){
//code
}

Why does the above piece of code not compile properly? It just comes up with the error:
"C:\Pinguino\source\/user.c:15:40: error: expected identifier or '(' before '{' token

error while linking C:\Pinguino\examples\01.Basics\blink_interrupt_example.o"

Sorry to be so annoying about this, i'm just new to PICs!
Reply
29-05-2013, 03:18 PM,
#9
RE: PWM Prescaler and overflow
I tried Moreno's frequency counter code which uses an ISR and it compiles fine so maybe check it compiles for you and then see what the key difference is?

It's here http://forum.pinguino.cc/showthread.php?...SR_wrapper

John
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)