PIC16F1615 simply won't blink

ArakelTheDragon

Joined Nov 18, 2016
1,362
The correct datasheet for reference.

All the analog modules have to be disabled, that means not only "PORTA", but also the comparators, clock pins(oscilator pins) if you use the internal oscillator, voltage reference and all other if present on your PIC.

An example for PIC "16F506". Do remember that for different "PICs" the registers may have different names. "XC8" exposes the registers as variables, but also allows us to use them as "bit field unions". Meaning you can choose to set the register as" REGISTER = 0x00" or you can choose to set an individual bit as "REGISTER.bit0 = 0".

Code:
*************************************************************************
*                                                                       *
*   Architecture:  Baseline PIC                                         *
*   Processor:     16F506                                               *
*   Compiler:      MPLAB XC8 v1.01 (Free mode)                          *
*                                                                       *
*************************************************************************
*                                                                       *
*   Files required: none                                                *
*                                                                       *
*************************************************************************
*                                                                       *
*   Description:    Lesson 7, example 1                                 *
*                                                                       *
*   Demonstrates basic use of ADC                                       *
*                                                                       *
*   Continuously samples analog input, copying value to 4 x LEDs        *
*                                                                       *
*************************************************************************
*                                                                       *
*   Pin assignments:                                                    *
*       AN0     = voltage to be measured (e.g. pot output)              *
*       RC0-3   = output LEDs (RC3 is MSB)                              *
*                                                                       *
************************************************************************/

#include <xc.h>


/***** CONFIGURATION *****/
// ext reset, no code protect, no watchdog, 4 MHz int clock
__CONFIG(MCLRE_ON & CP_OFF & WDT_OFF & IOSCFS_OFF & OSC_IntRC_RB4EN);

// Pin assignments
#define LEDS    PORTC           // output LEDs on RC0-RC3


/***** MAIN PROGRAM *****/
void main()
{
    //*** Initialisation
    
    // configure ports
    TRISC = 0b110000;           // configure RC0-RC3 as outputs
    CM2CON0 = 0;                // disable comparator 2 -> RC0, RC1 digital
    VRCON = 0;                  // disable CVref -> RC2 usable
    
    // configure ADC    
    ADCON0bits.ADCS = 0b11;     // clock = INTOSC/4  
    ADCON0bits.ANS  = 0b10;     // AN0, AN2 analog
    ADCON0bits.CHS  = 0b00;     // select channel AN0 
    ADCON0bits.ADON = 1;        // turn ADC on
                                // -> AN0 ready for sampling

    
    //*** Main loop
    for (;;)
    {
        // sample analog input
        ADCON0bits.GO = 1;          // start conversion
        while (ADCON0bits.nDONE)    // wait until done
            ;
        
        // display result on 4 x LEDs
        LEDS = ADRES >> 4;      // copy high nybble of result to LEDs
    }
}
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
Ok, the status is this. I've (with the help from you guys) got it to blink. And I've added the 10k MCLR pull up.

@JohnInTX The debugger seems to be working now. I kept getting some "The debugger could not be started" error, which wen't away with a restart and proper Debugger startup procedure. Or something. But I'm still missing the step through the code line by line, as Code Composer Studio has. Found many bugs that way.

@ArakelTheDragon Looks sweet, that resembles more what I'm used to with MSP430, not all these #pragma configs at start. They seem scary. And all the analog modules have to be disabled?? Yikes! Does a DAC count as a analogue module, or digital?

@nerdegutta I recently purchased this PK3 along with a PIC16F1615 (picked at random..), just because Linux CCS didn't support MSP430G2553, so I thought I see what the big fuzz was with these pic's:) Now I'm just playing around, trying to get it to blink, and figuring out how to use the IDE. I will build upon it later.
 

ArakelTheDragon

Joined Nov 18, 2016
1,362
The "FileName.hex" is what you load with the programmer or with "Proteus", not the "debug.cof". Its created after compiling in the project directory.
Ok, the status is this. I've (with the help from you guys) got it to blink. And I've added the 10k MCLR pull up.

