As i had trouble finding this information which will be the subject of this post myself, i decided to leave a note about it it here in case someone gets "lost" on it as well !
The Olimex boards PIC32 PWM pins info !
Digital Pins D0, D1 and D2 are the one on which you can use analogWrite ( and PWM as well as theres a PWM_set_frequency(u32 freq), /*
PWM_set_frequency--------------------------------
@param: frequency in hertz (range 3kHz .. 12MHz)
PB is Peripheral Bus Clock
let's say p = TMR Prescale Value
PWM Period = (PR + 1) * TPB * p
so (PR + 1) = PWM Period / (TPB * p)
but PWM Period = 1 / PWM Frequency
so (PR + 1) = (1/PWM Frequency) / (1/TPB * p)
and (PR + 1) = FPB / (PWM Frequency * p)
then (PR + 1) = FPB / PWM Frequency / p -------------------------------- ------------------ PR3+1 calculation
_pr3_plus1 = GetPeripheralClock() / freq; // FOSC / PWM Frequency
Timer3 prescaler calculation
PR3 max value is 0xffff, so PR3+1 max value is 0x10000 = 65536
highest prescaler value is 256
256 * 65536 = 0x1000000 = 16777216 :*/
PWM_set_dutycycle(u8 pin, u16 duty), /* PWM_set_dutycycle
1. Set the PWM period by writing to the selected timer period register (PRy).
2. Set the PWM duty cycle by writing to the OCxRS register.
3. Write the OxCR register with the initial duty cycle.
4. Enable interrupts, if required, for the timer and output compare modules. The output
compare interrupt is required for PWM Fault pin utilization.
5. Configure the Output Compare module for one of two PWM Operation modes by writing
to the Output Compare mode bits, OCM<2:0> (OCxCON<2:0>).2:0>2:0>
6. Set the TMRy prescale value and enable the time base by setting TON
(TxCON<15>) = ‘1’. */15>
PWM_set_percent_dutycycle(u8 pin, u8 percent) /* PWM_set_percent_dutycycle--Set a percentage duty cycle, allowing max 100 PWM steps.Allowed range: 0..100The duty cycle will be set to the specified percentage of the maximum for the current PWM frequency.Note: The number of available PWM steps can be lower than 100 with (very) high PWM frequencies-- */
Relying with my limited experience with the PIC16F family, i went digging for info on the code and found something that helped me, at least for now !
So to clear that ill leave an excerpt of the pwm.c, with some additional comments for those who might interest:
#if defined(PIC32_PINGUINO) || defined(PIC32_PINGUINO_OTG) switch (pin) { case 2: TRISDSET=0x10; /* D2 in the Board, RD4 pin which is also connected to the button (see schematics); 0b10000 in binary */
TRISDCLR=0x01; OC1CON=0; OC1R=setpoint; OC1RS=setpoint; OC1CON=0x000E; /*Binary 1110 activates OCM's bit 2-0, where 1110 = PWM mode on OCx; and bit 3-0 1110 OCTSEL: Output Compare Timer Select bit where 1 = Timer3 as the clock source for this OCMP module */
OC1CON|=0x8000; /* Binary 1000000000000000 is bit 15 ON: Output Compare Peripheral On bit(1), where 1 = Output Compare peripheral is enabled and 0 = Output Compare peripheral is disabled */ return 1; break; case 1: TRISDCLR=0x08; /* D1 in the Board, RD3 pin, 0b1000 in binary . This Pin is also TX1*/
OC4CON=0; OC4R=setpoint; OC4RS=setpoint; OC4CON=0x000E; OC4CON|=0x8000; return 1; break; case 0: TRISDCLR=0x04; /* D0 in the Board, RD2 pin, 0b100 in binary where 0 is a number. This Pin is also RX1 */ OC3CON=0; OC3R=setpoint; OC3RS=setpoint; OC3CON=0x000E; OC3CON|=0x8000; return 1; break; default: return 0; } #endif
I have had both this and the DIP DIY version with the PIC32MX250F128B, with 128k of flash and 32k of RAM, 17 I/O, 2 UART etc , that with a works with a 8 Mhz crystal for a finally system frequency of 40 Mhz.
As the Pinguino board from Olimex uses an IDE that is "Arduino Compatible" but with a PIC32MX440F256H , you can imagine how handy that can be ! The specs that Olimex got us used to makes it worth it all the more !
Things like DCDC power supply allow power supply voltage from 9 to 30V DC, Li-Ion rechargeable battery power supply option with BUILT-IN on-board charger, so when you attach battery it is automatically charged and kept in this state until the other power source is removed (and it AUTOMATICALLY will power the board); original Arduino design had flaw and the connectors were not spaced at 0.1" this make perfo board use impossible, So this has a connector on 0.1" !!; UEXT connector; RTC - Real Time Clock.; NOISE IMMUNE design. and last but least the chip itself which is a PIC32MX440F256H @ 80 Mhz microcontroller with 256KB Flash and 32KB RAM
Of course you can use the Arduino C++ style with:
int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
BUT ... Ill leave you also a more direct way of blinking leds, released by Olimex itself ( they have some good examples, though mainly related to their MODboards). The code been partially commented by me just to help in our quest !
/* PIC32-PINGUINO Blinks both LEDs (Green and Yellow) If you have any questions, email support@olimex.com OLIMEX, JULY 2012 http://www.olimex.com */ #if ( defined(PIC32_PINGUINO_OTG) || defined(PIC32_PINGUINO) || defined(PIC32_PINGUINO_MICRO) ) //Definitions for // -- PIC32-PINGUINO // -- PIC32-PINGUINO-OTG // -- PIC32-PINGUINO-MICRO #define BUTTON1INIT TRISDbits.TRISD0 = 1 /*YELLOW LED is PORTD1, which is connected directly and exclusively into a led on the board */ #define YLEDINIT TRISDCLR = 0x02; #define YLED1 PORTDSET = 0x02; #define YLED0 PORTDCLR = 0x02; #define YLEDSWITCH PORTD ^= 0x02; //GREEN LED is D13 in board , PORTG6 pin / 0b1000000 on chip
#define GLEDINIT TRISGCLR = 0x40; /* Clearing the Port/bit */
#define GLED1 PORTGSET = 0x40; /* Setting the bit */
#define GLED0 PORTGCLR = 0x40; /* Clearing the bit */
#define GLEDSWITCH PORTG ^= 0x40; /* Toggling the bit/led */
#define YELLOW 0
#define GREEN 1
#define BOTH 2
#endif
//Blinks an LED five times - mode is YELLOW, GREEN or BOTH
void blinkled(unsigned char mode);
void setup() {
// put your setup code here, to run once:
YLEDINIT; /* Clearing the Port/bit */
GLEDINIT; /* Clearing the Port/bit */
}
void loop() {
// put your main code here, to run repeatedly:
blinkled(GREEN); /* Using the function below created ! */
blinkled(YELLOW);
blinkled(BOTH);
}
void blinkled(unsigned char mode)
{
//Turn off all LEDs
YLED0; /* Clearing the bit */
GLED0; /* Clearing the bit */
char i;
// Blink
switch(mode)
{
case YELLOW:
for(i=0; i<10;i++)
{
YLEDSWITCH;/* Toggling the bit/led */
delay(100);
}
break;
case GREEN:
for(i=0; i<10;i++)
{
GLEDSWITCH;/* Toggling the bit/led */
delay(100);
}
break;
case BOTH:
for(i=0; i<10;i++)
{
YLEDSWITCH;/* Toggling the bit/led */
GLEDSWITCH;/* Toggling the bit/led */
delay(100);
}
break;
}
}
Reference :
- Pinguino's Website and Forum
http://www.pinguino.cc/
- Compatibility Pinguino vs. Arduino
http://wiki.pinguino.cc/index.php/Compatibility
- Pinguino's blog (Info about the DIY of the PIC32MX or the 8Bit with PIC18F)
http://blog.pinguino.cc/
- Documentation about the DIY of the PIC32MX And the IDE download
http://code.google.com/p/pinguino32/downloads/list
Olimex Industrial-Grade board
https://www.olimex.com/dev/pic32-pinguino.html
No comments:
Post a Comment
Feel free to contact me with any suggestions, doubts or requests.
Bless