Please help check over PIC16F18856 C Code

Status
Not open for further replies.
T

treez

Guest
Hello,
Please could you help see if this C code has got any faults in it? It builds OK, but I wonder if it will work?
It uses the PIC16F18856. It just puts it through its paces. We have a new board arriving on Friday, and we must get it working by Friday to go to a show and be programmed by the software engineer.
The software engineer will kill us if the board isnt good....so we want to get the micro to do some basic stuff like taking pins high and low.....to see if the board's ok.

Code:
//TEST CODE FOR "590P" Board
//This just exercises  the basic features of the board
//so that we can see that the board is good.

//PIC16F18856
//XC8 compiler
//MPLAB X V3.61
//date 27 oct 2017

// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1  // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF      // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF    // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available// Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

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

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

#define  _XTAL_FREQ 4000000

    uint16_t   count;
    uint16_t   count1;
    uint16_t   count2;

void main(void) {
    
    //Setup ports
        TRISA = 0x90;   //
        TRISB = 0x3A;    //
        TRISC = 0x15;   //
        
        ANSELA = 0x10;   //
        ANSELB = 0x00;
        ANSELC = 0x05;
        
        WPUA = 0x00;
        ODCONA = 0x00;
        SLRCONA = 0x00;
        INLVLA = 0x00;      //ST?
        CCDPA = 0x00;
        CCDNA = 0x00;
        
        WPUB = 0x00;
        ODCONB = 0x00;
        SLRCONB = 0x00;
        INLVLB = 0x00;      //ST?
        CCDPB = 0x00;
        CCDNB = 0x00;
        
        WPUC = 0x00;
        ODCONC = 0x00;
        SLRCONC = 0x00;
        INLVLC = 0x00;      //ST?
        CCDPC = 0x00;
        CCDNC = 0x00; 
        
        INTCON = 0x00;
       
        CM1CON0 = 0x00;
        CM1CON1 = 0x00;
        CM2CON0 = 0x00;
        CM2CON1 = 0x00;

        PWM6CON = 0x00;
        PWM7CON = 0x00;
        
        ZCDCON = 0x00;
        
        //Initialize ports
        LATAbits.LATA0 = 0;  //HI switchs ON 24v rail to ext
        LATAbits.LATA1 = 0;  //hi switchs off DALI Buck
        LATAbits.LATA2 = 1;  //pwm dimming
        LATAbits.LATA3 = 0;  //nc
        //LATAbits.LATA4 = 0;  //ADC temperature   
        LATAbits.LATA5 = 0;  //nc   
        LATAbits.LATA6 = 1;  //nc 
        //LATAbits.LATA7 = 0;  //low means undervoltage
                  
        LATBbits.LATB0 = 0;   //nc    
        //LATBbits.LATB1 = 0; //digi id
        LATBbits.LATB2 = 0;   //dali tx    
        //LATBbits.LATB3 = 0;   //dali rx
        //LATBbits.LATB4 = 0;   //input from EH pin of st25dv04k
        //LATBbits.LATB5 = 0;   //input from st25dv04k gpo      
        LATBbits.LATB6 = 0;    //icspclk
        LATBbits.LATB7 = 0;    //icspdat
                
        //LATCbits.LATC0 = 0; //ADC from Iout
        LATCbits.LATC1 = 0;   //HI TURNS OFF LED DRIVER
        //LATCbits.LATC2 = 0;  //ADC from Vout    
        LATCbits.LATC3 = 0;  //OUTPUT TO CLK OF ST25DV04K
        //LATCbits.LATC4 = 0;  //input from SDA of ST25DV04K
        LATCbits.LATC5 = 0;  //NC   
        LATCbits.LATC6 = 0; //OUTPUT TO TEST PIN
        LATCbits.LATC7 = 0; //OUTPUT TO TEST PIN
     
        //DO PWM dimming
        //--------------------------------------------------------
        
        
 here:       
        count1 = 0;
        while (1) {
            
        //Do PWM dimming
        count = 0;
        while(1) {
        LATAbits.LATA2 = 0;
        __delay_us(100);
        LATAbits.LATA2 = 1;
        __delay_us(900);
        count = count + 1;
        if (count >1000) {break;}
        }
        
        //Do PWM dimming
        count = 0;
        while(1) {
        LATAbits.LATA2 = 0;
        __delay_us(300);
        LATAbits.LATA2 = 1;
        __delay_us(700);
        count = count + 1;
        if (count >1000) {break;}
        }
        count1 = count1 + 1;
        if (count1 >1000) {break;}     
}
        //-----------------------------------------------------------
        
        // set leds on full power
        LATAbits.LATA2 = 0;
        
   
        //SWITCH ON DALI BUCK....
        LATAbits.LATA1 = 0;
        
        //DO SOME TRANSMITTING OF DALI
        //--------------------------------------------------------
        count1 = 0;
        while (1) {
            
        //Do PWM dimming
        count = 0;
        while(1) {
        LATBbits.LATB2 = 0;
        __delay_us(433);
        LATBbits.LATB2 = 1;
        __delay_us(433);
        count = count + 1;
        if (count >1000) {break;}
        }
        
        //Do PWM dimming
        count = 0;
        while(1) {
        LATBbits.LATB2 = 0;
        __delay_us(433);
        LATBbits.LATB2 = 1;
        __delay_us(433);
        count = count + 1;
        if (count >1000) {break;}
        }
        count1 = count1 + 1;
        if (count1 >1000) {break;}     
}
        LATBbits.LATB2 = 0; //TURN OFF dali NPN
        //-----------------------------------------------------------
        
        //Now Turn LEDs on and off
        //--------------------------------------------------------
        count1 = 0;
        while (1) {
            
        //Do PWM dimming
        count = 0;
        while(1) {
        LATCbits.LATC1 = 0;
        __delay_ms(433);
        LATCbits.LATC1 = 1;
        __delay_ms(433);
        count = count + 1;
        if (count >10) {break;}
        }
        
        //Do PWM dimming
        count = 0;
        while(1) {
        LATCbits.LATC1 = 0;
        __delay_ms(433);
        LATCbits.LATC1 = 1;
        __delay_ms(433);
        count = count + 1;
        if (count >10) {break;}
        }
        count1 = count1 + 1;
        if (count1 >10) {break;}     
}
        //-----------------------------------------------------------
      
    
        goto here;
  

    return;
}
 

