Author Topic: PIC16F18856 C code is OK?  (Read 1763 times)

0 Members and 1 Guest are viewing this topic.

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 1516
  • Country: 00
PIC16F18856 C code is OK?
« on: October 24, 2018, 07:43:47 pm »
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: [Select]
//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;
}
 

Offline hexreader

  • Frequent Contributor
  • **
  • Posts: 269
  • Country: england
Re: PIC16F18856 C code is OK?
« Reply #1 on: October 24, 2018, 09:02:44 pm »
 
The following users thanked this post: ocset

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: PIC16F18856 C code is OK?
« Reply #2 on: November 04, 2018, 12:57:42 am »
OMG that "code" hurt my eyes.
 
The following users thanked this post: ocset

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15340
  • Country: fr
Re: PIC16F18856 C code is OK?
« Reply #3 on: November 04, 2018, 01:42:20 am »
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

The work atmosphere at your company sounds very inviting...

Or maybe the software engineer has accumulated good reasons to be angry? ;D
 
The following users thanked this post: Jacon, ocset

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: PIC16F18856 C code is OK?
« Reply #4 on: November 04, 2018, 10:39:45 pm »
I had a friend that went to work for a company doing LED fixtures for cars in Buckingham, I won't say the name of the company, it began with W though, and he sent me some stuff - he wasn't a very good software guy, and the code he sent me was horrific looking, I wasn't surprised until he told me it was written by someone else and it was production code that he was tasked with updating!

Maybe there is a trend between LED display / lamp companies and dodgy code?  :-DD
 
The following users thanked this post: ocset

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4108
  • Country: us
Re: PIC16F18856 C code is OK?
« Reply #5 on: November 05, 2018, 09:29:32 am »
Quote
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 goo
I take it the software engineer does not send you the working code for some reason? If the code is his and the hardware is yours, he has skin in the game. If he is travelling to a show to meet you, he has skin in the game. So what happened when you asked him to write and send you a program, or at least a hex, for testing the board? He has knowledge of the board and the best way to test it, and he already has code that is configured for that board and for interacting with other components on the board. And he can write code, presumably. He could do this in 1 hr, including the bitching and moaning and coffee break.

I like this guy's job, all the fuckups he has made in the past that you, apparently, have to fix without bothering him.  >:D 
« Last Edit: November 05, 2018, 09:41:28 am by KL27x »
 
The following users thanked this post: ocset

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13979
  • Country: gb
    • Mike's Electric Stuff
Re: PIC16F18856 C code is OK?
« Reply #6 on: November 05, 2018, 09:48:20 am »
So what happened when you plugged the chip into a breadboard and ran your code ?
And if you didn't, why not -  if it's so important to get stuff up & running quickly?

Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 
The following users thanked this post: ocset


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf