Model Boat Mayhem

Please login or register.

Login with username, password and session length.
Pages: 1 2 [3] 4 5 6 7   Go Down

Author Topic: Help writing pic code  (Read 22862 times)

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #50 on: September 15, 2014, 07:25:23 PM »

Hi Ian
The valves are operated by servos. The water bypass valve is just open or closed but the gas valve needs to be in either the fully open position or in the pilot flame position where it can be altered with a pot to give a suitable flame.
Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #51 on: September 15, 2014, 08:01:03 PM »

Hi Stew,

The pilot flame on my steamer is via a 1/8th inch crimped pipe. The pipe is gently crimped in several places until the desired flame size is achieved. The pipe is drilled in three places so that I have effectively three pilot flames.

The pilot flame is also used to evaporate the liquid propane/butane fuel (I use the gas cylinder inverted to prevent it freezing up) before the gas goes back to the pilot flame and also to the main burner control valve.

If you could use solenoids, it would simplify the electronics a lot by not requiring PWM forming circuits (or software!) and might get away with a quad power op amp chip.

This may simplify your requirement.

Ian
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #52 on: September 15, 2014, 08:30:51 PM »

Hi Ian,

I appreciate what you're saying but as I said at the beginning of this thread I have 2 goals, 1 is to make my unit smaller and the other is to learn how to program pics.
I don't think using a quad op amp would simplify the circuit enough for me to reduce the size significantly as I already use a quad op amp to operate the sensors and even adding a dual would make the circuit too complicated to fit the size of circuit board I want to use.
The beauty of using a pic is that all the terminals can be directly attached to the pic pins and the connections don't have to meander all over the board. There will also be a much smaller parts count using a pic. It is very easy to run out of space when using stripboard, and there always seems to be 1 hole too few.

Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #53 on: September 16, 2014, 10:05:54 AM »

Hi Stew,

It was worth exploring the options before going down the PIC route, it was precisely what I did and I ended up with a system that could very easily be adapted and, if the new version didn't work, I could reload the previous software in a few minutes.

Once you've got a PIC interfacing with a standard servo, it opens up lots of applications.

Have you decided on which PIC programmer to use? I started with the Maplin's Vellerman  K8048 (Code: N36AC) and have recently moved on to Microchips PICKit 2 Starter Kit (from Rapid Electronics) because my new computer only has USB connections. The Starter Kit comes with a PIC16F690 which has the timers and analogue inputs.

Full steam ahead now for a PIC ABC that has more potential than the original!

Ian

Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #54 on: September 16, 2014, 10:48:18 AM »

Hi Ian,

I originally bought a pic k150, a cheap chinese programmer, which I have used to burn a few example progs. However i spotted a genuine picKit 2 on ebay which was going for a song so I had to buy it, should be here in the next couple of days.
I've also ordered a couple of pic16f684 chips.
I've found a bit of code which gives a 1ms delay and I should be able to alter it to give my variable delay.
What I need now is 2 timers, 1 to count to 20ms and the other somewhere between 5 and 10 seconds.
Which timer would be best for each of these?

Stew
Logged

boneash

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 187
  • In an actual war zone there are 2 types of ships,
  • Location: Kent UK
Re: Help writing pic code
« Reply #55 on: September 16, 2014, 10:59:41 AM »

have just seen this one on ebay, any good?
Logged

boneash

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 187
  • In an actual war zone there are 2 types of ships,
  • Location: Kent UK
Re: Help writing pic code
« Reply #56 on: September 16, 2014, 11:00:54 AM »

Have just seen this one ........
 
eBay item number:
281440864725
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #57 on: September 16, 2014, 11:13:00 AM »

Hi boneash
that's a bargain but look at the sellers feedback score, I'd be wary.
Stew
Logged

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #58 on: September 16, 2014, 09:43:16 PM »


What I need now is 2 timers, 1 to count to 20ms and the other somewhere between 5 and 10 seconds.
Which timer would be best for each of these?

