News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Ports and other Registers

Started by JackB, Jan 03, 2025, 11:30 PM

Previous topic - Next topic

JackB

Hello,

I'm trying to add the value of PORTC to a var, as shown on page 36 of the Positron8 BASIC Compiler Manual

and I'm having a bit of problem getting it accurate, I tried to use 2 word variable to add or multiply

but still it's not working fine.

Let me know if this is possible with the current compiler, or it will be in the future compiler edition

and do I have to buy another compiler for the added features.

from the terminal the PORTC Binary: ",Bin PORTC,"  Decimal: ", Dec PORTC,"  are ok

the  " MyWord: ",Dec MyWord," are missing the count.

Terminal
 PORTC Binary: 0       Decimal: 0   MyWord: 0
 PORTC Binary: 1       Decimal: 1   MyWord: 1
 PORTC Binary: 10      Decimal: 2   MyWord: 2
 PORTC Binary: 11      Decimal: 3   MyWord: 3
 PORTC Binary: 100     Decimal: 4   MyWord: 4
 PORTC Binary: 101     Decimal: 5   MyWord: 5
 PORTC Binary: 110     Decimal: 6   MyWord: 6
 PORTC Binary: 111     Decimal: 7   MyWord: 7
 PORTC Binary: 1000    Decimal: 8      MyWord: 0
 PORTC Binary: 1001    Decimal: 9   MyWord: 9
 PORTC Binary: 1010    Decimal: 10  MyWord: 10
 PORTC Binary: 1011    Decimal: 11  MyWord: 11
 PORTC Binary: 1100    Decimal: 12  MyWord: 12
 PORTC Binary: 1101    Decimal: 13  MyWord: 13
 PORTC Binary: 1110    Decimal: 14  MyWord: 14
 PORTC Binary: 1111    Decimal: 15  MyWord: 15
 PORTC Binary: 10000   Decimal: 16     MyWord: 24
 PORTC Binary: 10001   Decimal: 17  MyWord: 17
 PORTC Binary: 10010   Decimal: 18  MyWord: 18
 PORTC Binary: 10011   Decimal: 19  MyWord: 19
 PORTC Binary: 10100   Decimal: 20  MyWord: 20
 PORTC Binary: 10101   Decimal: 21  MyWord: 21
 PORTC Binary: 10110   Decimal: 22  MyWord: 22
 PORTC Binary: 10111   Decimal: 23  MyWord: 23
 PORTC Binary: 11000   Decimal: 24     MyWord: 16
 PORTC Binary: 11001   Decimal: 25  MyWord: 25
 PORTC Binary: 11010   Decimal: 26  MyWord: 26
 PORTC Binary: 11011   Decimal: 27  MyWord: 27
 PORTC Binary: 11100   Decimal: 28  MyWord: 28
 PORTC Binary: 11101   Decimal: 29  MyWord: 29
 PORTC Binary: 11110   Decimal: 30  MyWord: 30
 PORTC Binary: 11111   Decimal: 31  MyWord: 31
 PORTC Binary: 100000  Decimal: 32     MyWord: 40



Thanks.


Device = 18F14K22   
Declare Xtal = 64
Config_Start
  FOSC = IRC                           ' Internal RC oscillator
  PLLEN = On                           ' PLL is under software control
  MCLRE = OFF
  LVP = OFF  'OFF:PORTC.3 I/O
Config_End

ANSEL = 0
ANSELH= 0
OSCCON = $70                   ' 16Mhz for PLLEN:64Mhz / p:17
Declare Hserial_Baud = 9600    ' USART 1 Baudrate
Declare HRSOut1_Pin = PORTB.7  ' USART 1 out pin

Dim I As Byte

Dim Key As Word
Dim MyWord As Word

TRISA = %00001000              ' 0:Out 1:In
TRISB = %00100000
TRISC = %00000000

 
MAIN:                                  ' Main Routine

While PORTA.3 = 1: Wend                ' Button Press
DelayMS 100                            ' Bounce delay
While PORTA.3 = 0: Wend                ' Button release
DelayMS 100                            ' Bounce delay


MyWord = 0 : MyWord1 = 0
PORTC = %00000000

For Key = 0 To 32

MyWord = MyWord + PORTC

SerOut PORTA.0,84, [" PORTC Binary: ",Bin PORTC,"  Decimal: ", Dec PORTC," MyWord: ",Dec MyWord,13,10]
Inc PORTC
Next I

GoTo MAIN



Pepe

The program works well

JackB

Hello Pepe,

if you look at the terminal listing, the value offset are the one deffective, eg:

PORTC Binary: 111     Decimal: 7   MyWord: 7
-->>>> PORTC Binary: 1000    Decimal: 8      MyWord: 0
PORTC Binary: 1001    Decimal: 9   MyWord: 9

Binary 1000, Decimal 8  but Myword 0

RGV250

Hi Jack,
Why are you adding portc to my word and then again IE myword = myword + portc
Try just my word = portc.

Bob

trastikata

Hello JackB,

instead of reading and modifying PORTC try with LATC.

RGV250

