PDA

View Full Version : Serial data sending#do someone came across programming 16f676 with TRH031M?



Hanson
- 11th April 2014, 02:01
Dear experts,

Currently I am trying to program pic16f676 as a microcontroller to send serial data from TRH031M (kinda 13.56MHz transmitter)
http://www.farnell.com/datasheets/460824.pdf

Im using SPI serial interface in TRH031M and I faced difficulties in figuring out my coding. Kindly help.

Charlie
- 12th April 2014, 15:02
The answer is obvious. There's an error in your code.

Hanson
- 13th April 2014, 16:19
Dear Charlie,

I am new in picbasicpro. Where can i find a sample SPI coding to be referred? Currently Im using TRH031M as "slave" in SPI mode and pic16f877 as a MCU (master in SPI mode)

HenrikOlsson
- 13th April 2014, 16:57
Hi,
Start by taking a look in the PBP3 installation folder. There's sub-folder called EXAMPLES\GENERAL PBP.
The file shift.pbp demonstrates how to use the SHIFTIN/SHIFTOUT commands and the files spimast.pbp and spislave.pbp demonstrates how to use the SSP/MSSP module in the PIC to do SPI at the hardware level.

The PBP manual covers the use of SHIFTIN/SHIFTOUT and there should be plenty of example code around the forum. Probably not exactly the code you need but plenty to get ideas from. If you get stuck post specific questions and you'll most likely get plenty of help.

With that said, if you're new to PBP then perhaps getting up to speed by interfacing a simple shiftregister or something as a start might be a good idea.

/Henrik.

Hanson
- 15th April 2014, 09:25
Hi Henrik,

As per my understanding, to perform SPI between a master and a slave, 2 programmable PICs are needed (1 for master& 1 for slave) while TRH031M I am using is kinda transmitter which cannot be programmed. So, can I send only data from my PIC16f877(master) to TRH031M without programming TRH031M? Kindly advice!

Thanks.

HenrikOlsson
- 15th April 2014, 10:09
Hi,
With SPI there's always one master and one or more slaves. The master is always responsible for generating the clock. In your case the PIC would be the master and the TRH031M would be the slave. Master/slave has nothing to do with the direction of the data, it can go from master to slave, from slave to master or both ways.

So, yes, of course you can send and receive data to/from the TRH031M without reprogramming it - it's already "programmed" to act as a slave.

/Henrik.

Hanson
- 16th April 2014, 08:54
Hi Henrik,

Thank you so much for the clarification made. Considering "slave(trh031m)" already received data from "master(16f877)" and I wish to send received data through TX1 of TRH031M, still I need to program one PIC to perform this task, isn't it?

Regards

HenrikOlsson
- 16th April 2014, 10:20
Hi,
Yes, of course, you need to program the PIC to send and receive commands and data to/from the TRH031M according to the datasheet.

/Henrik.

Hanson
- 22nd April 2014, 07:36
Hi Henrik,

Let's say Im now sending a serial data "Hanson" and i set my start frame as "AAAAA" and end frame as "EEEEE". So the serial data Im sending is like:
"AAAAAHANSONEEEEE"

at sending side:

start:
SEROUT dataout,N300,["AAAAA","HANSON", "EEEEE"]
pause 100
goto start

My problem is at the receiving side, I wish to filter my received data before storing them. The only solution I have thought was "nested IF loop" but microcode studio don't support this kinda command rather just "IF...THEN". Right?
so, how can I write my coding to perform this data filtering task?
eg:
check if data_in[0]="A" ;if yes then proceed checking for second "A"
check if data_in[1]="A" ;if yes then proceed checking for third "A"
check if data_in[2]="A" ;if yes then proceed checking for forth and fifth "A"
check if data_in[13]="E" ;if yes then check for second "E"
.
.
.
check if data_in[16]="E" ;if all conditions matched (receive first 5 "A" and last 5 "E" properly) then store data_in
else loop back subroutine and repeat checking for first "A"

Regards

HenrikOlsson
- 22nd April 2014, 08:27
Hi,
Not sure what you mean by The only solution I have thought was "nested IF loop" but microcode studio don't support this kinda command rather just "IF...THEN".
First of all MicroCodeStudio is just the editor, the support or lack there of for any commands etc is down to the compiler - which is PBP. And yes, PBP supports nested IF/THEN

