# MPLAB X, PWM, Compile error

#### Ctenom

Joined Nov 1, 2010
59
Hi i have spent the last few hour's trying to get PWM to work on my pic18f16k22. Im using the C18 compiler in MPLABX V6.1 The probolem im having is it compiles with this error

Error - could not find definition of symbol 'SetDCPWM1' in file './build/default/production/_ext/916313547/PWM.o'.
Errors : 1

The PWM.h file definatly contains the definition and is included in my config.
Rich (BB code):
void SetDCPWM1 ( unsigned int duty_cycle);
I started off wrighting my own code then found several examples on the web and thay all compile with this error

My last code looks like this using some code from a C18 cheat sheet by AtomSoftTech.

Rich (BB code):
#include <p18Cxxx.h>
#include <pwm.h>
#include <delays.h>

#pragma config FOSC = INTIO67               // Oscillator Selection
#pragma config WDTEN = OFF                  // Watchdog Timer
#pragma config MCLRE = INTMCLR              // MCLR Pin Enable bit
#pragma config DEBUG = ON                  // Background Debug

#define PWM_Tris TRISCbits.TRISC2 //Define a nice name for the PWM TRIS bit.
char direction; //direction holder for out loop
void main (void)
{
char DutyCyc = 0x66; //Start Duty Cycle

OSCCON = 0x72; //8MHz clock

while(!OSCCONbits.IOFS); //Wait for OSC to become stable

PWM_Tris = 0; //Set the pin a OUTPUT

SetOutputPWM1 (SINGLE_OUT, PWM_MODE_1); //Set as single output pwm and mode 1 (P1A,P1B,P1C,P1D active-high)

OpenPWM1(DutyCyc); //Set the duty cycle and start the PWM module

while(1){

if(DutyCyc == 0x12) //Check if we reached our minimum. If so set direction (up)

direction = 1;

if(DutyCyc == 0x66) //Check if we reached our maximum. If so clear direction (down)

direction = 0;

SetDCPWM1(DutyCyc); //Set the new duty cycle

if(direction == 1) //based on direction if 1 (up) add 2 each time to the duty cycle

DutyCyc+=2;

else // if 0 (down) minus 2 each time to the duty cycle

DutyCyc-=2;

Delay10KTCYx(3); //small delay (15mS)
}
}
Thank you for taking time to read my post.

#### Ctenom

Joined Nov 1, 2010
59
I installed the new version after completely deleting the old one.
And got a slightly different error but still the same problem.

Error - could not find definition of symbol 'SetDCPWM1' in file './build/default/production/PWM.o'.
Errors : 1

And i was expecting that to work as well

#### nsaspook

Joined Aug 27, 2009
10,897
For some of the chips the routines may have been renamed or renumbered. Check the

"PIC18F Peripheral Library Help Document" in the C18 doc folder.

#### Ctenom

Joined Nov 1, 2010
59
Just tried updating my C18 from 3.39 to 3.40 still no joy.

#### Ctenom

Joined Nov 1, 2010
59
Thanks for your help, i belive it might just be teething problems with MPLABX as i have tried just about everything now. I finally got it working using HI-TECH C heres my test code (makes a pretty fading led).

Rich (BB code):
#include <htc.h>

__CONFIG (1,FOSC_INTIO67);
__CONFIG (2,WDTEN_OFF);
__CONFIG (3,MCLRE_INTMCLR);
__CONFIG (4,STVREN_OFF & DEBUG_ON & LVP_OFF);

void main()
{
unsigned char   dc ;

TRISC = 0 ;
PORTC = 0 ;

PR2 = 0b01111100 ;
T2CON = 0b00000101 ;
CCP1CON = 0b00001100 ;
CCP2CON = 0b00111100 ;

for(;;)
{

for(dc = 5 ; dc < 128 ; dc++)
{
CCPR1L = dc ;
CCPR2L = 128 - dc ;
_delay(6000);
}
for(dc = 127 ; dc > 5 ; dc--)
{
CCPR1L = dc ;
CCPR2L = 128 - dc ;
_delay(4000);
}
}
}
Now the proper coding can begin.

#### stahta01

Joined Jun 9, 2011
133
The PWM.h file definatly contains the definition and is included in my config.
Rich (BB code):
void SetDCPWM1 ( unsigned int duty_cycle);
That code is the function prototype and is the declaration NOT the definition of the function. If this was normal C (instead of embedded C) I would say you are missing a Library that needs linked into the project.
Since it is a embedded C, could be a missing(or wrong) Compiler or Linker Options; but, I would guess that a object file needs linked that provides the function.

Quote from http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=188
a definition causes the compiler to allocate storage for an object, whereas a declaration merely associates an object with a certain type, without allocating storage for it.
Tim S.

#### ErnieM

Joined Apr 24, 2011
8,353
Ctenom: When you were using the C18 did you set the project library include search path? I don't use MPLABX (I use the non-X) but that path and a few others needs to be set for every new project. MPLAB will show the correct path by default, but you still have to select it and OK it.

Tim: What would be the difference between embedded and this "normal" C? My PIC C compilers are pretty much ANSI standard, with some minor defined exceptions.

#### stahta01

Joined Jun 9, 2011
133
Tim: What would be the difference between embedded and this "normal" C? My PIC C compilers are pretty much ANSI standard, with some minor defined exceptions.
I have learned that embedded C have a lot of non standard extensions and sometimes you need to link to an system object file instead of with a system library file. Note: Borland also had this linking with system object file; so, it is not just a embedded C issue.

Note: My main experience in the embedded world is the Dynamic C [like] Compiler from Rabbit (now owned by Digi). And old experience (20+ years ago) with OS9 from Microware. And, PIC Assembly/C as a Student Assistant Teacher. Note: Rabbit Dynamic C IS NOT a ANSI C Language; but getting closer every release cycle.
Edit: The Microware C Compiler required linking to system object files to get a good build like the Borland C Compiler did.

Tim S.

Last edited: