[SOLVED] PIC 16F887 and MATLAB Interfacing using RS232

Status
Not open for further replies.

nitishn5

Full Member level 6
Joined
Mar 27, 2011
Messages
375
Helped
92
Reputation
186
Reaction score
101
Trophy points
1,323
Location
Bangalore, India, India
Activity points
4,211
I am trying to interface an 16F887 and MATLAB using the serial port.
I am using the CCS C compiler.

I am able to run the ex_float.c example using hyperterminal.

But when I tried the following code to get 3 characters from MATLAB, it is giving weird numbers.

Code:
#include<16F887.h>
#fuses HS, NOWDT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay (clock=4000000)
#use rs232(baud=9600, xmit=PIN_E2, rcv=PIN_E1, stream=COM1, ERRORS)
#include <stdlib.h>
#include<flex_lcd.c>
#include <INPUT.c>
float a = 0;
float b = 0;
float c = 0;
void main()
{
	delay_us(50);
	output_low(PIN_D7);
	lcd_init();
	lcd_putc("\fReady");
	a = get_float();
	b = get_float();
	c = get_float();

	while(TRUE)
	{
		printf(lcd_putc,"\f A = %f", a);
		delay_ms(500);
		printf(lcd_putc,"\f B = %f", b);
		delay_ms(500);
		printf(lcd_putc,"\f C = %f", c);
		delay_ms(500);
	}
}

In MATLAB I run the following after the LCD shows "Ready"

Code:
clc; clear all;
sercon = serial('COM1',...
    'ByteOrder', 'littleEndian',...
    'BaudRate', 9600,...
    'DataBits', 8,...
    'DataTerminalReady', 'on',...
    'FlowControl', 'none',...
    'Parity', 'none',...
    'RequestToSend', 'off',...
    'StopBits', 1,...
    'Terminator', 'LF');

fopen(sercon);

% fprintf(sercon, '%s\f', '');
pause(0.5);
fprintf(sercon, '12.3');
pause(0.5);
fprintf(sercon, '23.4');
pause(0.5);
fprintf(sercon, '154.6');
pause(0.5);

fclose(sercon);
delete(sercon);


I am getting numbers that are way off. for the above case, I have to run the matlab code 4-5 times to get the mcu to respond. And then I get values such as A = 1.0, B=1.0, C = 1461.0.

I tried with integer numbers and the problem is similar. The resulting numbers are different that those entered.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…