PDA

View Full Version : 18F1320 And I/0



johnyman34
- 10th December 2006, 19:13
Hi

I want to put all I/O pins of 18f1320 as I/0 digital.
i´m defining it like this

ADCON1=%01111111

But Pin2(RA0/AN0) is erratic!
What i´m doing wrong?

Regards

mister_e
- 10th December 2006, 19:30
You should have it good, but can you post your code?

Anyways... the following work here on a EasyPIC 4 board


asm
__CONFIG _CONFIG1H, _XT_OSC_1H
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG4L, _LVP_OFF_4L
endasm
PORTA=0
TRISA=0
PORTB=0
TRISB=0
ADCON1=%01111111

ByteA var byte

Start:
'
' LED show on PORTA
' -----------------
for bytea=0 to 4
PORTA = DCD BYTEA
PAUSE 200
NEXT

TRISA=255 ' turn PORTA as input
Spin:
PORTB=PORTA ' display PORTA push button status on PORTB
GOTO Spin

johnyman34
- 10th December 2006, 20:07
This is my code:

'{$STAMP BS2p}

define osc 4



ADCON1=%01111111





'CONSTANTES DE INICIALIZAÇÃO e controlo 1-wire

OW_FERst CON %0001 'Front-End Reset
OW_BERst CON %0010 'Back-End reset
OW_BitMode CON %0100
SkipROM CON $CC 'Skip ROM Command
WriteScratch CON $0F 'Write ScratchPad
CopyScratch CON $55 'Copy ScratchPad to Memory
ReadScratch con $AA
ReadMemory CON $F0 'Read Memory
SearchROM CON $F0
Validation_Key CON $A5
Endereco_Inicial CON $00 'Target Address in Memory
ReadROM CON $33 'Read ROM Command
CopyLockAppReg CON $5A
ReadStatusReg CON $66

'CONSTANTES DE INICIALIZAÇÃo e controlo LCD
clrLCD CON 12 ' Clear entire LCD screen.
Light_on CON 14
CR CON 13
N9600 CON $4054
'variáveis 1-wire



'crc VAR Word
'romdata1 VAR Byte(8)

'outras variaveis
ROMDATA VAR Byte(32)
Ink_remain VAR Word
digi_ink_remain VAR Word
contador var word
StatusReg VAR Word
crc_lido var word
crc_gravado var word

SMT_lida VAR Word


DGC_lido VAR Word


pin var byte
pin_digichip var byte
idx VAR Byte
blank VAR Byte
ink_end VAR Byte
St_count VAR Byte(8)
cor VAR Word
factor VAR Word
idx1 VAR Byte
ink_flag_status var bit
ds2431_flag var bit
AppReg1 var byte
ee var byte

st_count(0)=0
st_count(1)=0
st_count(2)=0
st_count(3)=0
st_count(4)=0
st_count(5)=0
st_count(6)=0
st_count(7)=0

low portA
low portB

LOW 12
pause 500



GOSUB LCD_light_ON
Serout2 12,N9600,["V1 18F1320 9/12/06"]
GOSUB lcd_cr
pause 2000




' goto loop



boot_init:
FOR cor =1 TO 4
ink_flag_status=0
gosub St_continua
next
st_count(0)=0
st_count(1)=0
st_count(2)=0
st_count(3)=0
st_count(4)=0
st_count(5)=0
st_count(6)=0
st_count(7)=0
gosub identifica_cor

Loop: 'inicio do loop para leitura de ink_remain




FOR cor =1 TO 4
ink_flag_status=0

PAUSE 1500
GOSUB init_lcd
Serout2 12,N9600, ["COR ",dec1 cor]
GOSUB Lcd_cr

GOSUB Ler_Readscratch
GOSUB ink_flag 'detecta se RB2(ink flag) está sem tinta
GOSUB Lcd_cr

'GOSUB Ink_Remain_read
avanca0:
NEXT

GOTO loop

Read_ROM: '33 le o rom id do ds2430
gosub cor_pin_rb1

