PIC Development Board with PK3

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Don't know why code #249 does not work for you. It works for me.
If code #257 is working, then why not just remove the "Receive++;" and use the remaining code as the basis for future serial communications?
I also don't understand where is the problem

Are you sure code in post #220 confirm two way communication ?

1622897636850.png


Bash:
#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 = 0x01;
    TRISC = 0xBF;
    TRISD = 0xFF;
    /**
    ANSELx registers
    */
    ANCON0 = 0x00;
    ANCON1 = 0x00;

    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}

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();
}

//Receive Byte
char Receive_Byte(void)
{
    if(RCSTAbits.OERR == 1)
    {
       RCSTAbits.CREN = 0;
       NOP();
       RCSTAbits.CREN = 1;
    }
    while(RCIF == 0 );               // Wait till the rx register becomes empty

    return RCREG;
}

//Send a byte
void Send_Byte(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_Byte(*p);
     p++;
   }
}

void main(void)
{
    char Receive ;
    char message[]= {"Hello\n"};

    SYSTEM_Initialize();

    __delay_ms(1000);  // Wait for 1000 ms

    String(message); // Send one byte to hyper terminal

    while (1)
    {
      Receive = Receive_Byte(); //  Receive a byte from hyper terminal
    
      Send_Byte(Receive);       // send received byte back to hyper terminal     
    }

    return;
}
I changed cable and PC but I don't get echo
 
Last edited:

hexreader

Joined Apr 16, 2011
619
"I changed cable and PC but I don't get echo"

JohnInTx gave instructions on linking TX and RX on CN3 (which is marked CN7 on your board)
I gave instructions on linking TX and RX on CN7. I gave two reminders that you ignored.

You failed to respond to either suggestion :(

Do the TX/RX loop test and report on the results.
No point worrying about code until you prove the hardware, the terminal program and the cabling.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Do the TX/RX loop test and report on the results.
Connected CN3 Tx to Rx (PIC removed from board ) and board is powered when performing test

When I type I see the text on terminal

1622907677583.png


Not Connected CN3 Tx to Rx (PIC removed from board ) and board is powered when performing test

When I type I see garbage on terminal

1622907755305.png
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
You DID reinstall the PIC and reconnect the serial lines as shown in the drawing I posted earlier, yes? Post a clear picture of that, please.
I've attached the picture
Not Connected CN3 Tx to Rx (PIC removed from board ) and board is powered when performing test

When I type I don't see anything on terminal

1622910555370.png
 

Attachments

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Great! Now Check the baud rate settings for your oscillator speed
baud rate settings for 8 Mhz

C:
void EUSART1_Initialize(void)
{
   // SPEN enabled; RX9 8-bit; RX9D 0; CREN enabled; ADDEN disabled; SREN disabled;  Overrun error;
    RCSTA1 = 0xB2;
    
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave_mode;
    TXSTA1 = 0x24;
    
    SPBRG1 = 51;
    
    SPBRGH1 = 0;

}

SPBRGH1 = 0;
SPBRG1 = 51; // 51 for 8 MHz Table 22-4 in the datasheet.

(BRGH = 0, BRG16 = 1)
For a device with FOSC of 8 MHz, desired baud rate of 9600, Asynchronous mode, and 8-bit BRG:
Desired Baud Rate = FOSC/(64 ([SPBRGHx:SPBRGx] + 1)) Solving for SPBRGHx:SPBRGx:
X = ((FOSC/Desired Baud Rate)/64) – 1
= ((8000000/9600)/16 -1
= 833.33/16-1
= 52-1 = 51
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
What's next?
I can send data from PIC to PC but I can't receive data from PC

There may be many issues
  1. There may be problem with development board
  2. There may be problem with code
  3. There may be problem with cable
  4. There may be problem with PC

I've fixed one problem of terminal. I have tried many possible solution but I couldn't solve my problem. I'm struggling to solve my problem
 

JohnInTX

Joined Jun 26, 2012
4,787
Post a clear picture of your board and connections. Also post the exact code you are using.

EDIT: Looking ahead:
With everything hooked up and turned on, put the meter on the RX pin of the PIC verify a logic 1 there. Hit a few keys. The meter should fluctuate indicating that something at least is getting to the pin. Since it echoed the characters typed with CN3 installed, you know the MAX232, cable, PC are OK (or should be).

Next, set a breakpoint in the receive function right after
while(RCIF == 0 );
Start from RESET. Hit a 'U' on the terminal. If the code breaks, inspect RCREG. If it is 55h that means that the PIC is receiving correctly and the problem is further down the software sequence. If not, inspect the other flags in RCSTA to see what's going on.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Current code for testing

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 = 0x01;
    TRISC = 0xBF;
    TRISD = 0xFF;
    /**
    ANSELx registers
    */
    ANCON0 = 0x00;
    ANCON1 = 0x00;

    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}

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();
}

