CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Communication between PIC 16f877a and MATLAB
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

Communication between PIC 16f877a and MATLAB
PostPosted: Tue Apr 28, 2020 12:38 am     Reply with quote

Hi,
I am self-studying PIC with CCS 5.0. I am working on sending/receiving data to/from PIC over MATLAB. Currently I am stuck in the first step.
My goal is to send a string from PIC 16f877a over a RS-232 port to PC. On PC, use MATLAB to read this strings. For now it just simple as that. I read the Help from CCS and my workaround is as follows (using puts method after declaring use RS232):

Code:
#include <16f877a.h>
#use delay(clock=4000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use RS232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7,bits=8,parity=N)
void main(void)
{
   puts("123");
}


On MATLAB, my code:
Declare used serial port :
Code:
>> s = serial('COM5')

Check information for matching with PIC
Code:
>> get(s)
    ByteOrder = littleEndian
    BytesAvailable = 0
    BytesAvailableFcn =
    BytesAvailableFcnCount = 48
    BytesAvailableFcnMode = terminator
    BytesToOutput = 0
    ErrorFcn =
    InputBufferSize = 512
    Name = Serial-COM5
    ObjectVisibility = on
    OutputBufferSize = 512
    OutputEmptyFcn =
    RecordDetail = compact
    RecordMode = overwrite
    RecordName = record.txt
    RecordStatus = off
    Status = closed
    Tag =
    Timeout = 10
    TimerFcn =
    TimerPeriod = 1
    TransferStatus = idle
    Type = serial
    UserData = []
    ValuesReceived = 0
    ValuesSent = 0

    SERIAL specific properties:
    BaudRate = 9600
    BreakInterruptFcn =
    DataBits = 8
    DataTerminalReady = on
    FlowControl = none
    Parity = none
    PinStatus = [1x1 struct]
    PinStatusFcn =
    Port = COM5
    ReadAsyncMode = continuous
    RequestToSend = on
    StopBits = 1
    Terminator = LF

Open port:
Code:
>> fopen(s)


Read data:
Code:
>> fread(s)
Warning: The specified amount of data was not returned within the Timeout period.
'serial' unable to read any data. For more information on possible reasons, see Serial Read Warnings.

ans =

  1×0 empty double row vector


I searched on some Internet sources and also tried:
Code:
>> fgets(s)
Warning: A timeout occurred before the Terminator was reached.
'serial' unable to read all requested data. For more information on possible reasons, see Serial Read Warnings.

ans =

    'õõõõõõõõõõõõ'


In both cases it shows nothing or some weird characters.
Could you give me some suggestions on this problem? Thank you very much for your time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 12:44 am     Reply with quote

Try it as follows, with the while(TRUE) at the end of main():
Code:

#include <16f877a.h>
#fuses NOWDT, XT, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4M)

#use RS232 (baud=9600, UART1, Errors)

