XC8 C code to make RC5 of PIC16F1947 give out a square wave - Page 1
Forum section: electronics / general

author: ocset
author_rank: Super Contributor
timestamp: September 28, 2017, 11:16:43 am
content: Hello,The following code is simply meant to produce a square wave output on RC5 of PIC16F1947.This is so we can test if the output of the opto that RC5 drives into has a quick enough rise/fall time.It is written in XC8 C and MPLAB X.Do you agree that i did not need to disable the Alternative Pin Function register (APFCON)?Also, have i set up the OPTION_REG up properly so that the __delay_ms() function works properly?Also, do you think ive set up the other registers correctly?It builds OK but we cant test it till we get the boards in on Monday, but we need it ready.Code://This code simply puts out a square wave on RC5//Period = 3ms//V3 of 2IRA//PIC16F1947-I/PT//XC8 compiler//MPLAB X V3.61#include <xc.h>#include <stdint.h>#define _XTAL_FREQ 4000000uint8_t count;void main(void) {//setup portsTRISA = 0x83; //light and temp analog inputs, DS2411R inputTRISB = 0x09; //rb0=rx;rb3=33kpullup;TRISC = 0x00; //TRISD = 0x00;TRISE = 0x00;TRISG = 0x00;//setup registersANSELA = 0x00;ANSELE = 0x00;ANSELF = 0x00;ANSELG = 0x00;ADCON0 = 0x00;ADCON1 = 0x00;CM1CON1 = 0x00;CM2CON1 = 0x00;DACCON0 = 0x00;OPTION_REG = 0x87; //psa? ps=111?; pullups disabledINTCON = 0x00;WPUB = 0x00; //disable all pullups on port BWPUG = 0x00;FVRCON = 0x00;//initialize outputs//LATAbits.LATA0 = 0; input LIGHT//LATAbits.LATA1 = 0; input TEMPLATAbits.LATA2 = 0;LATAbits.LATA3 = 0;LATAbits.LATA4 = 0;LATAbits.LATA5 = 0;LATAbits.LATA6 = 0;//LATAbits.LATA7 = 0; input DS32411R//LATBbits.LATB0 = 0; input RXLATBbits.LATB1 = 0;LATBbits.LATB2 = 0;//LATBbits.LATB3 = 0; input 33k pullupLATBbits.LATB4 = 0;LATBbits.LATB5 = 0; //OUTPUT SHUTDOWNLATBbits.LATB6 = 0;LATBbits.LATB7 = 0;LATCbits.LATC0 = 0;LATCbits.LATC1 = 0;LATCbits.LATC2 = 0;LATCbits.LATC3 = 0;LATCbits.LATC4 = 0;LATCbits.LATC5 = 0; //output TXLATCbits.LATC6 = 0;LATCbits.LATC7 = 0;LATDbits.LATD0 = 0;LATDbits.LATD1 = 0;LATDbits.LATD2 = 0;LATDbits.LATD3 = 0;LATDbits.LATD4 = 0;LATDbits.LATD5 = 0;LATDbits.LATD6 = 0;LATDbits.LATD7 = 0;LATEbits.LATE0 = 0;LATEbits.LATE1 = 0;LATEbits.LATE2 = 0;LATEbits.LATE3 = 0;LATEbits.LATE4 = 0;LATEbits.LATE5 = 0;LATEbits.LATE6 = 0;LATEbits.LATE7 = 0;LATFbits.LATF0 = 0;LATFbits.LATF1 = 0;LATFbits.LATF2 = 0;LATFbits.LATF3 = 0;LATFbits.LATF4 = 0;LATFbits.LATF5 = 0;LATFbits.LATF6 = 0;LATFbits.LATF7 = 0;LATGbits.LATG0 = 0;LATGbits.LATG1 = 0;LATGbits.LATG2 = 0;LATGbits.LATG3 = 0; //output PWM DIMLATGbits.LATG4 = 0;LATGbits.LATG5 = 0; //MCLR//LATGbits.LATG6 = 0; NO SUCH THING//LATGbits.LATG7 = 0; NO SUCH THING//5 second delayfor (count=1;count<=50;count++) {__delay_ms(100);}while(1){LATCbits.LATC5 = 0;__delay_ms(3);LATCbits.LATC5 = 1;__delay_ms(3);}return;}

author: Ian.M
author_rank: Super Contributor
timestamp: September 28, 2017, 11:47:20 am
content: I don't see any configuration bit settings - use theMPLAB X wizardand add the generated code to your .c file.APFCON doesn't control RC5 so doesn't matter. The LCD controller can use RC5 but its disabled on powerup and the pin is setup for digital I/O. The MSSP1 module is also disabled on poweruo so you dont need to worry about its SDO1 output either.Provided the watchdog is disabled, the OPTION_REG setting cannot affect the XC8 delay functions.Use the MPLAB X simulator to check if RC5 toggles with the correct period. Although its a bit iffy for some peripherals, if you cant get simple I/O working in the simulator, odds are it wont work on the real chip either.