Greetings from Newbie and a question


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2006
    Posts
    36

    Default Greetings from Newbie and a question

    Hello forum members, I recently discovered using a PIC in place of discreet electronic components and purchased a MikroElektronica prototyping board. It's totally awesome hardware and worth every penny of it's price, although the Basic programming environment is a bit sparse compared to PicBasic Pro. So I downloaded the demo version and am trying it out.

    I have created a program to accomplish some process control that I need and am posting it below. Please keep in mind that I am REALLY a newbie - pretty much my first programming since high school on an Apple2e (showing my age) so be gentle please.
    There are lot's of comments so it's pretty self explanatory as to what I'm trying to do. I'm using "Virtual Breadboard" (which I also think is terrific) to test my results. So far, this works "OK" but I would like the air pump to stay on without turning off until the switch is opened again. My test environment shows me that when the "PulseOut" is happening AND the air pump switch is on, it toggles the air pump relay on and off. I didn't expect that.

    Could someone more experienced look at it and tell me how I could do it better or more efficiently (without learning assembly?)
    Thanks to all who take the time:
    '************************************************* ***************
    '* Name : WaterInjectioncontrol.BAS *
    '* Author : [Chris Helvey] *
    '* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 7/20/2006 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************
    ' Author: Chris Helvey
    ' Date: 07/19/06
    ' Water Injection Sensor Program used to check the presence of a signal from
    ' the various sensors. Turn the system on after a delay of detecting
    ' vacuum. Check pressure switch and the water level float switch. When the
    ' pressure is too low, it will turn on a relay to the air pump and when
    ' the water is low 'will turn on a LED/Buzzer as a reminder (or a relay for a
    ' pump.).

    'Switches are a +5V HIGH on the input
    'Inputs (on Port B) are as follows:
    'Pin4 = Pressure switch
    'Pin5 = Water Level Float Switch
    'Pin6 = Vacuum switch
    'Pin7 = Temperature switch (preset value on the switch)

    ' Outputs (on Port C) are as follows:
    'Pin0 = Air Pump relay
    'Pin1 = "Water Low" LED or water pump relay
    'Pin2 = Injector Power relay (not currently used)
    'Pin3 = Injector "open" pulse

    TRISB = %11111111 ' Set all PORTB to inputs
    TRISC = %00000000 ' Set all PortC to outputs

    AllOff:
    PortC = 0 ' Turn off all outputs

    'Initial Air Pressure check to get pump going 1st if needed

    while PortB.4 = 1 'Turn air pump on while pressure switch hi
    PortC.0 = 1 'Keep it on until pressure is high enough
    wend
    PortC.0 = 0 'Turn off air pump now that it is ready

    InitialOn: 'Wait for vacuum and temp to come up
    'and blink LED while waiting '
    'LED pulse
    Repeat
    PORTC.1 = 1 'Pulse the LED while in this loop
    PAUSEus 500
    PORTC.1 = 0
    PAUSEus 500
    Until PortB.6 = 1 and PortB.7 = 1 'Check Vacuum and Temp
    'Turn on injector power only if Vacuum
    'is detected and temp is High enough
    'Loop until ready

    pauseus 1000 'Pause to be sure engine running

    SensorCheck:
    'Check Air Pressure
    If PortB.4 = 1 then portc.0=1
    ' else portc.0=0 "Else" doesn't work added to above line or here.
    If PortB.4 = 0 Then portc.0=0
    'CheckWater Level
    If PortB.5 = 1 Then portc.1=1
    If PortB.5 = 0 Then portc.1=0
    'Send pulse to injector while there is vacuum present.
    If PortB.6 = 1 Then PULSOUT PORTC.3,50
    'Check Temperature and restart if too low (wait for temp to come up)
    If portB.7 = 0 then AllOff:
    Goto SensorCheck: 'Loop the sensor check unless temp goes low
    End

  2. #2
    yourmomOS's Avatar
    yourmomOS Guest


    Did you find this post helpful? Yes | No

    Default Pic model??

    What type of chip are you using?

  3. #3
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Default Pretty good

    Hi and welcome,

    that's pretty good code for a newbie but can I make a couple of suggestions to make life easier for yourself (not that I'm a pro by a long shot!):

    1). At the start of your program set up some variables so that you don't need to keep referring to which pin does what. By this I mean:

    'INPUTS
    Pressure_sw var portb.4 'Pressure_sw could be anything really so it could be smaller, say prsuresw instead or whatever you want that's easy to remember!
    Wlevel_sw var portb.5
    vaccum_sw var portb.6
    'etc,etc

    'OUTPUTS
    Airpump_relay var portc.0
    'etc,etc

    now in your program you can write:

    while Pressure_sw = 1 'Turn air pump on while pressure switch hi
    Airpump_relay = 1 'Keep it on until pressure is high enough
    wend
    Airpump_relay = 0 'Turn off air pump now that it is ready

    This is just a suggestion but it makes things a lot easier to read (especially when you come back to a program several months later!)

    2). Using the 'else' command I noticed you had problems so I thought I would re-write the code you posted with the proposed variables added and also showing how to use 'else'. You need to add a new line for the else and then add an endif after your instruction(s) also with their own lines:

    Code:
    'Switches are a +5V HIGH on the input
    'Inputs (on Port B) are as follows:
    'Pin4 = Pressure switch
    'Pin5 = Water Level Float Switch
    'Pin6 = Vacuum switch
    'Pin7 = Temperature switch (preset value on the switch)
    
    ' Outputs (on Port C) are as follows:
    'Pin0 = Air Pump relay
    'Pin1 = "Water Low" LED or water pump relay
    'Pin2 = Injector Power relay (not currently used)
    'Pin3 = Injector "open" pulse
    
    TRISB = %11111111 ' Set all PORTB to inputs
    TRISC = %00000000 ' Set all PortC to outputs
    
    AllOff:
    PortC = 0 ' Turn off all outputs
    
    'Initial Air Pressure check to get pump going 1st if needed
    
    while Pressure_sw = 1 'Turn air pump on while pressure switch hi
    Airpump_relay = 1 'Keep it on until pressure is high enough
    wend
    Airpump_relay = 0 'Turn off air pump now that it is ready
    
    InitialOn: 'Wait for vacuum and temp to come up
    'and blink LED while waiting '
    'LED pulse
    Repeat
    WATER_LOW = 1 'Pulse the LED while in this loop
    PAUSEus 500
    WATER_LOW = 0
    PAUSEus 500 
    Until Vac_sw = 1 and Temp_sw = 1 'Check Vacuum and Temp 
    'Turn on injector power only if Vacuum
    'is detected and temp is High enough
    'Loop until ready 
    
    pauseus 1000 'Pause to be sure engine running
    
    SensorCheck:
    'Check Air Pressure 
    If Pressure_sw = 1 then 
    Airpump_relay=1 
    else
    Airpump_relay=0
    endif
    'CheckWater Level
    If Wlevel_sw = 1 Then 
    Water_low=1
    else
    Water_low=0
    endif
    'Send pulse to injector while there is vacuum present.
    If Vac_sw = 1 Then PULSOUT Injector,50 
    'Check Temperature and restart if too low (wait for temp to come up) 
    If Temp_sw = 0 then AllOff: 
    Goto SensorCheck: 'Loop the sensor check unless temp goes low
    End
    I can't see where your problem is with the pump turning off but you could try commenting out this line:
    If portB.7 = 0 then AllOff
    or if you use the code I posted above, then it would be this line:
    If Temp_sw = 0 then AllOff

    I could be barking up the wrong tree but from a quick glance at your code I'm thinking the temperature switch might not be high?

    I hope this helps and your project sounds quite interesting

    Regards

    Rob
    Last edited by Rob; - 22nd July 2006 at 10:41.

  4. #4
    Join Date
    Jul 2006
    Posts
    36


    Did you find this post helpful? Yes | No

    Smile

    Thanks for the suggestions. I WILL be using them. Well, it seems my free "simulator" slows things down quite a bit, or is just broken. That was why I was getting results that were unpredictable. I decided to load it into a real pic and try it and it works exactly as expected with a few glitches I'm still figuring out (Trial by fire.) :-) It's coming along.
    I am using a 16f877a that came with the MikroElektronica board.
    I intend to get it working and then buy a plain pic (or maybe even a smaller one,) build it in with the supporting circuitry in a box, and forget about it. Additional sensors will be easy if needed in the future and tweaks will all be in the software instead of messing around with hardware. I LOVE IT!
    I can tell this is going to be my new favorite obsession...

    I'm sure I'll have a few more questions as I go, so I'll keep you posted. Thanks

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Rob
    I can't remember how to add code the 'proper' way with its own scrolling box.
    Rob, use the term code between square brackets (remove the space between the [ and C of code)

    Code:
    [ code ] [ /code ]
    Last edited by malc-c; - 22nd July 2006 at 10:26.

  6. #6
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Talking Forgetful

    Hi Malc-c,

    cheers for the reminder of "code", "close code"! If I had a brain that was big enough to support both my short term and long term memory I would probably be able to ......... no it's gone!

    Chris, I presume this project is for a turbo-charged car?! Let me guess, something old skool, RS Turbo? You don't have to say, just interested!

    Cheers

    Rob
    Last edited by Rob; - 22nd July 2006 at 10:42.

  7. #7
    Join Date
    Jul 2006
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    On demand Hydrogen generation from water as a catalyst/fuel in the combustion process. Water cools the flame instead of liquid gasoline, which is a terrible waste in all internal combustion engines today.
    (The things they DON'T tell you in car 101. :-) )

    I have the circuit working perfectly (with clearly labeled variables now.) I can't imagine how much time/pulled hair this saves even WITH the learning curve of using a PIC/Basic.

    I'll be using another, separate one dedicated to creating a custom square wave (PWM) needed for another part of the design.

    Chris

  8. #8
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ChrisHelvey
    On demand Hydrogen generation from water as a catalyst/fuel in the combustion process. Water cools the flame instead of liquid gasoline, which is a terrible waste in all internal combustion engines today.
    (The things they DON'T tell you in car 101. :-) )
    That sounds VERY interesting - best of luck with it!

    I have the circuit working perfectly (with clearly labeled variables now.) I can't imagine how much time/pulled hair this saves even WITH the learning curve of using a PIC/Basic.
    - Glad variables are helping you!

    I'll be using another, separate one dedicated to creating a custom square wave (PWM) needed for another part of the design.
    Chris, a little help with your project... you may already know this but anyways... the PIC16F877A also has Hardware PWM which is fantastic! You can set it and implement other code at the same time without worrying about its output frequency. You can easily change its frequency and stop the output alltogether. Check it out before using the PWM command.

    Good luck

    Rob

  9. #9
    Join Date
    Jul 2006
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    I did NOT know that. That's fantastic. I don't use the PWM command to access it?

    I suppose I should look at the datasheet, but if you feel inclined to point me the right direction, it's appreciated.

    I was actually able to make a very nice square wave with an adjustable period/duty cycle by using pin on, pin off, and pauses. I won't need to make any duty cycle changes on-the-fly, but I may want to be able to turn it on and off at will.

    Thanks.

  10. #10
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    94


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ChrisHelvey
    I suppose I should look at the datasheet, but if you feel inclined to point me the right direction, it's appreciated.
    This is a small portion of code that I wrote recently and may help you although it was written for a 16F628A:
    Code:
    INTCON = %11000000     ' These 2 lines set the Timer 1 to be
    PIE1 = %00000001         ' utilised by enabling Global and Peripheral interrupts
    
    T2CON = %00000111           ' Timer 2 is needed by HPWM command
    
    CCP1CON = %00001100        ' This is needed also for HPWM command
    
    ....................
    
        HPWM 1, 127, 0          ' Make sure the jets are off
    
    pause 1000
    
    frequency = 2510                ' Set frequency at start -15Hz for Hotspring. - FREQUENCY WAS A VARIABLE I ASSIGNED!
    
    hpwm 1, 127, frequency        ' Output start frequency of 2510Hz
    The best thing to do is to read the datasheet for that particular PIC though as you quite rightly said. Also refer to the Pic Basic Pro manual - extremely helpful!!

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts