PDA

View Full Version : 16F1826 cannot get MCLR to act as an INPUT



SOTASOTA
- 10th February 2014, 20:00
Here is my initialization code for 16F1826 . I want RA5 to act as an INPUT. However, when I check PortA.5 no matter if it is HI or LO, it does not seem to function as an INPUT.
I have a 100k ohm resistor to +5VDC from pin4. I simply short the pin to GND to simulate the port as LO.


#CONFIG
__config _CONFIG1, _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _STVREN_OFF
#ENDCONFIG

'_MCLRE_OFF = MCLR pin functions as INPUT RA5, MCLR internally tied to VDD

DEFINE OSC 4
DEFINE ADC_BITS 10 ' 10 bit A/D Conversion
DEFINE ADC_CLOCK 4 ' 4MHz Clock
DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time

TRISA = %00101100 ' Set RA2, RA3 and RA5 as Inputs
TRISB = %00000000 ' All PortB as Output

'ADC Setup
ADCON1 =%11000000 ' Right justified
ANSELA =%00000010 ' AN2 on PortA.2 Analog
ANSELB =%00000000 ' Digital inputs

adval var word

mainloop:
if PortA.5= 1 then PortB.1 = 1 'turn ON an LED
if PortA.5= 0 then PortB.1= 0 'turn OFF an LED
goto mainloop


I do not get it. Help! Thanks!

AvionicsMaster1
- 10th February 2014, 22:50
I assuming what you mean by doesn't work is that the LED doesn't blink or stay on steady. If so, you might want to try some pauses in your mainloop. Such as

mainloop:
if PortA.5= 1 then PortB.1 = 1 'turn ON an LED
Pause 500
if PortA.5= 0 then PortB.1= 0 'turn OFF an LED
pause 500
goto mainloop

You may also have too big of a resistor in series with your LED. If you're using 5VDC supply to a 2.2VDC LED with a max current of 20mA then you'll need 140ish ohm resistor in series with it. Check LED orientation and connection especially if using a proto board.

But as important to all of that I think you need to set your oscillator config. If I RFDS correctly your config of FOSC_HS needs an external oscillator. For an internal oscillator it probably should read FOSC_INTOSC. That is a guess but I also guess you need to set the OSCCON to your desired oscillator speed.

As always, I hope it helps.

SOTASOTA
- 11th February 2014, 01:23
The LED is working if I set PortB.1=1 manually in code.
I have an external 4MHz crystal.

DEFINE OSC 4 should set the frequency. All other code commands work fine like the ADCIN.

If I short out PortA.5 and hold it LOW, then the code should read a LOW on the port and turn on the LED.

Mind boggling.

Aussie Barry
- 11th February 2014, 06:45
I am not sure if this is the cause of your problem but 100k for a pull up resistor seems a bit too high.
Try using the weak pull up associated with RA5 instead of the external pull up resistor - see page 123 of the datasheet for details.

Cheers
Barry
VK2XBP

Ioannis
- 11th February 2014, 13:30
Maybe stupid question, but are counting the pins correctly? Does seem strange... Also try 1K resistor, or directly Vdd/Vss connection.

Are you programming the chip with ICSP or do you move the chip to a programmer and the move it back to your test board?

Ioannis

aratti
- 11th February 2014, 14:28
mainloop:
Pause 10
if PortA.5= 1 then PortB.1 = 1 'turn ON an LED
if PortA.5= 0 then PortB.1= 0 'turn OFF an LED
goto mainloop



Reduce the value of your pullup resistor from 100k to 10k and add a delay in the loop as per the above example.

Then it should work as you expected.

Cheers

Al.

LinkMTech
- 11th February 2014, 14:56
I think a write to the latch register might do it:



mainloop:
Pause 10
if PortA.5= 1 then LATB.1 = 1 'turn ON an LED
if PortA.5= 0 then LATB.1= 0 'turn OFF an LED
goto mainloop

SOTASOTA
- 11th February 2014, 22:59
SOLVED!! I had a bad LED. All is working now. Sorry for the confusion.

aratti
- 12th February 2014, 02:55
In post # 3 you wrote:


The LED is working if I set PortB.1=1 manually in code...........


Which is in contradiction with your last post.

Regards

Al.

SOTASOTA
- 13th February 2014, 02:31
In post # 3 you wrote:


Which is in contradiction with your last post.

Regards

Al.

A poor solder joint that worked "sometimes". So, I replaced the LED thinking it was it initially. The re and re fixed the problem. I should have said a bad solder connection to the LED. But I only found this out after replacing the LED.