//=====================
void main(void)
{
puts("123");

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 12:58 am     Reply with quote

The other thing is this:

#use delay(clock=4000000)
#fuses NOWDT,HS,NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
These two lines, may slightly 'disagree' with one another.

Weird characters could be saying that the PIC is not actually running at the
speed you are telling the code it is running at.

Now HS is the oscillator selection for a crystal running at >=4MHz, but
on most chips is only used if you are running above 4Mhz. Yet you are
saying you are running at 4Mhz. So what crystal is actually on the board
you are using?.
Also with a crystal, you should always use PUT.

Note that PCM has selected XT in his version... Very Happy

However he has left PUT off. The first character at least, risks
corruption starting off a crystal without PUT.
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 1:23 am     Reply with quote

Ttelmah wrote:
The other thing is this:

#use delay(clock=4000000)
#fuses NOWDT,HS,NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
These two lines, may slightly 'disagree' with one another.

Weird characters could be saying that the PIC is not actually running at the
speed you are telling the code it is running at.

Now HS is the oscillator selection for a crystal running at >=4MHz, but
on most chips is only used if you are running above 4Mhz. Yet you are
saying you are running at 4Mhz. So what crystal is actually on the board
you are using?.
Also with a crystal, you should always use PUT.

Note that PCM has selected XT in his version... Very Happy

However he has left PUT off. The first character at least, risks
corruption starting off a crystal without PUT.


Thank you PCM programmer and Ttelmah for your prompt replies. The actual crystal on the board is 4MHz, I have omitted the HS in the definition and also added PUT. I'm not sure what's wrong now because in MATLAB it shows empty strings when I use the new code:
Code:

>> fgets(s)
Warning: A timeout occurred before the Terminator was reached.
'serial' unable to read any data. For more information on possible reasons, see Serial Read Warnings.

ans =

  0×0 empty char array
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 2:12 am     Reply with quote

Quote:
in MATLAB it shows empty strings when I use the new code.

Post the new PIC code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 2:48 am     Reply with quote

#fuses NOWDT,XT, PUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT

Note the two changes.
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 2:51 am     Reply with quote

PCM programmer wrote:
Quote:
in MATLAB it shows empty strings when I use the new code.

Post the new PIC code.


It is actually your code, I only change NOPUT to PUT following Ttelmah.

Code:
#include <16f877a.h>
#fuses NOWDT, XT, PUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4M)

#use RS232 (baud=9600, UART1, Errors)

//=====================
void main(void)
{
puts("123");

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 2:55 am     Reply with quote

OK.

Understand.

PCM_Programmers code prints the string _once_. Once only.

You say
Quote:
empty strings
, implies you are reading multiple
times after this single transmission, and these _will_ be 'empty'....
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 5:20 am     Reply with quote

I'd have main()..

forever loop..
send 123
delay 1 second
forever loop..

By having your proram send the data forever at a 1 hz rate you have some time to see what's happening, using a scope...

Also, I'd use a terminal program , like RealTerm, FIRST, before using MATLAB. Terminal programs will be easier to setup and can confirm the PIC is working properly. So, I'd run the terminal program, connect the PC serial TX pin to the PC serial RX pin(aka loopback ). Typing on PC KBD should be seen in the PC screen. That will confirm the terminal program is working. NOW connect PC to PIC. The PC screen should show whatever the PIC is sending '123', in your case....

After this, then configure /run your MATLAB program. if it doesn't work properly, you now KNOW it's 'something' in the MATLAB code..
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 5:34 am     Reply with quote

There is one glaring thing:

In the MPLAB setup:

Terminator = LF

Nowhere in the code, do you send a LF....
Code:

#include <16f877a.h>
#fuses NOWDT, XT, PUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4M)

#use RS232 (baud=9600, UART1, Errors)

//=====================
void main(void)
{
    while (TRUE)
    {
        puts("123\n"); //note the '\n' LF
        delay_ms(250);
    }
}
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 6:34 am     Reply with quote

Thank you everyone for your kind help. The information you guys gave me would help a lot! I will try to get back to the topic if I have progress. I am reading some more documents regarding this issues (some parts in the UART are still foggy to me :D).
Ttelmah



Joined: 11 Mar 2010
Posts: 19541

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 7:01 am     Reply with quote

Have you tried sending the terminator?.

You are sending 123123123123123
No spaces or terminators between the numbers. No wonder MATLAB
can't work out what it is meant to decode.....
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 8:37 am     Reply with quote

Ttelmah wrote:
Have you tried sending the terminator?.

You are sending 123123123123123
No spaces or terminators between the numbers. No wonder MATLAB
can't work out what it is meant to decode.....


I would like to describe the problem carefully there:
I have checked on the datasheet of my development board, it shows that the crystal clock is 4MHz.


If I use your suggested code (FUSES XT). Nothing happens.
If I change XT to HS. I used the Realterm as suggested by temtronic (thank you very much) and I believe it begins to send data through the serial port.
But the sent data again, are some weird characters.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 8:41 am     Reply with quote

Quote:
I have checked on the datasheet of my development board

Post the name or model number of your development board, and post a
link to the website for the board.
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 8:49 am     Reply with quote

PCM programmer wrote:
Quote:
I have checked on the datasheet of my development board

Post the name or model number of your development board, and post a
link to the website for the board.


It looks like a hand made board, I don't see any information on the board and the datasheet about the manufacturer. (I borrow it from someone else)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group