News:

Let's find out together what makes a PIC Tick!

Main Menu

Eread problem with 18F24Q71

Started by John Drew, Dec 29, 2024, 08:13 AM

Previous topic - Next topic

John Drew

A new PIC for me and having some problems.
The first one is with EREAD. I'm fairly sure I have not set registers or PPS correctly but I'm a bit confused by the complexity.
Here is a cut down program that has assembler errors. Any help would be appreciated.
*********************************************
device 18f24q71
'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config_Start
  FEXTOSC = OFF              ;Oscillator not enable
  RSTOSC = HFINTOSC_64MHZ    ;HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
  CLKOUTEN = OFF             ;CLKOUT function is disable
  PR1WAY = OFF               ;PRLOCKED bit can be set and cleared repeatedl
  BBEN = OFF                 ;Boot block disable
  CSWEN = OFF                ;The NOSC and NDIV bits cannot be changed by user softwar
  FCMEN = OFF                ;Fail-Safe Clock Monitor disable
  FCMENP = OFF               ;Fail-Safe Clock Monitor Disable
  FCMENS = OFF               ;Fail-Safe Clock Monitor Disable
  MCLRE = EXTMCLR            ;If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCL
  PWRTS = PWRT_OFF           ;PWRT is disable
  MVECEN = OFF               ;Interrupt contoller does not use vector table to prioritze interrupt
  IVT1WAY = OFF              ;IVTLOCKED bit can be cleared and set repeatedl
  LPBOREN = OFF              ;Low-Power BOR disable
  BOREN = SBORDIS            ;Brown-out Reset enabled , SBOREN bit is ignore
  BORV = VBOR_1P9            ;Brown-out Reset Voltage (VBOR) set to 1.9
  ZCD = OFF                  ;ZCD module is disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
  PPS1WAY = ON               ;PPSLOCKED bit can be cleared and set only once; PPS registers remain locked after one clear/set cycl
  STVREN = ON                ;Stack full/underflow will cause Rese
  LVP = OFF                  ;HV on MCLR/VPP must be used for programmin
  DEBUG = OFF                ;Background Debugger disable
  XINST = OFF                ;Extended Instruction Set and Indexed Addressing Mode disable
  WDTCPS = WDTCPS_31         ;Divider ratio 1:65536; software control of WDTP
  WDTE = OFF                 ;WDT Disabled; SWDTEN is ignore
  WDTCWS = WDTCWS_7          ;window always open (100%); software control; keyed access not require
  WDTCCS = SC                ;Software Contro
  BBSIZE = BBSIZE_8192       ;Boot Block size is 8192 word
  SAFSZ = SAFSZ_NONE         ;NON
  WRTB = OFF                 ;Boot Block not Write protecte
  WRTC = OFF                 ;Configuration registers not Write protecte
  WRTD = OFF                 ;Data EEPROM not Write protecte
  WRTSAF = OFF               ;SAF not Write Protecte
  WRTAPP = OFF               ;Application Block not write protecte
  CPD = OFF                  ;Data EEPROM code protection disable
  CP = OFF                   ;PFM code protection disable
Config_End

'**** End of Fuse Configurator Settings ****
'-------------------------------------------------------------------------------

Declare Xtal=64

TRISB=%01000011                          '(outputs)    RB0, RB2, RB3, RB4, RB5, RB7
                                         '(inputs)     RB1,RB0
TRISA=%11111101                          'Configure A (PortA.0,A2-7) as inputs
                                         'and A.1 as output
Dim bNormal     as byte                  'normal or hhinc function
EData    $00                             'zero for normal use including azimuth, >0 for elevation unit
                                         'note if using shack unit then this MUST be zero

RunIt:
          DelayMS 1000
          bnormal = eread 0
End

The error codes are:
Assembler error 113 AS2514 :Symbol not previously defined (PP_RD)
Assembler error 113 AS2517 :Symbol not previously defined (NVMDAT)

lobogar

Dim bNormal     as byte
bnormal = eread 0
Hi! Can upper and lower case be a problem?

Frizie

Positron is not case sensitive.
Ohm sweet Ohm | www.picbasic.nl

top204

Unfortunately, microchip have done it again with this device, and changed SFRs and names, and I had not noticed it in my tests.

However, I had created a mechanism in the compiler for this and had cheneged it for other devices with SFr and bit name changes.

So the correction is to open the device's .ppi file, located here: "C:\Program Files (x86)\ProtonIDE\PDS\Includes\PPI\"

