any pwm generator code with pic 18f

Discussion in 'Embedded Systems and Microcontrollers' started by wewe, Apr 16, 2010.

  1. wewe

    Thread Starter Member

    Apr 9, 2010
    hello , i made a program on pic 16f877 that generates a variable pwm (with a potentiometer) , i will use it to control a dc motor (h bridge)
    but now i have this deadtime problem , on pic18f4331 there's this option of an output of 2 complementary pwms with this dead time thing
    can someone write me any sample code of this , a normal code that generates a pwm and its complementary on 2 pins with a deadtime
    any sample code with any number for frequency and dead time will help
    i am just trying to modify my pic 16f877 code to work on 18f4331 and add this dead time to it
    Last edited: Apr 17, 2010
  2. wewe

    Thread Starter Member

    Apr 9, 2010
    thx for all the replies lol
    anyway does anyone here know how to initialize your program in pic18f4331??
    the configuration bits table is really really long and doesn't give me one code to write , unlike in pic16f where i set the watchdog off , low voltage disabled and HS oscillator and i get a "3F42" , si u can durectly write in my code __CONFIG 0x3F42
    but in pic18f it's much more complicated and i have many many codes
    for example
    address value
    300001 c2
    300002 0F
    etc.... how can i write this in my code ???
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    The config style __CONFIG 0x3F42 never was a good idea since anyone reading the code would have to sit down and decode the config value bit by bit to see what has or has not been configured. A far better style of configuration was "__CONFIG _HS_OSC & _WDT_OFF & _LVP_OFF & .....".

    The PIC18 series can have either of two methods of configuration (but it must be one or the other, you can't mix).
    You can use the __CONFIG command, the same as in the PIC16 series but using the address option (_CONFIG1 _CONFIG2 etc);
    __CONFIG _CONFIG2, _BOR_ON_2 & _BORV_25_2

    Or use the new CONFIG command;
    CONFIG OSCS=ON, OSC=LP, WDT=ON, WDTPS=128 .....etc.

    See the PMLAB Assembler and Linker User guide pages 63-66 for details.
    wewe likes this.
  4. wewe

    Thread Starter Member

    Apr 9, 2010
    guys i downloaded ccs c compiler
    and i need some help here
    i want to activate one of the pwms that have a pin to generate a complementary pwm
    what instructions am i supposed to use ??
    i found some instructions regarding regular pwm (ccp1 pin) but it's useless for me , i want a pwm from one of the pins that generate a complementary pwm (i think it's called power control pwm)

    i also would like to know how , after compiling a project , i can have the hex and cof files to use in proteus
    Last edited: Apr 17, 2010
  5. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    Have a look at the help file in the PICC program directory, or download it if it's not there (pcd.chm).

    Open that and go to built-in functions -> directory. In there under Capture/Compare/PWM there is a 'setup_motor_pwm()' function.

    An example line it gives is:
    setup_motor_pwm(1,MPWM_FREE_RUN | MPWM_SYNC_OVERRIDES, timebase);

    It also mentions other related functions for the overall PWM mode and setting the duty cycle.
    wewe likes this.
  6. wewe

    Thread Starter Member

    Apr 9, 2010
    error unidentified identifier setup_motor_pwm
    okay it's setup_power_pwm (saw it in the help file u mentioned )
    thx for the help
  7. wewe

    Thread Starter Member

    Apr 9, 2010
    okay now to another question
    whenever i declare a variable , let's say like this
    int16 value;
    i get an error " a numeric expression must appear here" , i copied many little examples from the help to see if there's anything wrong with my declaration but i got the same error :S
    EDIT: solved , i cannot declare a variable in the middle of a code , it must be at the beginning of the function
    Last edited: Apr 17, 2010
  8. wewe

    Thread Starter Member

    Apr 9, 2010
    void main()
    {int16 value;
    int1 done;

    //Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

    // TODO: USER CODE!!

    setup_adc_ports (all_analog);
    set_adc_channel (0);

    done = adc_done();
    while(!done) {

    value = read_adc();

    that's a simple program i wrote , it's supposed to convert an analog entry to digital and them show it on port B
    the compiler gave me 0 errors , i took the "cof" file and didn't work on proteus
    i tried the debugger and ADRESH is still set to "0" , which means that the analog to digital conversion is not working , can someone here please check my code to see what's wrong , it's really a simple program
    as for the pic i used it's a 18f4331
    watchdog timer window off , low power programming disabled
    internal oscillator
    Last edited: Apr 17, 2010