Want square wave output on RB2 of PIC16F18856

Status
Not open for further replies.
T

treez

Guest
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 = EXT1X   // Power-up default value for COSC bits (EXTOSC operating per FEXTOSC bits)
#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 (Writing to NOSC and NDIV is allowed)
#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 = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF      // Brown-out reset enable bits (Brown-out reset disabled)
#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 = ON         // Zero-cross detect disable (Zero-cross detect circuit is always enabled)
#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

    uint8_t   count;

void main(void) {
    
    //Setup ports
        TRISA = 0x30;   //ADC INS
        TRISB = 0x08;    //ZCD & DALIRX
        TRISC = 0x0C;   //SELECT
        
        ANSELA = 0x30;   //ADC INs
        ANSELB = 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 ports
        LATAbits.LATA0 = 0;
        LATAbits.LATA1 = 0;
        LATAbits.LATA2 = 0;
        LATAbits.LATA3 = 0;
        //LATAbits.LATA4 = 0;     temp sensor input
        //LATAbits.LATA5 = 0;     light sensor input
        LATAbits.LATA6 = 0;
        LATAbits.LATA7 = 0;
                  
        //LATBbits.LATB0 = 0;      zero crossing input
        LATBbits.LATB1 = 0;
        LATBbits.LATB2 = 0;         //dali TX pin
        //LATBbits.LATB3 = 0;       dali RX pin
        LATBbits.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 resistor
        LATCbits.LATC4 = 0;
        LATCbits.LATC5 = 0;           
        LATCbits.LATC6 = 0;
        LATCbits.LATC7 = 0;
                
    //5 second delay
    for (count=1;count<=50;count++)   {
    __delay_ms(100);
    }

while(1){
        LATBbits.LATB2 = 0;
        __delay_ms(3);
        LATBbits.LATB2 = 1;
        __delay_ms(3);
}

    return;
}
 

Hi,

this needs 100% of processing power. No good software.

Use the PWM periferal, set it up and then no (0%) processing power is needed.

Klaus
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Thanks, you are absolutely correct.
Though for us this is simply a bit of test software, to test the dali TX circuitry, so we will stick with it, as to be honest we dont have time to read the datasheet on how to use the pwm peripheral.

I setup the registers, but there are many more registers...how do you know which ones need initialising and which ones dont?
The datasheet doesnt seem to say.

PIC16F18856
https://ww1.microchip.com/downloads/en/DeviceDoc/40001824B.pdf
 

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…