//Receive Byte
char Receive_Byte(void)
{
    if(RCSTAbits.OERR == 1)
    {
       RCSTAbits.CREN = 0;
       NOP();
       RCSTAbits.CREN = 1;
    }
    while(RCIF == 0 );               // Wait till the rx register becomes empty
  
    return RCREG;
}

//Send a byte
void Send_Byte(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_Byte(*p);
     p++;
   }
}

void main(void)
{
    char Receive ;
    char message[]= {"Hello\n"};
  
    SYSTEM_Initialize();
  
    __delay_ms(1000);  // Wait for 10 ms
  
    String(message); // Send one byte to hyper terminal
 
    while (1)
    {
      Receive = Receive_Byte(); //  Receive a byte from hyper terminal
      Send_Byte(Receive);       // send received byte back to hyper terminal         
    }

    return;
}
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
With everything hooked up and turned on, put the meter on the RX pin of the PIC verify a logic 1 there. Hit a few keys. The meter should fluctuate indicating that something at least is getting to the pin. Since it echoed the characters typed with CN3 installed, you know the MAX232, cable, PC are OK (or should be).
I measured 5.2V DC on RX pin of the PIC. Meter fluctuate

Next, set a breakpoint in the receive function right after
while(RCIF == 0 );
Start from RESET. Hit a 'U' on the terminal.
1622976415079.png
 

JohnInTX

Joined Jun 26, 2012
4,787
That looks good. Board is wired correctly and the character 'u' is received with the correct hex code. 75h is a lower case 'u', 55h is an upper case 'U' in ASCII.
Verify that the function returns the character to the caller and calling Send_Byte() sends it back to the PC.
Does it?

Also, on the DEBUG toolbar, stretch the debug bar to the right of the green RUN arrow to expose the STEP buttons. You need those more than the memory window.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
That looks good. Board is wired correctly and the character 'u' is received with the correct hex code. 75h is a lower case 'u', 55h is an upper case 'U' in ASCII.
Verify that the function returns the character to the caller and calling Send_Byte() sends it back to the PC.
Does it?
Result when line Send_Byte(Receive) execute
1622986364438.png

Edit Whatever I type it store at RCREG. Example If I type r It show 'r'; 0x72
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
So everything you type on the terminal should be echoed back to the screen.
Remove the breakpoints and try it.
Does it work now?

EDIT:
What is the voltage on pin 2 of the MAX232?
What is the voltage on pin 6 of the MAX232?
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
So everything you type on the terminal should be echoed back to the screen.
Remove the breakpoints and try it.
I've written small test program so If I type 1 on terminal LED should be turn on/off for 1 seconds

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>

#define LED              LATDbits.LATD2

void PIN_MANAGER_Initialize(void)
{
    /**
    LATx registers
    */
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    /**
    TRISx registers
    */
    TRISE = 0x07;
    TRISA = 0xEF;
    TRISB = 0x01;
    TRISC = 0xBF;
    TRISD = 0xFF;
    /**
    ANSELx registers
    */
    ANCON0 = 0x00;
    ANCON1 = 0x00;

    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}

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();
}

//Receive Byte
char Receive_Byte(void)
{
    if(RCSTAbits.OERR == 1)
    {
       RCSTAbits.CREN = 0;
       NOP();
       RCSTAbits.CREN = 1;
    }
    while(RCIF == 0 );               // Wait till the rx register becomes empty
  
    return RCREG;
}

//Send a byte
void Send_Byte(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_Byte(*p);
     p++;
   }
}

void main(void)
{
    char Receive ;
    char message[]= {"Hello\n"};
  
    SYSTEM_Initialize();
  
    __delay_ms(1000);  // Wait for 10 ms
  
    String(message); // Send one byte to hyper terminal
 
    while (1)
    {
      Receive = Receive_Byte(); //  Receive a byte from hyper terminal
    
      if (Receive == "1")
      {
          LED = 1;
          __delay_ms(1000);
          LED = 0;
          __delay_ms(1000);
      }
      else
      {
           LED = 0;
      }
    }

    return;
}
When I type 1 LED is not turning on/off
 

hexreader

Joined Apr 16, 2011
619
There are two fundamental errors that prevent LED from showing anything:

"TRISD = 0xFF;"

Comment this line (and all of the other lines above and below) to spot first error.
Check compiler error window for a hint on second error

EDIT: - and as usual, I will be happy to provide fixed code if requested.
Sorry for the times when I get grumpy. I really wish I had the same patience as trebla and JohnInTx :(
EDIT2: - "1" is a string of two bytes (one byte ASCII and terminator byte 0x00) hexadecimal 0x31, 0x00.
'1' is a single ASCII byte 0x31 - no second byte, no terminator
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
I really wish I had the same patience as trebla and JohnInTx
LOL! If it were not for the 3 way tag team we've created, things might be different :cool: I appreciate the respites, much appreciated.
Case in point @Djsarakar , It would be really nice to know if you got the terminal echo working before going off in some new task with new IO code and a host of new errors. That is ..uhh.. frustrating, for me at least, because I never quite know where things are and I don't like solving the same problem more than once.
I'll leave it here for now, carry on and good luck!
 
Top