PDA

View Full Version : 18f4680 pwm



Macgman2000
- 24th August 2010, 18:07
Hello,

I am trying to use the ECCP for a full H-bridge. I can not seem to get it working, any help would be appreciated in resolving my issue... see below.





INCLUDE "Modedefs.Bas"
DEFINE OSC 20


; Set port direction and turn off unused peripherals
CMCON = 7 ' shut offcomparators
TRISA = %00011111 ' Set PORTA to input "1" or output "0"
TRISB = %00000000 ' set portB to output "0" or input "1"
TRISC = %00000000
TRISD = %00000000
TRISE = %00000000

ADCON1 = %00001111 ' bit 0 ~ bit 3 sets ports as digital I/O. 1 = digital, 0 = analog
ADCON0 = %00000000 ' bit 0 disables A/D converter module.
ECCP1CON = %00000000 ' bit 0 ~ bit 3 turns off capture/compare/PWM


; Declare variables and aliases

duty var byte

PAA var LATA
PBB var LATB
PCC var LATC
PDD var LATD ' used to re/set portD.1, can not use portd.1 = x on latch ports
PEE var LATE


; initialize power on state of ports
LATA = 0
LATB = 0
LATC = 0
LATD = 0 ' force latches on portD to low
LATE = 0


ECCP1CON = %11001100 ' bit 0 ~ bit 3 turns off capture/compare/PWM
T2CON = %00010101 ' Turn on Timer2, Prescale=4
PR2 = 249 ' Set PR2 to get 5KHz PWM out

duty = 100
ECCPR1L = DUTY
ECCP1CON.4 = duty.0
ECCP1CON.5 = duty.1


Mainloop:

pauseus 1000

goto mainloop

Macgman2000
- 24th August 2010, 19:30
It works as is.... traces lifted off PCB...

WOZZY-2010
- 25th August 2010, 02:40
I played with this code a little today and it works well....Thanks

Just a few minor changes can give 10 bits PWM resolution.

Change duty var byte to duty var word.

Add the line ECCPR1L = duty >> 2 after ECCP1CON.5 = duty.1.

now the duty cycle can be set from 0 to 1023 instead of 0 to 255.

Bob W.