Pinguino Forum

Full Version: Enhanced PWM for PIC18F45K50?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey
I have question about HBridge_18F4550.pde example.
I know it is targeted at PIC18F4550, but can't it be ported it to PIC18F45K50?
This chip is also featuring the Enhanced PWM, but the pde does not compile for it.
Code:
Code:
/*-----------------------------------------------------
Author:  --<>
Date: Sun Jan 25 15:00:00 2015
Description: 18F4550 Enhanced PWM - Full Bridge

Function:
PWM_setEnhancedOutput(config, mode)

Pin:
P1A = 12 (RC2)
P1B = 26 (RD5)
P1C = 27 (RD6)
P1D = 28 (RD7)

Supported Config:
SINGLE_OUT      Single output: P1A modulated; P1B, P1C, P1D assigned as port pins
FULL_OUT_FWD    Full-bridge output forward: P1D modulated; P1A active; P1B, P1C inactive
HALF_OUT        Half-bridge output: P1A, P1B modulated with dead-band control; P1C, P1D assigned as port pins
FULL_OUT_REV    Full-bridge output reverse: P1B modulated; P1C active; P1A, P1D inactive

Supported Mode:
PWM_MODE_1    P1A,P1C active high, P1B,P1D active high
PWM_MODE_2    P1A,P1C active high, P1B,P1D active low
PWM_MODE_3    P1A,P1C active low, P1B,P1D active high
PWM_MODE_4    P1A,P1C active low, P1B,P1D active low
-----------------------------------------------------*/

// Pin Definitions
#define P1A     12   // (RC2)
#define P1B     26   // (RD5)
#define P1C     27   // (RD6)
#define P1D     28   // (RD7)
#define FLT0    0    // INT0

// Variables
u8 led_handle = 0;
int i = 512;

// Functions
void TMR0_Tick() {
    led_handle = (led_handle ^ 1);
    digitalWrite(USERLED,led_handle);
}

void setup() {

    // I/O Settings
    pinMode(USERLED, OUTPUT);
    pinMode(P1A, OUTPUT);
    pinMode(P1B, OUTPUT);
    pinMode(P1C, OUTPUT);
    pinMode(P1D, OUTPUT);
    pinMode(FLT0,INPUT);                    //FLT0 as input
    
    digitalWrite(USERLED, LOW);
    digitalWrite(P1A, LOW);
    digitalWrite(P1B, LOW);
    digitalWrite(P1C, LOW);
    digitalWrite(P1D, LOW);  
    OnTimer0(TMR0_Tick, INT_MILLISEC, 500);
    
    // Quick Output test
    digitalWrite(P1A, HIGH);
    delay(500);
    digitalWrite(P1A, LOW);
    digitalWrite(P1B, HIGH);
    delay(500);
    digitalWrite(P1B, LOW);
    digitalWrite(P1C, HIGH);
    delay(500);
    digitalWrite(P1C, LOW);
    digitalWrite(P1D, HIGH);
    delay(500);
    digitalWrite(P1D, LOW);
    delay(500);
    

    PWM.setAutoShutdown(TRUE);            // This set ECCP1AS = 0b01000000
    PWM.setASautoRestart(TRUE);
    
    PWM.setFrequency(4000);                // 4KHz
    PWM.setDutyCycle(CCP1,512);

}

