Autor Tema: COMUNICACION SERIAL CON MATLAB2015  (Leído 1766 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado mariner776

  • PIC10
  • *
  • Mensajes: 1
COMUNICACION SERIAL CON MATLAB2015
« en: 18 de Junio de 2015, 22:29:51 »
hola a todo el mundo  :).
Lo que estoy tratando de hacer es una comunicacion serial de un PIC16f877A hacia la computadora (MATLAB) como puente estoy usando un circuito el conversor FTDI232R, como programa estoy usando el MPLAB X con el compilador XC8, como podran observar mas adelante lo que estoy tratando de hacer, obtener un valor de una conversion A/D previa y primeramente enviarlo  una pantalla LCD 16x2 y seguidamente hacer uso del puerto serial para enviar este valor hacia la computadora, sin embargo el programa aunque compila sin errores no "comunica" el valor q quiero q aparezca en el matlab, haciendo referencia en este programa (matlab): "Warning: Unexpected Warning: A timeout occurred before the Terminator was reached." el codigo que hice es el siguiente:

#define _XTAL_FREQ 5000000

#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#include <xc.h>;
#include "lcd.h";
#include<stdio.h>;
#include "uart.h"

// BEGIN CONFIG
#pragma config FOSC = XT        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

void main()
{
    unsigned int u;
    float f;
    float v;
    char s[20];
    char q[20];
    int a;
    int b=0;
    ADCON0bits.ADCS  = 0;   //Selecting the clk division factor = FOSC/2
    ADCON1bits.ADCS2 = 0;   //Selecting the clk division factor = FOSC/
    ADCON1bits.ADFM = 1;    //Result right justified
    ADCON1bits.PCFG = 0;    //Setting all the analog ports as Analog inputs
    ADCON0bits.ADON = 1;    //Turns on ADC module
    ADCON0bits.CHS = 0;     //Selects channel 0 ( AN0 )
    TRISD = 0x00;
    Lcd_Init();
    UART_Init(9600);
    while(1)
    {
       ADCON0bits.GO = 1;
       while (ADCON0bits.nDONE) continue;   //wait till ADC conversion is over
       f = (ADRESH<<8) + ADRESL ;   //Merging the MSB and LSB
       v=f*5/1023;
       sprintf(s, "VALOR = %2.2g", v);
       Lcd_Set_Cursor(1,1);
       Lcd_Write_String(s);
       sprintf(q, "Integer = %f", f);
       Lcd_Set_Cursor(2,1);
       Lcd_Write_String(q);
       __delay_ms(50);
       nRBPU=0; //enables portb internal pull up resistors
       UART_Write(f);
       __delay_ms(50);
       
       
           
           
           
     
       
    }
   
}

Para la configuracion del puerto serial "UART.h":

char UART_Init(const long int baudrate)
{
   unsigned int x;
   x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
   if(x>255)
   {
      x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
      BRGH = 1;
   }
   if(x<256)
   {
     SPBRG = x;
     SYNC = 0;
     SPEN = 1;
          TRISC7 = 1;
          TRISC6 = 1;
          CREN = 1;
          TXEN = 1;
     return 1;
   }
   return 0;
}

char UART_TX_Empty()
{
  return TRMT;
}

char UART_Data_Ready()
{
   return RCIF;
}
char UART_Read()
{
 
  while(!RCIF);
  return RCREG;
}

void UART_Read_Text(char *Output, unsigned int length)
{
   unsigned int i;
   for(int i=0;i<length;i++)
      Output = UART_Read();
}

void UART_Write(char data)
{
  while(!TRMT);
  TXREG = data;
}

void UART_Write_Text(char *text)
{
  int i;
  for(i=0;text!='\0';i++)
     UART_Write(text);
}

sospecho que no estoy haciendo una conversion del valor FLOAT a un valor que el puerto serial del MATLAB entienda, la verdad no se y estoy estancado aqui, espero q alguno de ustedes me pueda ayudar, GRACIAS DE ANTEMANO

he usado el VIRTUAL TERMINAL del proteus y obtengo valores ALEATORIOS, es decir cuando cambio el voltaje de entrada del ADC tambien cambia lo que envia el PIC sin embargo son caracteres que no tienen que ver con el valor de lectura del ADC.

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re: COMUNICACION SERIAL CON MATLAB2015
« Respuesta #1 en: 19 de Junio de 2015, 15:32:40 »
Intenta agregarle un \r\n a tus sprintf o printf

Busca un indicador de fin de mensaje, yo crei que lo enviaba, pero por las dudas envialo.
Por otro lado no sabia que el matlab podia usar el RS232.


 

anything