Stew
I have no idea about the PIC you are using, but I would do it this way:
Id use only 1 timer for everything including the servo signal, it works better if you use a 16 bit timer for that to get a better resolution of the 1-2ms servo signal.

1. overflow:
Set the timer to 1-2ms until overflow with a variable that features the servo value you want to have + set output port to 1
Increment another variableX if variableX< 250 (=5s), if variableX > 250 do whatever you want to do, best is just to set another variableY from 0 to 1 and do the rest in the main program to keep the ISR short + set variableX to 0 again.
Next overflow:
set output port to 0 + set timer to 18ms until overflow (if timer overflows too fast set it to 9ms + another overflow with 9ms)

with this you just need 1 timer for all functions you just need another variable to check of 1. overflow should happen or next overflow.
Logged
regards

Tobias

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #59 on: September 16, 2014, 10:35:17 PM »

Hi Stew
There will be many strategies but here is another suggestion. Your PIC has 3 timers. TMR0 and TMR1 run off of the instruction clock. These are 8 bit timers so they can count up to 256 . TMR1 is a 16 bit timer and so can count up to 65, 536. This timer van run from the instruction clock or from am external oscillator such as a 32 KHz crystal.

However, TMR0 and TMR2 have what are called prescalers which means that they can effectively also count up to 65, 536.
Assuming you run the PIC at 4MHz like Ian suggested, so each instruction cycle is 1 usec, then each of these timers can reach a max time of 65.5 msecs.  That means you could use TMR1 to set a time period of between 1 msec and 2 msec to control the pulse widths to be sent in turn to each servo. TMR0 could be used to set the 20 msec delay that determines when you restart the servo control cycle. To get the 10 sec period you could then do what Time Bandit suggests and decrement a register that is initially set at 50 and when this reaches zero you reset it to 50 and decrement another register initially set at 10 (for a 10 sec delay).
If I've understood things correctly one of the servo pulse times will be set  at l msec plus a time depending of whether bypass valve should be open or closed. Then the other pulse width will equal 1 msec plus a short time calculated from one of the A D inputs plus a time dependent on whether the gas valve is open or almost closed.

Regard  Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #60 on: September 16, 2014, 10:36:28 PM »

Hi Tobias
The chip I am going to use has 3 timers, 2 8bit and 1 16bit.
I have some code which gives a 1ms delay which I can use for the servo positions. I need 1 of the timers to count to 20ms so that the program repeats every 20ms. I also need a timer which will stop the water servo operating until the water level has been low for approx 5seconds. The rest of the program still has to operate during this time.
I have no idea how to do all this with 1 timer
Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #61 on: September 16, 2014, 10:56:46 PM »

Hi Mike

TMR1 also has a 3bit prescaler so it can count to 524288. Is it possible to count how many times it overflows, say 10 times. This delay isn't critical, anything between 5 and 10 seconds would do.
The gas servo will be either closed:- 1ms + delay for pilot flame. or open:- 2ms
The water servo will be either closed:- 1ms. or open:- 2ms

Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #62 on: September 17, 2014, 07:42:40 AM »

Hi Stew
Yes, that should work O.K. However, using that method would mean you would have to use the third timer to generate the 20 msec timing periods.

 If you use TMR1 to time the pulse width then l would set its prescaler to 1:8. This will give a sufficient maximum timing period ( 2.048 msecs) whilst giving maximum time resolution. Although you don't need high timing resolution to open and close the valves, l guess it could be important for setting the pilot position. But decrementing a register and carrying out a new task when it reaches zero is dead simple -using decfsz command.

Regarding how to achieve all this, l find it very useful to draw a flow diagram with operation boxes and branch points etc. Initially operation boxes can describe complex operations which get broken down into actual PIC commands later.
Regards   Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #63 on: September 17, 2014, 10:41:19 AM »

