News:

;) This forum is the property of Proton software developers

Main Menu

Using the ICSP pins as GPIO

Started by JohnB, Jun 13, 2026, 11:39 AM

Previous topic - Next topic

JohnB

I have not been able to taggle either of the ISCP Clock or Data lines on the 16F15245.  I have added this in my Init code:

    pinmode(LATA.0, Output)
    pinHigh LATA.0
and set LVP=OFF

Is there anything else I need to do?
JohnB

trastikata

I did not check the PIC in question, but some pins have their analog function on by default at POR.

JohnB

At the start of Init I have

    ' Disable analog
    ANSELA = 0
    ANSELB = 0
    ANSELC = 0

I don't know if there is anything else I need to do to set it to digital.
JohnB

top204

In general, I have never had any problems with using the ICSP (In Circuit Serial Programming) pins, as general purpose I/O lines, as long as the programmer is disconnected from the circuit when it is operating, because some programmers can put a load on the pins.

The compiler sets a device's pins to digital before the user program starts , and disables some other peripherals. See the device's .def file. For example the "16F15245.def" file has the codes:

'-----------------------------------------------------------------------------------
' Make the device's I/O pins digital (if required) before the main program starts
' This can be added too by the user and shared on the forum
'
#IfnSym __SYSCOM_LIBRARY_OFF_      ' Has the library been disabled in the compiler?
    ANSELA = $00
    ANSELB = $00
    ANSELC = $00
    SLRCONA = $00
    SLRCONB = $00
    SLRCONC = $00
    INLVLA = $00
    INLVLB = $00
    INLVLC = $00
#EndIfSym


For a test, try a none-complex piece of code that will set the pins to repeatedly output low and output high, with a pair of LEDs connected to them. Even in the 21st century, you cannot beat a pair of flashing LEDs to signal a good or bad result.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A code listing template for a PIC16F15245 device operating at 32MHz with the internal oscillator.
'
' Written by Les Johnson for the Positron BASIC compiler.
'
    Device = 16F15245                                       ' Tell the compiler what device to compile for
    Declare Xtal = 32                                       ' Tell the compiler what frequency the device is operating at (in MHz)
    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 HSerial_Baud = 9600
    Declare HSerout1_Pin = PORTB.7
'
' Define the pins to use for the LEDs
'
$define LED1_Pin PORTA.0
$define LED2_Pin PORTA.1
'
' Create any global variables, constants and aliases here
'

'--------------------------------------------------------------------------------------------------
' The main program starts here
' Flash two LEDs
'
Main:
    Setup()                                                 ' Setup the program and peripherals

    Do                                                      ' Create a loop
        PinSet LED1_Pin                                     ' \ Illuminate the LEDs
        PinSet LED2_Pin                                     ' /
        DelayMs 500                                         ' Wait for half a second
        PinClear LED1_Pin                                   ' \
        PinClear LED2_Pin                                   ' / Extinguish the LEDs
        DelayMs 500                                         ' Wait for half a second
    Loop                                                    ' Do it forever

'--------------------------------------------------------------------------------------------------
' Setup the program and peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_Init_32MHz()                                        ' Setup the device to operate with its internal oscillator at 32MHz
    PinLow LED1_Pin                                         ' \
    PinLow LED2_Pin                                         ' / Set the pins as output low
EndProc

'--------------------------------------------------------------------------------------------------
' Setup the PIC16F15245 to operate with its internal oscillator at 32MHz
' Input     : None
' Output    : None
' Notes     : None
'
Proc Osc_Init_32MHz()
    OSCEN   = %00000000
    OSCFRQ  = %00000101                                     ' FRQ 32 MHz
    OSCTUNE = %00000000
    DelayMs 100                                             ' Wait for the oscillator to stabilise
EndProc

'--------------------------------------------------------------------------------------------------
' Setup the config fuses for the internal 32MHz oscillator.
'
    Config1 FEXTOSC_OFF,_                                   ' External Oscillator not enabled
            RSTOSC_HFINTOSC_32MHZ,_                         ' HFINTOSC (32 MHz)
            CLKOUTEN_OFF,_                                  ' CLKOUT function is disabled. I/O function on RA4
            VDDAR_HI                                        ' VDD Internal analog systems are calibrated for operation between VDD = 2.3V - 5.5V

    Config2 MCLRE_EXTMCLR,_                                 ' If LVP = 0, MCLR pin is MCLR. If LVP = 1. RA3 pin function is MCLR
            PWRTS_PWRT_OFF,_                                ' PWRT is disabled
            WDTE_OFF,_                                      ' WDT disabled. SEN is ignored
            BOREN_ON,_                                      ' Brown-out Reset Enabled. SBOREN bit is ignored
            BORV_LO,_                                       ' Brown-out Reset Voltage (VBOR) set to 1.9V
            PPS1WAY_OFF,_                                   ' The PPSLOCKED bit can be set and cleared as needed
            STVREN_ON                                       ' Stack Overflow or Underflow will cause a reset

    Config4 BBSIZE_BB512,_                                  ' 512 words boot block size
            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 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

Then use the same code listing template, and read the pins and transmit the results to a serial terminal. If they signal a 1 when they are brought high, and 0 when they are grounded, then they are digital I/O lines.

Regards
Les

streborc

Master Clear (RA3 / pin 4) is tied high, or is it being used as an input?

Fanie

The program pin can be an input only.  The program pin is usually made higher than the supply voltage to force the pic into program mode.
Be careful using low voltage programming.  On some micro's this pin is internally set as an input and will hence not program a second time, but there is two ways to keep the micro programmable. 
If I have it right, the one is to not define it as an input and keep it exclusively for programming, the other is a software delay wherein the programmer is allowed to start the micro's programming.  I don't know if the same applies to pics.
You may be able to re-program using high voltage programming.

On the other pins, as Les said, should be no problem using them as I/O's, and re-programming provided that the impedance is high enough for the programmer's clock and data signals to drive the pins.

According to the data sheet you need to setup PortA, LatA, AnselA and TrisA
Example 16-2. Initializing PORTA in C
// This code example illustrates initializing the PORTA register.
// The other ports are initialized in the same manner.
 PORTA = 0x00; // Clear PORTA
 LATA = 0x00; // Clear Data Latch
 ANSELA = 0x00; // Enable digital drivers
 TRISA = 0x38; // Set RA[5:3] as inputs and set others as outputs

Example 16-1 shows the port initialization in ASM.

Don't write to Lat registers, they only buffer the port registers.
Instead of pinHigh LATA.0, try pinHigh PortA.0

Fanie

One more thing.  On the 16F17146 I use.
I tried getting a simple I/O routine going which would not work, using Les's upside down (sorry Les) code.
The setup on the bottom frustrated me so I moved it to the top and the I/O's worked.  I'm not saying what Les did was incorrect, who knows what I did in my program.  Maybe I was just lucky.

This upside down setup of the C programs is very irritating (for me). Not sure if one should flip the monitor or hang from the roof like a bat.  But that's just me.
I also could not find the default setting for the micro's registers. If there is, reference me please. else you have to setup every register.

JohnB

I finally got to the bottom of this, it was down to a disaster I had on my PC where my entire D drive became corrupted.  I restored from my NAS but it turned out that all restored files were set to read only. So what I was doing was making changes and compiling and reprogramming only to find I was programming with my earlier file. Now I can confirm that SetMode and SetHigh are working perfectly. Thanks for all the helpful suggestions. 

My Windows is running on empty as my C: drive is virtually full.  I dont know whether this caused the original problem but I am not going to risk it so off to my local PC Shop to try and get a larger C drive.
JohnB