Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with the PIC16F627A, cant get ports to act right

Status
Not open for further replies.

FusionITR

Member
I'm trying to get all of the pins on my 627A to just have all digital inputs and outputs, and I cant seem to achieve that after pouring through the datasheet all night.

I have a basic program test program to test the output of the pins and weird stuff happens, the problem is with PORTA.

I have this test program:

Code:
	LIST P=16F627A			; Define PIC
	include "p16f627a.inc"	; include the defaults for the chip
	errorlevel -302			; Suppress bank warning
	__config 3F43h			; WDT Off, EC Oscillator, CP off

;****************************************************************************
;*  Set Up Varibles                                                         *
;****************************************************************************

COUNT1	equ	0x20
COUNT2	equ 0x21
WTEMP	equ 0x26

;****************************************************************************
;*  Set Up Ports                                                            *
;****************************************************************************

	bsf STATUS,5
	movlw 0x00
	movwf TRISA

	movlw 0x00
	movwf TRISB

	bcf STATUS,5

;****************************************************************************
;*  Set Initial Conditions                                                  *
;****************************************************************************

	movlw 0x07
	movwf CMCON
	clrf INTCON
	clrf RCSTA
	clrf T1CON
	movlw b'1100'
	movwf PORTA
	clrf PORTB

;****************************************************************************
;*  Main Program                                                            *
;****************************************************************************

Main
	movlw 0xFF
	movwf PORTB
	movlw 0x08
	xorwf PORTA
	call LongDelay
	clrf PORTB
	xorwf PORTA
	call LongDelay
	goto Main

;****************************************************************************
;*  LongDelay ~200ms                                                        *
;****************************************************************************

LongDelay
	movwf WTEMP
	movlw 0xFF
	movwf COUNT1
	movlw 0xFF
	movwf COUNT2
	movf WTEMP,W
LongDelayLoop
	decfsz COUNT1,1
	goto LongDelayLoop
	decfsz COUNT2,1
	goto LongDelayLoop
	return

;****************************************************************************
;*  End Program                                                             *
;****************************************************************************

	end

Which flashes PORTB (which works), but PORTA acts strange. First of all, port A is suppose to look flash like this

1100
0100

But port A looks like this

1000

but doesnt flash. I know the pic is doing *something* with port A because when I hook up a multimeter to port A, i dont get a constant voltage, it fluxuates from 0 to 3+ volts or so (cant measure accurately, I have a cheap radioshack multimeter).

However, when I comment out the flashing of porta like so:

Code:
;****************************************************************************
;*  Main Program                                                            *
;****************************************************************************

Main
	movlw 0xFF
	movwf PORTB
;	movlw 0x08               <==Commented Out
;	xorwf PORTA               <==Commented Out
	call LongDelay
	clrf PORTB
;	xorwf PORTA               <==Commented Out
	call LongDelay
	goto Main

The code works like it should and I get the flashing of Port B and Port A staying a constant 1100.

What am I doing wrong here? Im guessing its some sort of configuration problem since it simulates like it should in MPLAB. I thought I turned off all of the comparators and stuff but am I missing something here?
 
Have a read of my tutorials, the first of which do pretty well what you are trying to do!.

BTW - your bank switching lines "bsf STATUS,5" would make far more sense is you used the MicroChip includes rather than '5' - I've no idea if that's even the correct bit?.

You should also consult the datasheet, not all pins can be outputs, and one is open-collector.
 
Nigel Goodwin said:
Have a read of my tutorials, the first of which do pretty well what you are trying to do!.

BTW - your bank switching lines "bsf STATUS,5" would make far more sense is you used the MicroChip includes rather than '5' - I've no idea if that's even the correct bit?.

You should also consult the datasheet, not all pins can be outputs, and one is open-collector.

Ok I've read though some of your tutorials and they still dont explain what is happening with my program and why the pic doesnt behave like it simulates in MPLAB. Heres my new program (taken and modified from your example).

Code:
	LIST P=16F627A			; Define PIC
	include "P16F627A.INC"	; include the defaults for the chip
	errorlevel -302			; Suppress bank warning
	__config 3F43h			; WDT Off, EC Oscillator, CP off

;****************************************************************************
;*  Set Up Varibles                                                         *
;****************************************************************************

	cblock 	0x20 			; General Purpose Registers
		COUNT1				; Counter
		COUNT2 				; Counter
		WTEMP				; Temp W Register Placement
	endc
	
	org	0x0000				; org sets the origin, 0x0000 for the 16F628,
							; this is where the program starts running	

;****************************************************************************
;*  Set Up Ports                                                            *
;****************************************************************************

	movlw	0x07
	movwf	CMCON			; Turn Comparators Off
   	bsf 	STATUS,RP0		;select bank 1
   	movlw 	0x00			;set PortB all outputs
   	movwf 	TRISB
	movwf	TRISA			;set PortA all outputs
	bcf		STATUS,RP0		;select bank 0

