PIC Development Board with PK3

hexreader

Joined Apr 16, 2011
619
Can you make it simple. just make code for ds1307 only.
Here is my attempt at simple...
Code:
//
#define _XTAL_FREQ 20000000     // crystal 20MHz

// PIC18F45K80 Configuration Bit Settings
// 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 = ON      // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// 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.

#pragma warning disable 520

#include <xc.h>

unsigned char Result1;
unsigned char Result2;
unsigned char Result3;
unsigned char Result4;

// The code should set the R/W bit according to its function
#define DS1307_ADDRESS      0xD0         //I2C slave address of DS1307 (8 bit format)
#define Word_Address        0x00


#define LCD_RS              LATBbits.LATB4
#define LCD_TRIS_RS         TRISBbits.TRISB4
#define LCD_RW              LATBbits.LATB5
#define LCD_TRIS_RW         TRISBbits.TRISB5
#define LCD_E               LATBbits.LATB6
#define LCD_TRIS_E          TRISBbits.TRISB6

#define  LCDPORT            LATD
#define  LCDTRISD           LATD

//Prototype function
void Port_Initialized (void);
void WriteNibble(unsigned char command);
void WaitLCDBusy(void);
void WriteCommand(unsigned char command);

void Port_Initialized (void)
{
// LATx registers
    LATA =  0x00;
    LATB =  0x00;
    LATC =  0x00;
    LATD =  0x00;
    LATE =  0x00;

//  TRISx registers
    TRISA = 0x00;    // All are output, Unused
    TRISB = 0x00;   // all are output, Unused
    TRISC = 0x18;    //
    TRISD = 0x00;   // all are output, Unused
    TRISE = 0x00;   // All are output, Unused

    ANCON0 = 0x00;    // digital port
    ANCON1 = 0x00;    // Set to digital port
    CM1CON = 0x00;      // Comparator off
    CM2CON = 0x00;      // Comparator off
    ADCON0 = 0x00;      // A/D conversion Disabled
    ADCON1 = 0x00;      // A/D conversion Disabled
    ADCON2 = 0x00;     // A/D conversion Disabled
}

void WaitLCDBusy(void)
{
    __delay_ms(5);
}

//Send a command to the LCD
void WriteCommand(unsigned char command)
{
    WaitLCDBusy();                        //wait until not busy
    LCD_RS = 0;                           //setup to send command
    WriteNibble(command);                 //write the high nibble
    WriteNibble( (unsigned char)(command<<4) ); //then the low nibble  
}

//Initialized  LCD
void LCD_Initialized()
{
    LCDTRISD &=0x0f;                //ensure data bits are output
    LCD_E=0;                        //clear enable
    LCD_RS = 0;                     //going to write command
    LCD_TRIS_E=0;                   //Set enable to output
    LCD_TRIS_RS=0;                  //set RS to output
    LCD_TRIS_RW=0;
    LCD_RW=0;
    __delay_ms(30);                 //delay for LCD to initialise.
    WriteNibble(0x30);              //Required for initialisation
    __delay_ms(5);                  //required delay
    WriteNibble(0x30);              //Required for initialisation
    __delay_ms(1);                  //required delay
    WriteCommand(0x20);             //set to 4 bit interface
    WriteCommand(0x2c);             //set to 4 bit interface, 2 line and 5*10 font
    WriteCommand(0x01);             //clear display
    WriteCommand(0x06);             //move cursor right after write
    WriteCommand(0x0C);             //turn on display
}

//Send a character to the LCD
void WriteChar(unsigned char chr)
{
    WaitLCDBusy();                         //wait until not busy
    LCD_RS=1;                              //Setup to send character
    WriteNibble(chr);                      //write the high nibble          
    WriteNibble( (unsigned char)(chr<<4)); //then the low nibble
}

//Send any 4 bits to the LCD
void WriteNibble(unsigned char command)
{
    LCDPORT &= 0x0f;                        //clear the data bits
    LCDPORT|=((command & 0xf0));            //or in the new data
    LCD_E = 1;                              //enable the LCD interface
    NOP();                                  //change to delay of 1uS
    NOP();
    NOP();
    LCD_E = 0;                              //disable it
}

void LCD_Data( unsigned char *string)
{
    while (*string != '\0')
    {
      WriteChar(*string);
        string++;
    }
}

