any pwm generator code with pic 18f

Thread Starter

wewe

Joined Apr 9, 2010
31
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:

Thread Starter

wewe

Joined Apr 9, 2010
31
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 ???
 

AlexR

Joined Jan 16, 2008
732
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 _CONFIG0, _CP_OFF_0
__CONFIG _CONFIG1, _OSCS_OFF_1 & _RCIO_OSC_1
__CONFIG _CONFIG2, _BOR_ON_2 & _BORV_25_2
.................
etc.

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.
 

Thread Starter

wewe

Joined Apr 9, 2010
31
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:

rjenkins

Joined Nov 6, 2005
1,013
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.
 

Thread Starter

wewe

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

Thread Starter

wewe

Joined Apr 9, 2010
31
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:

Thread Starter

wewe

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

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0|ADC_CONT_A|ADC_WHEN_INT0|ADC_INT_EVERY_OTHER);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

// TODO: USER CODE!!

set_tris_a(0xff);
setup_adc_ports (all_analog);
set_adc_channel (0);
setup_adc(ADC_CLOCK_INTERNAL);
read_adc(ADC_START_AND_READ);


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

value = read_adc();
OUTPUT_B(value);}


___________________
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:
Top