News:

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

Main Menu

Trouble with ADIn in 16f15213

Started by midali, May 05, 2025, 05:20 PM

Previous topic - Next topic

midali

Hi ,

ADIn doesn't work on this PIC . I'm doing something wrong or is it a compiler problem ?

   Device = 16F15213                              ' Tell the compiler what device to compile for 
   Declare Xtal = 4                               ' Tell the compiler what frequency the device is operating at 4 MHz
        OSCCON = $20
        OSCEN = $40
        OSCFRQ = 2
        OSCSTAT = 0
        OSCTUNE = 0
       
Declare  Adin_Res   = 8   '8 bit
Declare  Adin_Tad   = FRC 'RC oscillator chosen for the ADC
Declare  Adin_Stime = 50  'Allow 50us sample time       

Declare Hserial_Baud = 9600
Declare HRSOut_Pin = PORTA.2
       
PORTA  = %00000000
ANSELA = %00000011
TRISA  = %00000011

Dim VALUE As Byte

DelayMS  200 

Do:
    VALUE = ADIn 0
    HSerOutLn [ "Value= " ,Dec VALUE ]
    DelayMS 100
Loop
End                 

On serial, with 3,00V on PORTA.0 I receive strange an random values :

Value= 64
Value= 64
Value= 64
Value= 64
Value= 0
Value= 0
Value= 64
Value= 0
Value= 64
Value= 64
Value= 0
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 64
Value= 0
Value= 64
Value= 0
Value= 0
Value= 64
Value= 64


Thank you !

trastikata

You forgot to right-align the result.

top204

#2
Reading the PIC16F15213 datasheet, and comparing the assembler code created by the compiler, it looks as though it should work.

However, the newer devices sometimes have a bit more setup required than the ADC Declares can cater for, and the ADC Declares should now be considered as 'for backward compatabilty only', for the older devices that had a straightforward, and standard, ADC peripheral, and I will add that to the compiler's manual.

I can see no clear reference to 8-bit operation in the device's datasheet except for the justification of the ADRESL/ADRESH SFRs as was the case with the older devices, and with the newer devices, it can never be taken for granted that they work like the original devices. So try changing the resolution to 10-bit, instead of 8, and right justifying the result by setting the ADFM bit of ADCON1. Also in your code, you are only telling the compiler what pin to use for USART TX, so the device will default to its RX pin of RA1, because it will not set the RX pin's PPS.

Try the code below, using procedures to setup and read the 10-bit ADC on a PIC16F15213 device. It is untested because I do not have a PIC16F15213 device, but it follows the datasheet, and the compiler's library subroutine, and the code is fully controllable and alterable:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Read the ADC on a PIC16F15213 device (untested because no device available)
' Written for the Positron8 BASIC compiler by Les Johnson.
'
    Device = 16F15213                                       ' Tell the compiler what device to compile for
    Declare Xtal = 32                                       ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial_Baud = 9600
    Declare HRSOut_Pin = PORTA.2
    Declare HRSIn_Pin  = PORTA.4
'
' Create global variables here
'
    Dim wADC_Value As Word
   
'---------------------------------------------------------------------------------------------------
' The main program starts here
' Read the ADC and transmit the 10-bit value to a serial terminal
'
Main:
    Setup()                                                 ' Setup the program and any peripherals

    Do                                                      ' Create a loop
        wADC_Value = ADC_Read10(0)                          ' Read the ADC
        HRSOutLn Dec wADC_Value                             ' Transmit the ASCII value to a serial terminal
        DelayMS 200                                         ' Create a delay between the ADC reads
    Loop                                                    ' Do it forever

'---------------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_Int32MHz()                                          ' Setup the device to use the internal oscillator at 32MHz
    PinInput PORTA.0                                        ' Make the pin an input (just in case)
    ANSELAbits_ANSA0 = 1                                    ' Make AN0 pin analogue
    ADC_Init()                                              ' Setup the ADC peripheral on a PIC16F15213 device
EndProc

'---------------------------------------------------------------------------------------------------
' Read the ADC on a PIC16F15213 device
' Input     : pChan holds the ADC channel to read
' Output    : Returns the 10-bit ADC value
' Notes     : None
'
Proc ADC_Read10(pChan As Byte), Word
    ADCON0 = %00000001                                      ' Clear the channel bits of ADCON0 and enable the ADC
    pChan = pChan << 2                                      ' Move the channel bits into the correct position
    ADCON0 = ADCON0 | pChan                                 ' Or the channel bits into the CHS bits of ADCON0
    ADCON1bits_ADFM = 1                                     ' Right Justified for 10-bit operation
    DelayUS 50                                              ' A delay before sampling
    ADCON0bits_GO_nDONE = 1                                 ' \
    Repeat : Until ADCON0bits_GO_nDONE = 0                  ' / Start a sample and wait for it to finish
    Result.Byte1 = ADRESH                                   ' \
    Result.Byte0 = ADRESL                                   ' / Return the sample value
EndProc

'---------------------------------------------------------------------------------------------------
' Setup the ADC peripheral on a PIC16F15213 device
' Input     : None
' Output    : None
' Notes     : Set for ADC Conversion Clock of FRC
'
Proc ADC_Init() 
    ADCON1 = %11110000                                      ' Right Justified for 10-bit operation. +Vref is VDD. Clock is Frc
    ADACT  = %00000000
    ADRESL = $00
    ADRESH = $00
    ADCON0 = %00000000
EndProc

'---------------------------------------------------------------------------------------------------
' Setup the PIC16F15213 device to use the internal oscillator at 32MHz
' Input     : None
' Output    : None
' Notes     : May not need to be called when using the 'RSTOSC_HFINTOSC_32MHZ' config1 fuse
'
Proc Osc_Int32MHz()
    OSCEN = $00
    OSCFRQ = %00000101                                      ' FRQ is 32MHz
    OSCTUNE = $00
EndProc

'---------------------------------------------------------------------------------------------------
' Setup the configuration fuses for s PIC16F15213 or PIC16F15214 device to use its internal oscillator at 32MHz
' The OSC pins are set for standard I/O pins
'
    Config1 FEXTOSC_OFF,_                           ' External Oscillator not enabled
            RSTOSC_HFINTOSC_32MHZ,_                 ' HFINTOSC is 32MHz
            CLKOUTEN_OFF,_                          ' CLKOUT function is disabled; I/O function on RA4
            VDDAR_HI                                ' Internal analogue systems are calibrated for operation between VDD = 2.3V - 5.5V

    Config2 MCLRE_EXTMCLR,_                         ' MCLR pin is MCLR
            PWRTS_PWRT_OFF,_                        ' Power-up Timer is disabled
            WDTE_OFF,_                              ' WDT disabled. SEN is ignored
            BOREN_SBOREN,_                          ' Brown-out Reset enabled according to SBOREN bit
            BORV_LO,_                               ' Brown-out Reset Voltage (VBOR) set to 1.9V
            PPS1WAY_OFF,_                           ' The PPSLOCKED bit can be set and cleared as needed (unlocking sequence is required)
            STVREN_ON                               ' Stack Overflow or Underflow will cause a reset

    Config4 BBSIZE_BB512,_                          ' Boot Block Size 512 words
            BBEN_OFF,_                              ' Boot Block is disabled
            SAFEN_OFF,_                             ' SAF is disabled
            WRTAPP_OFF,_                            ' Application Block is not write-protected
            WRTB_OFF,_                              ' Boot Block is not write-protected
            WRTC_OFF,_                              ' Configuration Registers are not write-protected
            WRTSAF_OFF,_                            ' Storage Area Flash (SAF) is not write-protected
            LVP_OFF                                 ' High Voltage on MCLR/Vpp must be used for programming

    Config5 CP_OFF                                  ' User Program Flash Memory code protection is disabled

midali

Dear Mr. Les,
    It's working .
    Thank you very much for your prompt and helpful response. I truly appreciate your dedication and the fantastic work you've put into Positron Compiler. Your support means a lot!