//Initialize I2C in master mode
void I2C_Initialized(void)
{
    SSPSTAT=0x80; //Slew rate control is disabled for Standard Speed mode (100 kHz and 1 MHz)
    SSPCON1=0x28; // I2C Master mode, clock = FOSC/(4 * (SSPADD + 1))
    SSPCON2=0x00;
    SSPADD = 49;  //100kHz clock @ 20MHz Fosc SSPADD = ( (Fosc/4) / BiteRate )-1
   // SSPADD = ( 20MHz / 100KHz ) - 1 = 49 //
}

// Send an I2C START
// Return 0 if all ok, 1 if bus collision
__bit I2C_Start(void)
{
    BCLIF = 0;  //Clear 'Bus collision" flag
    SEN = 1;    //initiate a START cycle
    while (SEN);    //wait until it has been sent
    return BCLIF;   //return value of BCLIF flag
}
// Send an I2C STOP
void I2C_Stop(void)
{
    PEN = 1;    //initiate a STOP cycle
    while (PEN);    //wait until it has been sent
}
// Send an I2C REPEATED START
void I2C_Restart(void)
{
    RSEN = 1;    //initiate a REPEATED START cycle
    while (RSEN);    //wait until it has been sent
}
//Receive one byte. ackflag=0 to send ACK, or 1 to send NAK in reply
//Send one byte. Return 0 if ACK received, or 1 if NAK received
__bit I2C_Sendbyte(unsigned char dat)
{
    SSPBUF = dat;

    asm("nop");     // <<<--- wait a little for R_W to be set

    while (R_W);    //wait until byte sent and ACK/NAK received
    return ACKSTAT;
}

unsigned char I2C_Recvbyte(unsigned char ackflag)
{
    RCEN = 1;   // initiate a RECEIVE cycle
    ACKDT =(__bit)ackflag;    //specify if we should send ACK or NAK after receiving
    while (RCEN);   //wait until RECEIVE has completed
    ACKEN = 1;  //initiate an ACK cycle
    while (ACKEN);  //wait until it has completed
    return SSPBUF;
}
//Send an array of data to an I2C device.
//Return 0 if all OK, 1 if bus error, 2 if slave address NAK, 3 if slave register NAK, 4 if slave data NAK
unsigned char DS1307_Write(unsigned char slave_address, unsigned char start_reg, unsigned char buflen, const unsigned char * bufptr)
{
    if (I2C_Start() )   //send a start, and check if it succeeded
        return 1;   //abort if bus collision
    //send the I2C slave address (force R/W bit low)
    if (I2C_Sendbyte(slave_address & 0xfe))  // <<---- Added AND to force R/W- low.  Comment said it but code didn't do it
    {
        I2C_Stop(); //if address was NAKed, terminate the cycle
        return 2;   //and return error code
    }
    //send the device register index
    if (I2C_Sendbyte(start_reg))
    {
        I2C_Stop(); //if register was NAKed, terminate the cycle
        return 3;   //and return error code
    }
    //send the data. buflen might be zero!
    for (; buflen>0; --buflen)
    {
        if (I2C_Sendbyte(*bufptr++))
        {
            I2C_Stop(); //if register was NAKed, terminate the cycle
            return 4;   //and return error code
        }
    }
    I2C_Stop();
    return 0;   //no error
}
//Receive an array of data from an I2C device.
//Return 0 if all OK, 1 if bus error, 2 if slave address NAK, 3 if slave register NAK
unsigned char DS1307_Read(unsigned char slave_address, unsigned char start_reg, unsigned char buflen, unsigned char * bufptr)
{
    //do a dummy zero length write cycle to set the register address
    unsigned char retval = DS1307_Write(slave_address, start_reg, 0, 0);
    if (retval)
    {
        return retval;  //abort if there was an error
    }
    //now start the READ cycle
    if (I2C_Start() )   //send a start, and check if it succeeded
        return 1;   //abort if bus collision
    //send the I2C slave address (force the R/W bit high)
    if (I2C_Sendbyte(slave_address | 0x01))
    {
        I2C_Stop(); //if address was NAKed, terminate the cycle
        return 2;   //and return error code
    }
    //receive the data.
    for (; buflen>0; --buflen)
    {
        unsigned char ackflag = (buflen == 1);   //1 if this is the last byte to receive => send NAK

        *bufptr++ = I2C_Recvbyte(ackflag);
    }
    I2C_Stop();
    return 0;   //no error
}
const unsigned char DS1307_data[] =
{
    0x00,   // seconds
    0x00,   // minutes
    0x12,   // hours
    0x07,   // day
    0x23,   // date
    0x05,   // month
    0x01,   // year
};
unsigned char rd_buf[7];


