Quiero saber si es posible conectar un dispositivo ttl directamente a mi PIC18LF4680 para comunicación en serie a través de USART. Puedo conectar el dispositivo ttl directamente a mi Arduino Uno sin problemas. Este es mi hardware:
Eldispositivottltiene6pines.(dtr,rxd,txd,vcc(3.3vo5v),cts,gnd).
TengodosfragmentosdecódigodiferentesacontinuaciónquerealizanlacomunicaciónUSART.
Laversiónuno(I)utilizalabibliotecadeperiféricos"usart.h". La versión dos (II) utiliza "TXREG" y "RCREG" para enviar y recibir datos.
Ambas versiones funcionan bien en mi entorno virtual (proteus 8 professional), Pero no en el entorno del mundo real. ¿Me estoy perdiendo un paso? Necesito un especial ¿biblioteca? ¿O no es posible con este chip?
Versión 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;
}
}
}
-------------------------------------------- --------- Versión 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
}
}
Toda la ayuda es apreciada. Gracias!