79Views1Replies

Author Options:

PIC18LF4680 microcontroller and usb ttl for usart / serial data Answered

hi everyone.
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.

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
    }
}

------------------------------------------------------------


Any and all help is appreciated. Thanks!



Discussions

0
None
newbie14

3 years ago

here are images of my hardware

pic18LF4680-pinout.pngusb-ttl.jpg