// Test variable for one byte transfer
const unsigned char testchar = 'A';
unsigned char testchar_readback[17];  // init to this to see difference after read

void main(void)
{
    unsigned char i = 0;
    unsigned char Data2 [17] = "tst ";
    unsigned char old_second = 99;
   
    Port_Initialized ();
    LCD_Initialized();
    I2C_Initialized();
    LCD_Data(Data2);

    // Write test buffer
    Result1 = DS1307_Write(DS1307_ADDRESS , Word_Address , sizeof(DS1307_data), DS1307_data);
     __delay_ms(10);

    // Read test buffer back
     Result2 = DS1307_Read(DS1307_ADDRESS, Word_Address , sizeof(rd_buf), rd_buf) ;
     __delay_ms(10);

    // Write Minutes 1 minute to location 0x1, size 1
    //Result3 = DS1307_Write(DS1307_ADDRESS, 0x01 , 1, 31);
     __delay_ms(10);

    while(1){
        __delay_ms(100);                                                        // limit read rate of RTC
        Result4 = DS1307_Read(DS1307_ADDRESS, 0 , 7, testchar_readback);        // read RTC registers
        if(old_second != (testchar_readback[0] & 0x0f)){                        // only allow LCD update if seconds units have changed
            LCD_Initialized();
            Data2[0] = ((testchar_readback[4] >> 4) & 0x0f) + '0';  
            Data2[1] = (testchar_readback[4] & 0x0f) + '0';  
            Data2[2] = ((testchar_readback[5] >> 4) & 0x0f) + '0';  
            Data2[3] = (testchar_readback[5] & 0x0f) + '0';  
            Data2[4] = '2';  
            Data2[5] = (testchar_readback[6] & 0x0f) + '0';  
            Data2[6] = ' ';  
            Data2[7] = ((testchar_readback[2] >> 4) & 0x0f) + '0';  
            Data2[8] = (testchar_readback[2] & 0x0f) + '0';  
            Data2[9] = ':';  
            Data2[10] = ((testchar_readback[1] >> 4) & 0x0f) + '0';  
            Data2[11] = (testchar_readback[1] & 0x0f) + '0';  
            Data2[12] = ':';  
            Data2[13] = ((testchar_readback[0] >> 4) & 0x0f) + '0';  
            Data2[14] = (testchar_readback[0] & 0x0f) + '0';  
            Data2[15] = ' ';  
            Data2[16] = 0;  
            LCD_Data(Data2);
            old_second = testchar_readback[0] & 0x0f;                           // prevent next LCD update until seconds have chanaged
        }
    }
}
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Here is my attempt at simple...
Thanks and sorry for late reply. I had health issues. Program is working it proves that battery is not necessary for testing

Can you explain two points in code ?

  1. Why are you Initializing LCD two times ? inside in while loop
  2. What happen in line ----> (old_second != (testchar_readback[0] & 0x0f)) ?

Code:
void main(void)
{
    unsigned char i = 0;
    unsigned char Data2 [17] = "tst ";
    unsigned char old_second = 99;

    // Write test buffer
    Result1 = DS1307_Write(DS1307_ADDRESS , Word_Address , sizeof(DS1307_data), DS1307_data);
     __delay_ms(10);

    // Read test buffer back
     Result2 = DS1307_Read(DS1307_ADDRESS, Word_Address , sizeof(rd_buf), rd_buf) ;
     __delay_ms(10);

    // Write Minutes 1 minute to location 0x1, size 1
    //Result3 = DS1307_Write(DS1307_ADDRESS, 0x01 , 1, 31);
     __delay_ms(10);

    while(1){
        __delay_ms(100);                                                        // limit read rate of RTC
        Result4 = DS1307_Read(DS1307_ADDRESS, 0 , 7, testchar_readback);        // read RTC registers
        if(old_second != (testchar_readback[0] & 0x0f)){                        // only allow LCD update if seconds units have changed
            LCD_Initialized();
            Data2[0] = ((testchar_readback[4] >> 4) & 0x0f) + '0';
            Data2[1] = (testchar_readback[4] & 0x0f) + '0';
            Data2[2] = ((testchar_readback[5] >> 4) & 0x0f) + '0';
            Data2[3] = (testchar_readback[5] & 0x0f) + '0';
            Data2[4] = '2';
         
            LCD_Data(Data2);
            old_second = testchar_readback[0] & 0x0f;                           // prevent next LCD update until seconds have chanaged
        }
    }
}
 
