Hot!PIC12F1572 Sample Code

Author
Sarel
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2017/10/11 02:02:01
  • Location: 0
  • Status: offline
2017/10/11 13:48:33 (permalink)
0

PIC12F1572 Sample Code

Maybe if someone can help me solve this one, Im trying to get TMR1 going on the PIC12F1572, the minute I enable PEIE_bit, the program stops working. So either disable PEIE or TMR1ON_bit. Any suggestions would be appreciated.
Im using MikroC and just dont have the time now to change to MPlab.
Herewith the code, ignore the variables, I just edited my program out to cut down to only TMR1
 
//PI-12F1572 32Mhz with PLL
//
// VALVE CONTROLLER FOR SERVO VALVE
// +---+
// - VDD-| |-VSS
// SYNC/RA5-| |-RA0/DTI
// RA4/PWM2-| |-RA1/PWM1
// RA3/CLK-| |-RA2/DTO
// +----+
//
//
//
unsigned int state,temp_bit, rx_data,i, vald, pump, pwm1_on, pwm2_on, flowrate ,old_flwrte;
unsigned int valve_state ,t1=0, setP ;
void interrupt()
{
if(TMR1IF ==1 )
{
TMR1IF_bit = 0;
LATA1_bit =! LATA1_bit;
delay_ms(10);
TMR1H = 0xFA;
TMR1L = 0xC8;
}
}

void init()
{
OSCCON = 0b11110000; // 8 Mhz oscillator with PLL to give 32Mhz.
TMR1H = 0xFA;
TMR1L = 0xC8;
GIE_bit = 1; // Enable Global Interrupts
PEIE_bit = 1; // Enable Peripheral Interrupts
T1CON = 0b01000101; //
ANSELA = 0; // Analog off.
TRISA0_bit = 0;
TRISA1_bit = 0;
TRISA2_bit = 0;
LATA0_bit = 0;
LATA1_bit = 0;
LATA2_bit = 0;
TMR1IE_bit = 1;
TMR1IF_bit = 0;
 
}
void main()
{
init();

while(1)
{
LATA2_bit =! LATA2_bit;
delay_ms(100);
}
}
#1

