PDA

View Full Version : Loop Problem please help



wildbilly
- 13th July 2006, 07:36
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

sayzer
- 13th July 2006, 09:28
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>

malc-c
- 13th July 2006, 10:11
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.

Acetronics2
- 13th July 2006, 10:43
Hi,Billy

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

it's to try

Alain

keithdoxey
- 13th July 2006, 10:51
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 ?

wildbilly
- 15th July 2006, 06:58
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

keithdoxey
- 15th July 2006, 11:34
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

wildbilly
- 15th July 2006, 14:42
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.