Electronic – PIC18LF4680 and usb ttl for usart / serial communication

picttluart

I want to know if it's possible to connect a ttl device directly to my
PIC18LF4680 for serial communication via USART . I can connect the ttl device directly to my Arduino Uno without issues. This is my hardware :

PIC18LF4680 pin-out

ttl adapter

The ttl device has 6 pins
(dtr , rxd , txd , vcc(3.3v or 5v) , cts, gnd) .

I have two different codes snippets below that perform USART communication.

Version one(I) utilizes the "usart.h" peripheral library.
Version two(II) uses "TXREG" and "RCREG" for sending and receiving data.

Both versions run well in my virtual environment (proteus 8 professional),
but not in the real world environment. Am I missing a step? Do I need a special
library? Or is it not possible with this chip?

Version I —————————————

#include "fuses.h"
#include <p18lf4680.h>
#include <stdio.h>
#include <stdlib.h>
#include <plib/usart.h>


void main(void) {

    TRISB = 0x00;
    OSCCON = 0x76;          // 8mhz (0111 0110)

     LATBbits.LATB4 = 0; 
    LATBbits.LATB1 = 0; 
    LATBbits.LATB0 = 0; 

    unsigned char txt1[] = "Hello World \r\n";
    unsigned char txt2[] = "Enter a number.... \r\n";

    CloseUSART();

    OpenUSART(USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH &
            USART_ADDEN_OFF ,
            52);


    for(int x=0;x<=20;x++){__delay_ms(50);}

    // write/send intro to PC

    while(BusyUSART());
    putsUSART((char *)txt1);

    for(int x=0;x<20;x++){__delay_ms(50);}

    while(BusyUSART());
    putsUSART((char *)txt2);

    for(int x=0;x<20;x++){__delay_ms(50);}


    while(1){
        sdata = ReadUSART();

        switch(sdata){
            case '1':
                LATBbits.LATB4 = 1; 
                LATBbits.LATB1 = 0; 
                LATBbits.LATB0 = 0;

            break;

            case '2':
                LATBbits.LATB4 = 0;
                LATBbits.LATB1 = 1; 
                LATBbits.LATB0 = 0; 

            break;

            case '3':
                LATBbits.LATB4 = 0; 
                LATBbits.LATB1 = 0; 
                LATBbits.LATB0 = 1; 

            break;


            default:
                LATBbits.LATB4 = 0;
                LATBbits.LATB1 = 0;
                LATBbits.LATB0 = 0; 


            break;
        }
    }

}

—————————————————–
Version II——————————————–

#include "fuses.h"
#include <p18lf4680.h>
#include <stdio.h>
#include <stdlib.h>

#define STRLEN 12

volatile unsigned char t;
volatile unsigned char rcindex;
volatile unsigned char rcbuf[STRLEN];

void USART_init(void){

    TXSTAbits.TXEN = 1;     // enable transmitter
    TXSTAbits.BRGH = 1;     // high baud rate mode
    RCSTAbits.CREN = 1;     // enable continous receiving

    // configure I/O pins
    TRISCbits.TRISC7 = 1;     // RX pin is input
    TRISCbits.TRISC6 = 1;     // TX pin is input (automatically configured)

    SPBRG = 52;            

    PIE1bits.RCIE = 1;      // enable USART receive interrupt
    RCSTAbits.SPEN = 1;     // enable USART


}

void USART_putc(unsigned char c)
{
    while (!TXSTAbits.TRMT); // wait until transmit shift register is empty
    TXREG = c;               // write character to TXREG and start transmission
}

void USART_puts(unsigned char *s)
{
    while (*s)
    {
        USART_putc(*s);     // send character pointed to by s
        s++;                // increase pointer location to the next character
    }
}

void main(void) {

    OSCCON = 0x76;          // 8mhz (0111 0110)

    USART_init();

    USART_puts("Init complete! \n");

    INTCONbits.PEIE = 1;    // enable peripheral interrupts
    INTCONbits.GIE = 1;     // enable interrupts

    while(1)
    {

    }

}

void interrupt ISR(void)
{
    if (PIR1bits.RCIF)  // check if receive interrupt has fired
    {
        t = RCREG;      // read received character to buffer

        // check if received character is not new line character
        // and that maximum string length has not been reached
        if ( (t != '\n') && (rcindex < STRLEN) )
        {
            rcbuf[rcindex] = t; // append received character to string
            rcindex++;          // increment string index
        }
        else
        {
            rcindex = 0;        // reset string index
            USART_puts(rcbuf);  // echo received string
        }

        PIR1bits.RCIF = 0;      // reset receive interrupt flag
    }
}

Every and All help is appreciated. Thanks!

Best Answer

talking hardware:

  1. make sure you connect MCLR_ to VDD (directly or by a pullup resistor)
  2. if the pic is running at 5V, use the USB TTL with 5V mode and not 3.3V
  3. you should measure VDD at RC6 if your code initialized UART correctly
  4. make sure your connections are correct. know what RXD and TXD of the converter mean. RXD is the receiver for PC-side or device-side? you can double check that everything is connected right using a voltmeter.

firmware:

  1. make sure your config bits are right, especially oscillator configuration bits. make sure you programmed the pic to use internal oscillator. add a blinking LED to your main just to make sure if your pic is actually "running"

  2. TRISC6 and TRISC7 should be set. Initially after a power-on-reset those bits are set but make sure you don't clear TRISC7 somewhere in your code.