If myData[0] = "A" THEN
If myData[1] = "B" THEN
If myData[2] = "C" THEN
' Do whatever
ENDIF
ENDIF
ENDIF

And yes PBP supports IF/THEN in loops...


i VAR BYTE
DataIn VAR BYTE[16]
ActualData VAR BYTE [6]

' DataIn is 16 bytes containg A A A A A H A N S O N E E E E E

' Verify that the first 5 bytes are all "A"
For i = 0 to 4
If DataIn[i] <> "A" THEN GOTO Start ' Nope, not qualified, start over...
NEXT

' Verify that the last 5 bytes are all "E"
For i = 11 to 15
If DataIn[i] <> "E" THEN GOTO Start ' Nope, not qualified, start over...
NEXT

' Yes, header and footer qualified. Now extract actual data, 6 bytes, store in array ActualData
For i = 5 to 10
ActualData[i-5] = DataIn[i]
NEXT


You could/should also look into the WAIT or possibly WAITSTR modifiers of the SERIN command. Or, once you've got all the data in the array, you might want to look at the ARRAYREAD command to parse the data. Lots of options available.

/Henrik.

Hanson
- 22nd April 2014, 09:29
Hi Henrik,





i VAR BYTEDataIn VAR BYTE[16]ActualData VAR BYTE [6]' DataIn is 16 bytes containg A A A A A H A N S O N E E E E E' Verify that the first 5 bytes are all "A"For i = 0 to 4 If DataIn[i] <> "A" THEN GOTO Start ' Nope, not qualified, start over...NEXT' Verify that the last 5 bytes are all "E"For i = 11 to 15 If DataIn[i] <> "E" THEN GOTO Start ' Nope, not qualified, start over...NEXT' Yes, header and footer qualified. Now extract actual data, 6 bytes, store in array ActualDataFor i = 5 to 10 ActualData[i-5] = DataIn[i]NEXT






If first 5 bytes received are correct then GOTO start. In this case if first 5 bytes received correctly and last 5 bytes are wrong? It runs "start" subroutine without checking the last 5 bytes?

Regards

HenrikOlsson
- 22nd April 2014, 09:49
Hi,

If first 5 bytes received are correct then GOTO start.
No.... It checks the first 5 bytes, if any of them is NOT "A" it jumps to start.


In this case if first 5 bytes received correctly and last 5 bytes are wrong? It runs "start" subroutine without checking the last 5 bytes?
No.... If it finds that the first 5 bytes are received correct it continues to check the last 5 bytes. If they TOO are correct it extracts the data. If any of the last the 5 bytes are wrong it'll jump back to start without extracting the data.

Only when the first bytes are "A" AND the last 5 bytes are "E" will it extract the data. As soon as ANY of the first 5 bytes OR the last 5 bytes does NOT match will it jump back to start - which is whay you said it should do:

if all conditions matched (receive first 5 "A" and last 5 "E" properly) then store data_in


/Henrik.

Hanson
- 22nd April 2014, 09:59
Hi Henrik,

Thanks alot! My brain isn't functioning well just now. Sorry for the confusion.

Hanson
- 22nd April 2014, 10:17
Hi Henrik,

Now considering I have successfully received and stored the data in PIC16f877 as "ActualData" and I wish to send the stored data to the "slave (TRH031M)".
Can I use SHIFTIN/OUT method to shift data into SDO pin and send to trh031m? Following is my coding:

************************************************** ********************************************
'send received data from MU to TRH031M (master to slave)
ActualData var word[8]
j var byte[8]
B0 var byte

send1:
SHIFTIN SDI, SCK, MSBPRE, [ActualData\8]
j=ActualData
SHIFTOUT SDO, SCK, MSBFIRST, [j]

SSPBUF = "?" ' send ? to start conversion
GoSub letclear ' wait for buffer to clear
IF SSPBUF<>"!" Then send1 ' wait for reply (!)

For j = 0 to 7 ' loop for 8 characters
SSPBUF = 0 ' write to SSPBUF to start clock
GoSub letclear ' wait for receipt
B0[j] = SSPBUF ' store received character in array
Next j ' get next character
Return

letclear:
IF SSPIF = 0 Then letclear ' wait for SPI interupt flag
PauseUs 25 ' 25uS fudge factor
SSPIF = 0 ' reset flag
Return

