Menu

problem with a/d use

Help
2009-04-24
2013-05-30
  • Nobody/Anonymous

    using:
    #chip 12F683, 8 'mhz
    #config OSC=INTOSC, MCLRE=off, WDT=off

    dir gpio.0 in 'a/d
    dir gpio.2 in 'nimh
    dir gpio.3 in 'start
    dir gpio.4 out 'relay
    dir gpio.5 out 'led
    dim volt1 as byte
    dim volt2 as byte

    start:
    if gpio.3 on then
    set gpio.4 on 'relay
    set gpio.5 on 'led
    wait 1 s
    if gpio.2 on then nimh
    if gpio.2 off  then lipo
    end if
    wait 255 ms
    wait 255 ms
    set gpio.5 on 'led
    wait 255 ms
    wait 255 ms
    set gpio.5 off 'led
    goto start

    sub lipo
    do
    if readad10(an0) >= 156 then
    set gpio.4 off   'relay
    exit sub
    end if
    loop
    end sub

    sub nimh
    volt1 = 0
    do
    volt2 = readad10(an0)
    if volt2 >= volt1 then
    volt1 = volt2
    end if
    volt2 = readad10(an0)
    if volt2 < volt1 -1 then
    set gpio.4 off 'relay
    exit sub
    end if
    loop
    end sub

    when in the sub lipo it works great
    but when in the sub nimh it is exiting the sub when the a/d reaches 156 just like in the sub lipo, and I don't know why?

     
    • Nobody/Anonymous

      You are using byte variables to 10 bit ADC... use 8 bit ADC:

      volt2 = readad(an0)

       
    • Nobody/Anonymous

      i fixed that it is "dim volt1 as word" now, but not the problem

       
    • Nobody/Anonymous

      Ok... i suposse you declared both volt1 and volt2 as word.

      I think this is a battery charger and you want to detect when voltage start to go down for nimh battery.

      I think you sould have a delay inside nimh sub to take readings every 1seg for example and filter this readings to avoid reading errors.

      And i think you should have an infinite loop to stay when charging is complete... i think in your actual code when you exit from nimh sub you go to start again... if gpio2 is off then you will go to lipo sub... not sure but may be this.

      Perhaps you need a blinking led sub (for example) to go when charging is complete.

      Regards.

       
    • Nobody/Anonymous

      i did this:
      sub nimh
      volt1 = 0
      do until gpio.4 off
      volt2 = readad10(an0)
      wait 1 s
      volt2 = volt2 + readad10(an0)
      volt2 = volt2 / 2
      if volt2 >= volt1 then
      volt1 = volt2
      end if
      volt2 = readad10(an0)
      wait 1 s
      volt2 = volt2 + readad10(an0)
      volt2 = volt2 / 2
      if volt2 < volt1 -1 then
      set gpio.4 off 'relay
      exit sub
      end if
      loop
      end sub

      but when it exits the sub it goes to start but the start switch is off so it just flashes the led to say it is done, so I do not know why it exits the nimh sub at the ad value of 156?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.