;****************************************************************************
;*  Set Initial Conditions                                                  *
;****************************************************************************

	movlw b'1100'
	movwf PORTA

;****************************************************************************
;*  Main Program                                                            *
;****************************************************************************

Main	
	movlw	0xff
	bsf PORTA,3
	movwf	PORTB
	nop
	nop
	call	Delay
	movlw	0x00
	bcf PORTA,3
	movwf	PORTB
	call	Delay
	goto	Main

;****************************************************************************
;*  LongDelay 0.772ms                                                       *
;****************************************************************************

Delay
	movwf WTEMP
	movlw 0x25
	movwf COUNT1
	movlw 0x25
	movwf COUNT1
	movf WTEMP,w
DelayLoop
	decfsz COUNT1,1
	goto DelayLoop
	decfsz COUNT2,1
	goto DelayLoop
	return

;****************************************************************************
;*  End Program                                                             *
;****************************************************************************

	end

Port A should flash like this:

1100
0100

but instead, it flashes like this:

1000
0000

As in, RA2 is never on, even though based on my program and simulation its always high.

Also, what exactly is the deal with RA4 and RA5? I have an LED hooked up to them and they never get enough voltage to turn on the diode but all other pins can. What type of pins are these versus the other pins on the pic?
 
Also, what exactly is the deal with RA4 and RA5? I have an LED hooked up to them and they never get enough voltage to turn on the diode but all other pins can. What type of pins are these versus the other pins on the pic?

AFAIK, RA5 is an input only pin. You can't drive an led from it.

RA4 is an open drain pin. You drive an led by connecting the cathode to it and pulling up the anode via a limiting resistor.

I suspect you are driving the leds by connecting the led anode to the I/O pin and grounding the cathode, reverse to above. I hope you have a current limiting resistor in series with the led.

As in, RA2 is never on, even though based on my program and simulation its always high.

One thing that the simulator does not take into account is when the I/O pin itself is loaded so that it is forced low even if the output is set high. The instruction "bsf PORTA,3" is read-modify-write intruction. That is, the whole port is read, modified by setting bit3 high then written back to the port. If RA2 is forced low by the load, it will read low and then written back low. Therefore, the state of the RA2 pin will be changed in the real world, by the "bsf PORTA,3" instruction.
 
motion said:
One thing that the simulator does not take into account is when the I/O pin itself is loaded so that it is forced low even if the output is set high. The instruction "bsf PORTA,3" is read-modify-write intruction. That is, the whole port is read, modified by setting bit3 high then written back to the port. If RA2 is forced low by the load, it will read low and then written back low. Therefore, the state of the RA2 pin will be changed in the real world, by the "bsf PORTA,3" instruction.

I'm sorry but I dont really understand, what do you mean "if RA2 is forced low by the load"? When you do a bsf PORTA,3 why exactly is it read low and written by low? The way I see it, it should just read the port, and write the port again with RA3 high this time.

Am I missing something in your explination?
 
FusionITR said:
I'm sorry but I dont really understand, what do you mean "if RA2 is forced low by the load"? When you do a bsf PORTA,3 why exactly is it read low and written by low? The way I see it, it should just read the port, and write the port again with RA3 high this time.

Am I missing something in your explination?

You intialized the port by writing b'1100' to it. However, let us say you shorted RA2 to ground. When the PIC16F627A executes the "bsf PORTA,3" instruction, the vaue it reads from the porta is b'1000" even if the value stored in its latches is b'1100'. After executing the instruction, the resulting value of b'1000' is written back to porta. Thus the port will now toggle from b'1000' and b'0000' as you have described.

You don't really have to short the pin to ground to get a low reading. A sufficiently low impedance load like an LED to grond might be enough.
 
motion said:
FusionITR said:
I'm sorry but I dont really understand, what do you mean "if RA2 is forced low by the load"? When you do a bsf PORTA,3 why exactly is it read low and written by low? The way I see it, it should just read the port, and write the port again with RA3 high this time.

Am I missing something in your explination?

You intialized the port by writing b'1100' to it. However, let us say you shorted RA2 to ground. When the PIC16F627A executes the "bsf PORTA,3" instruction, the vaue it reads from the porta is b'1000" even if the value stored in its latches is b'1100'. After executing the instruction, the resulting value of b'1000' is written back to porta. Thus the port will now toggle from b'1000' and b'0000' as you have described.

You don't really have to short the pin to ground to get a low reading. A sufficiently low impedance load like an LED to grond might be enough.

Wow thanks man, this is the explanation I needed, this probably explained other problems I had too. This little tidbit about how the ports works probably would of left me scraching my head alot in the future.

Now I have everything working the way its suppose to, and will change my design considerations based on this.

Thanks again!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top