Last edited:

hexreader

Joined Apr 16, 2011
619
Sorry to hear of health problems. Hope you have a speedy recovery.

  1. Why are you Initializing LCD two times ? inside in while loop
Laziness.... I could not be bothered looking up which LCD command to clear the display.
You should change LCD init within loop to either LCD clear, or LCD set position top left.

  1. What happen in line ----> (old_second != (testchar_readback[0] & 0x0f)) ?
This makes sure that the LCD is only updated when the RTC seconds unit digit changes. Without this, LCD will update too often and will cause annoying flicker.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
My next plan is to make temperature meter that show temperature on display . I have J type of thermocouple one lead is yellow and one is blue inside big black wire

Can ADS1115 ADC read J type thermocouple ?

1621943912983.png


@hexreader @JohnInTX
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I'm having trouble with loose connection, causing jumper wires. I needed 4 pin 4K7 resistor pack for RTC. I searched a lot but it's not available in local market.

I found one a472j pack in junkyard but there are 9 pins

Can I break this a472j pack into two parts so that one part for RTC and other part for EEPROM ?

@JohnInTX , @hexreader Can it be done ?
 

hexreader

Joined Apr 16, 2011
619
You can make one 4-pin resistor pack from 9 pin IF YOU ARE LUCKY.
Use sharp wire cutters to snap off the 5 pins at the end. KEEP THE END WITH THE DOT.
This may, or may not work. The pack may shatter or short.

This is a really terrible idea, but I must admit I did it when desperate. I think I succeeded 4 out of 5 times. The failed pack shattered in the wrong place.

I recommend you do not do this, but I suspect that you will.

You cannot make two packs, as there is only one common pin. The dot shows the common pin, which connects to +5V. Be sure to connect the resistor pack the right way round. The dot on the resistor pack should be next to PIN 8 of 1307 RTC, or PIN 8 of serial EEPROM
 
Last edited:

hexreader

Joined Apr 16, 2011
619
Use multimeter on ohms range
Check that resistance from 'dot' pin to each of the other three pins is approximately 4.7 Kiloohms.
If you measure about 4.7 Kiloohms all 3 times, then the pack is good
If the pack is broken, it will show only a few ohms, or open circuit on one or more measurements

You could try this before you cut the pack if you want to get used to doing this test
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
You could try this before you cut the pack if you want to get used to doing this test
unfortunately that RS pack got damaged when I was disordering from IC.

I made my new RS pack with 4K7 Resistor as you suggested post #136

I am sure I have done exactly as you told in post #136, But I am waiting for your confirmation
 

Attachments

Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Shopping list:

1) 10x 0.1 uF ceramic capacitor (for decoupling - needs mounting on track side of development board, as designer failed to provide space on PCB)

3) 10x 4K7 resistors - 10x 10K resistors

4) 2x 4-pin resistor pack (3x 4k7 with single common connection)
i am frustrated I ran pervious working program on the board to display message but LCD display is not working on my board. I checked continuity signal with multimeter all is okay but I don't understand what's problem.

I want to fix my hardware. Where do i have to connect the decoupling capacitor and 10k resistor on the board
 

hexreader

Joined Apr 16, 2011
619
10K resistors have no obvious purpose right now. They are just handy as pull-up or pull-down resistors for switches or wherever an input should not be left floating.

Your development board should have been designed with a 0.1uF decoupling capacitor across the supply pins of every IC fitted to the board. Sadly there is no space to fit them now. You could solder to the bottom of your board, but it will be ugly and could lead to short circuits if the leads are too long or poorly fitted.

If you really want to solder to the bottom of your board, put a capacitor across pin 11 (Vdd) and pin 12 (Vss) underneath the 40-pin socket. Another one goes between pin 32 (Vdd) and pin 31 (Vss).

Datasheet Figure 2-1 shows the ideal power connections, but it is too late to re-design your board that way.