Does not build for me, due to forum mangling the include statement
I guessed at:
#include xc.h
#include stdint.h (brackets removed for forum)


10% and 30% PWM on A2 works OK, but I was unwilling to wait 33 minutes for the count1 loop to finish. Did you really mean to loop 1000 times just for the pin A2 PWM?

Let me try at tidier code - if Forum allows....

Code:
//TEST CODE FOR "590P" Board
//This just exercises  the basic features of the board
//so that we can see that the board is good.

//PIC16F18856
//XC8 compiler
//MPLAB X V3.61
//date 27 oct 2017

// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1  // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF      // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF    // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available// Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)

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

#include <xc.h>         // forum will remove xc.h within braces
#include <stdint.h>    // forum will remove stdint.h within braces

#define  _XTAL_FREQ 4000000

uint16_t   count;
uint16_t   count1;
uint16_t   count2;

void main(void){
    
    //Setup ports
    TRISA = 0x90;   //
    TRISB = 0x3A;    //
    TRISC = 0x15;   //

    ANSELA = 0x10;   //
    ANSELB = 0x00;
    ANSELC = 0x05;

    WPUA = 0x00;
    ODCONA = 0x00;
    SLRCONA = 0x00;
    INLVLA = 0x00;      //ST?
    CCDPA = 0x00;
    CCDNA = 0x00;

    WPUB = 0x00;
    ODCONB = 0x00;
    SLRCONB = 0x00;
    INLVLB = 0x00;      //ST?
    CCDPB = 0x00;
    CCDNB = 0x00;

    WPUC = 0x00;
    ODCONC = 0x00;
    SLRCONC = 0x00;
    INLVLC = 0x00;      //ST?
    CCDPC = 0x00;
    CCDNC = 0x00; 

    INTCON = 0x00;

    CM1CON0 = 0x00;
    CM1CON1 = 0x00;
    CM2CON0 = 0x00;
    CM2CON1 = 0x00;

    PWM6CON = 0x00;
    PWM7CON = 0x00;

    ZCDCON = 0x00;

    // Initialise ports
    LATAbits.LATA0 = 0;                 // HI switches ON 24v rail to ext
    LATAbits.LATA1 = 0;                 // hi switches off DALI Buck
    LATAbits.LATA2 = 1;                 // PWM dimming
    LATAbits.LATA3 = 0;                 // nc
    //LATAbits.LATA4 = 0;                 // ADC temperature   
    LATAbits.LATA5 = 0;                 // nc   
    LATAbits.LATA6 = 1;                 // nc 
    //LATAbits.LATA7 = 0;                 // low means undervoltage

    LATBbits.LATB0 = 0;                 // nc    
    //LATBbits.LATB1 = 0;               // digi id
    LATBbits.LATB2 = 0;                 // dali tx    
    //LATBbits.LATB3 = 0;                 // dali rx
    //LATBbits.LATB4 = 0;                 // input from EH pin of st25dv04k
    //LATBbits.LATB5 = 0;                 // input from st25dv04k gpo      
    LATBbits.LATB6 = 0;                 // icspclk
    LATBbits.LATB7 = 0;                 // icspdat

    //LATCbits.LATC0 = 0;               // ADC from Iout
    LATCbits.LATC1 = 0;                 // HI TURNS OFF LED DRIVER
    //LATCbits.LATC2 = 0;               // ADC from Vout    
    LATCbits.LATC3 = 0;                 // OUTPUT TO CLK OF ST25DV04K
    //LATCbits.LATC4 = 0;               // input from SDA of ST25DV04K
    LATCbits.LATC5 = 0;                 // NC   
    LATCbits.LATC6 = 0;                 // OUTPUT TO TEST PIN
    LATCbits.LATC7 = 0;                 // OUTPUT TO TEST PIN

    //DO PWM dimming
    //--------------------------------------------------------

    // main loop    
    while(1){       
        count1 = 0;
        while (1) {

            // one second of 10% PWM dimming pin A2
            count = 0;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(100);
                LATAbits.LATA2 = 1;
                __delay_us(900);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }

            // one second of 30% PWM dimming pin A2
            count = 0;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(300);
                LATAbits.LATA2 = 1;
                __delay_us(700);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }
            count1 = count1 + 1;
            if (count1 > 1000) {break;}                  // repeat 1000, 30% dim for 1000 loops (33 minutes !!)
        }

        //-----------------------------------------------------------

        // set LEDs on full power
        LATAbits.LATA2 = 0;


        //SWITCH ON DALI BUCK....
        LATAbits.LATA1 = 0;

        //DO SOME TRANSMITTING OF DALI
        //--------------------------------------------------------
        count1 = 0;
        while (1) {

            //Do PWM dimming
            count = 0;
            while(1) {
                LATBbits.LATB2 = 0;
                __delay_us(433);
                LATBbits.LATB2 = 1;
                __delay_us(433);
                count = count + 1;
                if (count > 1000) {break;}
            }

            //Do PWM dimming
            count = 0;
            while(1) {
                LATBbits.LATB2 = 0;
                __delay_us(433);
                LATBbits.LATB2 = 1;
                __delay_us(433);
                count = count + 1;
                if (count >1000) {break;}
            }
            count1 = count1 + 1;
            if (count1 > 1000) {break;}     
        }
        
        LATBbits.LATB2 = 0; //TURN OFF dali NPN
        //-----------------------------------------------------------

        //Now Turn LEDs on and off
        //--------------------------------------------------------
        count1 = 0;
        while (1) {

            //Do PWM dimming
            count = 0;
            while(1) {
                LATCbits.LATC1 = 0;
                __delay_ms(433);
                LATCbits.LATC1 = 1;
                __delay_ms(433);
                count = count + 1;
                if (count > 10) {break;}
            }

            //Do PWM dimming
            count = 0;
            while(1) {
                LATCbits.LATC1 = 0;
                __delay_ms(433);
                LATCbits.LATC1 = 1;
                __delay_ms(433);
                count = count + 1;
                if (count > 10) {break;}
            }
            count1 = count1 + 1;
            if (count1 > 10) {break;}     
        }
        //-----------------------------------------------------------
      
    
    }
  
}

