problem with filling a sector buffer

61 views
Skip to first unread message

Zet Weeh

unread,
Feb 20, 2023, 12:12:04 PM2/20/23
to jallib

Hi Matt
I think this is a question for you. You made a beautiful sample for writing to a sd card.
Every  start of the program I use the part of your lib to control the connection to the sd card.
I can write and read the sd card byte for byte. I use a bitmap image of Marilyn Monroe of 504 bytes.

The half of the next program works perfect:
var byte gegeven
var byte data
var word step1
var word teller

sd_start_write(50)          
   for count(marilyn) using teller loop
      sd_data_byte = marilyn[teller]
   end loop
sd_stop_write
This works perfect.


I also tried to write to the sd card with a 'sector buffer' because most arrays are larger.
But first the array of Marilyn. It can be written in one sector.
Attempt 1:
print_string(serial_data, "Writing to sector 100")
print_crlf(serial_data)

sd_start_write(100)        
--fill the sector buffer
   for 512 using teller loop
      sd_sector_buffer[teller] = marilyn[teller]
   end loop
--    write the sector buffer to sector 100
   sd_write_sector_address(100)
sd_stop _write

I also tried:
Attempt 2:

  sd_start_write(150)
        for 512 using step1 loop
           gegeven = marilyn[step1]
           sd_sector_buffer[step1] = gegeven
       end loop
-- write the sector buffer to the sd card
 sd_write_sector()
 sd_stop _write

Both attempts does not work.
I get rubbish when I read the sectors.
Can you tell me how I get it working.

Regards,
Peter

Matthew Schinkel

unread,
Feb 20, 2023, 6:21:13 PM2/20/23
to jallib
Hi, have you tried another sd card? What PIC are you using?

Please try example 3 in the sample

;   --------------------------------------------------------------------------------
;   -- EXAMPLE #3 write to 2 sectors using a sector buffer at a address
;   -- user friendly and fastest.
;   --------------------------------------------------------------------------------
;   -- fill the sector buffer with data
;   for 512 using step1 loop                     -- loop till the end of the sector buffer
;      sd_sector_buffer[step1] = "B"             -- set each byte of data
;   end loop
;   -- write the sector buffer to sector 20
;   sd_write_sector_address(20)
;   for 512 using step1 loop                     -- loop till the end of the sector buffer
;      sd_sector_buffer[step1] = "C"             -- set each byte of data
;   end loop
;   -- write the sector buffer to sector 21
;   sd_write_sector_address(21)
;   --
;   -- read back the same sectors
;   -- read sector 20 into the sector buffer
;   sd_read_sector_address(20)
;   -- now send it to the serial port
;   for 512 using step1 loop                     -- loop till the end of the sector buffer
;      serial_write (sd_sector_buffer[step1]) -- send each byte via serial port
;   end loop
;   -- read sector 21 into the sector buffer
;   sd_read_sector_address(21)
;   -- now send it to the serial port
;   for 512 using step1 loop                     -- loop till the end of the sector buffer
;      serial_write (sd_sector_buffer[step1]) -- send each byte via serial port
;   end loop

Matt.

Zet Weeh

unread,
Feb 20, 2023, 6:37:02 PM2/20/23
to jal...@googlegroups.com
Hi Matt
I know that and I tried it before I wanted to write an array to the SD card. 
It works perfect. 
The problem is how to write an array to the SD card. 

Regards,
Peter 

Op 21 feb. 2023 om 00:21 heeft Matthew Schinkel <mattsc...@hotmail.com> het volgende geschreven:

Hi, have you tried another sd card? What PIC are you using?
--
You received this message because you are subscribed to a topic in the Google Groups "jallib" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jallib/I1L0r6P-2xQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/8415623e-2bbb-424c-9498-cd9b3c3b651cn%40googlegroups.com.

Matthew Schinkel

unread,
Feb 20, 2023, 11:05:35 PM2/20/23
to jallib
Hi, ok so the original sample is working? I'll try what you have but one thing I see is that your loop is 512 but marilyn is only 504. The loop should only be the size of the array.

for 504 using teller loop

      sd_sector_buffer[teller] = marilyn[teller]
 end loop

Matt.

Matthew Schinkel

