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 32026 times)

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Help writing pic code
« on: August 04, 2014, 07:22:46 pm »

Hi shipmates
I have built a boiler control unit similar in operation to a Cheddar ABC unit using opamps and timer chips. This unit works well but is on the bulky side.
I would now like to scale it down using a pic or maybe a picaxe chip.
Is there anyone out there who could help with writing the code for this. I am not looking for someone to write the code only to hold my hand and point me in the right direction. I have bought a book to try and teach myself but I don't understand a word of it.
regards
Stew
Logged

dreadnought72

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 1,892
  • Wood butcher with ten thumbs
  • Location: Airdrie, Scotland
Re: Help writing pic code
« Reply #1 on: August 05, 2014, 12:10:46 am »

I'd love to be able to help. I'm learning/developing working code for the Arduino: if you've not set your heart on Picaxe then DO dip a toe into the world of Arduino - you might be surprised at what it can do!

PM me your email address and we'll take it from there (and report back here!)

Best wishes,

Andy
Logged
Enjoying every minute sailing W9465 Mertensia

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #2 on: August 05, 2014, 08:03:01 am »

Hi Dreadnought72
Thanks for your offer of help.
I've looked at the arduino but it is simply too large, even without the board the actual chip is almost as large as the enclosure I want to put it in.
You may still be able to help me with the coding though. What language do you use to code the arduino?

regards
Stew
Logged

dreadnought72

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 1,892
  • Wood butcher with ten thumbs
  • Location: Airdrie, Scotland
Re: Help writing pic code
« Reply #3 on: August 05, 2014, 10:04:52 am »

The Arduino Nano is 0.73" x 1.70" ... how small do you need to go!?  :-)

The language is a form of C/C++ and pretty straightforward for anyone with a toe in the world of Javascript or ActionScript.

Thanks for the PM - I'll send you my email address and we'll take it from there.

Andy
Logged
Enjoying every minute sailing W9465 Mertensia

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #4 on: August 05, 2014, 12:02:59 pm »

If you change from PIC to AVR I could be able to help.


I have learned BASCOM a year ago, which is a basic dialect.
But also the Arduino code is able to flash normal AVR.
Arduino is mainly the hardware around the mikrocontroller.

Would be a good idea if you describe what the control has to do incl. Inputs and outputs.
Then I can check which chip to choose and how much effort it would be to programm it.
Logged
regards

Tobias

richald

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 1,941
  • Retired and loving it!
  • Location: Driffield, East Yorks.
Re: Help writing pic code
« Reply #5 on: August 05, 2014, 05:16:57 pm »

I'm with dreadnought72 - the Arduino is possibly the best way to go -
I have been doing a bit of programming on it and I am impressed with
its capabilities. It is also cheap if you go to somebody like Hobbyking!

Arduino Nano V3.0 Microcontroller Board Ģ5.48 !

Richard
Logged
Senior member of the OGG (Order of the Grumpy Gits)
Membership Number : 002

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #6 on: August 05, 2014, 05:50:35 pm »

I have to admit, the ARDUINO is a very good development tool and for beginners probably the best to start with, since you just have to deal with programming and not with designing the surrounding circuit.
At least the hardware (I never took a deeper dive in programming it) is designed pretty perfect, if you need it.

BUT,
basically itīs just an ATMEL AVR with some hardware around it. The hardware around it, of which 90% is useless in 90% of the cases in RC-modelling, makes it also expensive.
Who needs a USB in a model?
Compared to a Atmega8 at Ģ0.5  in my opinion itīs too expensive and too overpowered for simple applications in RC models.
In addition to this you can use only 4? of 100? different AVR controllers with a lot of different features.
For example I really like the small Attiny due to their abilities like high frequency PWM and DAC capabilities.

I canīt really judge the programming since I never used more than 10 min to check on some code.
But what I experienced is a lot of "inexplicable" errors you read of...
The start is very easy since you can use a lot of "premanufactured" libraries, but on some point you have to understand what these libraries do, to use the controller correctly (e.g. using the same timer twice is not a very bright idea).
And thatīs the problem what you can read in several forums every day "the servo library is not working correctly".
The problem is always that people are not able to read the datasheets of the controllers or just donīt care what happens in several libraries. Thatīs why these threads are called "timer1 threads"  :}