- - - Updated - - -

So after reducing the A2 loop count to something more reasonable (>3 gives about 30 seconds to finish A2 test)...

B2 pin gives a steady square wave - not sure if that was the intention or not. Again the loop count is very large

C1 pin toggles very slowly. Did you maybe use delay_ms function when you really wanted delay_us ?
 
Last edited:
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
You have posted code dated almost a year ago (date 27 oct 2017) and want ti get a review by Saturday.

The code as posted does not build without errors.

Useful comments are nonexistent.

There are only tests for output pins.

In short the code tests less that the minimum functionality suggested by what is vaguely hinted at in the comments.

It is lucky you do not work where I do. If I submitted this code for review my peers would suggest that I seek employment with a company where my talents are better matched to the task requirements. Those tasks would compel using the phrase "You want fries with that?" quite often.
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
You seem to have selected 1MHz internal clock, but set xtal frequency to 4MHz. This will make your delay functions wildly innacurate.

Does your board use internal oscillator, or external crystal / clock?

If the answer is crystal, then that is probably the most important thing to test.

- - - Updated - - -

For what it is worth (not much) here is some code that might be something close to what I guess you maybe want.

It is still terrible code, but gives a few seconds exercise of three pins in turn without having to wait days for the last loop to complete.

Reset pin is disabled - is that what you really want?
Code:
//TEST CODE FOR "590P" Board
//This just exercises  the basic features of the board
//so that we can see that the board is good.

//PIC16F18856
//XC8 compiler
//MPLAB X V3.61
//date 27 oct 2017

// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF                // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1              // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF               // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF                  // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF                  // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF                  // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON                   // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF                // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON                   // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO                    // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF                    // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF                // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF                 // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31           // WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF                   // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7            // WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC                  // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF                    // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available        // Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF                    // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF                     // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF                    // DataNVM code protection bit (Data EEPROM code protection disabled)

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

#include <xc.h>                             // forum will remove xc.h within braces :(
#include <stdint.h>                         // forum will remove stdint.h within braces :(

#define  _XTAL_FREQ 1000000

uint16_t   count;
uint16_t   count1;
uint16_t   count2;

