PDA

View Full Version : PWM Problem



cihhan
- 14th June 2008, 16:14
Hi everyone,

I am new to Pic and PicBasicPro, but I have to finish this project as soon as possible. I am trying to use 2 PWM outputs using a Pic16F877. altough it works in Proteus, unfortunately I could not make it work in a real design (To be honest I also have a problem in proteus simulator: after 4.30V it goes back to 4V and stops there, doesnt go higher). If you can give me any idea on my problem, I would really be appreciative. The code is below:

'************************************************* ********
adcon1 = 2

trise = 0

trisa = %11111

trisb = %00010110
input portb.4
input portb.1
input portb.2
mode var portb.4
plus var portb.1
minus var portb.2

TRISC = %11111001 ' PORTC are input lines except C1,C2 is HPWM output

' Define pwm parameters
DEFINE CCP1_REG PORTC 'HPWM PORTC.1
DEFINE CCP2_REG PORTC 'HPWM PORTC.2
DEFINE CCP1_BIT 2 'CCP1 PİNİ PORTC.2
DEFINE CCP2_BIT 1 'CCP1 PİNİ PORTC.1
DEFINE HPWM1_TIMER 1 'Hpwm 2 timer select
DEFINE HPWM2_TIMER 1 'Hpwm 3 timer select
'DEFINE OSC 4 '4MHZ'LİK OSİLATÖR

Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

' Define LCD parameters
DEFINE LCD_DREG PORTD ' I/O port with LCD. (Defining LCD data port)
DEFINE LCD_DBIT 4 ' When using a 4 bit data bus, (0 or 4) designate beginning of data bits.
DEFINE LCD_RSREG PORTD ' Defining LCD's register port.
DEFINE LCD_RSBIT 2 ' Register select pin.
DEFINE LCD_EREG PORTD ' Defining LCD's enable port.
DEFINE LCD_EBIT 3 ' Defining LCD's enable pin.
DEFINE LCD_BITS 4 ' Defining LCD has 4-bit data bus
DEFINE LCD_LINES 2 ' LCD has two charecter lines

out1 var byte
out2 var byte
out1 = 0
out2 = 0

out1plus var byte
out1minus var byte
out2plus var byte
out2minus var byte

out1plus = 0
out1minus = 0
out2plus = 0
out2minus = 0

new1 var word
new2 var word
old1 var word
old2 var word

old1 = 0
old2 = 0

lcdout $fe, 1, "Welcome to"
lcdout $fe, $c0, "application"
pause 3500

loop:


adcin 0, new1
adcin 1, new2

if (old1 <> new1 and old2 <> new2) then
lcdout $fe, 2
lcdout $fe, 1, "Input 1 => ", DEC (new1*100)/51
lcdout $fe, $c0, "Input 2 => ", DEC (new2*100)/51
endif
old1 = new1
old2 = new2

if (mode == 0) then

if (plus == 1 ) then '
out1plus = out1plus + 1
else
if (minus == 1 ) then '
out1minus = out1minus + 1
endif
endif
else
if (plus == 1 ) then '
out2plus = out2plus + 1
else
if (minus == 1) then '
out2minus = out2minus + 1
endif
endif
endif

if (out1plus >= 10 ) then
if (out1<255) then
out1 = out1 + 1
endif
out1plus = 0
endif

if (out2plus >= 10 ) then
if (out2<255) then
out2 = out2 + 1
endif
out2plus = 0
endif

if (out1minus >= 10 ) then
if (out1>0) then
out1 = out1 - 1
endif
out1minus = 0
endif

if (out2minus >= 10 ) then
if (out2>0) then
out2 = out2 - 1
endif
out2minus = 0
endif


HPWM 1, out1, 10000
HPWM 2, out2, 10000

goto loop

end
'************************************************* ********

cihhan
- 14th June 2008, 18:43
Sorry if I bothered you, bcs it was about our circuit. Somehow there was an short circuit that drove us crazy :)

Thank you very much :)