Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] USART connection between two PIC18F Starter Kits

Status
Not open for further replies.

Victor Badoiu

Newbie level 1
Joined
Feb 18, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
11
Hello,

I am new to PIC18 world and I have a big problem. I bought two PIC18F Stater Kits with PIC18F46J50 microcontroller, Oled display and card reader and I want to connect them using RS232. I already connect the two boards by soldering two wires on D2 and D3 pins.

I've read that I need to use USART2, since USART1 is already connected to the card reader. So, I need to reprogram the pins RD2 and RD3 (RP20 and RP19). The problem is that I cannot find a single demo USART2 that is working. I want to send a string of characters from Board no1. to Board no2. using RS232, and eventually print the characters on the OLED.

Here is a piece of code that I use to transmit the characters. I think it works, but i need another code for board no2, to see something (the receive part).

Code:
#define _XTAL_FREQ 8000000 //The speed of your internal(or)external oscillator
 #include <p18cxxx.h>
 #include <usart.h>
 #include <PPS.h>

int i = 0;

 // CONFIG1L
#pragma config WDTEN = OFF          // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config PLLDIV =3            // PLL Prescaler Selection bits - Divide by 3 (12 MHz oscillator input)
#pragma config STVREN = ON          // Stack Overflow/Underflow Reset  (Enabled)
#pragma config XINST = OFF          // Extended instruction set disabled

// CONFIG1H
#pragma config CPUDIV = OSC1        // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF            // Code Protect (Program memory is not code-protected)

// CONFIG2L
#pragma config OSC = HSPLL          //HS oscillator, PLL enabled, HSPLL used by USB
#pragma config T1DIG = ON           // T1OSCEN Enforcement (Secondary Oscillator clock source may be selected)
#pragma config LPT1OSC = OFF        // Low-Power Timer1 Oscillator (High-power operation)
#pragma config FCMEN = OFF          //Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF           //Two-Speed Start-up disabled

// CONFIG2H
#pragma config WDTPS = 32768        // Watchdog Postscaler (1:32768)

// CONFIG3L
#pragma config DSWDTOSC = INTOSCREF // DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = T1OSCREF    // RTCC Clock Select (RTCC uses T1OSC/T1CKI)
#pragma config DSBOREN = OFF        // Zero-Power BOR disabled in Deep Sleep
#pragma config DSWDTEN = OFF        // Deep Sleep Watchdog Timer (Disabled)
#pragma config DSWDTPS = 8192       //1:8,192 (8.5 seconds)

// CONFIG3H
#pragma config IOL1WAY =OFF         //IOLOCK bit can be set and cleared
#pragma config MSSP7B_EN = MSK7     // MSSP address masking (7 Bit address masking mode)

// CONFIG4L
#pragma config WPFP = PAGE_1        // Write/Erase Protect Page Start/End Location (Write Protect Program Flash Page 0)
#pragma config WPEND = PAGE_0       //Start protection at page 0
#pragma config WPCFG = OFF          //Write/Erase last page protect Disabled

// CONFIG4H
#pragma config WPDIS = OFF             //WPFP[5:0], WPEND, and WPCFG bits ignored

 #define USE_AND_MASKS

unsigned char Txdata[] = "MICROCHIP_USART";
void Delay1Second(void);

 void main (void)
 {
     unsigned char config=0,spbrg=0,baudconfig=0,i=0;

// //------PPS Configuration to map ESUART2 to correct pins ----
// another reconfiguration of the pins
//     PPSUnLock();
//
//     // Configure RD3/RP20 as UART_TX
//     iPPSOutput(OUT_PIN_PPS_RP20, OUT_FN_PPS_TX2CK2);
//
//      // Configure RD2/RP19 as UART_RX
//     iPPSInput(IN_FN_PPS_RX2DT2, IN_PIN_PPS_RP19);
//
//
//     PPSLock();

     // REMAP ID PORT
   
    PPSCON = 0x00;              // unlock peripheral Pin select register
    RPOR19 = 0x05;              // assign USART2 TX to RP19/RD2
    RPINR16 = 0x14;             // assign USART2 RX to RP20/RD3
    PPSCON = 0x01;              // lock peripheral Pin select register

    TRISDbits.TRISD2 = 0;       // TX2 output
    TRISDbits.TRISD3 = 1;       // RX2 input
 //------USART Setup ----

     Close2USART();  //turn off usart if was previously on

     spbrg = 51;

     Open2USART(USART_TX_INT_OFF &
                USART_RX_INT_OFF &
                USART_ASYNCH_MODE &
                USART_EIGHT_BIT &
                USART_CONT_RX &
                USART_BRGH_HIGH, spbrg);

     baudconfig = BAUD_8_BIT_RATE & BAUD_AUTO_OFF;
     baud2USART (baudconfig);

 while(1){

//------USART Transmission ----
    while(Busy2USART());                         //Check if Usart is busy or not
   puts2USART((char *)Txdata);                //transmit the string
    Delay1Second();
}            
 }

 void Delay1Second()
{
    for(i=0;i<100;i++)
    {
         __delay_ms(10);
    }
}
 

Aaaaalways test your communications with a known host.. like communicating each board with a PC first... (go buy a cheap usb/ttl cable to test )

then, what compiler are you using? It seems XC8 but...

finally... did you read the XC8 Peripheral Library Manual? it seems you need to use the OR operator '|' to join the settings

Code:
     Open2USART(USART_TX_INT_OFF |
                USART_RX_INT_OFF |
                USART_ASYNCH_MODE |
                USART_EIGHT_BIT |
                USART_CONT_RX |
                USART_BRGH_HIGH, spbrg);

     baudconfig = BAUD_8_BIT_RATE | BAUD_AUTO_OFF;
     baud2USART (baudconfig);

also avoid trying to lock/unlock the PPS, it comes unlocked and for your test it should be enough... (sadly locking/unlocking isn't as easy as setting it to 1 or 0... please read the datasheet for the correct sequence. for the time being, avoid that stuff)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top