Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F1519 and Simulator Question

Status
Not open for further replies.

Huck

New Member
Greetings to the Forum. Am using 1519 to implement a CW decoder. Noticed that it has LFINTOSC @ 31KHz and I would like to use it rather than having to add a 32KHz crystal for the external osc option. However, I cannot get TIMER1 to advance in simulator. This leaves me unsure whether I have missed some detail in setting up either TIMER1 or the Clock source OR perhaps the simulator is not capable of that particular function (LFINTOSC). It will operate using fosc/4 and prescaler but don't need anything nearly so fast for cw timing. TNX for any enlightenment provided.

PS No particular reason for using 1519; had a bunch I bought cheap...usually use 16F87- series but this project uses strings and variables which use more ram than is available in that group.
 
EDIT: Oops! I read the OP as PIC16F1619 and not 1519. I think the example code still somewhat applies, though.

Well, I don't know if it would be of any help, but here is some code I wrote the other day for the PIC16F1615 (a lower pin-count version of the 1619) for decoding IR pulse codes from a TV remote to switch an LED on RA0 on and off. Signals from a remote basically use variable width pulses, so it is functionally very similar to CW. I coded it for timer 2 and used an interrupt-on-change for RA2 to trigger the timer to measure the length of each pulse. I found that I was not able to run this code from the 31KHz clock, as the logic inside the interrupt took too long to run and would cause the code to crash. Increasing the clock speed to 500KHz seemed to fix it. For the considerably slower pulses of CW code, that probably won't be an issue. This code is VERY rough and admittedly not super-well commented, though.

Code:
// PIC16F1615 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#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 = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switch Over (Internal External Switch Over mode is enabled)
#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 = ON       // PLL Enable Bit (4x PLL is always enabled)
#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)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <PIC16F1615.h>

//volatile int t = 0;
volatile long data = 0;
volatile char counts = 0;
volatile char flag =0;
volatile char printflag = 0;

void init(){
 
    OSCCON =0x38; //500KHz
 
    TRISA = 0xFE; //A0 as output.
    LATA = 0x01;
 
    T2CLKCON = 0x01; //clock source = Fosc
    T2HLT = 0x00; //rising edge triggered, free running with software gate
    T2CON = 0x20; //T2 off, 4:1 prescaler, 1:1 postcaler (125KHz)
    T2PR = 75; //125KHz/75=1.67KHz or 0.6ms delay
 
    TMR2=0;     //clear TMR2 counter
    TMR2IF = 0; //clear TMR2 interrupt flag
    PIE1 =0x02; //T2 interrupt enabled

    ANSA2 = 0;
    IOCAP2 = 1;
    IOCAN2 = 1;
    IOCIF = 0;
    IOCAF2 = 0;
    INTCON = 0xC0; //global interrupts enabled, peripheral interrupts enabled
    IOCIE = 1;

 
}

void main(void) {
 
    init();
 
    //LATA = 0;
 
    while(1){
        while(!printflag);
        if (data == 0x61A000FF) LATA |= 0x01;       //turns LED on
        else if (data == 0x61A0807F) LATA &= 0xFE;  // turns LED off
        printflag = 0;
        data = 0;
    }
 
    return;
}

void interrupt ISR(){
    if(TMR2IF && TMR2IE){
        TMR2IF = 0;
        counts++;
        if(counts > 50){ //~30ms
            T2ON=0; //timer 2 off
            printflag = 1;
            flag = 0;
            counts = 0;
        }
    }
    if(IOCIF){
        IOCIF = 0;
        IOCAF2 = 0;
        //LATA = 0;
        if(flag){
            if(PORTA & 0x04){
                TMR2 = 0;
                counts = 0;
            }
            else{
                data = data << 1;
                if (counts>1){
                    data++;
                }
            }
        }
        else{
            if(PORTA & 0x04){
                if (counts> 10){
                    flag = 1;
                }
            }
            else{
                TMR2 = 0;
                counts = 0;
                T2ON = 1;
            }
        }
    }
}


However, if you just want to make sure you have the oscillator and timer set up correctly, you might start with a simple "blink" program to get started and make sure you have everything set up correctly first:


Code:
// PIC16F1615 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#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 = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switch Over (Internal External Switch Over mode is enabled)
#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 = ON       // PLL Enable Bit (4x PLL is always enabled)
#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)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <PIC16F1615.h>

volatile int t = 0;

