PIC16F18856 C  code is OK? - Page 1
Forum section: electronics / general

author: ocset
author_rank: Super Contributor
timestamp: October 24, 2018, 07:43:47 pm
content: 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 okCode://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 4000000uint16_t count;uint16_t count1;uint16_t count2;void main(void) {//Setup portsTRISA = 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 portsLATAbits.LATA0 = 0; //HI switchs ON 24v rail to extLATAbits.LATA1 = 0; //hi switchs off DALI BuckLATAbits.LATA2 = 1; //pwm dimmingLATAbits.LATA3 = 0; //nc//LATAbits.LATA4 = 0; //ADC temperatureLATAbits.LATA5 = 0; //ncLATAbits.LATA6 = 1; //nc//LATAbits.LATA7 = 0; //low means undervoltageLATBbits.LATB0 = 0; //nc//LATBbits.LATB1 = 0; //digi idLATBbits.LATB2 = 0; //dali tx//LATBbits.LATB3 = 0; //dali rx//LATBbits.LATB4 = 0; //input from EH pin of st25dv04k//LATBbits.LATB5 = 0; //input from st25dv04k gpoLATBbits.LATB6 = 0; //icspclkLATBbits.LATB7 = 0; //icspdat//LATCbits.LATC0 = 0; //ADC from IoutLATCbits.LATC1 = 0; //HI TURNS OFF LED DRIVER//LATCbits.LATC2 = 0; //ADC from VoutLATCbits.LATC3 = 0; //OUTPUT TO CLK OF ST25DV04K//LATCbits.LATC4 = 0; //input from SDA of ST25DV04KLATCbits.LATC5 = 0; //NCLATCbits.LATC6 = 0; //OUTPUT TO TEST PINLATCbits.LATC7 = 0; //OUTPUT TO TEST PIN//DO PWM dimming//--------------------------------------------------------here:count1 = 0;while (1) {//Do PWM dimmingcount = 0;while(1) {LATAbits.LATA2 = 0;__delay_us(100);LATAbits.LATA2 = 1;__delay_us(900);count = count + 1;if (count >1000) {break;}}//Do PWM dimmingcount = 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 powerLATAbits.LATA2 = 0;//SWITCH ON DALI BUCK....LATAbits.LATA1 = 0;//DO SOME TRANSMITTING OF DALI//--------------------------------------------------------count1 = 0;while (1) {//Do PWM dimmingcount = 0;while(1) {LATBbits.LATB2 = 0;__delay_us(433);LATBbits.LATB2 = 1;__delay_us(433);count = count + 1;if (count >1000) {break;}}//Do PWM dimmingcount = 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 dimmingcount = 0;while(1) {LATCbits.LATC1 = 0;__delay_ms(433);LATCbits.LATC1 = 1;__delay_ms(433);count = count + 1;if (count >10) {break;}}//Do PWM dimmingcount = 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;}

author: hexreader
author_rank: Frequent Contributor
timestamp: October 24, 2018, 09:02:44 pm
content: Answer already given here:https://www.edaboard.com/showthread.php?380899-Please-help-check-over-PIC16F18856-C-Code

author: Wilksey
author_rank: Super Contributor
timestamp: November 04, 2018, 12:57:42 am
content: OMG that "code" hurt my eyes.

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 04, 2018, 01:42:20 am
content: Quote from: treez on October 24, 2018, 07:43:47 pmThe work atmosphere at your company sounds very inviting...Or maybe the software engineer has accumulated good reasons to be angry?

author: Wilksey
author_rank: Super Contributor
timestamp: November 04, 2018, 10:39:45 pm
content: 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?

author: KL27x
author_rank: Super Contributor
timestamp: November 05, 2018, 09:29:32 am
content: QuoteI 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.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: November 05, 2018, 09:48:20 am
content: 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?