Loop Problem please help


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30

    Default Loop Problem please help

    I am having problems with this code. I am sure someone much smarter than I am will figure this out quickly. I am using a reed relay as an indicator of PortB.6 = 1. Because of the loop the reed relay is buzzing because it is switching on and off. Obviously my code is not correct. I need the relay to be latched on. Can someone point me in the right direction. I have spent more than 36 hrs and now I need the forums help.

    ' 10-bit A/D conversion
    ' Connect analog input to channel-0 (RA0)
    DEFINE LCD_DREG PORTB 'LCD data port
    DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4
    DEFINE LCD_RSREG PORTB 'LCD register select port
    DEFINE LCD_RSBIT 4 'LCD register select bit
    DEFINE LCD_EREG PORTB 'LCD enable port
    DEFINE LCD_EBIT 5 'LCD enable bit
    DEFINE LCD_BITS 4 'LCD bus size 4 or 8
    DEFINE LCD_LINES 1 'Number lines on LCD
    'DEFINE LCD_COMMANDUS 2000 'Command delay time in us
    'DEFINE LCD_DATAUS 50 'Data delay time in us

    adval var word 'Create adval to store result

    TRISA = %11111111 ' Set PORTA to all input
    TRISB = %00000000 ' Set PORTB to all output
    ADCON1.7=1 ' RIGHT justify result
    ADCON0 = %11000001 ' Configure and turn on A/D Module
    Pause 500 ' Wait .5 second

    ' Define ADCIN parameters

    DEFINE ADC_BITS 10 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 10 ' Set sampling time in microseconds


    loop: ADCON0.2 = 1 'Start Conversion

    notdone: pause 5
    if ADCON0.2 = 1 Then notdone 'wait for low on bit-2 of ADCON0, conversion finished

    adval.highbyte = ADRESH 'move HIGH byte of result to adval
    adval.lowbyte = ADRESL 'move LOW byte of result to adval

    Lcdout $fe, 1 'Clear screen
    Lcdout "ADC Value: ", DEC adval 'Display the decimal value

    if adval >500 and adval<600 then
    PortB.6 = 1
    endif
    Pause 100 'Wait .1 second
    Goto loop 'Do it forever

    End

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    I am not sure about your code, but if you are driving your relay with a transisitor, use 100uF capacitor on the base of the transistor (before the resistor). This is to avoid "buzzing" as you say.

    <a href="http://img7.picsplace.to/img7/24/TR_Drv.GIF"><img src="http://img7.picsplace.to/img7/24/thumbs/TR_Drv.GIF" alt="Image Hosting by PicsPlace.to" ></a>
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Default

    How are you constructing the hardware ? I was experiencing strange behaviour of the hardware in a recent project, and it wasn't until someone pointed out that I had too higher value resistor which limited the current the PIC could draw and thus kept causing the PIC to reset that the matter was resolved. As Sayzer pointed out, using a simple driver to power the relay might resolve your problem. If not post the full schematic as well and hopefully you might get further advice.

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Default

    Hi,Billy

    Driving ' strange' loads ( capacitive or inductive ) work much better with HIGH statement ...

    it's to try

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    I would say there is some form of hardware fault.

    From your code, I dont see how the relay could buzz as you only have code to turn it on if Adval is between 500 and 600. There is nothing in the code that I can se that will turn it OFF eg PortB.6 = 0.

    The buzzing could be due to the PIC resetting. Connect an LED to an unused pin and light it for 1 second then turn it off in the initialisation code. If you see the LED flashing or aapparently staying on then for some reason your PIC is resetting.

    From the code you have posted, the first time ADVAL is 500-600 the relay will operate and then stay that way forever.

    I notice that you update the LCD inside the loop and that LCD Enable is on B5.
    This pin will be going high and low as the LCD is updated.
    Do you have a short between B5 and B6 ?
    Keith

    www.diyha.co.uk
    www.kat5.tv

  6. #6
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30


    Did you find this post helpful? Yes | No

    Default Loop Problem

    Thanks to all who responded. I have dug deeper and found that replacing the 16F88 with 16F84 or 16F628 works just fine. Not the ADC portion, just turning PORTB.6 on. With the 16F88 the LED would pulse. What is strange is that selecting the 16F88 in the device selection menu in MicroCode Studio Plus and then compile would give strange errors. To fix the errors I had to change the directory. I stripped the code to bare minimum because the issue was with holding the port pin on solid. As you can see, the code is very simple. With code below, the 84 and the 628 works as expected. I know the 88 is fine as I tried three. The problem could be with MicroCode Studio.

    loop:Lcdout $fe, 1 'Clear screen
    Lcdout "Testing 16F88"
    Pause 100 'Wait

    HIGH PORTB.6

    Goto loop ' Do it forever

    END

  7. #7
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default Disable the analogue inputs

    ANSEL = %00000000 ' Disable Analogue Inputs

    I had the same problem the first time I used a 16F88

    Oops, just realise that you NEED analogue inputs but disable the ones that are on the Port B pins that you are using

    Use ANSEL=%00000001 ' Enable AN0 only

    By default the analgue inputs are enabled. To update your LCD display the PortB data register is read, modified and written back to. Because B6 (your relay pin) by default is AN5, this is read as a ZERO rather than the ONE you set it to in your code. This means that when you initially set it it works OK but as soon as the LCD is updated again it gets rewritten as ZERO.

    HTH
    Last edited by keithdoxey; - 15th July 2006 at 13:39.
    Keith

    www.diyha.co.uk
    www.kat5.tv

  8. #8
    Join Date
    Nov 2005
    Location
    Ontario Canada
    Posts
    30


    Did you find this post helpful? Yes | No

    Default Loop Problem

    Thank you. I will try that ansel in a few minutes and get back to post the results. Thank you very much on your observation about the LCD. I noticed you mentioned that before. I have always wondered why in all the examples I have seen in the DEFINES, the ports were not all the same. I could not find any explanation as to why that was. Thank you for being persistent in pointing that out and offering the explanation.

Similar Threads

  1. Controlsystem for compact sporting (clay shooting)
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th July 2009, 16:48
  2. Using input from Switch to control a loop
    By MrRoboto in forum mel PIC BASIC
    Replies: 9
    Last Post: - 2nd February 2009, 05:02
  3. Serin to Serin2 ??
    By Gixxer in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th January 2008, 03:56
  4. Serial Relays
    By tazntex in forum General
    Replies: 3
    Last Post: - 17th May 2007, 17:42
  5. Newbie question:
    By Izone in forum mel PIC BASIC
    Replies: 2
    Last Post: - 26th February 2006, 16:24

Members who have read this thread : 1

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