void init(){
 
    OSCCON =0xF0; //8MHz INTOSC x4= 32MHz (only needed for INTOSC)
 
    TRISA = 0xFE; //A0 as output. 0xFE for r1 and higher, 0xFD for r0 boards
    LATA = 0x01;  //change to 0x01 for boards r1 and higher, 0x02 for r0 boards
 
    T2CLKCON = 0x00; //clock source = Fosc/4
    T2HLT = 0x00; //rising edge triggered, free running with software gate
    T2CON = 0x60; //T2 off, /64 prescaler, 1:1 postcaler (125KHz)
    T2PR = 125; //125KHz/125=1KHz or 1ms delay
 
    TMR2=0;     //clear TMR2 counter
    TMR2IF = 0; //clear TMR2 interrupt flag
    INTCON = 0xC0; //global interrupts enabled, peripheral interrupts enabled
    PIE1 =0x02; //T2 interrupt enabled
 
}

void delay_millis(int time){
    t=time;
    TMR2=0;
    T2ON=1;
    while(t>0);
    T2ON=0;
}

void main(void) {
 
    init();
 
    while(1){
        delay_millis(1000);
        LATA ^= 0x01; //change to 0x01 for boards r1 and higher, 0x02 for r0 boards
    }
 
    return;
}

void interrupt ISR(){
    if(TMR2IF && TMR2IE){
        TMR2IF = 0;
        t--;
    }
}
 
Last edited:
hi Huck,
If you post your PIC code, I will try to run it in Oshonsoft simulator.
E
 
Good Morning
I am using Oshonsoft Basic and running the code in the Oshonsoft simulator. The program seems to run(as much as it can without actual input) except that TIMER1 does not run.
I suppose the real test would be to write a simple routine to exercise this function only - time and turn on LED after delay, for instance - burn it to the PIC and run it to see if it works. The program is currently a little rough around the edges but will post it later . It's time to get ready for work.
 
What are your T1GCON and T1CON settings? It is hard to believe the timer won't actually run in simulation. There can be problems of course, like capture, but the timer still runs. Be sure T1CON,TMR1ON is set (bit 0).
 
OK, when I intended to use the PIC16F876, I checked TMR1 operation with a short program (in Simulator)

Define CONFIG = 0x3d71
Dim et As Word
Dim testet As Word
Dim x As Byte
T1CON = %00110001 ' although bit functions are different, the same values set the same conditions in the 1519
TMR1H = 0
TMR1L = 0
time:
WaitMs 2
et.HB = TMR1H
et.LB = TMR1L
testet = TMR1
TMR1H = 0
TMR1L = 0
x = x + 1
Goto time

I haven't figured out this board; hence the code not in box. But in the simulator, this code incremented the TMR1 registers. The same code with T1CON adjusted for the PIC16f1519 runs but TMR1 stays at zero. I have tried several different approaches on the 1519, external signal on RC.0 and external oscillator selected, etc, system clock fosc and intruction clock fosc/4, etc with no joy. Next stop: make this program blink an LED, dump it to the IC and breadboard it to see if it works. I've never encountered any problems with Oshonsoft Sim. before and am still tempted to believe I am missing something.
TNX again for the listen.
 
Hello Huck,

Unfortunately for Enhanced Mid Range Microcontrollers, oshonsoft PIC16 Simulator Ide only support the following peripherals on simulation:
https://www.oshonsoft.com/pic16helptopics.html

The list of currently simulated peripherals
- Digital I/O
- Interrupts
- A/D Converter Module

For Mid range Microcontrollers, oshonsoft PIC simultor Ide only support the following peripherals on simulation:
https://www.oshonsoft.com/pichelptopics.html

The list of currently simulated peripherals
- Digital I/O
- Data EEPROM Memory
- Interrupts
- Timer0 Module
- A/D Converter Module
- Comparator Module
- Voltage Reference Module
- Hardware UART Module
- Timer1 Module (Bits T1OSCEN and \T1SYNC in T1CON are ignored.)
- Timer2 Module
- Capture, Compare and PWM modes of CCP modules
- Reading FLASH program memory
 
'Morning. Tnx JoseRafa83.....you can add the exception for LFINTOSC to the parts of Timer1 which are not implemented.. Also WDT, etc. Loaded the following
to controller and hooked it up on breadboard. LED on PORTD.7 flashed at about 30Hz rate, just like expected.

Define CONFIG1 = 0x3484
Define CONFIG2 = 0x3c13
ConfigPin PORTD.7 = Output
OSCCON = %01111010
T1CON = %11000001
TMR1H = 0
TMR1L = 0
time:
PORTD.7 = TMR1H.7
Goto time
End
So, I guess overestimating the simulator was the root of the problem. On the good side, I can now proceed. Thank you to the group.
 
Just FYI, I have found that MPLab 8.92/MPLab SIM (and maybe by extension MPLabX) works reasonably well with TMR1 and 16F1519, even when the simulation fails with other enhanced midrange chips. I was even able to simulate capture.
 
Tnx. MPLAB is much changed since I used it last, probably mid 2000s. I have used SourceBoost C/C++ and most recently the Oshonsoft products. It looks pretty alien at this point. I'll have to look into it more when I get time. I did see some references to Fortran and that interests me.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top