Whilst decoupling capacitors are a good idea, my guess is that your unreliability issues may not go away once capacitors are soldered to your board - but I could be wrong. I have never used a PIC without decoupling capacitors, so I do not know what happens without them.
 
Last edited:

hexreader

Joined Apr 16, 2011
619
A random idea comes to mind..... perhaps a silly one.

Why not use serial port and terminal program to do all of your display needs for a while?
If serial is reliable, then maybe you can get away without nasty bodge capacitors soldered beneath your board.

I much prefer serial to LCD wherever I have the choice.
 

hexreader

Joined Apr 16, 2011
619
I have a cheap development board that I use quite often and have had no reliability problems with it.

I checked just now and it too has no decoupling capacitors.

Maybe you can get away without them.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
A random idea comes to mind..... perhaps a silly one.

Why not use serial port and terminal program to do all of your display needs for a while?
If serial is reliable, then maybe you can get away without nasty bodge capacitors soldered beneath your board.

I much prefer serial to LCD wherever I have the choice.
@hexreader your test programming not working for me

This is my UART code working fine for my Its running with 8 Mhz internal crystal. I would try to implement your test program for EEPROM

C:
#define _XTAL_FREQ 8000000
// Configuration bits: selected in the GUI
// CONFIG1L
#pragma config RETEN = OFF    // VREG Sleep Enable bit->Ultra low-power regulator is Disabled (Controlled by REGSLP bit)
#pragma config INTOSCSEL = HIGH    // LF-INTOSC Low-power Enable bit->LF-INTOSC in High-power mode during Sleep
#pragma config SOSCSEL = DIG    // SOSC Power Selection and mode Configuration bits->Digital (SCLKI) mode
#pragma config XINST = OFF    // Extended Instruction Set->Disabled
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator->Internal RC oscillator
#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 = OFF    // Power Up Timer->Disabled
#pragma config BOREN = SBORDIS    // Brown Out Detect->Enabled in hardware, SBOREN disabled
#pragma config BORV = 3    // Brown-out Reset Voltage bits->1.8V
#pragma config BORPWR = ZPBORMV    // BORMV Power level->ZPBORMV instead of BORMV is selected
// CONFIG2H
#pragma config WDTEN = OFF    // Watchdog Timer->WDT disabled in hardware; SWDTEN bit disabled
#pragma config WDTPS = 1048576    // Watchdog Postscaler->1:1048576
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit->ECAN TX and RX pins are located on RB2 and RB3, respectively
#pragma config MSSPMSK = MSK7    // MSSP address masking->7 Bit address masking mode
#pragma config MCLRE = ON    // Master Clear Enable->MCLR Enabled, RE3 Disabled
// CONFIG4L
#pragma config STVREN = ON    // Stack Overflow Reset->Enabled
#pragma config BBSIZ = BB2K    // Boot Block Size->2K word Boot Block size
// CONFIG5L
#pragma config CP0 = OFF    // Code Protect 00800-01FFF->Disabled
#pragma config CP1 = OFF    // Code Protect 02000-03FFF->Disabled
#pragma config CP2 = OFF    // Code Protect 04000-05FFF->Disabled
#pragma config CP3 = OFF    // Code Protect 06000-07FFF->Disabled
// CONFIG5H
#pragma config CPB = OFF    // Code Protect Boot->Disabled
#pragma config CPD = OFF    // Data EE Read Protect->Disabled
// CONFIG6L
#pragma config WRT0 = OFF    // Table Write Protect 00800-01FFF->Disabled
#pragma config WRT1 = OFF    // Table Write Protect 02000-03FFF->Disabled
#pragma config WRT2 = OFF    // Table Write Protect 04000-05FFF->Disabled
#pragma config WRT3 = OFF    // Table Write Protect 06000-07FFF->Disabled
// CONFIG6H
#pragma config WRTC = OFF    // Config. Write Protect->Disabled
#pragma config WRTB = OFF    // Table Write Protect Boot->Disabled
#pragma config WRTD = OFF    // Data EE Write Protect->Disabled
// CONFIG7L
#pragma config EBTR0 = OFF    // Table Read Protect 00800-01FFF->Disabled
#pragma config EBTR1 = OFF    // Table Read Protect 02000-03FFF->Disabled
#pragma config EBTR2 = OFF    // Table Read Protect 04000-05FFF->Disabled
#pragma config EBTR3 = OFF    // Table Read Protect 06000-07FFF->Disabled
// CONFIG7H
#pragma config EBTRB = OFF    // Table Read Protect Boot->Disabled
#include <xc.h>
void PIN_MANAGER_Initialize(void)
{
    /**
    LATx registers
    */
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    /**
    TRISx registers
    */
    TRISE = 0x07;
    TRISA = 0xEF;
    TRISB = 0xFF;
    TRISC = 0xBF;
    TRISD = 0xFF;
    /**
    ANSELx registers
    */
    ANCON0 = 0xFF;
    ANCON1 = 0x7F;
    /**
    WPUx registers
    */
    WPUB = 0x00;
    INTCON2bits.nRBPU = 1;
     
}
void OSCILLATOR_Initialize(void)
{
    // SCS FOSC; HFIOFS not stable; IDLEN disabled; IRCF 8MHz_HF;
    OSCCON = 0x60;
    // SOSCGO disabled; MFIOSEL disabled; SOSCDRV Low Power;
    OSCCON2 = 0x00;
    // INTSRC INTRC; PLLEN disabled; TUN 0;
    OSCTUNE = 0x00;
    // ROSEL System Clock(FOSC); ROON disabled; ROSSLP Disabled in Sleep mode; RODIV Fosc;
    REFOCON = 0x00;
}
void EUSART1_Initialize(void)
{
    // Set the EUSART1 module to the options selected in the user interface.
    // ABDOVF no_overflow; TXCKP async_noninverted_sync_fallingedge; BRG16 16bit_generator; WUE disabled; ABDEN disabled; RXDTP not_inverted;
    BAUDCON1 = 0x08;
    // SPEN enabled; RX9 8-bit; RX9D 0; CREN enabled; ADDEN disabled; SREN disabled;
    RCSTA1 = 0x90;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave_mode;
    TXSTA1 = 0x24;
    //
    SPBRG1 = 0xCF;
    //
    SPBRGH1 = 0x00;

}