************************************************** ************************************

Regards/

HenrikOlsson
- 22nd April 2014, 11:16
Hi,
I don't know if it's you or me who are the most confused right now.....
You seem to be mixing SHIFTIN/SHIFTOUT with the use of the MSSP module, use either or. And, previously you said you used SEROUT on another PIC to send data, then you can't use SHFTIN to receive it - if that's what you're trying to do. And, no, you can't use any of the methods to shift data into the SDO pin since that is the data output pin.


ActualData var word[8]
j var byte[8]
B0 var byte

Any reason for ActualData to be WORDS?



send1:
SHIFTIN SDI, SCK, MSBPRE, [ActualData\8]
j=ActualData
SHIFTOUT SDO, SCK, MSBFIRST, [j]
Not sure what the purpose of this is but there's no reason to assign the value of ActualData to J and then send J out using SHIFTOUT, you could just as well shift out ActualData directly (instead of a copy of it).



For j = 0 to 7 ' loop for 8 characters
SSPBUF = 0 ' write to SSPBUF to start clock
GoSub letclear ' wait for receipt
B0[j] = SSPBUF ' store received character in array
Next j ' get next character
Return
Here you're treating B0 as an array when it's decalred as a BYTE. This is a critical error as it will write to memory location "beyond" B0 and corrupt what ever is there. PBP does not do any boundry checks on arrays so you need to be careful.

This post probably isn't of much help to you but I've quite honestly lost track of what exactly you're trying to do. Now, we've got SERIN, SHIFTIN/SHIFTOUT and the use of the MSSP module in the mix - all at once and I'm struggeling to keep up. Again, I suggest that you get used to various ways of doing SPI by interfacing something simpler than the TRH031M - which, by the way, I've never used so I don't know how it works. But I looked, briefly, at the datasheet and nowhere can I find that you're supposed to send "?" to start a "conversion" - I thought the chip was for RFID communications, not a converter....

/Henrik.

Hanson
- 24th April 2014, 04:10
Hi,

Sorry Henrik for making you confused. I am not sure that I can use SERIN with SHIFTIN/OUT so I mistakenly mixed them up.

Actually what Im doing is transmit a data at 13.56MHz by using TRH031M and the data is from pic16f877. Since trh031m has 4 type of modes, SPI,parallel etc, i configured my hardware to perform SPI mode. So Im trying to use "master slave" communication to send my data from PIC to trh031m as trh031m already pre-configured as a "slave" by default which u told me in the previous comment.

Can you kindly teach me what is the correct way to send my received data from PIC to trh031m? Do "master slave" communication work in performing my task?

appreciate your kind helps.

Regard.

HenrikOlsson
- 24th April 2014, 06:41
Hi,
All I can say is that the PIC and PBP supports SPI communcation, either by using SHIFTIN/SHIFTOUT or using the MSSP peripheral module built into most devices. That gives you the interface between the PIC (master in this case) and the TRH031M (slave in this case). I see no reason for it not to work, however there are occasions where SHIFTIN/SHIFTOUT is NOT an option because the the slave devices is shifitng in and out data at the same time, in those cases you must use the MSSP module because the SHIFTIN/SHIFTOUT either shift data in OR shifts data out they can't do both simultanously.

But, the TRH031M is a complicated device, it's not as simple as just "send some data" to it. It needs setting up, it needs commands sent to it, it needs data sent to it, it needs new commands sent to it, it needs to be polled for status and so on. You absolutely must read and understand the datasheet or you won't be able to make this work. Again, I've never used the device so *I* don't know how it works, I've just very briefly browsed thru the datasheet.

I strongly suggest you put your PIC, a 74*595 (serial in, parallel out) and a 74*165 (parallel in, serial out) shiftregisters, a couple of LEDs and a couple of switches on a breadboard and get used to communicating with those. Use SHIFTOUT to write data to the 74*595 and SHIFTIN to read data from the 74*165. When you have that working and understand how it works, try the MSSP module instead of SHIFTIN/SHIFTOUT. With that you'll be able to write one byte to the 74*595 and read one byte from the 74*165 all in one go - which MIGHT be what's needed for the TRH031M - I'm not sure.

THEN get back to the TRH031M.

/Henrik.