void loop() {
    
    PWM.setEnhancedOutput(FULL_OUT_FWD, PWM_MODE_1);    // 2 sec FORWARD
    delay(2000);
    PWM.setEnhancedOutput(FULL_OUT_REV, PWM_MODE_1);    // 2 sec REVERSE
    delay(2000);
    
    // Duty Cycle Test
    i += 100;    
    if (i >= 1023) {
        i = 0;
    }    
    PWM.setDutyCycle(CCP1,i);
}
Compilation log:
Code:
[OUT] Compiling: C:\pinguino-11\user\examples.Control\DC_motor\HBridge_18F4550.pde
[OUT] Board: Pinguino 45k50
[OUT] Proc: 18f45k50
[OUT] Arch: 8
[OUT] Bootloader: v4
[DEBUG] Starting 'verify' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Starting 'preprocess' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Starting 'remove_comments' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Time spent for 'remove_comments': 0.00 s
[DEBUG] Ending 'remove_comments'
[DEBUG] Starting 'remove_comments' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Time spent for 'remove_comments': 0.00 s
[DEBUG] Ending 'remove_comments'
[DEBUG] Starting 'get_regobject_libinstructions' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Time spent for 'get_regobject_libinstructions': 0.00 s
[DEBUG] Ending 'get_regobject_libinstructions'
[DEBUG] Starting 'replace_word' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Time spent for 'replace_word': 0.05 s
[DEBUG] Ending 'replace_word'
[DEBUG] Time spent for 'preprocess': 0.09 s
[DEBUG] Ending 'preprocess'
[DEBUG] Starting 'compile' from 'qtgui.pinguino_api.pinguino_tools'
[DEBUG] Time spent for 'compile': 0.11 s
[DEBUG] Ending 'compile'
[DEBUG] Time spent for 'verify': 0.22 s
[DEBUG] Ending 'verify'
[OUT] ERROR: 344:  #error 'Enhanced PWM modes not available or not yet supported for your porcessor.'
[OUT] 392:  #error 'Enhanced PWM modes not available or not yet supported for your porcessor.'
[OUT] 443:  #error 'Enhanced PWM modes not available or not yet supported for your porcessor.'
[OUT] 78:  too many parameters
[OUT] 79:  too many parameters
[OUT] 88:  too many parameters
[OUT] 90:  too many parameters
[OUT]
>>>
stdout
Code:
In file included from C:\pinguino-11\user\source\/define.h:15,
                 from C:\pinguino-11\user\source\main.c:56:
C:/pinguino-11/p8/include/pinguino/core/pwm.c:344:2: error: #error "Enhanced PWM modes not available or not yet supported for your porcessor."
C:/pinguino-11/p8/include/pinguino/core/pwm.c:392:2: error: #error "Enhanced PWM modes not available or not yet supported for your porcessor."
C:/pinguino-11/p8/include/pinguino/core/pwm.c:443:2: error: #error "Enhanced PWM modes not available or not yet supported for your porcessor."
Processor: 18f45k50
sdcc: Calling preprocessor...
sdcc: Generating code...
/dvpt/pinguino/git-copy/pinguino-compilers/sources/sdcc/src/pic16/main.c:701 setting interrupt vector addresses 0xc00
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:198: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:198: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:198: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:198: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:198: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:202: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:202: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:202: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/oscillator.c:202: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/interrupt.c:895: warning 94: comparison is always true resp. false due to limited range of data type
C:/pinguino-11/p8/include/pinguino/core/interrupt.c:895: warning 94: comparison is always true resp. false due to limited range of data type
C:/pinguino-11/p8/include/pinguino/core/interrupt.c:895: warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG
C:/pinguino-11/p8/include/pinguino/core/interrupt.c:895: warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG
C:/pinguino-11/p8/include/pinguino/core/interrupt.c:899: warning 126: unreachable code
C:/pinguino-11/p8/include/pinguino/core/interrupt.c:906: warning 126: unreachable code
C:\pinguino-11\user\source\/user.c:78: warning 112: function 'PWM_setAutoShutdown' implicit declaration
C:\pinguino-11\user\source\/user.c:79: warning 112: function 'PWM_setASautoRestart' implicit declaration
C:\pinguino-11\user\source\/user.c:78: error 101: too many parameters
C:\pinguino-11\user\source\/user.c:79: error 101: too many parameters
C:\pinguino-11\user\source\/user.c:88: warning 112: function 'PWM_setEnhancedOutput' implicit declaration
C:\pinguino-11\user\source\/user.c:90: warning 112: function 'PWM_setEnhancedOutput' implicit declaration
C:\pinguino-11\user\source\/user.c:88: error 101: too many parameters
C:\pinguino-11\user\source\/user.c:90: error 101: too many parameters




EDIT: I have tried editing pwm.c, and it turns out that only "#if defined(__18f4550)" should be changed to "#if defined(__18f4550) || defined(__18f45k50)" and it compiles, but I don't have a Hbridge setup at hand so I can't test if it's working with PIC18F45K50 - I'm going to test it soon.
(04-10-2015, 12:26 AM)B4lfiz4r Wrote: [ -> ]EDIT: I have tried editing pwm.c, and it turns out that only "#if defined(__18f4550)" should be changed to "#if defined(__18f4550) || defined(__18f45k50)" and it compiles, but I don't have a Hbridge setup at hand so I can't test if it's working with PIC18F45K50 - I'm going to test it soon.

You did the right thing and it should work for both.
Once you did your tests, if it works, tell me and will update the pwm.c file.