My summary:
If you want to develop you own working mikrocontroller applications and you are able to handle the (pretty easy) circuits outside the controller (which I guess should be no problem if you can develop a circuit with opamps and timer chips) youīl better go with single mikrocontroller chips and "C" or "BASCOM" or even "Assembler".
For people who ever saw basic, BASCOM is probably the easiest language to learn. If you are used to "C" or "C++" it makes no sense to change.
This does also appy if you want minimalistic hardware (package size and price).

If you want a perfect development environment with incredibly fast learning progress on the first steps, youīll better go with ARDUINO.

Still waiting for some details on your project  :}
Logged
regards

Tobias

fpravenscroft

  • Shipmate
  • *
  • Offline Offline
  • Posts: 23
  • Location: St. Helems
Re: Help writing pic code
« Reply #7 on: August 05, 2014, 06:15:39 pm »

hi megatron
   if you download the pick axe progamer it has a flow chart that will convert to the program
 also there is a manual that you can download
the sound systems for deisel engines are on picaxe chips
hope this helps
regards
peter
Logged

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #8 on: August 05, 2014, 07:42:42 pm »

Hi everyone
The box I am hoping to use is 50x35 and as I will be building on stripboard that gives me a useable grid of 18x12. The arduino nano appears to be 18x8 so that's out of the question.
I need 5 inputs, 1 digital, 1 analogue, and 3 that could be either. I also need outputs for 2 servos and 3 leds.
I am seriously considering the picaxe 14M2 as this is fairly cheap, has a 7x4 footprint, has 12 i/o pins plenty of adc's and support in the software for outputting to servos.
They are programmed using Basic and as fpravenscroft says they will even program from a flow chart.
Has anyone used a picaxe before and if so what's the verdict.
I am still open to suggestions so if you know of something better that fits the bill let us know.
regards
Stew
Logged

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #9 on: August 05, 2014, 08:16:46 pm »

Programm from a flowchart (like Siemens S7?) sounds nice. Even if you once found out how to program "manually" itīs probably too time consuming.
But beware of the "Servo-out" functions, no matter what you are going to do in the end. Never saw one that worked good (despite one Arduino lib), the servo are always jittering if the code gets more complex than just controlling servoīs.
I recommend to programm it manually with a timer interrupt control.

I donīt have a better solution, just maybe the same as AVR.
Check out the Attiny44/84  http://www.atmel.com/Images/doc8006.pdf
The hardware is probably comparable, the only thing that counts for AVR in my opinion is that (at least here in germany) more people are using it and can help debugging. Also the prices should be lower. Attiny44 is less than 1€ here in germany.
Not sure how big your programm is going to be, but you have to consider if you are using basic that the programs get around twice the size as if programmed in Assembler. 2kb can get small pretty fast  %%

Not so sure about the costs of the development environment for the PICīs.
If you need no further environment this should do it to flash the AVR chip. http://www.ebay.de/itm/USB-ISP-USBASP-Programmiergerat-Programmierer-fur-Atmel-AVR-ATMEGA8-ATTiny-OPV-/281121456079?pt=Wissenschaftliche_Ger%C3%A4te&hash=item417424ffcf
Iīm not a big fan of risking my personal computer hardware by flashing directly over a serial port with some DIY programmer.




Logged
regards

Tobias

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #10 on: August 05, 2014, 08:45:33 pm »

Hi Time Bandit
Yes the Attiny chips seem very similar to the picaxe chips. Similar size and price.
It all comes down to which one people know the best.
As you say 2k of program isn't a lot. I'll not know how much I need until the program is written. I had wanted to write it in assembler but I just can't get my head around it.
I hadn't heard of the Attiny chip until you mentioned it, over here the picaxe is popular as it's taught in schools.
I'll wait and see what the majority think.
Thanks
Stew
Logged

dreadnought72

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 1,892
  • Wood butcher with ten thumbs
  • Location: Airdrie, Scotland
