Back after a long absence.

General discussion on mikroBasic.
Post Reply
Author
Message
BigAl
Posts: 31
Joined: 09 Sep 2008 13:58

Back after a long absence.

#1 Post by BigAl » 26 Feb 2015 01:53

Hi to all,

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.

User avatar
marina.petrovic
Posts: 2986
Joined: 18 Apr 2013 08:11

Re: Back after a long absence.

#2 Post by marina.petrovic » 26 Feb 2015 11:55

Hi,

Please, take a look at the example written for our DC MOTOR click, maybe this example can help you:
http://www.libstock.com/projects/view/9 ... lick-board

and "ADC on LEDs" and "PWM" examples written for EasyPIC5: http://www.mikroe.com/easypic/v5/

With ADC_Read(2) you will get 10-bit results of AD conversion and with result1 = (result1 >>2)
you send two most significant pits into "result1".

Double check your Project Settings just to be sure, and also whether you set properly ANSEL register.

Best regards,
Marina

BigAl
Posts: 31
Joined: 09 Sep 2008 13:58

Please help with PWM problem I'm at a loss.

#3 Post by BigAl » 01 Mar 2015 19:56

Hi Marina,

I have studied and studied the examples and though they work they do not do what I wanted.

I have been trying for days to get something like I need using various methods like for next loops and case select etc.

All I have got is what looks like it should work to me but doesn't.

I am using a known good 16F887 in the easypic5 board and have a 10k pots wiper connected to RA2 and B, C and D LEDs enabled. When I run the program the Port B leds move with the pot perfectly but the Port D ones do not light and nor do I get the expected PWM controlled LED on Port C, not a flicker!

I know ALL the parts are ok and that they CAN function as desired since I have run other programs to test them.

If anyone can help with my MikroBasic Pro Problem I would be over the moon. I don't expect someone to write the program for me, just tell me where I am going wrong as for the life of me I cannot find the problem. Please don't think this is a post where I can't be bothered to do it myself. I have a lot of patience and keep trying different things but at the moment my Wife is starting to get angry that I'm spending all my time trying to get this right. I have simplified the program so it does the very least possible just to get me seeing results and still no joy.

All I am trying to accomplish is reading a pot and then setting up a PWM to slowly ramp up to where the pot is set to and from then on to simply follow the pots movements. I have had some success with the following part but can't seem to integrate the two parts together. In fact the more I put into the program the less it seems to function. The delay at first is there to let the Pic power up enough to get a stable reading before the ADC conversion starts. Also I am converting the 10bit ADC to 8bit using the >>2 to divide the 10bit.

Thanks for reading my tome, Al

Code: Select all

dim speed as byte
dim ramp as byte
dim potread as word

main:
delay_ms(2000)
ramp = 1
ANSEL = 0x0C
TRISA = 0xFF
ANSELH = 0
TRISB = 0
TRISD = 0
PORTD = 0
PWM1_Init(2000)

 do
 potread = (ADC_Read(2) >>2) 
 inc (ramp)
 delay_ms(20)
 PWM1_Set_Duty(ramp)
 PWM1_Start()
 PORTD = ramp
 PORTB = potread
 loop until ramp = potread

while 1
  potread = (ADC_Read(2) >>2)
  PORTB = potread
  PORTD = ramp
  delay_ms(10)
  PWM1_Set_Duty(ramp)
  PWM1_Start()
wend

end.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Back after a long absence.

#4 Post by janni » 01 Mar 2015 23:53

You've forgotten to assign new values to ramp in the second loop :wink: . Use this

Code: Select all

while 1
  ramp = (ADC_Read(2) >>2)
  PORTB = ramp
  PORTD = ramp
  delay_ms(10)
  PWM1_Set_Duty(ramp)
wend
BTW, there's no need to use PWM1_Start() in the loop.

BigAl
Posts: 31
Joined: 09 Sep 2008 13:58

Re: Back after a long absence.

#5 Post by BigAl » 02 Mar 2015 01:47

Hi Janni,

Still the same I'm afraid, no output on PWM at all. :( Good to know I don't need to use the PWM_Start again though. To be honest I was just covering all the bases since things have been wrong. Would you think the PWM would be outputting with my code?

The only thing I am using Port B and D for is so I can debug the problem a bit, but sadly even thats not helping.

Baffled, Al

BigAl
Posts: 31
Joined: 09 Sep 2008 13:58

Re: Back after a long absence.

#6 Post by BigAl » 02 Mar 2015 13:38

Hi, I have found the problem. It was that the compiler was picking up another project even though it didn't show on the screen. I cleaned the folder of all files except the one I was working on and bingo! OK at last.

Thanks, Al

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Back after a long absence.

#7 Post by janni » 02 Mar 2015 14:02

I'm glad for you (for your wife too) :D .

BTW, the condition ending first loop would be safer if you used

Code: Select all

loop until ramp >= potread

BigAl
Posts: 31
Joined: 09 Sep 2008 13:58

Re: Back after a long absence.

#8 Post by BigAl » 02 Mar 2015 16:55

Thanks Janni. Good idea that I will do it. Could save overrun!

Al

Post Reply

Return to “mikroBasic General”