@JohnInTX The debugger seems to be working now. I kept getting some "The debugger could not be started" error, which wen't away with a restart and proper Debugger startup procedure. Or something. But I'm still missing the step through the code line by line, as Code Composer Studio has. Found many bugs that way.

@ArakelTheDragon Looks sweet, that resembles more what I'm used to with MSP430, not all these #pragma configs at start. They seem scary. And all the analog modules have to be disabled?? Yikes! Does a DAC count as a analogue module, or digital?

@nerdegutta I recently purchased this PK3 along with a PIC16F1615 (picked at random..), just because Linux CCS didn't support MSP430G2553, so I thought I see what the big fuzz was with these pic's:) Now I'm just playing around, trying to get it to blink, and figuring out how to use the IDE. I will build upon it later.
Yes a DAC counts as an analog module if its using a pin by default. The pragmas that I have written are normal and widely used. I recommend using them all. You should disable all the analogue modules to use the pins and save energy.

Its better to use "MPLAB X" with "Linux", its used for debugging, you can setup a watch for the variables and registers and it has build in everything. The code is normally uploaded with "MPLAB" or with "PicKit", but "PicKit" is depreciated as a software.

As for that "CCS" for "Linux", can I ask where did you get it from?
 

atferrari

Joined Jan 6, 2004
4,771
Ok, the status is this. I've (with the help from you guys) got it to blink. And I've added the 10k MCLR pull up.

@JohnInTX The debugger seems to be working now. I kept getting some "The debugger could not be started" error, which wen't away with a restart and proper Debugger startup procedure. Or something. But I'm still missing the step through the code line by line, as Code Composer Studio has. Found many bugs that way.

@ArakelTheDragon Looks sweet, that resembles more what I'm used to with MSP430, not all these #pragma configs at start. They seem scary. And all the analog modules have to be disabled?? Yikes! Does a DAC count as a analogue module, or digital?

@nerdegutta I recently purchased this PK3 along with a PIC16F1615 (picked at random..), just because Linux CCS didn't support MSP430G2553, so I thought I see what the big fuzz was with these pic's:) Now I'm just playing around, trying to get it to blink, and figuring out how to use the IDE. I will build upon it later.
Good to see you are in business now.

Whether you plan to keep doing things, or not, with the MSPxxx, try not to compare with them all the time. Focus on how the PICs are. Later perhpas, you could resort to whatever trick you crafted for the others.

I did so with PICs when coming from the 8051 family and worked for me.

Good luck.
 

AlbertHall

Joined Jun 4, 2014
12,347
But I'm still missing the step through the code line by line, as Code Composer Studio has. Found many bugs that way.
If you look at the program below you will see pink squares to left of lines 13 and 15. They are breakpoints set by clicking in that left hand column.
When you debug the program it will stop at those lines. Then you can click on the blue boxes with orange arrows to control the execution of the program. The second one does single-stepping. Hover the mouse over them to see what the others do.
[PS this is not a sensible program]
1577637225345.png
 

nerdegutta

Joined Dec 15, 2009
2,684
I've been using Linux at home for nearly 20 years now. For development I use KiCAD or eagleCAD and MPLAP X with XC8, and a few PICKit 3. The distros spans from Ubuntu, Debian, Lubuntu, Puppylinux, Peppermint and OpenSuse.

I started with PIC 16F628. It's a nice little PIC but it lacks the ADC functionality.

Good to see your're on the right track.
 

geekoftheweek

Joined Oct 6, 2013
1,221
Yes indeed, memory paging is a pain and it is very easy to make hard to find bugs. That is all taken care of 'under the hood' if you program in C.
Unfortunately I still have not embraced C so I know all too well about trying to figure out what it wrong only to find that the whole problem was a bank selection error. Even with the access memory feature of the 18f and friends it can still cause a considerable amount of cursing once you get into some of the newer ones and their multitude of peripherals.
 

jpanhalt

Joined Jan 18, 2008
11,087
Ok, the status is this. I've (with the help from you guys) got it to blink. And I've added the 10k MCLR pull up. <snip>