void SYSTEM_Initialize(void)
{
    PIN_MANAGER_Initialize();
    OSCILLATOR_Initialize();
    EUSART1_Initialize();
}
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 main(void)
{
    char message[]= {"Hello"};
    SYSTEM_Initialize();
   
    while (1)
    {
     __delay_ms(1000);
     string(message);
    }
   
    return;
}
 

hexreader

Joined Apr 16, 2011
619
Your program only tests communication PIC to PC.

Here is the same program modified for two-way communication. Make sure that when you type 'a' that you see a 'b' returned (and +1 for other characters) "hello" should show as "ifmmp"
Code:
#define _XTAL_FREQ 8000000
// Configuration bits: selected in the GUI
// CONFIG1L
#pragma config RETEN = OFF    // VREG Sleep Enable bit->Ultra low-power regulator is Disabled (Controlled by REGSLP bit)
#pragma config INTOSCSEL = HIGH    // LF-INTOSC Low-power Enable bit->LF-INTOSC in High-power mode during Sleep
#pragma config SOSCSEL = DIG    // SOSC Power Selection and mode Configuration bits->Digital (SCLKI) mode
#pragma config XINST = OFF    // Extended Instruction Set->Disabled
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator->Internal RC oscillator
#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 = OFF    // Power Up Timer->Disabled
#pragma config BOREN = SBORDIS    // Brown Out Detect->Enabled in hardware, SBOREN disabled
#pragma config BORV = 3    // Brown-out Reset Voltage bits->1.8V
#pragma config BORPWR = ZPBORMV    // BORMV Power level->ZPBORMV instead of BORMV is selected
// CONFIG2H
#pragma config WDTEN = OFF    // Watchdog Timer->WDT disabled in hardware; SWDTEN bit disabled
#pragma config WDTPS = 1048576    // Watchdog Postscaler->1:1048576
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit->ECAN TX and RX pins are located on RB2 and RB3, respectively
#pragma config MSSPMSK = MSK7    // MSSP address masking->7 Bit address masking mode
#pragma config MCLRE = ON    // Master Clear Enable->MCLR Enabled, RE3 Disabled
// CONFIG4L
#pragma config STVREN = ON    // Stack Overflow Reset->Enabled
#pragma config BBSIZ = BB2K    // Boot Block Size->2K word Boot Block size
// CONFIG5L
#pragma config CP0 = OFF    // Code Protect 00800-01FFF->Disabled
#pragma config CP1 = OFF    // Code Protect 02000-03FFF->Disabled
#pragma config CP2 = OFF    // Code Protect 04000-05FFF->Disabled
#pragma config CP3 = OFF    // Code Protect 06000-07FFF->Disabled
// CONFIG5H
#pragma config CPB = OFF    // Code Protect Boot->Disabled
#pragma config CPD = OFF    // Data EE Read Protect->Disabled
// CONFIG6L
#pragma config WRT0 = OFF    // Table Write Protect 00800-01FFF->Disabled
#pragma config WRT1 = OFF    // Table Write Protect 02000-03FFF->Disabled
#pragma config WRT2 = OFF    // Table Write Protect 04000-05FFF->Disabled
#pragma config WRT3 = OFF    // Table Write Protect 06000-07FFF->Disabled
// CONFIG6H
#pragma config WRTC = OFF    // Config. Write Protect->Disabled
#pragma config WRTB = OFF    // Table Write Protect Boot->Disabled
#pragma config WRTD = OFF    // Data EE Write Protect->Disabled
// CONFIG7L
#pragma config EBTR0 = OFF    // Table Read Protect 00800-01FFF->Disabled
#pragma config EBTR1 = OFF    // Table Read Protect 02000-03FFF->Disabled
#pragma config EBTR2 = OFF    // Table Read Protect 04000-05FFF->Disabled
#pragma config EBTR3 = OFF    // Table Read Protect 06000-07FFF->Disabled
// CONFIG7H
#pragma config EBTRB = OFF    // Table Read Protect Boot->Disabled
#include <xc.h>
void PIN_MANAGER_Initialize(void)
{
    /**
    LATx registers
    */
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    /**
    TRISx registers
    */
    TRISE = 0x07;
    TRISA = 0xEF;
    TRISB = 0xFF;
    TRISC = 0xBF;
    TRISD = 0xFF;
    /**
    ANSELx registers
    */
    ANCON0 = 0xFF;
    ANCON1 = 0x7F;
    /**
    WPUx registers
    */
    WPUB = 0x00;
    INTCON2bits.nRBPU = 1;
   
}
void OSCILLATOR_Initialize(void)
{
    // SCS FOSC; HFIOFS not stable; IDLEN disabled; IRCF 8MHz_HF;
    OSCCON = 0x60;
    // SOSCGO disabled; MFIOSEL disabled; SOSCDRV Low Power;
    OSCCON2 = 0x00;
    // INTSRC INTRC; PLLEN disabled; TUN 0;
    OSCTUNE = 0x00;
    // ROSEL System Clock(FOSC); ROON disabled; ROSSLP Disabled in Sleep mode; RODIV Fosc;
    REFOCON = 0x00;
}
void EUSART1_Initialize(void)
{
    // Set the EUSART1 module to the options selected in the user interface.
    // ABDOVF no_overflow; TXCKP async_noninverted_sync_fallingedge; BRG16 16bit_generator; WUE disabled; ABDEN disabled; RXDTP not_inverted;
    BAUDCON1 = 0x08;
    // SPEN enabled; RX9 8-bit; RX9D 0; CREN enabled; ADDEN disabled; SREN disabled;
    RCSTA1 = 0x90;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave_mode;
    TXSTA1 = 0x24;
    //
    SPBRG1 = 0xCF;
    //
    SPBRGH1 = 0x00;

}

void SYSTEM_Initialize(void)
{
    PIN_MANAGER_Initialize();
    OSCILLATOR_Initialize();
    EUSART1_Initialize();
}

// read a single charater from UART1
char chin(void)
{
    while(RC1IF == 0 );               // Wait till the rx register becomes empty
    return RCREG1;
}



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 main(void)
{
    char message[]= {"\r\nType away - \r\n\n"};
    char tempchar;
   
    SYSTEM_Initialize();
    __delay_ms(1000);
    string(message);
 
    while (1)
    {
        tempchar = chin();                                                      // read byte from UART
        tempchar++;                                                             // make echoed byte different to received byte
        send(tempchar);                                                         // echo it
    }
}
 

hexreader

Joined Apr 16, 2011
619
A much simpler test of your RS232 hardware is to connect "TX" to "RX" on connector CN7 "RS232"

This should echo characters and does not depend on the PIC in any way.

Remove the link and make sure that echoing stops.
 
Top