News:

;) This forum is the property of Proton software developers

Main Menu

16F18877 any one have set up code, ADC config etc

Started by TimB, Aug 01, 2021, 11:13 AM

Previous topic - Next topic

TimB


Hi people

I'm just starting using the 16F18877 and asking if anyone has the OSC set up code. ADC configs etc

Before I embark on it, I want to see if someone else has put the effort in already.

Thanks

Tim

top204

#1
It's not that straightforward Tim.

It depends on what oscillator type you are using, and at what frequency. Also, what ADC setup is being used, because there are a lot of tweaks that are needed for the timing and resolution of the ADCC peripheral on these devices.

The code below "should" set the device up to use its internal oscillator at 32MHz, and setup the ADCC peripheral for 10-bit standard mode using the FRC as its clock. A very basic setup for an ADC, but should give some results that can be further tweaked by altering its clock source. I don't have one of those devies handy, so I've set it up using the datasheet.

To test if the device is operating at the correct frequency, setup the Baud rate for USART1 and transmit some text. If it is received correctly, the oscillator is running at the correct frequency.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Written by Les Johnson for the Positron8 BASIC compiler.
' https://sites.google.com/view/rosetta-tech/home
'
    Device = 16F18877                            ' Choose the microcontroller
    Declare Xtal = 32                           ' Inform the compiler we are using a device operating at 32MHz

'------------------------------------------------------------------
'
Main:   
    Osc_32MHz()
   
    ADCC_Init()
   
    Stop
    
'------------------------------------------------------------------
' Setup the device to operate at 32MHz using its internal oscillator
' Input     : None
' Output    : None
' Notes     : For use on an PIC16F18877 device
'
Proc Osc_32MHz()
    OSCCON1 = $62       ' NOSC HFINTOSC. NDIV 4
    OSCCON3 = $00
    OSCEN = $00
    OSCFRQ = $06        ' HFFRQ 32_MHz
    OSCTUNE = $00
EndProc

'------------------------------------------------------------------
' Initialise the ADCC peripheral for standard mode
' Input     : None
' Output    : None
' Notes     : For use on an PIC16F18877 device
'
Proc ADCC_Init()
    ADLTHL = $00
    ADLTHH = $00
    ADUTHL = $00
    ADUTHH = $00
    ADSTPTL = $00
    ADSTPTH = $00
    ADRPT = $00
    ADPCH = $00
    ADCAP = $00
    ADPRE = $00
    ADCON1 = $00        ' ADPPOL VSS
    ADCON2 = $00        ' Basic mode
    ADCON3 = $00
    ADSTAT = $00
    ADREF = $00         ' ADNREF VSS. ADPREF VDD
    ADACT = $00
    ADCLK = $00         ' ADCCS FOSC/2
    ADCON0 = $94        ' ADFM right. ADON enabled. ADCONT disabled. ADCS FRC
    ADACQ = $00
EndProc

'------------------------------------------------------------------
' Setup the configs for internal oscillator
'
    Config1 FEXTOSC_OFF, RSTOSC_HFINT1, CLKOUTEN_OFF, CSWEN_ON, FCMEN_ON
    Config2 MCLRE_ON, PWRTE_ON, LPBOREN_OFF, BOREN_OFF, ZCD_OFF, PPS1WAY_OFF, STVREN_OFF
    Config3 WDTCPS_WDTCPS_2, WDTE_OFF
    Config4 WRT_OFF, SCANE_NOT_AVAILABLE, LVP_OFF
    Config5 CP_OFF, CPD_OFF

TimB


Hi

The set-up seems to be easy, I just had to set the OSCCON2 reg to HFINTOSC and the postscaler to 1

A simple Pin toggle seems to be working

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F18877

Config1 FEXTOSC_ECH, RSTOSC_HFINT32, CLKOUTEN_OFF, CSWEN_ON, FCMEN_ON
Config2 MCLRE_ON, PWRTE_ON, LPBOREN_OFF, BOREN_ON, BORV_HI, ZCD_OFF, ZCDDIS_OFF, PPS1WAY_ON, STVREN_ON, DEBUG_OFF
Config3 WDTCPS_WDTCPS_31, WDTE_OFF, WDTCWS_WDTCWS_7, WDTCCS_SC
Config4 WRT_OFF, SCANE_available, LVP_ON
Config5 CP_OFF, CPD_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
   
   
    Xtal = 32
   
   
    Dim TestPin As PORTC.7
   
    GoTo Start
 
   
Proc Init_SetUp()

    OSCCON2 = %01100000
    Output TestPin
   
EndProc

 
   
   
Start:

    Init_SetUp()
   
    DelayMS 1000
   
    While
   
        Toggle TestPin
       
        DelayMS 2000
 
    Wend


Now to try Usart TX and then ADC configuration



TimB

Thanks to Gabi I now have working USART

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 01/08/2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************


;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F18877

Config1 FEXTOSC_ECH, RSTOSC_HFINT32, CLKOUTEN_OFF, CSWEN_ON, FCMEN_ON
Config2 MCLRE_ON, PWRTE_ON, LPBOREN_OFF, BOREN_ON, BORV_HI, ZCD_OFF, ZCDDIS_OFF, PPS1WAY_ON, STVREN_ON, DEBUG_OFF
Config3 WDTCPS_WDTCPS_31, WDTE_OFF, WDTCWS_WDTCWS_7, WDTCCS_SC
Config4 WRT_OFF, SCANE_available, LVP_ON
Config5 CP_OFF, CPD_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
   
   
    Xtal = 32
   
    Declare Hserial_Baud = 9600        ' Set the baud rate
    Declare Hserial_Clear = 1            ' Enable Error clearing on received characters
   
    Declare RSOut_Pin = PORTC.7
    Declare Serial_Baud = 9600
    Declare RSOut_Mode = 0
   
    Dim TestPin As PORTC.7
   
    GoTo Start

' EUSART on RX=none / TX=RC7 9600  Error 0.04
Proc EUSART_INITIALIZE()
    'PPS registers
   RC7PPS = $10;  //RC7->EUSART:TX;
   ' ABDEN disabled; WUE disabled; BRG16 16bit_generator; SCKP Non-Inverted; ABDOVF no_overflow;
   BAUD1CON = $48
   ' ADDEN disabled; CREN disabled; SREN disabled; RX9 8-bit; SPEN enabled;
   RC1STA = $80
   ' TX9D 0x0; BRGH hi_speed; SENDB sync_break_complete; SYNC asynchronous; TXEN enabled; TX9 8-bit; CSRC client;
   TX1STA = $26
   ' SPBRGL 64;
   SP1BRGL = $40
   ' SPBRGH 3;
   SP1BRGH = 3
EndProc
   
Proc Init_SetUp()

    OSCCON1 = $10
    OSCCON2 = $70
    OSCCON3 = 0
    OSCEN = $40
    OSCFRQ = 5
    OSCSTAT = 0
    OSCTUNE = 0
   
    EUSART_INITIALIZE()
    High TestPin
   
EndProc

 
   
   
Start:

    Init_SetUp()
   
    DelayMS 1000
   
    While
   
        HRSOutLn "Test"
       
        DelayMS 2000
 
    Wend