void main(void){
    
    //Setup ports
    TRISA = 0x90;   //
    TRISB = 0x3A;    //
    TRISC = 0x15;   //

    ANSELA = 0x10;   //
    ANSELB = 0x00;
    ANSELC = 0x05;

    WPUA = 0x00;
    ODCONA = 0x00;
    SLRCONA = 0x00;
    INLVLA = 0x00;                      // ST?
    CCDPA = 0x00;
    CCDNA = 0x00;

    WPUB = 0x00;
    ODCONB = 0x00;
    SLRCONB = 0x00;
    INLVLB = 0x00;                      // ST?
    CCDPB = 0x00;
    CCDNB = 0x00;

    WPUC = 0x00;
    ODCONC = 0x00;
    SLRCONC = 0x00;
    INLVLC = 0x00;                      // ST?
    CCDPC = 0x00;
    CCDNC = 0x00; 

    INTCON = 0x00;

    CM1CON0 = 0x00;
    CM1CON1 = 0x00;
    CM2CON0 = 0x00;
    CM2CON1 = 0x00;

    PWM6CON = 0x00;
    PWM7CON = 0x00;

    ZCDCON = 0x00;

    // Initialise ports
    LATAbits.LATA0 = 0;                 // HI switches ON 24v rail to ext
    LATAbits.LATA1 = 0;                 // hi switches off DALI Buck
    LATAbits.LATA2 = 1;                 // PWM dimming
    LATAbits.LATA3 = 0;                 // nc
    //LATAbits.LATA4 = 0;                 // ADC temperature   
    LATAbits.LATA5 = 0;                 // nc   
    LATAbits.LATA6 = 1;                 // nc 
    //LATAbits.LATA7 = 0;                 // low means undervoltage

    LATBbits.LATB0 = 0;                 // nc    
    //LATBbits.LATB1 = 0;               // digi id
    LATBbits.LATB2 = 0;                 // dali tx    
    //LATBbits.LATB3 = 0;                 // dali rx
    //LATBbits.LATB4 = 0;                 // input from EH pin of st25dv04k
    //LATBbits.LATB5 = 0;                 // input from st25dv04k gpo      
    LATBbits.LATB6 = 0;                 // icspclk
    LATBbits.LATB7 = 0;                 // icspdat

    //LATCbits.LATC0 = 0;               // ADC from Iout
    LATCbits.LATC1 = 0;                 // HI TURNS OFF LED DRIVER
    //LATCbits.LATC2 = 0;               // ADC from Vout    
    LATCbits.LATC3 = 0;                 // OUTPUT TO CLK OF ST25DV04K
    //LATCbits.LATC4 = 0;               // input from SDA of ST25DV04K
    LATCbits.LATC5 = 0;                 // NC   
    LATCbits.LATC6 = 0;                 // OUTPUT TO TEST PIN
    LATCbits.LATC7 = 0;                 // OUTPUT TO TEST PIN

    //DO PWM dimming
    //--------------------------------------------------------

    // main loop    
    while(1){       
        count1 = 0;
        while (1) {

            // one second of 10% PWM dimming pin A2
            count = 0;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(100);
                LATAbits.LATA2 = 1;
                __delay_us(900);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }

            // one second of 30% PWM dimming pin A2
            count = 0;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(300);
                LATAbits.LATA2 = 1;
                __delay_us(700);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }
            count1 = count1 + 1;
            if (count1 > 3) {break;}                  // repeat 1000, 30% dim for 1000 loops (33 minutes !!)
        }

        //-----------------------------------------------------------

        // set LEDs on full power
        LATAbits.LATA2 = 0;


        //SWITCH ON DALI BUCK....
        LATAbits.LATA1 = 0;

        //DO SOME TRANSMITTING OF DALI
        //--------------------------------------------------------
        // square wave on pin B2
        count = 1;
        while(1) {
            LATBbits.LATB2 = 0;
            __delay_us(433);
            LATBbits.LATB2 = 1;
            __delay_us(433);
            count = count + 1;
            if (count > 5000) {break;}
        }
     
        LATBbits.LATB2 = 0; //TURN OFF dali NPN
        //-----------------------------------------------------------

        //Now Turn LEDs on and off
        //--------------------------------------------------------
        //Flash LED 30 times
        count = 1;
        while(1) {
            LATCbits.LATC1 = 0;
            __delay_ms(433);
            LATCbits.LATC1 = 1;
            __delay_ms(433);
            count = count + 1;
            if (count > 30) {break;}
        }
        //-----------------------------------------------------------
      
    
    }
  
}
You make pin A1 low in initialisation then take it low again later. Not sure what your real intention is, so I left it alone.
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
You seem to have selected 1MHz internal clock, but set xtal frequency to 4MHz. This will make your delay functions wildly innacurate.

Does your board use internal oscillator, or external crystal / clock?
Thanks, we are using the internal oscillator, and wish to set it to 4MHz. That RSTOSC thing in CONFIG, ...we cant work out what to set it to to get 4MHz, do you know?
 

Do you think you could possibly check over the attached, which also has ADC readings in the code?
It builds fine in MPLAB X v3.61
Its in XC8 C

Code:
//TEST CODE FOR "590P" Board
//This just exercises the basic features of the board
//so that we can see that the board is good.

//PIC16F18856
//XC8 compiler
//MPLAB X V3.61
//date 25 oct 2018

// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available// Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection disabled)

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

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

#define _XTAL_FREQ 4000000

    uint16_t count;
    uint16_t count1;
    uint16_t count2;
    uint16_t temp;