And within the [INFOSTART], change the line:

EE_RW_TYPE=1            ; The type of EEPROM read and write library to use for the device

to

EE_RW_TYPE=3            ; The type of EEPROM read and write library to use for the device

This will signal to the copmiler to use a different library subroutine for this particular device.

I've had to do this for several devices and write several differing library subroutines, as the .ppi files show, with their ADIN_TYPE, EE_RW_TYPE, UART_TYPE, FLASH_RW_TYPE, HPWM_TYPE and PPS_TYPE directives.

I'll go through all the newer Q71 devices and see if they all require the change in the ppi file, and add the ppi files to the up-and-coming updates for the compilers.

I still have no idea why microchip continually change SFR, and bit names, and bit positions for peripherals that have been with the devices since day one. Any changes should be make with extra bit positions but keep the original ones the same, and KEEP SFR names the same because they do the same job as they have always done!

John Drew

Thanks Les, much appreciated.
The device is more complicated than I hoped. I'll make the change to the PPI file.
The reason behind the use of the 18f24q71 was to explore the AtoD 12 bit resolution offering requested by a friend. I suspect there will be no increase in accuracy over 10bit but I'll see.
Once I fix the PPI I'll get back to the data sheet for the AtoD.
Best wishes,
John

top204

Reading the datasheet, and using procedure mechanisms that I have created for other devices... Below is a Code Listing template for PIC18FxxQ71 devices, operating at 64MHz using the internal oscillator.

The code has not been tested on a real device, but it looks OK, according to the datasheet.

There are big differences with some of the peripherals, especially the ADC peripheral, but the code below sets it in 'Basic Mode', so it is backward compatible. I must investigate the multi-sampling and filtering aspects of the new ADC peripherals, it may come in handy one day.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A Code Listing template for PIC18FxxQ71 devices, operating at 64MHz with their internal oscillator.
' The code listing also has 8-bit and 12-bit ADC setup and reading procedures.
'
' Written by Les Johnson for the Positron8 BASIC Compiler.
'
    Device = 18F24Q71                                               ' Tell the compiler what device to compile for
    Declare Xtal = 64                                               ' Tell the compiler what frequency the device is operating at (in MHz)
    Declare Float_Display_Type = Fast                               ' Use the compiler's faster floating point display routine
    Declare Auto_Heap_Arrays   = On                                 ' Make all arrays "Heap" types, so they always get placed after standard variables
    Declare Auto_Heap_Strings  = On                                 ' Make all Strings "Heap" types, so they always get placed after standard variables
    Declare Auto_Variable_Bank_Cross = On                           ' Make sure all multi-byte variables remain within a single RAM bank
'
' Setup USART1
'
    Declare Hserial1_Baud = 115200                                  ' Set USART1 Baud rate
    Declare HRSOut1_Pin   = PORTC.6                                 ' Set the TX pin for USART1
    Declare HRSIn1_Pin    = PORTC.7                                 ' Set the RX pin for USART1
'
' Create some ADC Input channel $defines for pins
'
$define cANA0 0
$define cANA1 1
$define cANA2 2
$define cANA3 3
$define cANA4 4
$define cANA5 5
$define cANA6 6
$define cANA7 7

$define cANB0 8
$define cANB1 9
$define cANB2 10
$define cANB3 11
$define cANB4 12
$define cANB5 13
$define cANB6 14
$define cANB7 15

$define cANC0 16
$define cANC1 17
$define cANC2 18
$define cANC3 19
$define cANC4 20
$define cANC5 21
$define cANC6 22
$define cANC7 23

$define cADC_Chan_ADCG1       $36
$define cADC_Chan_OPA1IN_Pos  $37
$define cADC_Chan_OPA2IN_Pos  $38
$define cADC_Chan_DAC2        $39
$define cADC_Chan_DAC3        $3A
$define cADC_Chan_VSS         $3B
$define cADC_Chan_Temp        $3C
$define cADC_Chan_DAC1        $3D
$define cADC_Chan_FVR_Buffer1 $3E
$define cADC_Chan_FVR_Buffer2 $3F
$define cADC_Chan_OPA1IN0_Pos $82
$define cADC_Chan_OPA1IN1_Pos $84
$define cADC_Chan_OPA1IN2_Pos $85
$define cADC_Chan_OPA1IN3_Pos $89
$define cADC_Chan_OPA2IN0_Pos $4C
$define cADC_Chan_OPA2IN1_Pos $41
$define cADC_Chan_OPA2IN2_Pos $4B
$define cADC_Chan_OPA2IN3_Pos $4A
'
' Create any global variables and alias' here
'
    Dim wADC_Result As Word                                         ' Holds the result of an ADC reading
   