Hi Mike
If I use TMR0 with a prescaler of 256 and count to 78 this will give a time of 19.968ms, close enough for what I need.
And if I allow it to loop 250 times, that will give me a delay of approx 5 seconds, again close enough for what I need.
I have some code to give a 1ms delay which I can also alter and use to give the pilot flame delay(If I use the 5 most significant bits of the analogue output of the pot I can get 11.5 degrees of servo movement with a 5bit resolution, should be good enough).
All I need to do now is put all this into some sort of order, configure the ports etc. and I should be good to go.

any comments
Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #64 on: September 17, 2014, 11:51:35 AM »

Hi Ian
My PICkit 2 arrived this morning with no cables or software.
I have managed to find a couple of cables but would like to know which software I need. Could you tell me what software you use and version numbers so that I can install the same and we're both singing from the same songsheet.
Stew
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #65 on: September 17, 2014, 04:17:16 PM »

WOW
2 new toys in 1 day.
Just received a lovely shiny analogue oscilloscope.
Gone to play.
Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #66 on: September 17, 2014, 06:14:16 PM »

Hi Mike
If I use TMR0 with a prescaler of 256 and count to 78 this will give a time of 19.968ms, close enough for what I need.
And if I allow it to loop 250 times, that will give me a delay of approx 5 seconds, again close enough for what I need.
I have some code to give a 1ms delay which I can also alter and use to give the pilot flame delay(If I use the 5 most significant bits of the analogue output of the pot I can get 11.5 degrees of servo movement with a 5bit resolution, should be good enough).
All I need to do now is put all this into some sort of order, configure the ports etc. and I should be good to go.

any comments
Stew

Hi Stew
Sounds like you have a workable plan.
How does your code generate the delay periods?

Re 'scopes, I've found my basic Picoscope was very useful for checking PWM outputs and absolutely invaluable for getting to the bottom of some weird problems with serial communication between PICs. Hope you enjoy yours.
Regards   Mike
Logged

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #67 on: September 17, 2014, 06:26:21 PM »

Its always good to use the resources economically since if the programs get more complex you dont want to use 2 timers just for the servo output.
Use a 16 bit timer with prescaler 8 @ 8Mhz clock speed (values change at 4Mhz)

So Timervalue for 1ms = 64535
2ms= 63535
1000 points resolution, no Servo can use this  :} (at 4Mhz 500 points)
Timervalue for the 17ms (2 servos at average 1,5ms) break between pulses = 48535
The ISR will look like this (V are variables with any name):


ISR:
If V1=1 then set Timer 1 to 65535-V2 (servovalue for servo 1 between 1000 and 2000) / set servoouputpin1 = 1
If V1=2 then set servooutputpin1=0 / set Timer 1 to 65535-V3 (servovalue for servo 2 between 1000 and 2000)/ set servoouputpin2 = 1
If V3=3 then set servooutputpin2=0 / set Timer 1 to 48535 (17ms break) / increment V4 if  water below level / if V4>249 set V5=1 (use V5 in main program to react on water low level) / if water lvl good set V4=0 V5=0

Increment V1 until V1=4 else set V1=1
return
Logged
regards

Tobias

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #68 on: September 17, 2014, 07:01:08 PM »

Hi Mike
This is the code for the 1ms delay

Wait1ms   movlw    d'250'       ;Initial value - tweak if req.
              nop
loop1ms   addlw    d'255'       ;dec W
              btfss    STATUS, Z    ;Zero flag set?
              goto     loop1ms      ;  No, keep looping
              return                ;  Yes, 1ms done

If I substitute the 5bit analogue value of the pot for the '250' in the first line it will give me the pilot flame delay.
I haven't worked out the code for the 20ms loop yet but everything will be nested inside it so that the program loops every 20ms irrespective of the delay for the servos.

Stew
Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #69 on: September 17, 2014, 07:44:01 PM »

Hi Stew,