void main(void) {
    
    //Setup ports
        TRISA = 0x90; //
        TRISB = 0x3A; //
        TRISC = 0x15; //
        
        ANSELA = 0x10; //
        ANSELB = 0x00;
        ANSELC = 0x05;
        
        ADCON0 = 0x00;  //ADON=0,LEFTJUST,ADGO = 0 WHEN COMPLETE
        ADCLK = 0x02;    //FOSC/6...ok for 4MHz and 1MHz osc
        ADREF = 0x00;    //vss and vdd
        PIE1 = 0x00;   //ADIE interrupt disabled
        ADACQ = 0;   //Software must wait the acquisition time.
        //ADPCH = 0x04;   A4 = TEMP
        //ADPCH = 0x10;   C0 = CURRENT
        //ADPCH = 0x12;   C2 = VOLTAGE
        
        WPUA = 0x00;
        ODCONA = 0x00;
        SLRCONA = 0x00;
        INLVLA = 0x00; //ST?
        CCDPA = 0x00;
        CCDNA = 0x00;
        
        WPUB = 0x00;
        ODCONB = 0x00;
        SLRCONB = 0x00;
        INLVLB = 0x00; //ST?
        CCDPB = 0x00;
        CCDNB = 0x00;
        
        WPUC = 0x00;
        ODCONC = 0x00;
        SLRCONC = 0x00;
        INLVLC = 0x00; //ST?
        CCDPC = 0x00;
        CCDNC = 0x00; 
        
        INTCON = 0x00;
       
        CM1CON0 = 0x00;
        CM1CON1 = 0x00;
        CM2CON0 = 0x00;
        CM2CON1 = 0x00;

        PWM6CON = 0x00;
        PWM7CON = 0x00;
        
        ZCDCON = 0x00;
        
        //Initialize ports
        LATAbits.LATA0 = 0; //HI switchs ON 24v rail to ext
        LATAbits.LATA1 = 0; //hi switchs off DALI Buck
        LATAbits.LATA2 = 1; //pwm dimming
        LATAbits.LATA3 = 0; //nc
        //LATAbits.LATA4 = 0; //ADC temperature 
        LATAbits.LATA5 = 0; //nc 
        LATAbits.LATA6 = 1; //nc 
        //LATAbits.LATA7 = 0; //low means undervoltage
                  
        LATBbits.LATB0 = 0; //nc 
        //LATBbits.LATB1 = 0; //digi id
        LATBbits.LATB2 = 0; //dali tx 
        //LATBbits.LATB3 = 0; //dali rx
        //LATBbits.LATB4 = 0; //input from EH pin of st25dv04k
        //LATBbits.LATB5 = 0; //input from st25dv04k gpo 
        LATBbits.LATB6 = 0; //icspclk
        LATBbits.LATB7 = 0; //icspdat
                
        //LATCbits.LATC0 = 0; //ADC from Iout
        LATCbits.LATC1 = 0; //HI TURNS OFF LED DRIVER
        //LATCbits.LATC2 = 0; //ADC from Vout 
        LATCbits.LATC3 = 0; //OUTPUT TO CLK OF ST25DV04K
        //LATCbits.LATC4 = 0; //input from SDA of ST25DV04K
        LATCbits.LATC5 = 0; //NC 
        LATCbits.LATC6 = 0; //OUTPUT TO TEST PIN
        LATCbits.LATC7 = 0; //OUTPUT TO TEST PIN
     
        //DO PWM dimming   (A2 = PWM DIMMING))
        //--------------------------------------------------------
        
        
 here: 
        count1 = 0;
        while (1) {
            
        //Do PWM dimming
        count = 0;
        while(1) {
        LATAbits.LATA2 = 0;
        __delay_us(100);
        LATAbits.LATA2 = 1;
        __delay_us(900);
        count = count + 1;
        if (count > 1000) {break;}
        }
        
        //Do PWM dimming
        count = 0;
        while(1) {
        LATAbits.LATA2 = 0;
        __delay_us(300);
        LATAbits.LATA2 = 1;
        __delay_us(700);
        count = count + 1;
        if (count >1000) {break;}
        }
        count1 = count1 + 1;
        if (count1 >1000) {break;} 
}
        //-----------------------------------------------------------
        
        // set leds on full power
        LATAbits.LATA2 = 0;
        
        //SWITCH ON DALI BUCK....
        LATAbits.LATA1 = 0;
        
        //DO SOME TRANSMITTING OF DALI  (B2 = DALI TX))
        //--------------------------------------------------------
        count1 = 0;
        while (1) {
            
 
        count = 0;
        while(1) {
        LATBbits.LATB2 = 0;
        __delay_us(433);
        LATBbits.LATB2 = 1;
        __delay_us(433);
        count = count + 1;
        if (count >1000) {break;}
        }
        
        count = 0;
        while(1) {
        LATBbits.LATB2 = 0;
        __delay_us(433);
        LATBbits.LATB2 = 1;
        __delay_us(433);
        count = count + 1;
        if (count >1000) {break;}
        }
        count1 = count1 + 1;
        if (count1 >100) {break;} 
}
        LATBbits.LATB2 = 0; //TURN OFF dali NPN
        //-----------------------------------------------------------
        
        //Now Flash LEDs on and off (C1)
        //--------------------------------------------------------
        count1 = 0;
        while (1) {
            
        count = 0;
        while(1) {
        LATCbits.LATC1 = 0;
        __delay_ms(433);
        LATCbits.LATC1 = 1;
        __delay_ms(433);
        count = count + 1;
        if (count >10) {break;}
        }
        
        count = 0;
        while(1) {
        LATCbits.LATC1 = 0;
        __delay_ms(433);
        LATCbits.LATC1 = 1;
        __delay_ms(433);
        count = count + 1;
        if (count >10) {break;}
        }
        count1 = count1 + 1;
        if (count1 >10) {break;} 
}
        LATCbits.LATC1 = 0;
        //-----------------------------------------------------------
      
        
        //Check the undervoltage pin. (A7)
        //C1 = turn off/ON led driver
        if (PORTAbits.RA7 == 0) {LATCbits.LATC1 = 1;}
        if (PORTAbits.RA7 == 1) {LATCbits.LATC1 = 0;}
        
        
        //Check 24V will switch ON/OFF (A0 High switchs ON to external)
        //--------------------------------------------------------
        count1 = 0;
        while (1) {
            
        count = 0;
        while(1) {
        LATAbits.LATA0 = 0;
        __delay_ms(433);
        LATAbits.LATA0 = 1;
        __delay_ms(433);
        count = count + 1;
        if (count >10) {break;}
        }
        
        count = 0;
        while(1) {
        LATAbits.LATA0 = 0;
        __delay_ms(433);
        LATAbits.LATA0 = 1;
        __delay_ms(433);
        count = count + 1;
        if (count >10) {break;}
        }
        count1 = count1 + 1;
        if (count1 >10) {break;} 
}
        LATAbits.LATA0 = 0;
        //-----------------------------------------------------------
        
        
        //Read temperature via ADC ANA4
        //62 bits is 0.8v = 30degc
        ADON = 1;   //Turn on ADC
        ADRESH = 0x00;  //Clear the register
        ADPCH = 0x04;  //select temperature channel
        ADGO == 1;  //Start the conversion
        while (1) {
        if (ADGO == 0) {break;}
        }
        temp = ADRESH;
        if(temp > 0x3E) {LATCbits.LATC1 = 0;}  //turn off leds if temp >30degc
here1:  if(temp > 0x3E) {goto here1;}        
       
        goto here;


    return;
}
 