'------------------------------------------------------------------------------------------------
' The main program starts here
' Transmit text from USART1 to check the oscillator is operating at the correct speed.
' Also read the ADC in 12-bit mode and transmit the decimal ASCII sample value to a serial terminal.
'
Main:
    Setup()                                                         ' Setup the program and any peripherals
    HRSOutLn "Hello World"                                          ' Transmit to make sure the device is operating at the correct speed
    Do                                                              ' Create a loop
        wADC_Result = ADC_Read12(cANA0)                             ' Read the 12-bit ADC from pin RA0
        HRSOutLn "ADC = ", Dec wADC_Result                          ' Transmit the ADC sample value
        DelayMS 500                                                 ' Create a delay so texts are not transmitted as a blur
    Loop                                                            ' Loop forever

'------------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    ANSELAbits_ANSELA0 = 1                                          ' Setup Pin PORTA.0 (RA0) for analogue mode
    ADC_Setup()                                                     ' Setup the ADC peripheral
EndProc

'------------------------------------------------------------------------------------------------
' Setup the ADC peripheral on a PIC18FxxQ71 device
' Input     : None
' Output    : None
' Notes     : Setup for Right Justified for 12-bit operation, and Basic Mode for backward compatability.
'
Proc ADC_Setup()
    ADCON0 = %10000100                                              ' Single Ended mode. Right Justified. Uses FOSC. ADC is enabled
    ADCON1 = %00000000
    ADCON2 = %00010000                                              ' Basic mode
    ADCON3 = %00000000
    ADACT = %00000000
    ADCLK = %00011111                                               ' ADC Oscillator set for FOSC/64
    ADCGA = %00000001                                               ' CGA0 enabled. CGA1 to CGA7 disabled
    ADCGB = %00000000                                               ' CGB0 to CGB7 disabled
    ADCGC = %00000000                                               ' CGC0 to CGC7 disabled
    ADREF = %00000000                                               ' -Vref is VSS. +Vref is VDD
    ADCTX = %00000000
    ADLTHL = $00
    ADLTHH = $00
    ADUTHL = $FF
    ADUTHH = $0F
    ADSTPTL = $00
    ADSTPTH = $00
    ADACCL = $00
    ADACCH = $00
    ADACCU = $00
    ADCNT = $00
    ADRPT = $00
    ADRESL = $00
    ADRESH = $00
    ADPCH = %00000000                                               ' ADCHS set for ANA0
    ADNCH = $3B                                                     ' ADCHS is VSS
    ADACQL = $01
    ADACQH = $00
    ADCAP = $00
    ADPREL = $00
    ADPREH = $00
    ADSTAT = %00000000
    ADCSEL1 = %00000000
EndProc

'------------------------------------------------------------------------------------------------
' Get a 12-bit reading from the ADC on a PIC18FxxQ71 device
' Input     : pChan holds the ADC channel to read
' Output    : Returns the 12-bit ADC value. Also held in SFRs ADRESL and ADRESH.
' Notes     : Sets the ADFM bits to right justified 12-bit operation.
'
Proc ADC_Read12(pChan As WREG), wADRES_SFR
Global Dim wADRES_SFR As ADRESL.Word   
    ADPCH = pChan                                                   ' Load the channel into the relevant SFR
    ADCON0bits_ADON = 1                                             ' Enable the ADC (just in case)
    ADCON0bits_ADFM0 = 1                                            ' \ Setup the ADFM bits for right justified
    ADCON0bits_ADFM1 = 0                                            ' /
    DelayUS 50                                                      ' A delay before sampling
    ADCON0bits_ADGO = 1                                             ' \
    Repeat : Until ADCON0bits_ADGO = 0                              ' / Start a sample and wait for it to finish
EndProc

