PIC18LF4680 and USART / serial communication with usb-to-ttl

(instructions, reset, WDT, specifications...) PIC17Cxx, PIC18Fxxx

PIC18LF4680 and USART / serial communication with usb-to-ttl

Postby newbie14 » Sun Aug 07, 2016 7:20 am

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.
This is my hardware :

Image

Image

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

Code: Select all
#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---------------------------------------------------------------
--------------------------------------------------------------------------

Code: Select all
#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
    }
}



Chip Configurations ("fuses.h") ---------------------------------------------------------------

Code: Select all
// PIC18LF4680 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = IRCIO67 // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown-out Reset Voltage bits (VBOR set to 2.1V)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config BBSIZ = 1024 // Boot Block Size Select bits (1K words (2K bytes) Boot Block)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)
#define _XTAL_FREQ 8000000
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>


Every and All help is appreciated. Thanks.
newbie14
 
Posts: 3
Joined: Sun Aug 07, 2016 7:09 am
PIC experience: Experienced Hobbyist

Re: PIC18LF4680 and USART / serial communication with usb-to

Postby ric » Sun Aug 07, 2016 7:58 am

Which C compiler are you using?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: PIC18LF4680 and USART / serial communication with usb-to

Postby newbie14 » Sun Aug 07, 2016 4:10 pm

hi @ric. I'm using XC8 v1.38. My 'blink' programs work, but not usart.
newbie14
 
Posts: 3
Joined: Sun Aug 07, 2016 7:09 am
PIC experience: Experienced Hobbyist

Re: PIC18LF4680 and USART / serial communication with usb-to

Postby ric » Sun Aug 07, 2016 9:11 pm

In that case, I'd stick with the second version, as Microchip are killing off plib, but change:
Code: Select all
#include "fuses.h"
#include <p18lf4680.h>
#include <stdio.h>
#include <stdlib.h>

to
Code: Select all
#include "xc.h"
#include "fuses.h"


i.e. you don't need stdio or stdlib yet,
you should use xc.h instead of the PIC specific header file,
and the compiler should see xc.h before your CONFIG settings.

Also, try do to this WITHOUT interrupts first, just polling.

In answer to your first question, a direct TTL connection should work fine.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: PIC18LF4680 and USART / serial communication with usb-to

Postby newbie14 » Mon Aug 08, 2016 7:47 am

Hello @ric,
I took your suggestions into account, and made some updates to version 1. I modified the 'default:' condition in my switch statement with the following 'blink' loop

Code: Select all
default:

   while(1){
      LATBbits.LATB4 = 1;
      for(int x=0;x<=5;x++){__delay_ms(50);}
      
      while(BusyUSART()){}
      putsUSART((char *)txt1);
      
      LATBbits.LATB4 = 0;
      for(int x=0;x<=5;x++){__delay_ms(50);}
   }
   
break;


I also added LEDs to pins RB4 and TX to monitor the outcome.

Image

Everything appears to function as it should when I connect a 5v battery the microcontroller.
RB4 and TX blink at startup and in sync.

However, when I use the TTL device instead of the battery, the chip appears to 'crash'. LEDs are at a solid 'on' state, or sometimes they don't turn on at all.

Image

I began to suspect I may be connecting things incorrectly. I then found this schematic 'how to' for ttl devices and the ATMEGA1284 .

Image

Could this be the case for my controller? If so, do you happen to know where I could find documentation for my controller?

Thanks.
newbie14
 
Posts: 3
Joined: Sun Aug 07, 2016 7:09 am
PIC experience: Experienced Hobbyist

Re: PIC18LF4680 and USART / serial communication with usb-to

Postby ric » Mon Aug 08, 2016 8:08 am

So you are attempting to power the PIC from the USB-RS232 module?
Have you connected to BOTH sets of powr and gorund pins on the PIC? You should!
Have you put a 0.1uF cap close to each pair of power pins on the PIC? You should!
("Close" means "electrically close", i.e. with very short wires.)
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: PIC18LF4680 and USART / serial communication with usb-to

Postby Joseph Watson » Mon Aug 08, 2016 4:49 pm

For my 2 cents, I would mention a problem that I had when I tried to power a simple PIC18F4620 development board using one of these USB-to-TTL serial modules. I also had the TX and RX signals of the USB-to-TTL serial module connected to the PIC chip's RX and TX pins respectively. I had the odd problem that even when I switched off the power on the development board, it kept running anyway as long as the USB-to-TTL serial module stayed powered up via the USB.

The power LED stayed lit on the development board. Through the use of an oscilloscope, I could see that a small test program continued to run even with the development board's power switch turned off. At the time, the PIC chips I/O pins were essentially open so the PIC was not trying to drive any loads. It was not long before I figured out that the TX signal from the USB-to-TTL serial module was supplying the power through the PIC chip's RX pin protection diodes inside the PIC chip. Inserting a 10K resistor in series with the TX signal from the USB-to-TTL serial module dropped the current to a level where the PIC really did stop when the development board's power switch was off. I may also have added a resistor between the power rail and the ground rail on the development board to help assure that it really turned off.
NCR once refused to hire me because I was too short. I'm still waiting on my growth spurt.
User avatar
Joseph Watson
 
Posts: 49
Joined: Sat May 31, 2014 8:06 pm
Location: Ohio, USA
PIC experience: Experienced Hobbyist


Return to 16-Bit Core

Who is online

Users browsing this forum: No registered users and 12 guests

cron