I have been out of the electronics scene for a while now and only recently had the time to do anything like building and programming.
I have in the past done a few successful projects using Mikrobasic and thought that it would be simple enough to get back into. How wrong I was. I am not getting any younger and am at the same age as my father was when he started with altzheimers, though at the moment they say I am ok, but I am starting to wonder now I have begun to write (or try) a program again.
What I was trying to do was control a PMDC motor (brushed) using PWM and a simple potentiometer. All I wanted was to be able to turn the pot to any setting and when I switch on the circuit for the motor to soft start and ramp up to the setting. Then I decided that I also needed the pot to be controllable up and down. I already had the program working on my Easypic5 with a pic 16F887 and it seems logical to think it would be easy enough to expand on the simple slow ramp up to do ramp down aswell. Thats where it all fell apart.
Can anyone spot what I'm doing wrong here? I can adjust the pot to anywhere and turn on with the expected result but get no reaction if I then turn the pot down to reduce the speed. I added the PortB and PortD lines to show in real time what was happening but it just puzzled me further the LEDs on portB move in line with me moving the pot and all seems well there but the ones on portD just seem to be stuckoften showing a binary value of 94 but not always the same.
The LED on the PWM output channel does ramp up but won't ramp down again even though the binary value on portB goes from 0 to 255 both up and down with the pot. Something is wrong but I don't know what and have been staring at the screen for hours now.
Help would be very much appreciated, Al
Code: Select all
program pwmtestacd2
' Declarations section
dim ramp as byte
dim result1 as word
main:
ANSEL = 0x0C
TRISA = 0xFF
ANSELH = 0
TRISB = 0
TRISD = 0
ramp = 0
PWM1_Init(1000)
delay_ms(2000)
mainloop:
result1 = ADC_Read(2) ' Result of A/D conversion is copied to result1
result1 = (result1 >>2)
if ramp < result1 then Uploop
if ramp > result1 then Downloop
delay_ms(10)
else PORTB = result1 ' binary shown moved to PORTB
PORTD = ramp ' binary shown moved to PORTD
END IF
PWM1_Set_Duty(ramp)
PWM1_Start()
goto mainloop
Uploop:
ramp = ramp + 1
if ramp = result1 then mainloop
delay_ms(10)
else PORTB = result1 ' binary shown moved to PORTB
END IF
PWM1_Set_Duty(ramp)
PWM1_Start()
PORTD = ramp ' binary shown moved to PORTD
goto Uploop
Downloop:
ramp = ramp - 1
if ramp = result1 then mainloop
delay_ms(10)
else PORTB = result1 ' binary shown moved to PORTB
END IF
PWM1_Set_Duty(ramp)
PWM1_Start()
PORTD = ramp ' binary shown moved to PORTD
goto Downloop
end.