'------------------------------------------------------------------------------------------------
' Get an 8-bit reading from the ADC on a PIC18FxxQ71 device
' Input     : pChan holds the ADC channel to read
' Output    : Returns the 8-bit ADC value. Also held in SFR ADRESH.
' Notes     : Sets the ADFM bits to left justified 8-bit operation.
'
Proc ADC_Read8(pChan As WREG), ADRESH
    ADPCH = pChan                                                   ' Load the channel into the relevant SFR
    ADCON0bits_ADON = 1                                             ' Enable the ADC (just in case)
    ADCON0bits_ADFM0 = 0                                            ' \ Setup the ADFM bits for left justified
    ADCON0bits_ADFM1 = 0                                            ' /
    DelayUS 50                                                      ' A delay before sampling
    ADCON0bits_ADGO = 1                                             ' \
    Repeat : Until ADCON0bits_ADGO = 0                              ' / Start a sample and wait for it to finish
EndProc


'------------------------------------------------------------------------------------------------
' Setup the fuses to use the internal oscillator at 64MHz on a PIC18FxxQ71 device. With RA6 and RA7 as general purpose I/O
'
Config_Start
    FEXTOSC = Off                               ' External Oscillator not enabled
    RSTOSC = HFINTOSC_64MHZ                     ' Reset Oscillator is HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
    CLKOUTEN = Off                              ' CLKOUT function is disabled
    PR1WAY = Off                                ' PRLOCKED bit can be set and cleared repeatedly
    BBEN = Off                                  ' Boot block disabled
    CSWEN = On                                  ' Writing to NOSC and NDIV is allowed
    FCMEN = On                                  ' Fail-Safe Clock Monitor enabled
    FCMENP = On                                 ' Fail-Safe Clock Monitor enabled. Timer will flag FSCMP bit and OSFIF interrupt on EXTOSC failure
    FCMENS = Off                                ' Fail-Safe Clock Monitor Disabled
    MCLRE = EXTMCLR                             ' MCLR pin is MCLR operation
    PWRTS = PWRT_OFF                            ' Power-up timer is disabled
    MVECEN = Off                                ' Interrupt contoller does not use vector table
    IVT1WAY = On                                ' IVTLOCKED bit can be cleared and set only once
    LPBOREN = Off                               ' Low-Power BOR disabled
    BOREN = SBORDIS                             ' Brown-out Reset enabled. SBOREN bit is ignored
    BORV = VBOR_1P9                             ' Brown-out Reset Voltage (VBOR) set to 1.9V
    ZCD = Off                                   ' ZCD module is disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    PPS1WAY = Off                               ' PPSLOCKED bit can be set and cleared repeatedly
    STVREN = On                                 ' Stack full/underflow will cause reset
    LVP = Off                                   ' Low voltage programming disabled
    XINST = Off                                 ' Extended Instruction Set and Indexed Addressing Mode disabled
    WDTCPS = WDTCPS_2                           ' WDT Period Divider ratio 1:128. Software control of WDTPS
    WDTE = Off                                  ' WDT Disabled. SWDTEN is ignored
    WDTCWS = WDTCWS_7                           ' WDT window always open (100%). Software control
    WDTCCS = SC                                 ' WDT input clock software control
    BBSIZE = BBSIZE_128                         ' Boot Block size is 128 words
    SAFSZ = SAFSZ_NONE                          ' SAF Block Size none
    WRTB = Off                                  ' Boot Block not Write protected
    WRTC = Off                                  ' Configuration registers not Write protected
    WRTD = Off                                  ' Data EEPROM not Write protected
    WRTSAF = Off                                ' SAF not Write Protected
    WRTAPP = Off                                ' Application Block not write protected
    CPD = Off                                   ' Data EEPROM code protection disabled
    Cp = Off                                    ' PFM code protection disabled
Config_End

John Drew

Thanks Les, that's absolutely terrific. I spent all day experimenting with ADC. I've been watching a flashing LED count but I don't know if I've been successful. I can't count fast enough, I should hook up an LCD.
I have explored all the registers and tried to get averaging working.
I should walk before I run.

I will use your code tomorrow. Thanks a million.
Have a terrific 2025 surrounded by your forum friends.
Best wishes,
John

top204

Thanks John

I have just altered the above code listing for an anomaly in it, and added a Dec modifier to it.

HRSOutLn "ADC = ", Dec wADC_Result

I hope you all have a better 2025 than it was in 2024.

Best regards
Les

Sommi

I got this reply when first compiling

RAMDEF Section Error in the PPI file for this device
KISS - keep it simple and stupid

top204

Make sure you are using the latest compiler version, as there is no RAMDEF section in the newer device ppi files, and the compiler ignores it if it cannot be found, because the older devies had a RAMDEF, but newer devices do not have it.

The code listing above compiles perfectly using the latest compiler version, but I have not tested it on a real device.