• AVR Freaks

Hot!Please help debug our PIC16F1503 code (XC8 C Code)

Author
treez
Starting Member
  • Total Posts : 44
  • Reward points : 0
  • Joined: 2017/04/11 06:49:59
  • Location: 0
  • Status: offline
2019/03/24 10:25:03 (permalink)
0

Please help debug our PIC16F1503 code (XC8 C Code)

Hi,
Please help debug this code for PIC16F1503. It builds fine, but when we load it in tomorrow, it will not work, because our code never works first time.
The schematic for our code is as attached. It just reads the mains peak voltage regularly and spits out a representative value of what it is to an external data logger.
Its for PIC16F1503 and is in XC8 C.
// PIC16F1503 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 WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#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)

// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#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 = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

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

#include <xc.h>
#include <stdint.h>
#include <pic16f1503.h>

#define _XTAL_FREQ 4000000
 
    uint8_t count;
    uint8_t count1;
    uint8_t count2;
    uint8_t inamp;
    uint8_t ADCval;
    uint8_t MAXval;

void doADC(void);
        void doADC(void) {
        RC4 = 1;
        //Initiate ADRESH, ADRESL
        ADRESL = 0x00;
        ADRESH = 0x00;
        ADCON0bits.GO = 1; //start conversion (GODONE=1)
        while (ADCON0bits.GO) {;} //Wait for conversion to finish (GODONE CLR)
        ADCval = ADRESH;
        RC4 = 0;
        return;
        }
        
 //Pin useage
        //RA4 (PIN 3) = AN3 = ADC input
        //RA2 (PIN 11) = AN2 = DACOUT)
        //RC3 = PIN7 = General output
        //RC4 = PIN 6 = GENERAL OUTPUT = ADC indicator (see when ADC is done)
        
void main(void) {
    //Setup registers
    OSCCON = 0x6A; //Select 4MHz intosc
    TRISA = 0x10; //ADC input = RA4
    TRISC = 0x00; //unused so make all outputs.
    ANSELA = 0x10; //RA4 = ADC INPUT;RA2 = DAC output
    ANSELC = 0x00;
    INTCON = 0x00; //disable all interrupts
    FVRCON = 0x00;
    ADCON0 = 0x0D; //Select ADC channel AN3; ADC enabled
    ADCON1 = 0x60; //Left justified; FOSC/64; VDD=REF
    ADCON0bits.ADON = 1; //Turn on ADC module
    DACCON0 = 0x90; //DAC enabled, vref=vdd, DACOUT2 enabled
    WPUA = 0x00; //weak pullups disabled
    PORTA = 0x00; //zero outputs
    PORTC = 0x00; //zero outputs
    CM1CON1 = 0x00; //disabled comparator
    CM2CON1 = 0x00; //disabled comparator
    
    __delay_ms(10);
    MAXval = 0;
    

    count = 0; //Do ten flashes to show your in the "zone".
    while(1){
    LATCbits.LATC3 = 0;
    __delay_ms(10);
    LATCbits.LATC3 = 1;
    __delay_ms(10);
    count = count + 1;
    if (count >= 10) {break;}
    }
    
here:
    //This code picks out the mains peak.
    MAXval = 0;
    count1 = 0;
    while (1) {
    doADC();
    if (ADCval >= MAXval)
            {MAXval = ADCval;}
    count1 = count1 + 1;
    if (count1 >= 100) {break;}
    }
    
    //Now put MAXval representor out on DAC (ie mains peak representor)
    if (MAXval < 0x9C) {DACCON1 = 0x00;} //MAINS <180VAC
    if ((MAXval >= 0x9C) && (MAXval < 0xA4)) {DACCON1 = 0x02;} //MAINS 180-190VAC
    if ((MAXval >= 0xA4) && (MAXval < 0xAD)) {DACCON1 = 0x04;} //MAINS 191-200VAC
    if ((MAXval >= 0xAD) && (MAXval < 0xB6)) {DACCON1 = 0x06;} //MAINS 201-210VAC
    if ((MAXval >= 0xB6) && (MAXval < 0xBE)) {DACCON1 = 0x08;} //MAINS 211-220VAC
    if ((MAXval >= 0xBE) && (MAXval < 0xC7)) {DACCON1 = 0x0A;} //MAINS 221-230VAC
    if ((MAXval >= 0xC7) && (MAXval < 0xD0)) {DACCON1 = 0x0C;} //MAINS 231-240VAC
    if ((MAXval >= 0xD0) && (MAXval < 0xD8)) {DACCON1 = 0x0E;} //MAINS 241-250VAC
    if ((MAXval >= 0xD8) && (MAXval < 0xE1)) {DACCON1 = 0x11;} //MAINS 251-260VAC
    if ((MAXval >= 0xE1) && (MAXval < 0xEA)) {DACCON1 = 0x13;} //MAINS 261-270VAC
    if ((MAXval >= 0xEA) && (MAXval < 0xF2)) {DACCON1 = 0x15;} //MAINS 271-280VAC
    if ((MAXval >= 0xF2) && (MAXval < 0xFB)) {DACCON1 = 0x17;} //MAINS 281-290VAC
    if (MAXval >= 0xFB) {DACCON1 = 0x19;} //MAINS >291VAC
    
    goto here;

        //-----------------------------------------------------------------
        return;
}