Now I'm just playing around, trying to get it to blink, and figuring out how to use the IDE. I will build upon it later.
A good place to start is to be sure your schematic matches the chip you are using.
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
@atferrari Good advice. I will focus on the PIC at hand. I can see though, that my time with the MSP430 is helping me a bit.

@AlbertHall Yes, found the buttons now. I was just confused with the way Breakpoints work here. IIRC, I could set breakpoints everywhere in CCS, but in MPLAB X, they seem to break (the pink icon breaks) unless it's in the right spot. I read this has something to do with the compiler. Like I can't set a BP at a "{" or "int main()", for example. Don't know why I would do this, but it just was not an issue with CCS. I've learned to watch the SFR's while stepping. I like that.

@nerdegutta Thanks. I have been using Linux for about four months now. I REALLY hated it at first, but I've come to like it quite a lot. I'm not good with Linux, though.

@jpanhalt Yes, for sure. But like I wrote in the OP, I am new with KiCad and don't know how to import a symbol, so I just used the closest (numerical) one.

Next mission: Get something to blink, using interrupts and timers.
 

JohnInTX

Joined Jun 26, 2012
4,787
It needs to be a line which generates some code. Those examples don't actually generate code.
Keep in mind also that when stepping, the cursor line will follow the underlying code which might not exactly correspond to the written source due to compiler optimizations and just the way it generates code.

Have fun!
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
Makes sense.

I can't find the macros for these pragmas. Like how the compilers knows what INTOSC actually means. It's nice to know it's not just black magic. Aren't they defined in some header file? I think I've looked through them all. I have also looked in the \build\default\production directory. No luck.

Code:
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switch Over (Internal External Switch Over mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
#pragma config ZCD = OFF        // Zero Cross Detect Disable Bit (ZCD disable.  ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PLLEN = OFF      // PLL Enable Bit (4x PLL is enabled when software sets the SPLLEN bit)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

// CONFIG3
#pragma config WDTCPS = WDTCPS1F// WDT Period Select (Software Control (WDTPS))
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config WDTCWS = WDTCWSSW// WDT Window Select (Software WDT window size control (WDTWS bits))
#pragma config WDTCCS = SWC     // WDT Input Clock Selector (Software control, controlled by WDTCS bits)
 

AlbertHall

Joined Jun 4, 2014
12,347
It is defined in the *.inc files. I don't know whether XC8 uses these.
; Configuration Bits
;
; NAME Address
; CONFIG1 8007h
; CONFIG2 8008h
;
;==========================================================================

; The following is an assignment of address values for all of the
; configuration registers for the purpose of table reads
_CONFIG1 EQU H'8007'
_CONFIG2 EQU H'8008'

;----- CONFIG1 Options --------------------------------------------------
_FOSC_LP EQU H'3FF8'; LP Oscillator, Low-power crystal connected between OSC1 and OSC2 pins
_FOSC_XT EQU H'3FF9'; XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins
_FOSC_HS EQU H'3FFA'; HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins
_FOSC_EXTRC EQU H'3FFB'; EXTRC oscillator: External RC circuit connected to CLKIN pin
_FOSC_INTOSC EQU H'3FFC'; INTOSC oscillator: I/O function on CLKIN pin
_FOSC_ECL EQU H'3FFD'; ECL, External Clock, Low Power Mode (0-0.5 MHz): device clock supplied to CLKIN pins
_FOSC_ECM EQU H'3FFE'; ECM, External Clock, Medium Power Mode (0.5-4 MHz): device clock supplied to CLKIN pins
_FOSC_ECH EQU H'3FFF'; ECH, External Clock, High Power Mode (4-20 MHz): device clock supplied to CLKIN pins
 

JohnInTX

Joined Jun 26, 2012
4,787
In MPLABX select Window->Target Memory Views ->Configuration Bits. An interactive window will open. Review and select each option then click Generate Source Code to Output. Copy and paste from the Output Window to your source code. Just guessing but I get this (note that the Curiosity Boards need LVP enabled - update to fit your hardware including MCLR/ pullup)
This procedure will save you from rummaging through the .h files to set the CONFIG #pragmas. Be sure to set the PIC type and Hardware Debugger in Project Properties before using it.
 
Last edited:
Top