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 support@ccsinfo.com

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

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

Look at the crystal. Post the markings that you see on the crystal.
Also post the markings on the capacitors for the crystal.

Or post a link to a sharp, infocus, hires photo of the crystal and capacitors.
Use an image hosting website such as imgur.com to hold the image.
temtronic



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

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 9:59 am     Reply with quote

I'd change the Realterm baud rate, say to 4800, 19200, etc. and see if you get the correct '123' data.

let's say at 4800, you do get 123...
then you know the speed of the clock is off exactly by a factor of 2, so you may have an 8MHz xtal. The other common xtal was 20 MHz.

Hopefully you can SEE the number on the xtal though, something like 4.00, 8.00, 20.0. My like would be the xtal is soldered to the PCB with the marking towards the PCB......
Ttelmah



Joined: 11 Mar 2010
Posts: 19222

View user's profile Send private message

PostPosted: Tue Apr 28, 2020 11:28 am     Reply with quote

Are you sure Y1, is actually a crystal?. Reason I ask, is HS turns up the
drive to the oscillator. Now, ceramic resonators often need a higher
drive than genuine crystals.
I really would look at the crystal with a magnifier, and see what it says.

It wouldn't be beyond belief that somebody has substituted a faster
crystal than the circuit shows. This then needs the higher drive, and
gives the wrong baud rate.

Do the most basic test. The 'flash an LED' program. Simple code to
turn a pin on for one second, then off for one second. Test with a scope
or by putting an LED (with a suitable resistor) on the pin. Time how long
ten flashes take.

I suspect you are not actually running at 4MHz.

Other possibility is your connection is wrong.
How are you connecting to the PC?.
Presumably something like a USB to serial adapter?.
If so is this USB-RS232 or USB-TTL.
If it is USB-RS232, you need an RS232 adapter between the PIC
and the module.

You may simply have inverted data.
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Wed Apr 29, 2020 1:30 am     Reply with quote

To PCM Programmer, Ttelmah, temtronic : The problem is I didn't check the REAL crystal carefully. Thank all of you for those suggestions!
I just borrowed a magnifier and check on the crystal, it said 8 0 0. So I change the frequency to 8M in CCS. This is a quite valuable experience to me.
Everything now works like a charm.
I also make change to the program, now the PIC sends data everytime I push a button instead of constantly sending data. The result is now good.



Thanks temtronic for your suggestion about RealTerm. It saves a lot of time.

Also in MATLAB, now it receives the strings sending from the PIC. I would like to ask one more question:


In MATLAB, I successfully receive the strings, but it is in form

Code:
'string
'

The second single quotation mark is on another line as shown in the photo.
I check both code:
Code:
puts("123\n");

and
Code:
puts("123");

In MATLAB I always receive the same results (while In Realterm it displays correctly). Could anyone explain this?
Ttelmah



Joined: 11 Mar 2010
Posts: 19222

View user's profile Send private message

PostPosted: Wed Apr 29, 2020 1:45 am     Reply with quote

It had all the symptoms of 'wrong frequency'... Very Happy

Glad you have found it. Lesson is 'check everything'....

On the numbers, I suspect somewhere in the MPLAB settings, you are telling
it to only use 3 digits, so it is just taking 3 digits, and then accepting this
without needing the terminator.
temtronic



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

View user's profile Send private message

PostPosted: Wed Apr 29, 2020 4:45 am     Reply with quote

Hay, it's GREAT to see you got it working !!! 8.00 MHz is another 'popular' xtal for PICs.
I used to put a small drop of nail polish on the top of xtals to colour code them. yellow = 4 MHz.
S1gnature



Joined: 28 Apr 2020
Posts: 22

View user's profile Send private message

PostPosted: Wed Apr 29, 2020 11:33 am     Reply with quote

Thank you temtronic and Ttelmah Surprised. I feel great when everything works too, even it's just my curiosity about how this thing works. I borrow the development board from my nephew as he left it in the store room. This is the first time I work on both software and hardware. Now I'm trying sending data from PC to PIC to control some devices. It is quite interesting.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 29, 2020 4:39 pm     Reply with quote

S1gnature wrote:

In MATLAB, I successfully receive the strings, but it is in form

'string
'

The second single quotation mark is on another line as shown in the photo.
Could anyone explain this?

The single quotes are put there by Matlab. They are not caused by CCS.

This Matlab forum post discusses it somewhat:
https://www.mathworks.com/matlabcentral/answers/344657-single-quotes-around-dataset-elements-in-2017a

Regarding the reason why the 2nd single quote is on the next line:
It's possibly because your string buffer is long and Matlab is wrapping
the 2nd quote to the next line. I'm just guessing.
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 Previous  1, 2
Page 2 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