Re: Help writing pic code
« Reply #11 on: August 05, 2014, 09:13:54 pm »

Who needs a USB in a model?

Anyone who needs to revise their code from a laptop, maybe?

Andy
Logged
Enjoying every minute sailing W9465 Mertensia

richald

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 1,941
  • Retired and loving it!
  • Location: Driffield, East Yorks.
Re: Help writing pic code
« Reply #12 on: August 06, 2014, 06:48:25 am »

Stew

Just out of interest, what are you intending to put on your stripboard?

I'm guessing a bit here, but most of your connections would probably
go straight onto an Arduino Nano

Richard
Logged
Senior member of the OGG (Order of the Grumpy Gits)
Membership Number : 002

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #13 on: August 06, 2014, 07:20:11 am »

Anyone who needs to revise their code from a laptop, maybe?

Andy

Well that's why the chips have an SPI interface  %)


Back to topic.
Taught in schools? That sounds like the german school system is even worse than I ever thought.
The usage rate should be better with tge Picaxe then.






Logged
regards

Tobias

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #14 on: August 06, 2014, 11:15:14 am »

Hi Richald
The stripboard will be used to mount the chip and will contain the circuitry for the sensors as well as the leds and external connections.
I've uploaded a picture of my previous boxes so you all know where we are going.
The box in the centre is a Cheddar ABC unit. The large box is my take on this, and the smaller box is a unit which only maintains boiler pressure. My goal is to make the large box the same size as the small one.
No problems then.
Stew
Logged

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #15 on: August 06, 2014, 05:09:59 pm »

I made some small test about the code size.
The following BASCOM code does:
- reading in 1 RC channel
- 2 Servo outputs
- reading 4 analogue Inputs
- does nothing with the input values, no calculation...

Code Size: 0,75kb which means 36% of 2kb used.

Code: [Select]
'******************************************************
'
' (c)2014 Tobias Birkner
'
'******************************************************
'Project: Cheddar ABC Clone
'
'Prozessor: Attiny44
'Bascom-Version: 2.0.7.7
'
'
'Version 1.0
'06.08.14 Test Program
'******************************************************
'
'======================================================
'System
'======================================================
'Definition Attiny
$regfile "ATTiny44.dat"

' (8Mhz)
$crystal = 8000000
$hwstack = 32
$swstack = 10
$framesize = 40

' Pin configuration
Config Pinb.2 = Input                                       'RC_channel
Config Pina.0 = Input                                       'ADC0
Config Pina.1 = Input                                       'ADC1
Config Pina.2 = Input                                       'ADC2
Config Pina.3 = Input                                       'ADC3
Config Porta.4 = Output                                     'Servo1
Config Porta.5 = Output                                     'Servo2
Config Porta.6 = Output                                     'LED
Config Porta.7 = Output                                     'LED
Config Portb.1 = Output                                     'LED

Servo1 Alias Porta.4
Servo2 Alias Porta.5
1led Alias Porta.6
2led Alias Porta.7
3led Alias Portb.1

'Konfiguration des Timer 1
Config Timer0 = Timer , Prescale = 64                       'timer to read rc channel
Config Timer1 = Timer , Prescale = 8                        'timer for servos
Config Adc = Single , Prescaler = 64 , Reference = Avcc
Start Adc
'Configuration INT0
'Interrupt bei jedem Flankenwechsel (0->1 und 1->0)
Config Int0 = Change

Dim Reading As Bit
Dim Rc_value As Word
Dim Analogueinput(4) As Word
Dim Kanal As Byte
Dim Servo(2) As Word                                        'theoretical 1000 values resolution. 64036 = Middle   +500 -500 = normal servo way 90°, increase for 180°



'Interrupt-Service-Routines
On Int0 Rc_read
On Timer1 Servoirq                                          'servo

'Enable anything
Enable Timer0
Stop Timer0
Enable Timer1
Timer1 = 64036                                              '64036 = Middle   +500 -500 = normal servo way 90°, increase for 180°
Enable Int0
Enable Interrupts

