Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:01 06 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Need Some CSub Help

Author Message
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 327
Posted: 09:47am 11 Jun 2016
Copy link to clipboard 
Print this post

I've playing around with a SPI FRAM module and things are working fairly well. I can write data and read it back without any problems. MMBasic has a great SPI feature to write out a block of string data of known length. But, when reading a block of string data back, it is received as numeric data into an array. It's easy to convert the data in the array to a string using CHR$ however it is relatively slow. So, I tried to create CSub to move the data directly into the string. Unfortunately it is not working correctly.

MMBasic Code:
Dim AS Integer msgIn(65), NumOfChars
DIM STRING msg2$ LENGTH 65

FRAMsize = 8192 'Total # of BYTES on FRAM chip
' pin 3 is SPI Out for MicroMite 170
' pin 25 is SPI Clock for MicroMite 170
' Pin 14 is SPI In for MicroMite 170 and is not connected for this application
FRAM_CS = 23 'CS pin for the FRAM
Setpin FRAM_CS, Dout
Pin(FRAM_CS) = 1 ' Set CS to high to deselect chip

SPI OPEN 10000000, 0, 8 'Open SPI port 10Mhz, mode 0, 8 bits
pause 10

'Set start address for FRAM access
FRAMaddr = 0

msg$ = "1234567890123456789012345678901234567890123456789012345678901234"

Print "String to be written to FRAM:"
Print msg$
Print "Length of data to be written to FRAM: "; Len(msg$)
Print

ReadyWrite
WrtAddress (FRAMaddr)
Spi WRITE 64, msg$
CloseWrite
Pin(FRAM_CS) = 1 ' Set CS to high to deselect chip

Print "Data was written to FRAM. Ready to read data back."
Print


NumOfChars = 64
ReadyRead (FRAMaddr)
Spi READ NumOfChars, msgIn()

Print "Data has been read from FRAM"
PRint

msg3$ = ""
for i = 0 to NumOfChars - 1
msg3$ = msg3$ + chr$(msgIn(i))
next i
Print "Conversion of the returned array using CHR$() on each element:"
print msg3$
Print "Length = "; len(msg3$)
Print

msg2$ = STRING$( NumOfChars, "*" ) 'allocate string space for return string
Print "Result returned from C routine:"
Array2STR(msgIn(), msg2$, NumOfChars) 'Convert/move numeric data into string
Print msg2$
Print "Length = "; len(msg2$)

SPI Close
End

Sub ReadyWrite
Pin(FRAM_CS) = 0 ' activate FRAM
AA=spi(&H06) ' Send Write Enable op-code
Pulse FRAM_CS, .01 ' activate op-code
End Sub

Sub WrtAddress (WrtAddr)
Local HiByte, LoByte, HexAddr$
HexAddr$ = HEX$(WrtAddr ,4)
HiByte = Val("&H"+left$(HexAddr$,2))
LoByte = Val("&H"+RIGHT$(HexAddr$,2))
AA=spi(&H02) ' Send Write Op-Code
AA=Spi(HiByte) 'Send address
AA=Spi(LoByte)
End Sub

'Close FRAM Write Operation
Sub CloseWrite
Pulse FRAM_CS, .01 'Pulse CS to Stop writing
AA=spi(&H04) 'Disable Write
End Sub

Sub ReadyRead (ReadAddr)
Local HiByte, LoByte, HexAddr$
Pin(FRAM_CS) = 0 ' activate FRAM
HexAddr$ = HEX$(ReadAddr ,4)
HiByte = Val("&H"+left$(HexAddr$,2))
LoByte = Val("&H"+RIGHT$(HexAddr$,2))
Pin(FRAM_CS) = 0 ' activate FRAM
AA=spi(&H03) ' Send Write Op-Code
AA=Spi(HiByte) 'Send address
AA=Spi(LoByte)
End Sub


CSub Array2STR(Integer, String, Integer)
00000000
8CC20004 5C400006 00001021 14400014 00000000 8CC20000 10400011 00001021
00A21821 8C870000 A0670000 24420001 00021FC3 8CC70004 0067382A 14E0FFF8
24840008 8CC70004 14E30005 00000000 8CC30000 0043402B 1500FFF2 00A21821
03E00008 00000000
End CSub


C Routine:
void Array2STR(long long thearray[], unsigned char thestring[], long long *strlen)
{
int i;
for(i = 0; i < *strlen; i++)
{
thestring = thearray;
} // Next i

} // End of Array2STR

void main(){}


Sample Output:
RUN
String to be written to FRAM:
1234567890123456789012345678901234567890123456789012345678901234
Length of data to be written to FRAM: 64

Data was written to FRAM. Ready to read data back.

Data has been read from FRAM

Conversion of the returned array using CHR$() on each element:
1234567890123456789012345678901234567890123456789012345678901234
Length = 64

Result returned from C routine:
2345678901234567890123456789012345678901234567890
Length = 49
>


What am I doing wrong?

Thanks,
Curtis
I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
Nathan
Regular Member

Joined: 10/01/2016
Location: Germany
Posts: 49
Posted: 11:24am 11 Jun 2016
Copy link to clipboard 
Print this post

Hi Curtis,

I did not checked in detail.
So only some hints.

>> thestring[j] = thearray[j];
>>}

please try the following I replaced i by j, because i will be dissapear in the text:

thestring[j+1] = (unsigned char)(thearray[j]);
}
thestring[0] = *strlen ;

The first entry of a string is the length.

In your case:
Length = 49 is the ASCII code of the char "1"

Have fun,

NathanEdited by Nathan 2016-06-12
 
Justplayin

Guru

Joined: 31/01/2014
Location: United States
Posts: 327
Posted: 12:47pm 11 Jun 2016
Copy link to clipboard 
Print this post

  Nathan said  .
.
.
thestring[j+1] = (unsigned char)(thearray[j]);
.
.
.
The first entry of a string is the length.

In your case:
Length = 49 is the ASCII code of the char "1"


Thanks you Nathan,
That was it! Everything is working as expected now. I knew I had to be missing something simple.

--Curtis

I am not a Mad Scientist...  It makes me happy inventing new ways to take over the world!!
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025