This piece of code solved a lot of strange problems when I first started, it was one of the very few occasions that I resorted to searching the Web for a solution. Place it at the very start of your code. The memory of the 684 is only over one "page", so it is not a problem, but with larger PICs the memory may split over several pages and when you leave one page, due to an interrupt,  you have to make a note of where you have come from so you can return after servicing the interrupt. See Section 2.0 of the Data Sheet.

Notice the use of "H" to define the following number as an Hexadecimal address. I tend to enter the Instructions e.g.  ORG, GOTO etc, in upper case because they don't get confused with the comments after the semi-colon which are in lower case - that's just my style.

      ORG     H'0000'      ;Reset vector address. This is where the PIC starts after a physical Reset, i.e. pin 4 has been decked to Ground. See Data Sheet 12.3d.
      GOTO  RESET      ;goto RESET routine when boot. The RESET routine is the main body of the code after the Interrupt Service Routine.
      ORG   H'0004'      ; Interrupt vector address. All interrupts arrive at this physical address H'0004'
      NOP                 ; No operation.
                                 ; BEWARE!!! you must avoid the "GOTO INTERRUPT" or "GOTO ISR" statement at the interrupt vector as this inherits the
                                 ; page bits from the page that's interrupted! Just put a NOP at the interrupt vector
                                 ; and drop through to an interrupt routine in Page 0 at address physical address  h'0005'.
                                 ; From: http://www.piclist.org/techref/microchip/pages.htm

                                                Place your Interrupt Service Routine (ISR) from this location onwards and before the RESET routine.

;          **********************************
;              **  RESET :  main boot routine  **
;              **********************************

RESET   Your code starts here

What a lot to absorb!

Ian.


Logged

flashtwo

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 521
  • Location: Sevenoaks, Kent, England
Re: Help writing pic code
« Reply #70 on: September 17, 2014, 08:02:04 PM »

Hi Stew,

I originally wrote the code in Notepad on my old computer and compiled it using MPASM v5.51 and then used the Vellerman board to download to the PIC in circuit.

I only got the more up to date PicKit 2 because the old Vellerman board didn't support a USB connection that is on my new computer. I haven't used the new PicKit 2 other than proving that it worked OK. My current monotube boiler controller's software is fairly mature now and I haven't any changes to make at the moment.

The versions I've currently got are:-

PicKit 2 version 2.50.02,   MPLAB IDE version 8.70

Ian
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #71 on: September 17, 2014, 08:20:26 PM »

Hi Stew
That seems neat but I guess the nop should be in the loop rather than just before it so as to give 4 instruction clock cycles per loop.
Of course the disadvantage of relying heavily on this type of delay is that the PIC cannot do anything else while it's waiting but obviously that does not matter in this situation.
It will be a great step forwards to mimick the Cheddar control system but as control systems go this is very basic. To pick up on one of Ian's comments, once you have a PIC control working you can consider all sorts of improvements without turning on the soldering iron. If you look at Ian's thread on flash steam plant control you'see much discussion of other control systems that you could think about. Trouble is you could end up like me and get sidetracked with making valves and controls but never get anything actually finished  {-)
Regards   Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #72 on: September 17, 2014, 08:41:03 PM »

Hi Mike
I copied that code from page six of the Mark Hennessy web site that you showed me. He explains the code and how long it takes. He also seems to think the NOP does not need to be there at all.

Stew
Logged

TurboTyne

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 113
  • Location: Newcastle upon Tyne, England
Re: Help writing pic code
« Reply #73 on: September 17, 2014, 09:08:57 PM »

Ah yes, I overlooked the fact that btfss can take 2 cycles.  {:-{
Mike
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #74 on: September 18, 2014, 12:44:12 AM »

Hi Ian
I've installed PICkit v2.61 and built a programmer board and everything seems to work. I also downloaded mplabX but that seems very complicated. I'll try an earlier version.
Mike
Do you use mplab and if so which version?

Stew
Logged
Pages: 1 2 [3] 4 5 6 7   Go Up