'Einmalige Aktionen vor Hauptprogramm


Servo(1) = 64035
Servo(2) = 64035



Do

Servo(1) = 0                                                'unspecified
Servo(2) = 0                                                'Unspecified
Analogueinput(1) = Getadc(0)
Analogueinput(2) = Getadc(1)
Analogueinput(3) = Getadc(2)
Analogueinput(4) = Getadc(3)
'nothing specified

Loop
End




Rc_read:                                                    'read RC Channel value (with low resolution)
If Reading = 0 And Pinb.2 = 1 Then
   Start Timer0
   Reading = 1
Else
   Stop Timer0
   Rc_value = Timer0
   Timer0 = 0
   Reading = 0
End If
Return


Servoirq:
If Kanal = 0 Then
   If Servo1 = 0 Then                                       'if port low
      Timer1 = Servo(1)                                     'timer1 on reload value
      Servo1 = 1                                            'port=1
   Else                                                     'happens at next interrupt
      Servo1 = 0                                            'port=0
      Incr Kanal                                            'next servo
   End If
End If
If Kanal = 1 Then
   If Servo2 = 0 Then
      Timer1 = Servo(2)
      Servo2 = 1
   Else
      Servo2 = 0
      Incr Kanal
   End If
End If

If Kanal = 2 Then
  Timer1 = 53536                                            'Pause for 16ms
  Kanal = 0
End If
Return
Logged
regards

Tobias

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #16 on: August 06, 2014, 05:53:20 pm »

Hi Time Bandit
Is the 0.75kb the size of the basic program or the size of the compiled code?
There shouldn't be a lot more to add, only reading the inputs and turning outputs on or off, there's no real calculations to do.
Do you think doing it in less than 2kb is feasible?
Stew
Logged

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #17 on: August 06, 2014, 07:31:02 pm »

The compiled code  :}
But most of it is coded discrete, so if you are going to use functions like "Servo Out" it is going to need a lot more byte.
But if itīs not much more to do it should work (at least with this "style" of programming)

If you can tell me how it is working or should work Iīm going to program it.
Description like this "if input1=0 --> Servo 1 maximum angle" is enough to me.
Itīs probably a matter of only minutes for me, the bigger task is to describe it :-)
Google is not finding "cheddar abc unit".
Logged
regards

Tobias

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #18 on: August 06, 2014, 09:29:35 pm »

Hi Tobias
I've sent you a PM
look for Cheddar automatic boiler control
cheers
Stew
Logged

afb

  • FORGE ELECTRONICS
  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 64
  • more wires than wood in my boats!
  • Location: New Forest
    • FORGE ELECTRONICS
Re: Help writing pic code
« Reply #19 on: August 07, 2014, 02:14:41 pm »

Hi Megatron

I've used most of the range of PICAXE chips including the 14M2. As an earlier reply pointed out I use PICAXE chips in my (Technobots) Engine Sound Units, plus I have made ESCs, rudder mixers, servo slower/stretchers, RC switches etc all using PICAXE. I'm also working with Arduino as I need it's speed for audio processing, and based on my experience I think that if you have done little or no programming then you'll find programming in (PICAXE) Basic is the easiest option. However, it's *interpreted* basic so the code won't run nearly as fast as compiled code that you'd use on the Arduino - but still plenty fast enough for your application I'd have thought. Again as someone pointed out, the PICAXE's built-in servo command that can run in the background is subject to 'glitching' - as all my projects are RC related I let reading of the RX input(s) set the rate of the overall program loop and use the 'pulsout' command to refresh the servo once per loop - this gives jitter free operation. I'd be happy to give you a few pointers should you decide to go the PICAXE route. Note there is a very good PICAXE Forum bursting with clever chaps eager to help.

Alan Bond
Logged
in the land of the blind the one-eyed man is king

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #20 on: August 07, 2014, 05:29:21 pm »

Regarding the discussion about Picaxe or AVR I also read a comment about the speed of the picaxe controllers.
I wonder why it should be slower since itīs also running at 4 Mhz. It canīt be only the compilation since C-compiled code is not much smaller than basic compile code.
What I experience is, that clever assembler program code is much smaller than both of them.

