PDA

View Full Version : 16f876 PortA set-up trouble?



ronjodu
- 18th February 2006, 03:29
I am trying to read 2 Microchip TC74 i2c temp sensors (both different addresses, TC74A0 and TC74A5) on portA and send the result to a serial LCD for display. For some reason I am having trouble. Here are a few snippetts from my code. I have made this work on portB in the past. That's why I think I have a problem with portA setup. I get erratic numbers anywhere from 289 to 12500. It's probably something I'm overlooking.
Let me know if you need more info.

DEFINE LOADER_USED 1 'Only required if bootloader used to program
Define OSC 4

INCLUDE "modedefs.bas"
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

OPTION_REG.7 = 0
ADCON1 = 7 ' PORTA digital

PORTA = %00000000 'Set all INPUTS to 0
TRISA = %00111011 'Set PORTA RA0-RA5
PORTB = %00000000 'Set all outputs to 0 (off)
TRISB = %00000000 'Set PORTB to all output
PORTC = %00000000 'Set all outputs to 0 (off)
TRISC = %00000000 'Set PORTC to all output

'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
' Alias pins
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
scl VAR PORTA.2 'clock line for temp sensors
sda VAR PORTA.0 'data line for temp sensors
led1 var PORTC.0 'heartbeat LED
display var byte
temp1 VAR word
temp2 VAR word
control VAR BYTE



'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
' Clear LCD and pause to start
'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))

Pause 5000
Serout2 PORTC.4,84,[$fe, 1]
Pause 1000
Serout2 PORTC.4,84,[$fe, 1]
Pause 1000
Serout2 PORTC.4,84,[$fe,1,$fe,128,"START"]
Pause 1500

temp1 = 0
temp2 = 0
display = 0

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON = $31 ; Prescaler = 8, TMR1ON
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))
' Main program loop - updates the LCD and siteplayer with the time
'and status of temperatures and pumps
'((((((((((((((((((((((((((((((((((((((()))))))))) )))))))))))))))))))))))))))))

mainloop:

pause 10
GoSub showonlcd
pause 10
goto mainloop

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED1
display = display + 1
@ INT_RETURN
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''
showonlcd:

select case display

case 11

INTCON.7 = 0
display = display + 1
control = %10010000
i2cwrite sda,scl,control,$1,[$00] ' Place in normal operating mode
pause 10
i2cread sda,scl, control,$0,[temp1]'[avgtemp1] ' Read temperature
temp1 = (temp1*(9/5)) + 32
i2cwrite sda,scl, control,$1,[$01]
Serout2 PORTC.4,84, [$fe, 1]
Serout2 PORTC.4,84, [$fe,128,"MAIN TANK TEMP"]
Serout2 PORTC.4,84, [$fe, 196,#temp1, " DEGREES"]
INTCON.7 = 1

case 21

INTCON.7 = 0
display = display + 1
control = %10011010
i2cwrite sda,scl, control,$1,[$00] ' Place in normal operating mode
i2cread sda,scl, control,$0,[temp2]'[avgtemp2] ' Read temperature
temp2 = (temp2 * 9/5) + 32
i2cwrite sda,scl, control,$1,[$01] 'Place in stand-by operating mode
Serout2 PORTC.4,84, [$fe, 1]
Serout2 PORTC.4,84, [$fe,128,"Q/T TANK TEMP"]
Serout2 PORTC.4,84, [$fe, 196,#temp2, " DEGREES"]
INTCON.7 = 1
display = 0
end select
RETURN

goto mainloop
end

sougata
- 18th February 2006, 04:37
Hi,

When you are using an i2cread PBP does it in a open collector fashion. That means for a 1 it sets the pin to an input and for a 0 it sets the pin to an output with 0. This ensures that the i2cread routine can read the acknowledge from the peripheral. As it might hold the line low itself. It is obvious that the port directions are handled by PBP itself. What is really important are the pull-up resistors on the SDA and SCL line. Without them the open collector line is erratic!!!

Hope this helps

Regards

Sougata

ronjodu
- 18th February 2006, 19:31
Thanks for your reply. I do have 4.7K resistors on SDA and SCL. I tried deleting my portA data direction and still no good. I do get readings back from each sensor however they are strange readings. I'm off to read the datasheet and Midrange Micro sheets again.
Any other suggestions are welcome.