8 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 6255
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/11 16:46:13 (permalink)
    +1 (1)
    This does NOT create an interrupt service routine.
    void interrupt()

     
    try:
    void interrupt myisr()

     
     
     Edit: I just noticed you said you're using MikroC.
    Sorry, my advice may not be valid, I've never used that compiler.
     
    post edited by qhb - 2017/10/11 16:55:16
    #2
    Sarel
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2017/10/11 02:02:01
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 01:58:15 (permalink)
    0
    Thanks qhb, yes MikroC uses the void interrupt() for the isr. Im planning to get the code on MPlab, but new to it and my first attempt it did not want to build and I could not figure out why..... My gut feel is there is a compiler issue.
    #3
    witkatz
    Starting Member
    • Total Posts : 6
    • Reward points : 0
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 02:33:56 (permalink)
    +1 (1)
    I do not use MikroC too, but there is a general issue a've noticed in the posted code: a delay in the interrupt routine.
    delay_ms(10); // in the interrupt routine is a bad idea

    #4
    Sarel
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2017/10/11 02:02:01
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 02:44:52 (permalink)
    0
    Thanks Smile: Smile I just added it for debugging purpoises, the main thing was that TMR1 just bombed everything out. A friend tried the code on MPlab' simulator and apparently it worked fine ( he just changed the code to the MPlab standard). I will shortly load the code on the pic and will let you know. 
    #5
    Sarel
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2017/10/11 02:02:01
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 05:18:05 (permalink)
    0
    Ok, loaded into MPlab and a bit better off, the TMR1 does not crash the program, but still does not enter the ISR....
    Any ideas ? I tried both      void interrupt interrupt_handler()     as well as   void interrupt myisr() 
    Herewith my code.
     

    /*
    * PIC12F1572 UART example.
    *
    * Pins:
    * +----+
    * Vdd -|1 8|- Vss
    * RA5 -|2 7|- RA0/ICSPDAT ESUART TX
    * RA4 -|3 6|- RA1/ICSPLCK ESUART RX
    * RA3/MCLR -|4 5|- RA2
    * +----+
    */
    #include <xc.h>
    #include <pic12f1572.h>
    #define _XTAL_FREQ 16000000 // Oscillator frequency.
    #pragma config FOSC = INTOSC // INTOSC oscillator: I/O function on CLKIN pin.
    #pragma config WDTE = OFF // Watchdog Timer disable.
    #pragma config PWRTE = OFF // Power-up Timer enbable.
    #pragma config MCLRE = OFF // MCLR/VPP pin function is MCLR.
    #pragma config CP = OFF // Program memory code protection disabled.
    #pragma config BOREN = ON // Brown-out Reset enabled.
    #pragma config CLKOUTEN = OFF // CLKOUT function is disabled; I/O or oscillator function on the CLKOUT pin.
    #pragma config WRT = OFF // Flash Memory Write protection off.
    #pragma config STVREN = ON // Stack Overflow or Underflow will cause a Reset.
    #pragma config BORV = LO // Brown-out Reset Voltage (Vbor), low trip point selected.
    #pragma config LVP = OFF // High-voltage on MCLR/VPP must be used for programming.
    #include <stdio.h>
    #include <stdlib.h>
    #pragma code interrupt_handler=0x04
    #pragma interrupt interrupt_handler

    void init_pic()
    {
    OSCCON = 0b01111010; // 16 Mhz oscillator.
    ANSELA = 0; // Analog off.
    TMR1H = 0;
    TMR1L = 0;
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;
    T1CON = 0b01000001;
    TRISAbits.TRISA0 = 0;
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA2 = 0;
    LATAbits.LATA0 = 0;
    LATAbits.LATA1 = 0;
    LATAbits.LATA2 = 0;
    // TRISA = 0;
    // LATA1 = 0;
    // LATA2 = 0;
    }

    void interrupt interrupt_handler()
    //void interrupt myisr()
    {
    if (PIR1bits.TMR1IF==1)
    {
    PIR1bits.TMR1IF = 0;
    LATAbits.LATA0 =!LATAbits.LATA0; // Veiliger om na Latch te skryf
    TMR1H = 0;
    TMR1L = 0;
    }
    }
    int main()
    {
    init_pic();
    while (1)
    {
    LATAbits.LATA1 =!LATAbits.LATA1;
    //RA1 =!RA1; // Not sure if this is kosher......
    __delay_ms(10);
    }
    }
    #6
    witkatz
    Starting Member
    • Total Posts : 6
    • Reward points : 0
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 06:16:53 (permalink)
    +1 (1)
    Sarelbut still does not enter the ISR....

    In the newly posted code I miss the TMR1IE setting.
     
    And please enclose your source code with code tags for a better readability.
    post edited by witkatz - 2017/10/12 06:24:50
    #7
    Sarel
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2017/10/11 02:02:01
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 06:32:25 (permalink)
    0
    Great ! I added the code and it works fine.  Still worried about the MikroC code that did not work,the TMR1IE bit was set there,  but will log a fault at MikroElektronica.
    Thanks again !
    #8
    toms
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2006/03/07 18:06:24
    • Location: London, UK
    • Status: offline
    Re: PIC12F1572 Sample Code 2017/10/12 07:02:55 (permalink)
    0
    In your original code, I see a discrepancy between the name of the interrupt flag variable you are testing, and the name of the variable you are resetting:
     
    if(TMR1IF ==1 )
    {
    TMR1IF_bit = 0;

     
    TMR1IF vs TMR1IF_bit.
     
    So I assume the MCU is stuck in an endless loop re-entering the ISR every time it exits because the interrupt flag isnt cleared because one of those variable names are incorrect. :)
    #9
    Jump to:
    © 2017 APG vNext Commercial Version 4.5