Anyways,
I added the code for the described functions and deleted the readout of the reciever channel, I hope this helps a little bit ;-)
It was a little bit more work than I thought, and the size of the code increased to 2,1kb.
So NOW you have to go with AVR  {-)
It can be optimized by defining the variables different, but I think you will always scratch the maximum of the picaxe, which is not a good idea.

I really tried to make it understandable, but Iīm open to questions.
Maybe you also add your description on the functions here, that might help others.
If you want to change the pin configuration on the attiny due to your circuit layout, you just have to change the ALIAS functions and input/output settings. Just be careful with the ADC numbers, since these are fixed to a certain pin.
You canīt work with Aliases here.
TO compile the code you need BASCOM (free version up to 4kb code is enough).
Itīs probably still full of errors but I did not find the time to simulate or test it in reality.
I have to finish my 16 channel universal multi switch for 2,4 GHz radios and will be on short vacation for the next 3 days. :}

Just a few additional questions:
Is the feed tank sensor also giving an analogue output or is it "digital" low/high, I guessed the sensors a comparable and both need calibration?
And both level sensors work on the same valve/servo? Thatīs how it is programmed right now.
What should happen if both water levels are low at the same time? Bypass Valve opened and Warning LED lit I guessed? What about the gas valve?

One thing Iīm a little curious about. Do you want a 2 point regulation for the gas or or proportional regulation.
Does the pressure sensor have an linear voltage output according to pressure? If yes a proportional regulation of the servo is possible.
But I have no idea if this is necessary (would it save gas? prevent overpressure valve from letting out steam?) or not.

The setup works like this, I thought of a 2 way toggle switch, if itīs just a push switch you have to change the code.

Press Switch low (input on Attiny is connected to ground) and then power on the module with 5V --> it goes into setupmode
All 3 LED are blinking
place boiler sensor above water level and press switch to high
wait for 1s
place boiler sensor below water level and press switch to low again
1 LED stops flashing, 2 LED still flashing
place feed tank sensor above water level and press switch to high
wait for 1s
place feed tank sensor below water level and press switch to low again
1 LED stops flashing, 1 LED still flashing
Pilot flame adjustment, Servo is set to 2ms at the beginning of setupmode (what setting should the "water servo" have during setup?)
You adjust the pilot flame now
Press Switch to high, last LED stops flashing
Disconnect module from electric power
Press Switch to high
Wait for 30s
Repower the module with switch=high and it starts in normal mode with calibrated water level sensor without flashing the LED




Can you somehow calibrate the pressure sensor (with external pump, without steam pressure)? Maybe you do this once, and manually change the value before flashing the chip  (pressuresensvalue=512 = 2,5V)

And also a recommondation regarding your circuit.
Best is to separate servo and chip power (even if itīs just 2 different BEC modules)
If this is (as in most cases) not possible, do not just put a 100n cap between VCC and GND of the chip.
You need a bigger cap to decouple the interference from the servoīs and a strong power supply, otherwise you may get some very strange reactions from brown outs to erasion of the non volatile memory.

Code: [Select]
'******************************************************
'
' (c)2014 Tobias Birkner / For Non-Commercial use only
'
'******************************************************
'Project: Cheddar ABC Clone
'
'Prozessor: Attiny44
'Bascom-Version: 2.0.7.7
'
'
'Version 2.0
'1.0 06.08.14 Test Program
'2.0 07.08.14 Completed code for automatic boiler control
'******************************************************
'
'======================================================
'System
'======================================================
'Definition Attiny
$regfile "ATTiny44.dat"

' (8Mhz)
$crystal = 8000000
$hwstack = 32
$swstack = 10
$framesize = 40

' Pin configuration