'if cor=1 then pin=11
'if cor=2 then pin=10
'if cor=3 then pin=9
'if cor=4 then pin=8


' gosub init_lcd
OWOUT pin,OW_FERst,[readrom] ' send Read ROM command
' PAUSE 35
OWIN pin,OW_BERst,[ STR romData\8] ' le os oito bytes do rom id e grava na variavel romdata

for idx =0 to 7
serout2 12,N9600,[hex2 romdata(idx)]

next
GOSUB Lcd_cr
return



Read_Memory:: 'F0 'le a memoria da EEprom na totalidade
'CC
gosub init_lcd
OWOUT porta.3,OW_FERst,[$CC,$F0,$00] ' send Read ROM command
PAUSE 15
OWIN porta.3,OW_BERst,[ STR romData\32]


FOR idx = 0 TO 31
pause 5
serout2 12,N9600, [HEX2 romData(idx)] ' show ID, serial num, CRC

NEXT


return


identifica_cor:
gosub init_lcd
FOR cor =1 TO 4
gosub init_lcd
gosub cor_pin_rb1
pause 100

OWOUT pin,OW_FERst,[$CC,$F0,$00] ' send Read ROM command
PAUSE 100
OWIN pin,OW_BERst,[ STR romData\32]

pause 50
FOR idx = 0 TO 31

pause 5
serout2 12,N9600, [HEX2 romData(idx)] ' show ID, serial num, CRC

NEXT
if romdata(01)=1 then serout2 12,N9600, [" CYAN"]
if romdata(01)=2 then serout2 12,N9600, [" MAGENTA"]
if romdata(01)=3 then serout2 12,N9600, [" YELLOW"]
if romdata(01)=4 then serout2 12,N9600, [" BLACK"]
if romdata(01)=6 then serout2 12,N9600, [" LMagenta"]
if romdata(01)=5 then serout2 12,N9600, [" LCyan"]
if romdata(01)>6 then serout2 12,N9600, [" ERRO"]

pause 1000
next

return


cor_pin_rb1:
if cor=1 then pin=9 '
if cor=2 then pin=8
if cor=3 then pin=3
if cor=4 then pin=2

return

Ler_Readscratch:
gosub cor_pin_rb1
' gosub init_lcd
serout2 12,N9600, ["EE TintOr Cont" ]
pause 15
GOSUB Lcd_cr
OWOUT pin,OW_FERst,[$CC,$AA,$00] ' send Read readscratch
PAUSE 50

OWIN pin,OW_BERst,[ STR romData\5]


ee=romdata(0)


ink_remain.byte1 = romdata(1)
ink_remain.byte0 = romdata(2)
contador.BYTE1 = romdata(3)
contador.BYTE0 = romdata(4)

serout2 12,N9600, [HEX2 ee," ",dec3 ink_remain/10, " ", dec4 contador/10]
pause 500

GOSUB Lcd_cr
return


sometimes RA.0 give just FFFF... from routine Identifica_cor!

What can be the cause?

Regards

joao

mister_e
- 10th December 2006, 20:19
OK, you're a BS2 converted now :D

PIN is not valid in PBP. To write to a specific I/O you must use the according name. Something like PORTA.0=1

You can also use an alias

LED1 var PORTA.0

then later you use LED1=1

You can also use HIGH and LOW with a pin#

SEROUT and some other MAY work using a pin # but it's really not readable later ( let's say HIGH 8 ... what the hell is 8? yeah it's PORTA.0... few minutes later :D ) I suggest you use the I/O name or an alias to the I/O


ROMDATA VAR Byte(32)
i can't confirm it will work as is, usually we use

ROMDATA VAR Byte[32]

Just change your parenthesis to square bracket. Same thing when you want to read/write to a specific Array

ROMDATA[0]=123

As you're using a PIC with an internal USART, there's no need to use SEROUT/SEROUT2, use HSEROUT instead if your hardware permit. Better IMHO


HTH