• AVR Freaks

Hot!PIC18F repeats operation several times although it's not supposed to

Author
MicPic
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2019/05/28 03:53:55
  • Location: 0
  • Status: offline
2019/06/27 04:07:56 (permalink)
0

PIC18F repeats operation several times although it's not supposed to

Hey There,
the presented code shows several LED engagements in my logic analyzer that does not represent how i expect my code to work.
Any suggestions why i see what i see? Did i miss some basic initialization process of my PIC?
 
ps: i used this strange LED Code as i could not pass any variable to the delay function itself as i got error message no matter what datatype i passed.
 
 
/*
 * File: SJAnewmain.c
 * Author: MicPic
 *
 * Created on 28. Mai 2019, 13:27
 */

// PIC18F26K20 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config FOSC = INTIO67 // select internal Oscillator, 1MHz
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 18 // Brown Out Reset Voltage bits (VBOR set to 1.8 V nominal)

// CONFIG2H
#pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT is always enabled. SWDTEN bit has no effect)
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (00C000h-00FFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

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


#include <xc.h>
#include <pic18f26k20.h>
#include "delays.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include<stdio.h>
#include<string.h>

#define I2C_V1
#include <plib.h>
#include <i2c.h>

#define _XTAL_FREQ 8000000 // 8MHz to match Internal Oscillator Using 8MHz clock
#define sync_mode MASTER
#define slew SLEW_OFF

#define LTC_SETUP_PTAT (0b00000000 | 0b00001000 | 0b00000010 | 0b00000000)
#define LED_Port_RC2 (1<<2)
/*******************************************************************************
 * Name:
 * In: None
 * Out: None
 *******************************************************************************/
void oscillatorInit(void)
{
    // SCS FOSC; OSTS intosc; IRCF 16MHz_HFINTOSC/4; IDLEN disabled;
    OSCCON = 0x70;
    // INTSRC disabled; PLLEN enabled; TUN 0;
    OSCTUNE = 0x40;
    // Set the secondary oscillator
}

/*******************************************************************************
 * Name: INTERRUPT_Initialize
 * In: None
 * Out: None
 *******************************************************************************/
void INTERRUPT_Initialize (void)
{
   // IRQ_ContextFlag = 1;
    // Disable Interrupt Priority Vectors (16CXXX Compatibility Mode)
    RCONbits.IPEN = 0;

    // Clear peripheral interrupt priority bits (default reset value)

    // ADI
    IPR1bits.ADIP = 0;
    // TMRI
    INTCON2bits.TMR0IP = 0;
    // TXI
    IPR1bits.TXIP = 0;
    // RCI
    IPR1bits.RCIP = 0;
    // TMRI
    IPR1bits.TMR1IP = 0;
    // RBI
    INTCON2bits.RBIP = 0;

    //IRQ_ForwardFlag = 1; // tell the bootloader he can forward IRQs to the APP
}

/*******************************************************************************
 * Name: PIN_MANAGER_Initialize
 * In: None
 * Out: None
 *******************************************************************************/
void PIN_MANAGER_Initialize(void)
{
    LATB = 0x0;
    LATA = 0x0;
    ANSELbits.ANS0 = 1; /* FCT_GRP sind beide Analoge Pins*/
    ANSELbits.ANS1 = 1; /* HW_REV */
    ANSELbits.ANS2 = 0;
    ANSELbits.ANS3 = 0;
    ANSELbits.ANS4 = 0;
    ANSELHbits.ANS12 = 0;
    ANSELHbits.ANS11 = 0;
    ANSELHbits.ANS10 = 0;
    ANSELHbits.ANS9 = 0;
    ANSELHbits.ANS8 = 0;
    LATC = 0x0;
    TRISB = 0xFF; // Eingang des gesamten registers
    WPUB = 0xFF; // Each pin has internal weak pin-pullup
    TRISC = 0xFF & ~(LED_Port_RC2);
    TRISA = 0xFF; // Eingang

    INTCON2bits.nRBPU = 0x0;

    // enable interrupt-on-change individually
    // IOCB5 = 1;
    // enable interrupt-on-change globally
    // enable interrupt-on-change globally
    // INTCONbits.RBIE = 1;
}

void LED_blink (uint8_t count, BOOL longTime)
{
    uint8_t i;
     
    for (i=0;i<count;i++)
    {
        LATC2 = 0;
        ((longTime) ? (__delay_us(60) : __delay_us(30)));
        LATC2 = 1;
        ((longTime) ? (__delay_us(60) : __delay_us(30)));
    }
        
}
#define LongWait 1
#define ShortWait 0

void main(void){
    static unsigned char loop = 1;
    
    INTERRUPT_Initialize();
    PIN_MANAGER_Initialize();
    oscillatorInit();
     
    
    LED_blink(loop,ShortWait);
    __delay_us(100);
    LED_blink(loop,LongWait);
    loop++;
    while(1);

 
post edited by MicPic - 2019/06/27 04:08:59

Attached Image(s)

#1

3 Replies Related Threads

    rodims
    Super Member
    • Total Posts : 1519
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: PIC18F repeats operation several times although it's not supposed to 2019/06/27 04:36:33 (permalink)
    +1 (1)
     
    You successfully managed to remove the timing information from your Salea Logic Analyzer screen.
    And it is not clear where it starts / triggers.
    How can we know what  the analyzer really shows. 
     
    Your question is more like a riddle. It could be solved, but it would be easier, if you just simplify your code to find out the reason. Start with a simpler working version and then may be go back to your current version.
     
    And definitely solve your parameter problem first: 
    ps: i used this strange LED Code as i could not pass any variable to the delay function itself as i got error message no matter what datatype i passed.

     
    #2
    ric
    Super Member
    • Total Posts : 23829
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC18F repeats operation several times although it's not supposed to 2019/06/27 04:43:52 (permalink)
    +1 (1)
    Is that the real code?
    (The end of main() is missing.)
     
    With that exact code, I would expect to see RC2 go high for 130us, low for 60us, then stay high forever.



    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    ric
    Super Member
    • Total Posts : 23829
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC18F repeats operation several times although it's not supposed to 2019/06/27 04:46:56 (permalink)
    +2 (2)
    Oh and you can't pass a variable to __delay_us() because it is NOT a function, it is a macro.
    You can get a variable delay by making your own function that loops and calls __delay_us(1) repeatedly.
    (Although it will run a bit slower than you expect due to loop overheads)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    Jump to:
    © 2019 APG vNext Commercial Version 4.5