Config Pina.0 = Input                                       'Boiler lvl sensor
Config Pina.1 = Input                                       'pressure sensor
Config Pina.2 = Input                                       'Pilot flame pot
Config Pina.3 = Input                                       'Feed tank lvl sensor
Config Porta.4 = Output                                     'Servo1
Config Porta.5 = Output                                     'Servo2
Config Porta.6 = Output                                     'LED
Config Porta.7 = Output                                     'LED
Config Portb.0 = Output                                     'output for FET
Config Portb.1 = Output                                     'LED
Config Pinb.2 = Input                                       'Setup Mode Switch
Portb.2 = 1                                                 'internal pullup on

Sensorcalibswitch Alias Pinb.2                              'Digital in, internal pullup switched on, but you can add an external pullup resistor
Servo1 Alias Porta.4
Servo2 Alias Porta.5
Waterlowled Alias Porta.6
Gashighled Alias Porta.7
Waterbypassled Alias Portb.1
Electricpump Alias Portb.0

'Konfiguration des Timer 1
Config Timer0 = Timer , Prescale = 1024                     'timer for hyteresis 10s
Config Timer1 = Timer , Prescale = 8                        'timer for servos
Config Adc = Single , Prescaler = 64 , Reference = Avcc
Start Adc

Dim Reading As Bit
Dim Rc_value As Word
Dim Analogueinput(4) As Word
Dim Kanal As Byte
Dim Servo(2) As Word                                        'theoretical 1000 values resolution. 64036 = Middle   +500 -500 = normal servo way 90°, increase for 180°
Dim Eedummy As Eram Byte                                    'dummy since first adress is buggy
Dim Watersensvalue(2) As Word                               'average value between "no water present" and "Water present" 1= boiler 2=feed tank
Dim Eewatersensvalue(2) As Eram Word                        'saved value in EERAM
Dim Waterpresent(2) As Byte                                 'flag for 10s delay
Dim Tensecond As Word                                       'counter to increment since timer0 is too fast at 8Mhz
Dim Pressuresensvalue As Word                               'average value between "pressure high" and "pressure low"
Dim Eepressuresensvalue As Eram Word                        'saved value in EERAM
Dim Pilotflame As Word                                      'Amount of regulation value for pilot flame
Dim Calibstate As Byte                                      'flag for setupmode
Dim Ledtoggle As Byte                                       'counter to toggle LED during setup mode

Const Smv = 64036                                           'Servo Middle Value

'Interrupt-Service-Routines
On Timer1 Servoirq                                          'servo
On Timer0 Waterirq

'Enable anything
Enable Timer0
Stop Timer0
Enable Timer1
Start Timer1
Enable Interrupts

'Initialize before main program
Servo(1)=SMV+500   '1ms pump bypass valve open
Servo(2)=SMV-500   '2ms only Pilot flame at startup

Calibstate=0
If Sensorcalibswitch = 0 Then
   Goto Calibration
End If

watersensvalue(1)=EEwatersensvalue(1)
Waitms 10
If watersensvalue(1)=0 then             'only for first startup, if no calibration value is present
   watersensvalue(1)=512
End IF
watersensvalue(2)=EEwatersensvalue(2)
Waitms 10
If watersensvalue(2)=0 then             'only for first startup, if no calibration value is present
   watersensvalue(2)=512
End IF
 'pressuresensvalue=EEpressuresensvalue 'not needed right now
 'waitms10 'not needed right now
pressuresensvalue=512




'main program

Do
'read ADC
Analogueinput(1) = Getadc(0)
Analogueinput(2) = Getadc(1)
Analogueinput(3) = Getadc(2)
Analogueinput(4) = Getadc(3)

'Check if water present with optical sensor
If Analogueinput(1) <= Watersensvalue(1) Then               'ADC0
   Servo(1) = Smv + 500                                     '1ms bypass valve open
   Waterpresent(1) = 1
   Waterlowled=0
   Electricpump=0
   Tensecond = 0
   Stop Timer0
Endif
If Analogueinput(1) > watersensvalue(1) then
   Start Timer0
End if


'Check Feed Tank Level Sensor
If Analogueinput(4) <= Watersensvalue(2) Then               'ADC3
   WaterbypassLED=0
   waterpresent(2)=1
