Making RB2 of PIC16F18856 give a square wave output - Page 1
Forum section: electronics / general

author: ocset
author_rank: Super Contributor
timestamp: September 28, 2017, 11:14:35 am
content: Hello,The following code is simply meant to produce a square wave output on RB2 of PIC16F18856.This is so we can test if the output of the opto that RB2 drives into has a quick enough rise/fall time.It is written in XC8 C and MPLAB X.It builds OK but do you know how i set up the timer for the __delay_ms(3) command?Also, do you think ive set up the registers correctly.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 RB2//Period = 3ms//PIC16F18856//XC8 compiler//MPLAB X V3.61// 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 4000000uint8_t count;void main(void) {//Setup portsTRISA = 0x30; //ADC INSTRISB = 0x08; //ZCD & DALIRXTRISC = 0x0C; //SELECTANSELA = 0x00; //ADC INsANSELB = 0x00;ANSELC = 0x00;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;LATAbits.LATA1 = 0;LATAbits.LATA2 = 0;LATAbits.LATA3 = 0;//LATAbits.LATA4 = 0; temp sensor input//LATAbits.LATA5 = 0; light sensor inputLATAbits.LATA6 = 0;LATAbits.LATA7 = 0;//LATBbits.LATB0 = 0; zero crossing inputLATBbits.LATB1 = 0;LATBbits.LATB2 = 0; //dali TX pin//LATBbits.LATB3 = 0; dali RX pinLATBbits.LATB4 = 0;LATBbits.LATB5 = 0;LATBbits.LATB6 = 0;LATBbits.LATB7 = 0;LATCbits.LATC0 = 0;LATCbits.LATC1 = 0;//LATCbits.LATC2 = 0; pulldown resistor//LATCbits.LATC3 = 0; pulldown resistorLATCbits.LATC4 = 0;LATCbits.LATC5 = 0;LATCbits.LATC6 = 0;LATCbits.LATC7 = 0;//5 second delayfor (count=1;count<=50;count++) {__delay_ms(100);}while(1){LATBbits.LATB2 = 0;__delay_ms(3);LATBbits.LATB2 = 1;__delay_ms(3);}return;}

author: Buriedcode
author_rank: Super Contributor
timestamp: September 28, 2017, 04:25:52 pm
content: I'm unsure what exactly you are asking for. As far as I can tell, you wish to replace the __delay_ms(3) using a hardware timer?Although many hate it the 'configurator' in MPLABX works just fine for setting up timers. Also as that device has 6 timers, and peripheral pin select, then using the configurator is really the only option if you want to set things up quickly.You could either setup the timer for interrupts every 3ms or as you're doing in main, just waiting (polling the timer overflow flag). If you're just testing for rise/fall times, then I assume the 3ms delay doesn't have to be accurate.As for checking 'if it will work', no idea. It would take someone a reasonable amount of time to check all the register names, and values for you, when you could just flash it into a PIC on your prototype.

author: JPortici
author_rank: Super Contributor
timestamp: September 28, 2017, 05:57:40 pm
content: why did you open two topics with the exact same question?RTFM.The index at the end of "MPLAB XC8 C Compiler User Guide" has links to all the builtin functionsthe builtin delay doesn't need any timer, it's just a bunch of NOPs