matlab interface with pic16f877a

4 visualizaciones (últimos 30 días)
Shi Wei Foo
Shi Wei Foo el 19 de Mzo. de 2012
Hi there,
I want to make a simple program to tell my PIC which LED to turn on when I send an 'integer' data from my Matlab. Here's my matlab code. anything wrong with it?
serialport = serial('COM3');
set(serialport,'BaudRate',19200);
fopen(serialport);
fprintf (serialport,'1');
fclose(serialport);
and my PIC code is
#include <htc.h>
#include <pic.h>
__CONFIG(0x3F32);
#define LED1 RB6 //red LED1
#define LED2 RB7 //red LED2
unsigned int UART_read(void);
void UART_write(unsigned char data);
unsigned int rc_text,x;
unsigned int t_data;
// main function
//================================================================
void main(void)
{
TRISC6=0;
TRISC7=1;
TX9=0;
TXEN=1;
SYNC=0;
BRGH=1;
SPEN=1;
RX9=0;
CREN=1;
SPBRG=64;
TXIE=0;
RCIE=0;
TRISB=0b00000000;
while(1)
{
RCREG=0; //clear receive buffer
x=UART_read();
if (rc_text==1)
{
RB6 = 1;
}
else if (rc_text==2)
RB7 = 1;
else if (rc_text==3)
RB3 = 1;
if (RB4==1);
t_data = 999;
UART_write(t_data);
}
}
unsigned int UART_read(void)
{
while(RCIF==1)
rc_text = RCREG;
}
//function to write a byte out to UART TX
void UART_write(unsigned char data)
{
while(TXIF==0); //wait for the previous UART transmission to complete
TXREG=data; //write data for transmission
}
somebody please help!

Respuestas (1)

Walter Roberson
Walter Roberson el 19 de Mzo. de 2012
Your UART_read() code has no return statement in it. What value is it returning? What if RCIF starts out 0, then why is rc_text being left unchanged?
Your main() routine is looking for integer value 1, but your MATLAB code is writing the character '1' (integer value 49). Change your line
fprintf (serialport,'1');
to
fwrite(serialport, uint8(1));
  2 comentarios
Shi Wei Foo
Shi Wei Foo el 20 de Mzo. de 2012
Thanks Walter for reply.
the return statement is wrong. I've changed my code to this. Is this code correct?
while(1)
{
RCREG=0; //clear receive buffer
while(RCIF == 0);
x=UART_read();
if (x==1)
{
RB6 = 1;
}
else if (x==2)
RB7 = 1;
else if (x==3)
RB3 = 1;
if (RB4==1);
t_data = 999;
UART_write(t_data);
}
}
Thanks.
Walter Roberson
Walter Roberson el 20 de Mzo. de 2012
I do not know enough about the device to know if the revised code is correct.
You should not have the semi-colon after if (RB4==1) unless you want to do nothing in that case.
If you are trying to set t_data to 999 only in the case that RB4==1 then you have the problem that you are not setting t_data in any other condition.
Writing 999 to the UART is going to be a problem unless the MATLAB end is expecting to receive a 16 bit value and writing to the UART transmits a 16 bit value. If the UART only transmits 8 bits (which would be consistent with the rest of what you are doing), then the value transmitted would be 231, which is 999 mod 256.

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by