Endif
If Analogueinput(4) > Watersensvalue(2) Then
   Servo(1) = Smv + 500                                     '1ms pump bypass valve open, overwrites "bypass valve closed due to low lvl in boiler"!!!
   waterpresent(2)=0
   WaterbypassLED=1
End If

     'alternate programming with digital sensor voltages
     'If Tanklvlsens=0 then
     '   WaterbypassLED=0
     '   waterpresent(2)=1
     'Else
     '   Servo(1)=SMV+500   '1ms pump bypass valve open
     '   WaterbypassLED=1
     '   waterpresent(2)=0
     'End IF



'Boiler pressure regulation
'Read and calculate Pilot Flame Pot
Pilotflame = Analogueinput(3) / 10                          'ADC2, makes a maximum of 102 = 0,102 ms, by dividing it just by 5 it will reach 20° max

If Analogueinput(2) <= Pressuresensvalue Then               'ADC1
   Servo(2) = Smv + 500                                     '1ms
   GASHIGHLED=1
End If
If Analogueinput(2) > pressuresensvalue then
   Servo(2) = Smv - 500                                     '2ms
   Servo(2) = Servo(2) + Pilotflame                         'if servo is jittering use another variable to combine these calculations and give out only 1 value
   Gashighled = 0
End If


Loop
End


Servoirq:
If Kanal = 0 Then
   If Servo1 = 0 Then                                       'if port low
      Timer1 = Servo(1)                                     'timer1 on reload value
      Servo1 = 1                                            'port=1
   Else                                                     'happens at next interrupt
      Servo1 = 0                                            'port=0
      Incr Kanal                                            'next servo
   End If
End If
If Kanal = 1 Then
   If Servo2 = 0 Then
      Timer1 = Servo(2)
      Servo2 = 1
   Else
      Servo2 = 0
      Incr Kanal
   End If
End If

If Kanal = 2 Then
  Timer1 = 53536                                            'Pause for 16ms
  Kanal = 0
End If
Return

waterirq:
incr tensecond
Timer0=0
If Tensecond > 304 And Waterpresent(2) = 1 Then             '304 = 10s  = 304x32,8ms for 1 timer overflow
   waterpresent(1)=0
   Servo(1) = Smv - 500                                     '2ms bypass valve closed
   Waterlowled=1
   Electricpump=1
End If
Return

Calibration:

DO

INCR LEDtoggle
Analogueinput(1)=Getadc(0)
Analogueinput(2)=Getadc(1)
Analogueinput(3)=Getadc(2)
Analogueinput(4)=Getadc(3)


Pilotflame = Analogueinput(3)/10

Servo(1) = Smv + 500                                        ' 1ms
Servo(2) = Smv - 500                                        '2ms if servo is jittering use another variable to combine these calculations and give out only 1 value
Servo(2)= Servo(2)+Pilotflame

If Ledtoggle > 100 Then                                     'Toggle cycletime depending on time needed for ADC
   IF Calibstate=0 then
      Toggle WaterlowLED
      Toggle GASHIGHLED
      Toggle WaterbypassLED
   End if
   IF Calibstate=<2 then
      WaterlowLED=0
      Toggle GASHIGHLED
      Toggle WaterbypassLED
   End if
   IF Calibstate=<4 then
      WaterlowLED=0
      GASHIGHLED=0
      Toggle WaterbypassLED
   End if
   IF Calibstate=<5 then
      WaterlowLED=0
      GASHIGHLED=0
      WaterbypassLED=0
   End if
End if

'Calibration Boiler sensor
If Sensorcalibswitch=1 and Calibstate=0 then
   Watersensvalue(1) = Analogueinput(1)                     'Measures voltage without water when switch is switched on again
   Calibstate=1
End If
If Sensorcalibswitch=0 and Calibstate=1 then
   Watersensvalue(1) = Watersensvalue(1) + Analogueinput(1) 'Measures voltage with water when switch is switched off again
   Watersensvalue(1) = Watersensvalue(1) / 2
   Eewatersensvalue(1) = Watersensvalue(1)                  'writes average sensor value on EERAM
   Waitms 10
   Calibstate = 2
   Waterlowled = 0
End If

