PDA

View Full Version : porta.0 and porta.2 only goes high with HIGH command with PIC18F45K80



bmoe79
- 22nd January 2015, 14:35
Hi.

I have a problem that if I set porta.0 and portb.2 high with porta.0=1 and porta.2=1 then they will not go high, but if I do HIGH porta.0 and HIGH porta.2 then they will.
porta.1 and porta.3 works as expected.
Anyone know why?

In the display the code below gives me:
00001111
HIGH

00000000
LOW

00001010
=1

00000000
=0




' PIC18F45K80 Configuration Bit Settings


#CONFIG
CONFIG RETEN = OFF
CONFIG INTOSCSEL = HIGH
CONFIG SOSCSEL = DIG
CONFIG XINST = OFF
CONFIG FOSC = EC3
CONFIG PLLCFG = OFF
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRTEN = ON
CONFIG BOREN = OFF
CONFIG BORV = 3
CONFIG BORPWR = ZPBORMV
CONFIG WDTEN = OFF
CONFIG WDTPS = 1048576
CONFIG CANMX = PORTB
CONFIG MSSPMSK = MSK7
CONFIG MCLRE = ON
CONFIG STVREN = OFF
CONFIG BBSIZ = BB2K
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG




'************************* DEFINES ******************************
define OSC 64


define LCD_DREG PORTC
define LCD_DBIT 0
define LCD_RSREG PORTC
define LCE_RSBIT 4
define LCD_EREG PORTC
define LCD_EBIT 5
define LCD_BITS 4
define LCD_LINES 2
define LCD_COMMANDUS 3000
define LCD_DATAUS 100




'********************** PIN VARIABLES ***************************




'************************ VARIABLES *****************************




'************************ REGISTERS *****************************
ANCON0 = 000000
ANCON1 = 000000
ADCON0 = 000000
ADCON1 = 000000




'*********************** PORT CONFIG ****************************
TRISA = 100000 'porta direction 0=OUTPUT 1=INPUT
PORTA = 000000 'porta preset
TRISB = 010111 'portb direction 0=OUTPUT 1=INPUT
PORTB = 000000 'portb preset
TRISC = 000000 'portc direction 0=OUTPUT 1=INPUT
PORTC = 000000 'portc preset
TRISD = 111111 'portd direction 0=OUTPUT 1=INPUT
PORTD = 110000 'portd preset
TRISE = 001111 'porte direction 0=OUTPUT 1=INPUT
PORTE = 000000 'porte preset




'*********************** TIMER CONFIG ***************************




'********************* INTERRUPT CONFIG *************************




'************************************************* ***************
'******************* MAIN PROGRAM ************************
'************************************************* ***************
pause 500
lcdout $fe, $01
while 1
high porta.0
high porta.1
high porta.2
high porta.3
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "HIGH"
PAUSE 1000
low porta.0
low porta.1
low porta.2
low porta.3
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "LOW "
PAUSE 1000
porta.0 = 1
porta.1 = 1
porta.2 = 1
porta.3 = 1
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "= 1 "
PAUSE 1000
porta.0 = 0
porta.1 = 0
porta.2 = 0
porta.3 = 0
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "= 0 "
PAUSE 1000
wend


'************************************************* ***************
'******************* SUB ROUTINES **********************
'************************************************* ***************




'************************************************* ***************
'***************** INTERRUPT ROUTINE ********************
'************************************************* ***************


end

Dave
- 22nd January 2015, 15:17
Bmoe79, I notice that you are only setting 6 of the 8 bits of each port as well as not setting up the statement as: PORTE = %00000000 The "%" has to proceed the binary representation of the bit values to be valid. I notice you do not use it in any of your statements for setting the TRIS or PORT values.

bmoe79
- 22nd January 2015, 16:27
How weird, in the code everything is ok so it must have happened when I pasted the code.
Here is the code again


' PIC18F45K80 Configuration Bit Settings


#CONFIG
CONFIG RETEN = OFF
CONFIG INTOSCSEL = HIGH
CONFIG SOSCSEL = DIG
CONFIG XINST = OFF
CONFIG FOSC = EC3
CONFIG PLLCFG = OFF
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRTEN = ON
CONFIG BOREN = OFF
CONFIG BORV = 3
CONFIG BORPWR = ZPBORMV
CONFIG WDTEN = OFF
CONFIG WDTPS = 1048576
CONFIG CANMX = PORTB
CONFIG MSSPMSK = MSK7
CONFIG MCLRE = ON
CONFIG STVREN = OFF
CONFIG BBSIZ = BB2K
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG


'************************* DEFINES ******************************
define OSC 64


define LCD_DREG PORTC
define LCD_DBIT 0
define LCD_RSREG PORTC
define LCE_RSBIT 4
define LCD_EREG PORTC
define LCD_EBIT 5
define LCD_BITS 4
define LCD_LINES 2
define LCD_COMMANDUS 3000
define LCD_DATAUS 100


'************************ REGISTERS *****************************
ANCON0 = 000000
ANCON1 = 000000
ADCON0 = 000000
ADCON1 = 000000


'*********************** PORT CONFIG ****************************
TRISA = 100000 'porta direction 0=OUTPUT 1=INPUT
PORTA = 000000 'porta preset
TRISB = 010111 'portb direction 0=OUTPUT 1=INPUT
PORTB = 000000 'portb preset
TRISC = 000000 'portc direction 0=OUTPUT 1=INPUT
PORTC = 000000 'portc preset
TRISD = 111111 'portd direction 0=OUTPUT 1=INPUT
PORTD = 110000 'portd preset
TRISE = 001111 'porte direction 0=OUTPUT 1=INPUT
PORTE = 000000 'porte preset


'************************************************* ***************
'******************* MAIN PROGRAM ************************
'************************************************* ***************
pause 500
lcdout $fe, $01
while 1
high porta.0
high porta.1
high porta.2
high porta.3
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "HIGH"
PAUSE 1000
low porta.0
low porta.1
low porta.2
low porta.3
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "LOW "
PAUSE 1000
porta.0 = 1
porta.1 = 1
porta.2 = 1
porta.3 = 1
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "= 1 "
PAUSE 1000
porta.0 = 0
porta.1 = 0
porta.2 = 0
porta.3 = 0
lcdout $fe, $80, bin8 porta
lcdout $fe, $c0, "= 0 "
PAUSE 1000
wend


end

bmoe79
- 22nd January 2015, 17:02
Same thing again. So weird.
TRISx, PORTx, ANCONx and ADCONx is like below in my code

TRISA = %11100000 'porta direction 0=OUTPUT 1=INPUT
PORTA = %00000000 'porta preset
TRISB = %11010111 'portb direction 0=OUTPUT 1=INPUT
PORTB = %00000000 'portb preset
TRISC = %00000000 'portc direction 0=OUTPUT 1=INPUT
PORTC = %00000000 'portc preset
TRISD = %00111111 'portd direction 0=OUTPUT 1=INPUT
PORTD = %00110000 'portd preset
TRISE = %00001111 'porte direction 0=OUTPUT 1=INPUT
PORTE = %00000000 'porte preset

ANCON0 = %00000000
ANCON1 = %00000000
ADCON0 = %00000000
ADCON1 = %00000000

Tabsoft
- 22nd January 2015, 17:37
This might be a read-modify-write issue.
The 18FK4580 has Output Latch registers.
You should try writing to the port via the LATA register instead of the PORTA register.

bmoe79
- 22nd January 2015, 17:42
After taking a quick look in the datasheet it seems like that coulld be it.
I will test that tomorrow.
Thanks.

AvionicsMaster1
- 23rd January 2015, 15:39
You don't say what you've got hooked up to the port pins. Could you post a schematic?

Archangel
- 23rd January 2015, 18:55
Make sure to disable comparators & ECCP module. Using HIGH /LOW commands disables all analog (generally) for you. PICs default to Analog inputs and you have to be sure to turn it all off to get digital to work. And it makes sense too, analog inputs are incapable of causing any "unintended" actions downstream, like shorting something out or causing a machine to suddenly start running on power up. It is for THAT reason you should set your port states prior to assigning TRIS to outputs.

bmoe79
- 27th January 2015, 09:34
Setting the outputs with latx.x instead of portx.x solved the problem.
Tanks.

Tabsoft
- 27th January 2015, 13:57
Glad to hear this got you moving ahead.

There are plenty of posts on this forum regarding read-modify-write as well as a straight internet search on the subject if you want to understand the issue better.