Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to make my own files in project

Status
Not open for further replies.

Djsarkar

Member
Hi ,

I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30 I have written my own working code for ESUART. I want to make my own header file and c file in project
something like uart.h, uart.c and main c

I don't know what should I have define and declare in uart.h, uart.c

C:
// PIC18F45K80 Configuration Bit Settings

#define _XTAL_FREQ 20000000

// CONFIG1L
#pragma config RETEN = ON       // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW  // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS2        //  HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = ON      // Power Up Timer (Enabled)
#pragma config BOREN = OFF      // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0         // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW     // BORMV Power level (BORMV set to low power level)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1        // Watchdog Postscaler (1:1)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5   // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = OFF      // Master Clear Enable (MCLR Disabled, RE3 Enabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K     // Boot Block Size (1K word Boot Block size)

// CONFIG5L
#pragma config CP0 = ON         // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON         // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON         // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON         // Code Protect 06000-07FFF (Enabled)

// CONFIG5H
#pragma config CPB = ON         // Code Protect Boot (Enabled)
#pragma config CPD = ON         // Data EE Read Protect (Enabled)

// CONFIG6L
#pragma config WRT0 = ON        // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON        // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON        // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON        // Table Write Protect 06000-07FFF (Enabled)

// CONFIG6H
#pragma config WRTC = ON        // Config. Write Protect (Enabled)
#pragma config WRTB = ON        // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON        // Data EE Write Protect (Enabled)

// CONFIG7L
#pragma config EBTR0 = ON       // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON       // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON       // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON       // Table Read Protect 06000-07FFF (Enabled)

// CONFIG7H
#pragma config EBTRB = ON       // Table Read Protect Boot (Enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>


#define TRUE                  1


void Port_Initialized (void)
{
    ANCON0 = 0; // Set to digital port
    ANCON1 = 0; // Set to digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
    ADCON1 = 0; // A/D conversion Disabled
    ADCON2 = 0; // A/D conversion Disabled
    
    LATA =  0;
    LATB =  0;
    LATC =  0;
    LATD =  0;
    LATE =  0;
    
    TRISA = 0b0000000; // all are output, Unused
    TRISB = 0b0000000; // all are output, Unused
    TRISC = 0b0000000; // all are output, Unused
    TRISD = 0b0000000; //all are output, Unused
    TRISE = 0b0000000; // All are output, Unused
}

void send(char message)
{
    while(TXIF == 0 );  // Wait till the transmitter register becomes empty
    TXREG1 = message;
}
void string(char *p)
{
   while(*p != '\0') {
       __delay_ms(1);
     send(*p);   
     p++;
   }
}

void Uart_Initialized(void)
{
    //TXSTAx TRANSMIT STATUS AND CONTROL REGISTER
   TXSTA1bits.CSRC = 0; //: Don?t care.
   TXSTA1bits.TX9 = 0; //Selects 8-bit transmissionbit
   TXSTA1bits.TXEN = 1; // Transmit Enable
   TXSTA1bits.SYNC  = 0;   // Asynchronous mode
   TXSTA1bits.SENDB = 0;
   TXSTA1bits.BRGH =  1;   //  High speed mode
   TXSTA1bits.TX9D = 0;
   //RCSTAx: RECEIVE STATUS AND CONTROL REGISTER
   RCSTA1bits.SPEN  = 1 ;   //Serial port enabled
   RCSTA1bits.RX9 = 0; //Selects 8-bit reception
   RCSTA1bits.SREN  = 1 ;//Don?t care.
   RCSTA1bits.CREN  = 1 ;// Enables receiver
   RCSTA1bits.ADDEN = 0; //
   RCSTA1bits.FERR  = 0 ;// No framing error
   RCSTA1bits.OERR  = 1 ;   // Overrun error
   RCSTA1bits.RX9D   = 0 ;   //Selects 8-bit reception
 
    SPBRGH1 = 0;
    SPBRG1 = 129,
 
    INTCON = 0x00;
    PIR1 = 0x00;
    PIR2= 0x00;
    PIR3 = 0x00;
    PIR4 = 0x00;
    PIR5 = 0x00;
    PIE1 = 0x00;
    PIE2 = 0x00;
    PIE3 = 0x00;
    PIE4 = 0x00;
    PIE5 = 0x00;
    IPR1 = 0x00;
    IPR2 = 0x00;
    IPR3 = 0x00;
    IPR4 = 0x00;
    
}
void main(void)
{
   char message[]= {"Hello"};
   Port_Initialized ();
   Uart_Initialized ();
 
    while (1)
    {
     __delay_ms(1000);
     string(message);
    
    }
    
}
 
The usual convention is to put the informational definitions and function prototypes etc. in the .h; the things that will be used in teh program but do not allocate any memory or create code by themselves.

Like defines, structure typedefs, bit definitions and the like.

Then the actual code in the .c file

Make sure you put a safety guard condition around the .h content, eg.

C:
#ifndef uart_h
#define uart_h
 
// And the content here
 
#endif
 
Function definitions (i.e. the function name & body itself) go in the source file (*.c) while function declarations (i.e. the prototype) go in the header file (*.h). Only public functions need be declared (i.e. functions which will be used by code in other source files).

Example -

C:
/* serial.c */

#include <xc.h>
#include <stdint.h>
#include "serial.h"

/* This is a function definition */
void writeSerial(uint8_t byte) {
    while(!PIR1bits.TXIF);      // wait for TX buffer to transfer to TSR
    TXREG = byte;      // send byte
}

/* This is also a function definition */
uint8_t readSerial() {
    while(!PIR1bits.RCIF);    // wait for byte
    return RCREG;    // return with received byte
}

/* End of file */

C:
/* serial.h */

/* This is a function declaration. This function accepts an unsigned 8-bit integer data type but returns nothing */
void writeSerial(uint8_t);

/* This is also a function declaration. This function accepts no parameters but returns an 8-bit unsigned integer data type. */
uint8_t readSerial();

/* End of file */

Notice in the function declaration (i.e. the prototype), only the data type itself needs to be declared and not the name of the reserved variable.

The purpose of the function declaration is to tell the compiler that somewhere in one of your source files it will find a function with this name that accepts these data types as arguments and returns this data type (void data type accepts/returns nothing).
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top