'Calibration feed tank sensor
If Sensorcalibswitch = 1 And Calibstate = 2 Then
   Watersensvalue(2) = Analogueinput(4)                     'Measures voltage without water when switch is switched on again
   Calibstate=3
End If
If Sensorcalibswitch = 0 And Calibstate = 3 Then
   Watersensvalue(2) = Watersensvalue(2) + Analogueinput(4) 'Measures voltage with water when switch is switched off again
   Watersensvalue(2) = Watersensvalue(2) / 2
   Eewatersensvalue(2) = Watersensvalue(2)                  'writes average sensor value on EERAM
   Waitms 10
   Calibstate = 4
   GASHIGHLED=0
End if

If Sensorcalibswitch=1 and Calibstate=2 then
   Calibstate=5
   WaterbypassLED=0
End if

Loop
End
Logged
regards

Tobias

afb

  • FORGE ELECTRONICS
  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 64
  • more wires than wood in my boats!
  • Location: New Forest
    • FORGE ELECTRONICS
Re: Help writing pic code
« Reply #21 on: August 07, 2014, 05:41:58 pm »

Timebandit - the PICAXE basic code is NOT compiled, it is interpreted - that's why it's slower - I recall it takes about 1mSec per (high level basic) instruction at 4Mhz. The more recent xxM2 chips can be configured to run at 32Mhz.

Megatron - looks like Timebandit is well on the way with his solution, you'd do well to ride on his back! 

Logged
in the land of the blind the one-eyed man is king

Time Bandit

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 175
  • Model Boat Mayhem is Great!
  • Location: Deizisau, Germany
Re: Help writing pic code
« Reply #22 on: August 07, 2014, 06:48:01 pm »

Okay that makes sense, 1ms would probably be still enough for that one here.
BASCOM outputs definetly machine code, thatīs probably the cause why itīs as fast as C-Code.

Logged
regards

Tobias

megatron

  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 70
  • Location: North Shields
Re: Help writing pic code
« Reply #23 on: August 08, 2014, 02:24:03 pm »

Hi Tobias
The feed tank sensor is just a probe so the output will be digital and it won't need calibrating.
Yes both level sensors work on the same servo.
You're right, if both levels low, bypass valve open and LED lit. No effect on gas valve, we still need steam to get boat back.
The gas valve is either fully open or nearly closed (adjusted by pilot flame pot)
The pilot flame is not set during calibration. It is adjusted with pot and continually monitored while program is running.
During calibration the gas servo should be in pilot flame position and the bypass valve in open position (electric pump off)
The calibration switch will be a push button mounted on circuit board either NC or NO.
The pressure sensor does not need calibrating. It will output exactly half supply voltage when pressure correct. If signal greater than half Vs then gas valve open. If signal less than half Vs then gas valve closed to pilot flame. Does Attiny have a voltage comparator on board? If not how sensitive are the inputs for differentiating between a 1 and a 0, this needs to be very tight.
There's no need to switch off after calibration, just go straight to main program.
Control unit and servos will be powered from same battery, I may add a voltage regulator to power control unit.
What size cap would you recommend?

Hello afb
welcome to the discussion.
When I opened this topic I had 2 goals in mind.
Firstly to build a small boiler control unit, and secondly to try and learn how to program pics.
I really appreciate Time Bandit writing the Attiny code and I will probably use it.  I would also like some help trying to convert it to either pic or picaxe code as I think i will learn a lot from doing it.
Any help doing this I would also appreciate very much.

regards
Stew
Logged

afb

  • FORGE ELECTRONICS
  • Full Mayhemer
  • *****
  • Offline Offline
  • Posts: 64
  • more wires than wood in my boats!
  • Location: New Forest
    • FORGE ELECTRONICS
Re: Help writing pic code
« Reply #24 on: August 08, 2014, 02:33:16 pm »

Hi Stew

I've sent you a PM giving my contacrt details for if and when you need help

Alan Bond
Logged
in the land of the blind the one-eyed man is king
Pages: [1] 2 3 4 5 6 7   Go Up
 

Page created in 0.108 seconds with 22 queries.