Hi,
I tested on Amicus18 (without key press) and it works as expected.
Include "Amicus18.inc"

Dim I As Byte

Dim Key As Word
Dim MyWord As Word

TRISA = %00001000              ' 0:Out 1:In
TRISB = %00100000
TRISC = %00000000
 
MAIN:                                  ' Main Routine

MyWord = 0 ' : MyWord1 = 0
PORTC = %00000000

For I = 0 To 32

MyWord = PORTC
SerOut PORTA.0,84, [" PORTC Binary: ",Bin PORTC,"  Decimal: ", Dec PORTC," MyWord: ",Dec MyWord,13,10]
DelayMS 500     

Inc PORTC
Next I

GoTo MAIN

Bob

JackB

Hello trastikata, RGV250

RGV250:
I use MyWord = MyWord + PORTC to test the example on page 36, then I wrote this calculation

' Addr = LATC * 31 + Key '    LATC instead of PORTC

it will be use to address an EEPROM with 32 records of 32 bytes each record .


trastikata:
Thank you, your solution work great!

calculation work properly using LATC instead of PORTC.



PORTC = %00000000
For Key = 0 To 32   

Addr = LATC * 31 + Key    ' EEPROM Address calculation

' Terminal results
SerOut PORTA.0,84, [" PORTC Binary: ",Bin PORTC,"  Decimal: ", Dec PORTC,"  EEPROM Address: ",Dec Addr,13,10]

Inc PORTC
Next I





 LATC Terminal Results
                         
 PORTC Binary: 0        Decimal: 0    EEPROM Address: 0
 PORTC Binary: 1        Decimal: 1    EEPROM Address: 32
 PORTC Binary: 10       Decimal: 2    EEPROM Address: 64
 PORTC Binary: 11       Decimal: 3    EEPROM Address: 96
 PORTC Binary: 100      Decimal: 4    EEPROM Address: 128
 PORTC Binary: 101      Decimal: 5    EEPROM Address: 160
 PORTC Binary: 110      Decimal: 6    EEPROM Address: 192
 PORTC Binary: 111      Decimal: 7    EEPROM Address: 224
 PORTC Binary: 1000     Decimal: 8    EEPROM Address: 256
 PORTC Binary: 1001     Decimal: 9    EEPROM Address: 288
 PORTC Binary: 1010     Decimal: 10   EEPROM Address: 320
 PORTC Binary: 1011     Decimal: 11   EEPROM Address: 352
 PORTC Binary: 1100     Decimal: 12   EEPROM Address: 384
 PORTC Binary: 1101     Decimal: 13   EEPROM Address: 416
 PORTC Binary: 1110     Decimal: 14   EEPROM Address: 448
 PORTC Binary: 1111     Decimal: 15   EEPROM Address: 480
 PORTC Binary: 10000    Decimal: 16   EEPROM Address: 512
 PORTC Binary: 10001    Decimal: 17   EEPROM Address: 544
 PORTC Binary: 10010    Decimal: 18   EEPROM Address: 576
 PORTC Binary: 10011    Decimal: 19   EEPROM Address: 608
 PORTC Binary: 10100    Decimal: 20   EEPROM Address: 640
 PORTC Binary: 10101    Decimal: 21   EEPROM Address: 672
 PORTC Binary: 10110    Decimal: 22   EEPROM Address: 704
 PORTC Binary: 10111    Decimal: 23   EEPROM Address: 736
 PORTC Binary: 11000    Decimal: 24   EEPROM Address: 768
 PORTC Binary: 11001    Decimal: 25   EEPROM Address: 800
 PORTC Binary: 11010    Decimal: 26   EEPROM Address: 832
 PORTC Binary: 11011    Decimal: 27   EEPROM Address: 864
 PORTC Binary: 11100    Decimal: 28   EEPROM Address: 896
 PORTC Binary: 11101    Decimal: 29   EEPROM Address: 928
 PORTC Binary: 11110    Decimal: 30   EEPROM Address: 960
 PORTC Binary: 11111    Decimal: 31   EEPROM Address: 992
 PORTC Binary: 100000   Decimal: 32   EEPROM Address: 1024



top204

Remember, a port is very volatile and can change in an instant. Especially if it left floating.

At that position in the code, the compiler just sees PORTC as another byte variable and will read its contents as normal, so any movement on it is down to the Port itself having movement on its pins.

With a PORT read it is better to take a shadow reading before using it, in case it changes in-between checks or displays etc... Something like:

Dim bPORT_Shadow As Byte

bPORT_Shadow = PORTC     ' Take a snapshot of the contents of PORTC
HRsoutLn Bin8 bPORT_Shadow, ":", Dec bPORT_Shadow, ":", Hex2 bPORT_Shadow






JackB

#8
Hello Les,

does the LAT can assume the same task.

the PORTC will be connected directly to an Keyboard IC

Thanks.

top204

The LAT SFR is for the output of a port. i.e. Its Latch. So it will read the state of a port's pins.

JackB

I see, my next step is to connect a KBD to PORTC as input and do some test.

Thanks Les.