Thanks, we are using the internal oscillator, and wish to set it to 4MHz. That RSTOSC thing in CONFIG, ...we cant work out what to set it to to get 4MHz, do you know?
OSCFRQ register selects frequency of internal oscillator:
Code:
#define  _XTAL_FREQ 4000000

uint16_t   count;
uint16_t   count1;
uint16_t   count2;

void main(void){
    
    OSCFRQ = 0x02;                          // change oscillator to 4 MHz
    
    //Setup ports

- - - Updated - - -

Hmmm.... that does not seem to work correctly - I can bodge it with OSCFRQ=4, but I will have to look harder.

- - - Updated - - -

OK - think I have it now. Also need CSWEN = ON, OSCON1 and OSCEN registers set correctly.

Like this:

Code:
//TEST CODE FOR "590P" Board
//This just exercises  the basic features of the board
//so that we can see that the board is good.

//PIC16F18856
//XC8 compiler
//MPLAB X V3.61
//date 27 oct 2017

// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements

// CONFIG1
#pragma config FEXTOSC = OFF                // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1              // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF               // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON                   // Clock Switch Enable bit (The NOSC and NDIV bits can be changed by user software)
#pragma config FCMEN = OFF                  // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF                  // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON                   // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF                // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON                   // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO                    // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF                    // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF                // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF                 // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31           // WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF                   // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7            // WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC                  // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF                    // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available        // Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF                    // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF                     // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF                    // DataNVM code protection bit (Data EEPROM code protection disabled)

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

#include <xc.h>                             // forum will remove xc.h within braces :(
#include <stdint.h>                         // forum will remove stdint.h within braces :(

#define  _XTAL_FREQ 4000000

uint16_t   count;
uint16_t   count1;
uint16_t   count2;

void main(void){
    
    OSCEN = 0x20;                           // internal oscillator explicit selection
    OSCFRQ = 0x02;                          // change internal oscillator to 4 MHz
    OSCCON1 = 0x60;                         // select internal oscillator, divider = 1
    
    //Setup ports
    TRISA = 0x90;   //
    TRISB = 0x3A;    //
    TRISC = 0x15;   //

    ANSELA = 0x10;   //
    ANSELB = 0x00;
    ANSELC = 0x05;

    WPUA = 0x00;
    ODCONA = 0x00;
    SLRCONA = 0x00;
    INLVLA = 0x00;                      // ST?
    CCDPA = 0x00;
    CCDNA = 0x00;

    WPUB = 0x00;
    ODCONB = 0x00;
    SLRCONB = 0x00;
    INLVLB = 0x00;                      // ST?
    CCDPB = 0x00;
    CCDNB = 0x00;

    WPUC = 0x00;
    ODCONC = 0x00;
    SLRCONC = 0x00;
    INLVLC = 0x00;                      // ST?
    CCDPC = 0x00;
    CCDNC = 0x00; 

    INTCON = 0x00;

    CM1CON0 = 0x00;
    CM1CON1 = 0x00;
    CM2CON0 = 0x00;
    CM2CON1 = 0x00;

    PWM6CON = 0x00;
    PWM7CON = 0x00;

    ZCDCON = 0x00;

    // Initialise ports
    LATAbits.LATA0 = 0;                 // HI switches ON 24v rail to ext
    LATAbits.LATA1 = 0;                 // hi switches off DALI Buck
    LATAbits.LATA2 = 1;                 // PWM dimming
    LATAbits.LATA3 = 0;                 // nc
    //LATAbits.LATA4 = 0;                 // ADC temperature   
    LATAbits.LATA5 = 0;                 // nc   
    LATAbits.LATA6 = 1;                 // nc 
    //LATAbits.LATA7 = 0;                 // low means undervoltage

    LATBbits.LATB0 = 0;                 // nc    
    //LATBbits.LATB1 = 0;               // digi id
    LATBbits.LATB2 = 0;                 // dali tx    
    //LATBbits.LATB3 = 0;                 // dali rx
    //LATBbits.LATB4 = 0;                 // input from EH pin of st25dv04k
    //LATBbits.LATB5 = 0;                 // input from st25dv04k gpo      
    LATBbits.LATB6 = 0;                 // icspclk
    LATBbits.LATB7 = 0;                 // icspdat

    //LATCbits.LATC0 = 0;               // ADC from Iout
    LATCbits.LATC1 = 0;                 // HI TURNS OFF LED DRIVER
    //LATCbits.LATC2 = 0;               // ADC from Vout    
    LATCbits.LATC3 = 0;                 // OUTPUT TO CLK OF ST25DV04K
    //LATCbits.LATC4 = 0;               // input from SDA of ST25DV04K
    LATCbits.LATC5 = 0;                 // NC   
    LATCbits.LATC6 = 0;                 // OUTPUT TO TEST PIN
    LATCbits.LATC7 = 0;                 // OUTPUT TO TEST PIN

    //DO PWM dimming
    //--------------------------------------------------------

    // main loop    
    while(1){       
        count1 = 0;
        while (1) {

            // one second of 10% PWM dimming pin A2
            count = 0;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(100);
                LATAbits.LATA2 = 1;
                __delay_us(900);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }

            // one second of 30% PWM dimming pin A2
            count = 0;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(300);
                LATAbits.LATA2 = 1;
                __delay_us(700);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }
            count1 = count1 + 1;
            if (count1 > 3) {break;}                  // repeat 1000, 30% dim for 1000 loops (33 minutes !!)
        }

        //-----------------------------------------------------------

        // set LEDs on full power
        LATAbits.LATA2 = 0;


        //SWITCH ON DALI BUCK....
        LATAbits.LATA1 = 0;

        //DO SOME TRANSMITTING OF DALI
        //--------------------------------------------------------
        // square wave on pin B2
        count = 1;
        while(1) {
            LATBbits.LATB2 = 0;
            __delay_us(433);
            LATBbits.LATB2 = 1;
            __delay_us(433);
            count = count + 1;
            if (count > 5000) {break;}
        }
     
        LATBbits.LATB2 = 0; //TURN OFF dali NPN
        //-----------------------------------------------------------

        //Now Turn LEDs on and off
        //--------------------------------------------------------
        //Flash LED 30 times
        count = 1;
        while(1) {
            LATCbits.LATC1 = 0;
            __delay_ms(433);
            LATCbits.LATC1 = 1;
            __delay_ms(433);
            count = count + 1;
            if (count > 30) {break;}
        }
        //-----------------------------------------------------------
      
    
    }
  
}
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
As for the code with ADC added - seems we are back to square 1 with waiting 2 hours for first loop to complete. Don't think ADC code works even after the wait

