Buenas tardes, les hago un consulta porque ya hace varios dias que estoy con este problema y no puedo solucionar, realizo la comunicacion serial entre los pic y envio un valor entero para luego mostrarlo en el otro pic con un display, el dato que yo quiero que se muestre en el otro pic no muestra el mismo valor, pero si en el hyperterminal, en este punto no me estoy dando cuenta del error o el mal uso de sprint por ejemplo, les dejo los codigos y una captura de proteus, desde ya muchas gracias por ayuda.
Pic utilizados: 16f883
Compilardor: Mplab xc8
TX
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#define _XTAL_FREQ 20000000
#include "Usart.h"
void main(void)
{
int b = 16;
char q[20];
/*Inicializacion de comunicacion USART*/
Usart_int();
do
{
sprintf(q,"%d",b);
Usart_sent_string(q);
}while(1);
}
#ifndef USART_H
#define USART_H
void Usart_int();//función para iniciar el USART PIC asíncron, 8 bits, 9600 baudios
unsigned char Usart_read();//función para la recepción de caracteres
void Usart_sent_character(unsigned char);//función para la transmisión de caracteres
void Usart_sent_string(char*);//función para la transmisión de cadenas de caracteres
/*
Inicialización del módulo USART PIC modo asíncrono
en una función, a 8bits,a 9600 baudios */
void Usart_int(){
TRISCbits.TRISC7=1;//pin RX como una entrada digital
TRISCbits.TRISC6=0;//pin TX como una salida digital
TXSTAbits.CSRC = 0;//modo asincronico
TXSTAbits.TX9 = 0;//selecion modo de transimicion 8bit.
TXSTAbits.TXEN = 1;//se habilita la transimcion
TXSTAbits.SYNC = 0;//la transmicion sera asincronica
TXSTAbits.SENDB = 0;//no habilito el control por hardware
TXSTAbits.BRGH = 1;//modo de transmicion de alta velocidad
TXSTAbits.TRMT = 1;//se lo pondrá a 1 porque se está iniciando y tendría que estar vacío.
TXSTAbits.TX9D = 0;//el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
RCSTAbits.SPEN = 1;//se pondrá a 1 para habilitar el uso del módulo USART PIC.
RCSTAbits.RX9 = 0;//la recepción para que sea a 8 bits.
RCSTAbits.SREN = 0;//modo asincronico
RCSTAbits.CREN = 1;//se habilita la recepcion
RCSTAbits.ADDEN = 0;//recepción será a 8 bit.
RCSTAbits.FERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha recibido un dato no válido.
RCSTAbits.OERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha producido un error por sobreescritura de algún dato recibido
RCSTAbits.RX9D = 0;////el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
SPBRG = 129;//para una velocidad de 2400baudios con un oscilador de 20Mhz*/
}
/*
Recepción de datos del módulo USART PIC modo asíncrono
*/
unsigned char Usart_read()
{
if(PIR1bits.RCIF==1)//si el bit5 del registro PIR1 se ha puesto a 1
{
return RCREG;//devuelve el dato almacenado en el registro RCREG
}
}
/*
Transmisión de datos del módulo USART PIC modo asíncrono
*/
void Usart_sent_character(unsigned char chtr)
{
while(TXSTAbits.TRMT==0);// mientras el registro TSR esté lleno espera
TXREG = chtr;//cuando el el registro TSR está vacio se envia el caracter
}
/*
Transmisión de cadenas de caracteres con el módulo USART PIC modo asíncrono
*/
void Usart_sent_string(char* cadena)//cadena de caracteres de tipo char
{
while(*cadena !=0x00)//mientras el último valor de la cadena sea diferente de el caracter nulo
{
Usart_sent_character(*cadena);//transmite los caracteres de cadena
cadena++;//incrementa la ubicación de los caracteres en cadena para enviar el siguiente caracter de cadena
}
}
#endif /* USART_H */
RX
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#define _XTAL_FREQ 20000000
#include "Lcd_20x4.h"
#include "Usart.h"
void main(void)
{
ANSELH = 0x00;
unsigned char dato;
char q[20];
/*Inicializacion de comunicacion USART*/
Usart_int();
/*Inicializacion del LCD*/
Lcd_int();
do
{
dato = Usart_read();
sprintf(q,"Resultado:%d",dato);
Lcd_gotoxy(1,1);
Lcd_printf(q);
}while(1);
}
#ifndef LCD20x4
#define LCD20x4
#define PIN_RS PORTBbits.RB0
#define TRISBRS TRISBbits.TRISB0
#define PIN_EN PORTBbits.RB1
#define TRISBEN TRISBbits.TRISB1
#define LCD_D4 PORTBbits.RB4
#define LCD_D4_T TRISBbits.TRISB4
#define LCD_D5 PORTBbits.RB5
#define LCD_D5_T TRISBbits.TRISB5
#define LCD_D6 PORTBbits.RB6
#define LCD_D6_T TRISBbits.TRISB6
#define LCD_D7 PORTBbits.RB7
#define LCD_D7_T TRISBbits.TRISB7
/*Declaracion de funciones*/
void Lcd_int();
void Lcd_control_cmd(char);
void Lcd_port(char);
void Lcd_write_data_port(char);
void Lcd_printf(char*);
void Lcd_clear();
void Lcd_printf_String(char*);
void Lcd_gotoxy(char , char);
/*Funciones*/
void Lcd_int() //configuracion para inicializar el LCD
{
PIN_RS = 0;
PIN_EN = 0;
TRISBRS = 0;
TRISBEN = 0;
LCD_D4 = 0;
LCD_D4_T = 0;
LCD_D5 = 0;
LCD_D5_T = 0;
LCD_D6 = 0;
LCD_D6_T = 0;
LCD_D7 = 0;
LCD_D7_T = 0;
Lcd_port(0x00);
__delay_ms(20);
Lcd_control_cmd(0x03);
__delay_ms(5);
Lcd_control_cmd(0x03);
__delay_ms(11);
Lcd_control_cmd(0x03);
Lcd_control_cmd(0x02);
Lcd_control_cmd(0x02);
Lcd_control_cmd(0x08);
Lcd_control_cmd(0x00);
Lcd_control_cmd(0x0C);
Lcd_control_cmd(0x00);
Lcd_control_cmd(0x06);
}
void Lcd_control_cmd(char data) //pines de control para LCD
{
PIN_RS = 0;
Lcd_port(data);
PIN_EN = 1;
__delay_ms(4);
PIN_EN = 0;
}
void Lcd_port(char data) //
{
if(data & 1)
{
LCD_D4 = 1;
}
else
{
LCD_D4 = 0;
}
if(data & 2)
{
LCD_D5 = 1;
}
else
{
LCD_D5 = 0;
}
if(data & 4)
{
LCD_D6 = 1;
}
else
{
LCD_D6 = 0;
}
if(data & 8)
{
LCD_D7 = 1;
}
else
{
LCD_D7 = 0;
}
}
void Lcd_write_data_port(char data)//Modo de 4 bits LCD
{
char var;
char y;
var = (data & 0x0F);
y = (data & 0xF0);
PIN_RS = 1;
Lcd_port(y>>4);
PIN_EN = 1;
__delay_us(40);
PIN_EN = 0;
Lcd_port(var);
PIN_EN = 1;
__delay_us(40);
PIN_EN = 0;
}
void Lcd_printf(char *data)//Funcion para imprir lo que queremos ver en el LCD
{
while (*data) // Mientras no sea Null
{
Lcd_write_data_port(*data); // Envio el dato al LCD
data++; // Incrementa el buffer de dato
}
}
void Lcd_gotoxy(char x, char y)//Funcion de la posicion de posicion en el LCD
{
char temp;
char dato1;
char dato2;
if(y == 1)
{
temp = 0x80 + x - 1;
dato1 = temp >> 4;
dato2 = temp & 0x0F;
Lcd_control_cmd(dato1);
Lcd_control_cmd(dato2);
}
if(y == 2)
{
temp = 0xC0 + x - 1;
dato1 = temp >> 4;
dato2 = temp & 0x0F;
Lcd_control_cmd(dato1);
Lcd_control_cmd(dato2);
}
if(y == 3)
{
temp = 0x94 + x - 1;
dato1 = temp >> 4;
dato2 = temp & 0x0F;
Lcd_control_cmd(dato1);
Lcd_control_cmd(dato2);
}
if(y == 4)
{
temp = 0xD4 + x - 1;
dato1 = temp >> 4;
dato2 = temp & 0x0F;
Lcd_control_cmd(dato1);
Lcd_control_cmd(dato2);
}
}
void Lcd_clear()//Funcion para limpiar pantalla
{
Lcd_control_cmd(0);
Lcd_control_cmd(1);
}
void Lcd_printf_String(char *data)//Funcion imprime string
{
int i;
for(i=0;data[i]!='\0';i++)
Lcd_write_data_port(data[i]);
}
/*
* Guardar caracteres especiales. en la CGRAM
*/
void lcd_put_caracter(char adress, char caracter[]) {
int i;
Lcd_control_cmd(0x40 + (adress * 8));
for (i = 0; i < 8; i++) {
Lcd_write_data_port(caracter[i]);
}
}
void Lcd_Shift_Right()
{
Lcd_control_cmd(0);
Lcd_control_cmd(1);
Lcd_control_cmd(0x0C);
}
void Lcd_Shift_Left()
{
Lcd_control_cmd(0);
Lcd_control_cmd(1);
Lcd_control_cmd(0x08);
}
#endif
#ifndef USART_H
#define USART_H
void Usart_int();//función para iniciar el USART PIC asíncron, 8 bits, 9600 baudios
unsigned char Usart_read();//función para la recepción de caracteres
void Usart_sent_character(unsigned char);//función para la transmisión de caracteres
void Usart_sent_string(char*);//función para la transmisión de cadenas de caracteres
/*
Inicialización del módulo USART PIC modo asíncrono
en una función, a 8bits,a 9600 baudios */
void Usart_int(){
TRISCbits.TRISC7=1;//pin RX como una entrada digital
TRISCbits.TRISC6=0;//pin TX como una salida digital
TXSTAbits.CSRC = 0;//modo asincronico
TXSTAbits.TX9 = 0;//selecion modo de transimicion 8bit.
TXSTAbits.TXEN = 1;//se habilita la transimcion
TXSTAbits.SYNC = 0;//la transmicion sera asincronica
TXSTAbits.SENDB = 0;//no habilito el control por hardware
TXSTAbits.BRGH = 1;//modo de transmicion de alta velocidad
TXSTAbits.TRMT = 1;//se lo pondrá a 1 porque se está iniciando y tendría que estar vacío.
TXSTAbits.TX9D = 0;//el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
RCSTAbits.SPEN = 1;//se pondrá a 1 para habilitar el uso del módulo USART PIC.
RCSTAbits.RX9 = 0;//la recepción para que sea a 8 bits.
RCSTAbits.SREN = 0;//modo asincronico
RCSTAbits.CREN = 1;//se habilita la recepcion
RCSTAbits.ADDEN = 0;//recepción será a 8 bit.
RCSTAbits.FERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha recibido un dato no válido.
RCSTAbits.OERR = 0;//este bit trabaja automáticamente cuando se pone a 1 indica que se ha producido un error por sobreescritura de algún dato recibido
RCSTAbits.RX9D = 0;////el bit de paridad en la transmisión a 8 bits no influye y se lo pondrá a 0.
SPBRG = 129;//para una velocidad de 2400baudios con un oscilador de 20Mhz*/
}
/*
Recepción de datos del módulo USART PIC modo asíncrono
*/
unsigned char Usart_read()
{
if(PIR1bits.RCIF==1)//si el bit5 del registro PIR1 se ha puesto a 1
{
return RCREG;//devuelve el dato almacenado en el registro RCREG
}
}
/*
Transmisión de datos del módulo USART PIC modo asíncrono
*/
void Usart_sent_character(unsigned char chtr)
{
while(TXSTAbits.TRMT==0);// mientras el registro TSR esté lleno espera
TXREG = chtr;//cuando el el registro TSR está vacio se envia el caracter
}
/*
Transmisión de cadenas de caracteres con el módulo USART PIC modo asíncrono
*/
void Usart_sent_string(char* cadena)//cadena de caracteres de tipo char
{
while(*cadena !=0x00)//mientras el último valor de la cadena sea diferente de el caracter nulo
{
Usart_sent_character(*cadena);//transmite los caracteres de cadena
cadena++;//incrementa la ubicación de los caracteres en cadena para enviar el siguiente caracter de cadena
}
}
#endif /* USART_H */