unread,
Feb 21, 2023, 9:14:09 PM2/21/23
to jallib
Hi, this works from example 3, assuming the array is a constant (you didn't show the array in your example).

   const byte MARILYN[504] = {0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,
                              0x42,0x42,0x42,0x42}

                             
   -- fill the sector buffer with data
   for 504 using step1 loop
      sd_sector_buffer[step1] = MARILYN[step1]

   end loop
   -- write the sector buffer to sector 20
   sd_write_sector_address(20)

   -- read back the same sectors
   -- read sector 20 into the sector buffer
   sd_read_sector_address(20)
   -- now send it to the serial port
   for 504 using step1 loop                     -- loop till the end of the sector buffer

      serial_write (sd_sector_buffer[step1]) -- send each byte via serial port
   end loop

ZetWeeh

unread,
Feb 23, 2023, 12:21:24 PM2/23/23
to jal...@googlegroups.com

Hi Matthew

 

I tried your suggestion and sample.

I use the same large array of Mailyn with a ‘const byte’.

But it still does not work.

I send in the attachment the code and a capture with RealTerm.

You will see that with reading the sector nothing appears. I think the writing is the problem.

Writing byte for byte is going well and I removed that part code so you only see what is important.

Maybe there is a pragma problem or something else?

 

Regards,

Peter

18F4455 RealTerm capture.txt
18f4455_sd_card.jal

ZetWeeh

unread,
Feb 27, 2023, 5:16:48 PM2/27/23
to jal...@googlegroups.com

Hi Matthew

 

I sent you the code of the 18F4455 so you could bettter see what is goning wrong.

In the meantime I rewrote the code for the 16F18877. Also here byte for byte was working but the sample with a sector buffer refused to do what it had to do.

 

Regards,

Peter

 

Van: Matthew Schinkel
Verzonden: woensdag 22 februari 2023 03:14
Aan: jallib
Onderwerp: Re: [jallib] Re: problem with filling a sector buffer

 

Hi, this works from example 3, assuming the array is a constant (you didn't show the array in your example).

rob...@hotmail.com

unread,
Mar 11, 2023, 9:36:18 AM3/11/23
to jallib
Hi Peter, Matt,

I had a PIC 18F4550 which is from the same family as the 18F4455 and ran the program. I only had to change the serial port to invert the output and use 1 stopbit instead of 2 but that cannot be the issue. I did not make any other changes.

It seems to work OK so there must be something wrong with either the PIC, the hardware or the SD card. I ran the PIC at 5 Volt using a level shifter to connect to the SD car which was at 3.3 Volt.

I used the Marilyn data that was given in this post. Attached the program and the terminal output.

Kind regards,

Rob



Op maandag 27 februari 2023 om 23:16:48 UTC+1 schreef zet....@gmail.com:
18f4550_sd_card.jal
18F4550 RealTerm capture.txt
marilyn.jal

ZetWeeh

unread,
Mar 13, 2023, 5:26:50 PM3/13/23
to jal...@googlegroups.com

Hi Matt and Rob

 

It works!

 

Inverting the serial port gave rubbish and 1 stopbit I use standard.

But Matt wrote a mail: ‘I asked Rob …..’ and then a short second mail: try a pull-up resistor of 1K on CS and that was the solution for me.

 

And not so funny: sometimes I got about ten of ‘AAAAA’ and then rubbish.

I thought the ‘A’ is of earlier attemps and I even formatted the SD card!

In the capture of Rob I saw the whole Marilyn and it started with ‘AAAA’.

I thougt the writing did not succeed but now I know the reading crashed.

 

Rob: in the original code the writing and reading per byte of Marilyn was succesfull.

So there could not be a hardware problem.

 

I already changed the writing in writing the hex-code.

 

Thank you both for your help.

 

Regards,

Peter

Matthew Schinkel

unread,
Mar 14, 2023, 5:03:21 PM3/14/23
to jallib
Hi, Glad you got it to work. I had suggested the pull up resistor because I know some PICs have some open-drain pins. If you have any more issues add pull ups to sdi/sdo/clk pins as well.

I just looked through the 18f4455 manual. It doesn't look like pin_a5 has an open drain.

Does anyone know why this worked for him?

Matt.

ZetWeeh

unread,
Mar 19, 2023, 10:22:30 AM3/19/23
to jal...@googlegroups.com

Hi Matt

I waited some time for an answer (just like you).

Today I looked myself again. I removed the resistor and all kept working.

I was not surprised because ‘byte for byte’ had worked.

I used a micro sd card holder and on the website I saw that between 3V3 and GND a 100nF was placed.

When I used my original program of ‘byte for byte’ and ‘sector buffer’ that gave the problem earlier: problem was solved with the 100nF.

I think this is the explanation for the problem instead of the resistor.

 

 

Regards,

Peter

 

Van: Matthew Schinkel
Verzonden: dinsdag 14 maart 2023 22:03
Aan: jallib
Onderwerp: Re: [jallib] Re: problem with filling a sector buffer

 

Hi, Glad you got it to work. I had suggested the pull up resistor because I know some PICs have some open-drain pins. If you have any more issues add pull ups to sdi/sdo/clk pins as well.

 

I just looked through the 18f4455 manual. It doesn't look like pin_a5 has an open drain.

 

Does anyone know why this worked for him?

 

Matt.

 

 

On Monday, March 13, 2023 at 5:26:50 PM UTC-4 zet....@gmail.com wrote:

Hi Matt and Rob

 

It works!

 

Inverting the serial port gave rubbish and 1 stopbit I use standard.

But Matt wrote a mail: ‘I asked Rob ….’ and then a short second mail: try a pull-up resistor of 1K on CS and that was the solution for me.

Rob CJ

unread,
Mar 23, 2023, 3:53:09 PM3/23/23
to jal...@googlegroups.com
Hi Peter,

In general it is recommended to put a 100 nF capacitors close to every IC that you use for correct operation.

It might have been the cause of your issue.

Kind regards,

Rob

Van: jal...@googlegroups.com <jal...@googlegroups.com> namens ZetWeeh <zet....@gmail.com>
Verzonden: zondag 19 maart 2023 15:22
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: RE: [jallib] Re: problem with filling a sector buffer
 
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/415C44FC-5F1C-4E80-A6C6-D3E557617B1D%40hxcore.ol.

ZetWeeh

unread,
Mar 23, 2023, 4:41:21 PM3/23/23
to jal...@googlegroups.com

Hi Rob

 

You’re right.

It’s still working perfect!

Once again: look in the datasheet!

 

Regards,

Peter

 

Van: Rob CJ
Verzonden: donderdag 23 maart 2023 20:53
Aan: jal...@googlegroups.com
Onderwerp: Re: [jallib] Re: problem with filling a sector buffer

 

Hi Peter,

 

In general it is recommended to put a 100 nF capacitors close to every IC that you use for correct operation.

 

It might have been the cause of your issue.

 

Kind regards,


Rob

Reply all
Reply to author
Forward
0 new messages