If I get desperately bored I may investigate fixes, but no promises.
 

Thanks, i wonder if i omitted to set the ADC pre-charge and acqusition time......though i thought it would just not do the conversion until after an inbuilt pre-charge delay?
 

Well it seems I did get really bored...

I simplified ADC set-up and ADC now seems to work.

No idea if this comes anywhere close to what you want, but here is what I came up with:
Code:
// TEST CODE FOR "590P" Board
// This just exercises the basic features of the board
// so that we can see that the board is good.

// PIC16F18856
// XC8 compiler
// MPLAB X V3.61
// date 25 Oct 2018

// PIC16F18856 Configuration Bit Settings
//
// 'C' source line config statements

// updated by amateur coder who accepts no responsibility for anything, anywhere, ever
// don't blame me for the abysmal code, I found it on a forum
// if you think that this code is a steaming pile of poo, you should have seen it before!

// CONFIG1
#pragma config FEXTOSC = OFF                // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1              // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF               // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON                   // Clock Switch Enable bit (The NOSC and NDIV bits can be changed by user software)
#pragma config FCMEN = OFF                  // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF                  // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = ON                   // Power-up Timer Enable bit (PWRT enabled)
#pragma config LPBOREN = OFF                // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON                   // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO                    // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF                    // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF                // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = OFF                 // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31           // WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF                   // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7            // WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC                  // WDT input clock selector (Software Control)

// CONFIG4
#pragma config WRT = OFF                    // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = not_available        // Scanner Enable bit (Scanner module is not available for use)
#pragma config LVP = OFF                    // Low Voltage Programming Enable bit (High Voltage on MCLR/Vpp must be used for programming)

// CONFIG5
#pragma config CP = OFF                     // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF                    // DataNVM code protection bit (Data EEPROM code protection disabled)

// #pragma config statements should precede project file includes.

#include <xc.h>                             // forum will remove xc.h within braces :(
#include <stdint.h>                         // forum will remove stdint.h within braces :(

#define  _XTAL_FREQ 4000000

// global variables
uint16_t   count;                           // just a loop counter
uint16_t   count1;
uint8_t   temp;                             // temporary store for ADC high byte