post edited by treez - 2019/03/24 10:28:53

Attached Image(s)

#1

5 Replies Related Threads

    mlp
    boots too small
    • Total Posts : 765
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: Please help debug our PIC16F1503 code (XC8 C Code) 2019/03/24 11:52:44 (permalink)
    +1 (1)
    treez
    It builds fine, but when we load it in tomorrow, it will not work,

    "it will not work" is not a useful diagnostic aid.
    Describe in detail
    • what you expect to happen
    • what you observe happen
    #include <xc.h>
    Good
    #include <pic16f1503.h>
    Bad. Remove this line.



    Mark (this opinion available for hire)
    #2
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Please help debug our PIC16F1503 code (XC8 C Code) 2019/03/24 12:24:43 (permalink)
    0
    Replace
    void doADC(void);
            void doADC(void) {
            RC4 = 1;
            //Initiate ADRESH, ADRESL
            ADRESL = 0x00;
            ADRESH = 0x00;
            ADCON0bits.GO = 1; //start conversion (GODONE=1)
            while (ADCON0bits.GO) {;} //Wait for conversion to finish (GODONE CLR)
            ADCval = ADRESH;
            RC4 = 0;
            return;
            }
     

    with

    void doADC(void) {
        LATC4 = 1;
        ADCON0bits.GO = 1; //start conversion (GODONE=1)
        while (ADCON0bits.GO) {;} //Wait for conversion to finish (GODONE CLR)
        ADCval = ADRESH;
        LATC4 = 0;
    }

     
    There's lots of other poor programming practices in the rest of the code, but that's a start.
     
     

    Nearly there...
    #3
    treez
    Starting Member
    • Total Posts : 44
    • Reward points : 0
    • Joined: 2017/04/11 06:49:59
    • Location: 0
    • Status: offline
    Re: Please help debug our PIC16F1503 code (XC8 C Code) 2019/03/24 23:24:19 (permalink)
    0
    Thanks, how did you know it is "LATC4=0" instead of LATCbits.C4=0"?
    Is their a document that tells of this?
    #4
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Please help debug our PIC16F1503 code (XC8 C Code) 2019/03/24 23:30:14 (permalink)
    +1 (1)
    The full name of the bit is
    LATCbits.LATC4
    which follows the standard naming convention XC8 uses for PIC registers.
    Just "LATC4" is a short name that is also included in the device header file for those who prefer that format.
    I used it to match what you used for the PORT register bit.
     
    Personally, I always use the full name, but only once at the top of my C file where I will #define it to something more memorable.
    You can find all the definitions for your device in the ,matching XC8 device specific header file.
    If you have the latest copy of XC8, that is at:
    C:\Program Files (x86)\Microchip\xc8\v2.05\pic\include\pic16f1503.h

     

    Nearly there...
    #5
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Please help debug our PIC16F1503 code (XC8 C Code) 2019/03/24 23:36:12 (permalink)
    0
    The definition of that register starts at line#1736 in the header file
    // Register: LATC
    #define LATC LATC
    extern volatile unsigned char           LATC                __at(0x10E);
    #ifndef _LIB_BUILD
    asm("LATC equ 010Eh");
    #endif
    // bitfield definitions
    typedef union {
        struct {
            unsigned LATC0                  :1;
            unsigned LATC1                  :1;
            unsigned LATC2                  :1;
            unsigned LATC3                  :1;
            unsigned LATC4                  :1;
            unsigned LATC5                  :1;
        };
    } LATCbits_t;
    extern volatile LATCbits_t LATCbits __at(0x10E);

     
    Then the "short" name is also defined down at line#7514
    // LATC<LATC4>
    extern volatile __bit                   LATC4               __at(0x874);    // @ (0x10E * 8 + 4)
    #define                                 LATC4_bit           BANKMASK(LATC), 4


    Nearly there...
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5