void main(void){
    
    OSCEN = 0x20;                           // internal oscillator explicit selection
    OSCFRQ = 0x02;                          // change internal oscillator to 4 MHz
    OSCCON1 = 0x60;                         // select internal oscillator, divider = 1
    
    //Setup ports
    TRISA = 0x90;                           // RA4 temperature input, RA7 ?
    TRISB = 0x3A;                           //
    TRISC = 0x15;                           //

    ANSELA = 0x10;                          // RA4 analogue selection for temperature input
    ANSELB = 0x00;
    ANSELC = 0x05;

    WPUA = 0x00;
    ODCONA = 0x00;
    SLRCONA = 0x00;
    INLVLA = 0x00;                      // ST?
    CCDPA = 0x00;
    CCDNA = 0x00;

    WPUB = 0x00;
    ODCONB = 0x00;
    SLRCONB = 0x00;
    INLVLB = 0x00;                      // ST?
    CCDPB = 0x00;
    CCDNB = 0x00;

    WPUC = 0x00;
    ODCONC = 0x00;
    SLRCONC = 0x00;
    INLVLC = 0x00;                      // ST?
    CCDPC = 0x00;
    CCDNC = 0x00; 

    INTCON = 0x00;

    CM1CON0 = 0x00;
    CM1CON1 = 0x00;
    CM2CON0 = 0x00;
    CM2CON1 = 0x00;

    PWM6CON = 0x00;
    PWM7CON = 0x00;

    ZCDCON = 0x00;

    // Initialise ports
    LATAbits.LATA0 = 0;                 // HI switches ON 24v rail to ext
    LATAbits.LATA1 = 0;                 // hi switches off DALI Buck
    LATAbits.LATA2 = 1;                 // PWM dimming
    LATAbits.LATA3 = 0;                 // nc
    //LATAbits.LATA4 = 0;                 // ADC temperature   
    LATAbits.LATA5 = 0;                 // nc   
    LATAbits.LATA6 = 1;                 // nc 
    //LATAbits.LATA7 = 0;                 // low means undervoltage

    LATBbits.LATB0 = 0;                 // nc    
    //LATBbits.LATB1 = 0;               // digi id
    LATBbits.LATB2 = 0;                 // dali tx    
    //LATBbits.LATB3 = 0;                 // dali rx
    //LATBbits.LATB4 = 0;                 // input from EH pin of st25dv04k
    //LATBbits.LATB5 = 0;                 // input from st25dv04k gpo      
    LATBbits.LATB6 = 0;                 // icspclk
    LATBbits.LATB7 = 0;                 // icspdat

    //LATCbits.LATC0 = 0;               // ADC from Iout
    LATCbits.LATC1 = 0;                 // HI TURNS OFF LED DRIVER
    //LATCbits.LATC2 = 0;               // ADC from Vout    
    LATCbits.LATC3 = 0;                 // OUTPUT TO CLK OF ST25DV04K
    //LATCbits.LATC4 = 0;               // input from SDA of ST25DV04K
    LATCbits.LATC5 = 0;                 // NC   
    LATCbits.LATC6 = 0;                 // OUTPUT TO TEST PIN
    LATCbits.LATC7 = 0;                 // OUTPUT TO TEST PIN

    //DO PWM dimming
    //--------------------------------------------------------

    // main loop    
    while(1){       
        count1 = 0;
        while (1) {

            // one second of 10% PWM dimming pin A2
            count = 1;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(100);
                LATAbits.LATA2 = 1;
                __delay_us(900);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }

            // one second of 30% PWM dimming pin A2
            count = 1;
            while(1) {
                LATAbits.LATA2 = 0;
                __delay_us(300);
                LATAbits.LATA2 = 1;
                __delay_us(700);
                count = count + 1;
                if (count > 1000) {break;}               // quit after 1000 loops, (1 second) 
            }
            count1 = count1 + 1;
            if (count1 > 1) {break;}                     // repeat 1000, 30% dim for 1000 loops (33 minutes !!)
        }

        //-----------------------------------------------------------

        // set LEDs on full power
        LATAbits.LATA2 = 0;


        //SWITCH ON DALI BUCK....
        LATAbits.LATA1 = 0;

        //DO SOME TRANSMITTING OF DALI
        //--------------------------------------------------------
        // square wave on pin B2
        count = 1;
        while(1) {
            LATBbits.LATB2 = 0;
            __delay_us(433);
            LATBbits.LATB2 = 1;
            __delay_us(433);
            count = count + 1;
            if (count > 1000) {break;}
        }
     
        LATBbits.LATB2 = 0; //TURN OFF dali NPN
        //-----------------------------------------------------------

        //Now Turn LEDs on and off
        //--------------------------------------------------------
        //Flash LED 10 times
        count = 1;
        while(1) {
            LATCbits.LATC1 = 0;
            __delay_ms(433);
            LATCbits.LATC1 = 1;
            __delay_ms(433);
            count = count + 1;
            if (count > 2) {break;}
        }
        //-----------------------------------------------------------
      
        // Check the under-voltage pin. (A7)
        // C1 = turn off/ON led driver
        if (PORTAbits.RA7 == 0) {LATCbits.LATC1 = 1;}
        if (PORTAbits.RA7 == 1) {LATCbits.LATC1 = 0;}
        
        
        // Check 24V will switch ON/OFF (A0 High switches ON to external)
        //--------------------------------------------------------
        // toggle A0 for 10 cycles
        count = 1;
        while (1) {            
            LATAbits.LATA0 = 0;
            __delay_ms(433);
            LATAbits.LATA0 = 1;
            __delay_ms(433);
            count = count + 1;
            if (count > 2) {break;}
        }
         
        LATAbits.LATA0 = 0;
        //-----------------------------------------------------------
        
        
        // Read temperature via ADC ANA4 on pin RA4
        // 62 count is 0.8v = 30 deg C
        ADCON0 = 0x90;                                                          // ADC on, FRC clock
        ADPCH = 4;                                                              // channel select ANA4 on RA4
        __delay_ms(100);                                                        // give plenty of time for ADC to settle and for aquisition time

        ADGO = 1;                                                               // Turn on ADC
        while (ADGO) ;                                                          // wait for conversion to complete
        
        temp = ADRESH;
        if(temp > 0x3E){
            LATCbits.LATC1 = 0;                                                 // turn off LEDs if temp > 30 deg c
        }
        else{
            LATCbits.LATC1 = 1;                                                 // else turn on LEDs                
        }
       
    }
}
 
Last edited:
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
You are not using the built-in PWM peripheral, which sounds a waste of a powerful resource already available on the microcontroller. Moreover, a code having a lot of delays as you did, would not be able to do anything else. Think of having a look at examples with the usage of this module, there are tools on the Web which help us to make code with the necessary settings for each peripheral; ok, those tools in general do not cover the entire uC family, but its registers are quite often the same.
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Thanks Hexreader,
Much appreciated, your code looks great.
I have just amended my code to what you kindly put.
I noted that you deleted the following lines

Are you sure it will be OK to avoid setting these registers?
I need the result to be left justified because i am only reading the result as the top 8 bits. (accuracy doesnt matter too much)
Also, the ADCLK, i thought, is important to set as per the datasheet?
 

Wish I knew the answer to those questions, but I am just an amateur coder and just mess around until stuff seems to work. My code did assume power-up values for many ADC registers.

Don't have the patience to go into the science of it all.
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…