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

Op-Amp to match the SD card and a 5V powered PIC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
luis.rigoni



Joined: 14 Oct 2010
Posts: 12

View user's profile Send private message

Op-Amp to match the SD card and a 5V powered PIC
PostPosted: Sat Oct 30, 2010 10:13 pm     Reply with quote

Hello everyone,

Much has been said that the DO line of an SD card is not compatible with the DI pin of a PIC running at 5V, because the card works with 3.3V and can not supply enough voltage to a gate Schmitt Trigger.

It is possible to use internal PIC comparator module as operational amplifier and applying a gain in the output of the card to make it compatible with the DI input pin of the PIC? What would be the gain should be applied?

I'm using the PIC18F4550 and the compiler 4.093.

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 31, 2010 3:17 pm     Reply with quote

To do that, means that you will use "bit banging" i/o for the SPI instead
of using the hardware SPI. This will be a lot slower than using the
hardware module. Normally, people want some speed when they
are using an external SD card. They use the hardware SPI for this.

If you do want to use software SPI, you don't need the comparator.
You can use any i/o pin that has TTL input levels. The input type for
each pin is given in this section of the 18F452 data sheet:
Quote:
TABLE 1-3: PIC18F4X2 PINOUT I/O DESCRIPTIONS

You can see that all Port B pins have TTL inputs. These will work with
the 3.3v CMOS levels coming out of the SDO pin on the SD card.
You don't want a PIC pin that has an "ST" input (Schmitt Trigger).
These require 5v CMOS levels, which you don't have on the SD card.
18F452 data sheet:
http://ww1.microchip.com/downloads/en/devicedoc/39564c.pdf
Also, some of the pins on Port A could be used. The other ports are all
listed as "ST" inputs for digital i/o.

Previous threads on logic level translation:
http://www.ccsinfo.com/forum/viewtopic.php?p=45331
http://www.ccsinfo.com/forum/viewtopic.php?t=23154
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sun Oct 31, 2010 7:05 pm     Reply with quote

PCM programmer wrote:
To do that, means that you will use "bit banging" i/o for the SPI instead
of using the hardware SPI. This will be a lot slower than using the
hardware module. Normally, people want some speed when they
are using an external SD card. They use the hardware SPI for this.


Often when people are considering a software SPI for an SD/MMC interface they forget to take into consideration the sheer volume of traffic that needs to traverse the interface, especially if you are dealing with a FAT based file system.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Mon Nov 01, 2010 12:20 am     Reply with quote

PCM programmer wrote:
To do that, means that you will use "bit banging" i/o for the SPI instead of using the hardware SPI.
I must be missing something obvious, but why couldn't C1IN- be set to the threshold voltage (say 2V) and C1IN+ the 3.3V level data, and C1OUT be fed directly into the hardware SDI pin?
_________________
Andrew
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Nov 01, 2010 2:05 am     Reply with quote

andrewg wrote:
PCM programmer wrote:
To do that, means that you will use "bit banging" i/o for the SPI instead of using the hardware SPI.
I must be missing something obvious, but why couldn't C1IN- be set to the threshold voltage (say 2V) and C1IN+ the 3.3V level data, and C1OUT be fed directly into the hardware SDI pin?


It will depend on the frequency of the SPI interface. You may find it impacts the timing too much.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Mon Nov 01, 2010 2:30 am     Reply with quote

Presumably that would be the comparator response time, which in the case of the '2455 is 400ns max or 2.5MHz. So a 2MHz bus speed would probably be best, although 4MHz is a possibility.

Edit: or even the '4550! Same data sheet anyway...
_________________
Andrew
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Nov 01, 2010 3:49 am     Reply with quote

No,
Actually I think PCM programmer may be 'wrong' on this one, and what you want might work.

First comment, you are using the comparator as a comparator, not an 'op-amp'.

Concept is that you program the comparator, so that you have the -ve input connected to a voltage of perhaps 1-1.5v, then feed the signal from the SD card data output pin into the +ve input of the comparator. Then connect the output of the comparator, to the SDI pin of the PIC, so that the comparator provides the required level shifting.

I think PCM, is visualising using the software comparator output, since many of the PIC comparators don't offer you the option to access the output of the comparator, but on the 2455/4550, it does.

However you then hit the first problem. The internal Vref, can only be routed to the comparator +ve inputs, with the data using the -ve input. This would give a signal inversion, which wouldn't work.
Hence you will have to generate the reference externally using a couple of resistors.
Also, if the behaviour is going to be reliable, you will need to add some hysteresis to the comparator.

So potentially if you program the comparators as 'one independent comparator with output', then connect say a 1.5K resistor from the -ve input to 0v, and 6.8K to +5v, then feed the SDO signal from the card to the +ve input via a 1.5K resistor, and have perhaps 50K between the output, and the +ve input, you should get a signal on the output, that is compatible with the SDI pin of the PIC.

The response time of the comparator _will_ be worse than the data sheet figure, since this is the 'large signal' response, with the input swinging from rail to rail. With the slightly smaller signals involved here, add 50% for a 'safe guess'. Even so, it should be able to cope with SPI at perhaps 500KHz, which is significantly better than bit banging could manage... Smile
With the small amount of hysteresis here, noise immunity will be worse than on a standard input.

If you wanted to be really ingenious, you could sit down with a calculator, and work out resistor values to give switching points matching a 3.3v Schmitt input, which would help a little on the noise front, but I doubt if this would be needed.

Potentially quite a few resistors, and uses three pins on the PIC, but it might well work.

Best Wishes
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Mon Nov 01, 2010 4:02 am     Reply with quote

Ttelmah wrote:
The internal Vref, can only be routed to the comparator +ve inputs, with the data using the -ve input. This would give a signal inversion, which wouldn't work.
The PIC series in question has an output inversion bit, so that may not be a problem, however, the internal reference is only available when *both* comparators are selected (see data sheet for details).

Pin wastage or external components. Decisions, decisions....
_________________
Andrew
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Nov 01, 2010 5:15 am     Reply with quote

Yes.
However you would really 'need' to have some hysteresis, so using the external resistors is probably still best.
A quick 'back of envelope', suggests:

Input through 2K to +vin.
6K8 from Vout to +Vin.
5K from -Vin to 5v.
2K from -Vin to GND.

Depending on the actual output swing of the PIC, this should give quite reasonable 3.3v logic thresholds.

Best Wishes
luis.rigoni



Joined: 14 Oct 2010
Posts: 12

View user's profile Send private message

PostPosted: Tue Nov 02, 2010 4:11 pm     Reply with quote

Thanks for the replies!

PCM programmer wrote:
To do that, means that you will use "bit banging" i/o for the SPI instead of using the hardware SPI.


Maybe I expressed myself badly. I want to use the hardware SPI. Comparator would only serve to translate the level 3.3V -> 5V for the data channel SD -> PIC.

andrewg wrote:
Presumably that would be the comparator response time, which in the case of the '2455 is 400ns max or 2.5MHz. So a 2MHz bus speed would probably be best, although 4MHz is a possibility.


I agree. But, it is possible to configure the frequency of communication with the SD card using an external 20MHz crystal? I am also using USB (I think it is a constraint for the design, right?). My current #fuses are:

Code:
//configure a 20MHz crystal to operate at 48MHz
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48M)


Ttelmah wrote:
I think PCM, is visualising using the software comparator output, since many of the PIC comparators don't offer you the option to access the output of the comparator, but on the 2455/4550, it does.


Exactly. My idea was to set the comparator thus:

Code:
// Sets up 'One Independent Comparator with Output' (C1)
// C1 uses A0(Vin-) and A3(Vin+) as inputs and A4 as output
// page 276
setup_comparator(A0_A3_NC_NC_OUT_ON_A4);


And then connect the C1OUT (A4) in the PIC SDI pin.

Ttelmah wrote:
Input through 2K to +vin.
6K8 from Vout to +Vin.
5K from -Vin to 5v.
2K from -Vin to GND.


Ok I did it. I adapted the example ex_mmcsd for testing but has not worked 100%. The method mmcsd_init() returns the error code E0, and I can not find why the error.

My source code is:

Code:
#include <18F4550.h>
#device PASS_STRINGS = IN_RAM

//configure a 20MHz crystal to operate at 48MHz
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48M)

//media library, a compatable media library is required for FAT.
#use fast_io(b)
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_B1 //o
#define MMCSD_PIN_SDI     PIN_B0 //i
#define MMCSD_PIN_SDO     PIN_C7 //o
#define MMCSD_PIN_SELECT  PIN_C6 //o

#include <usb_cdc.h>
#include <stdlib.h> // for atoi32
#include <mmcsd.c>
#include <usb_bootloader.h>

void main(void)
{
   BYTE value, cmd;
   int32 address;
   int ret, err_mmcsd;
   
   usb_cdc_init();
   usb_init();
   
   while (!usb_cdc_connected());
   
   usb_task();

   if(usb_enumerated())
      printf(usb_cdc_putc, "\r\n\nex_mmcsd.c\r\n\n");
   
   usb_task();

   // Sets up 'One Independent Comparator with Output' (C1)
   // C1 uses A0(Vin-) and A3(Vin+) as inputs and A4 as output
   // page 276
   setup_comparator(A0_A3_NC_NC_OUT_ON_A4);
   err_mmcsd = mmcsd_init();
   if (err_mmcsd)
   {
      printf(usb_cdc_putc, "Could not init the MMC/SD! Error code: %X\n\r", err_mmcsd);
      while(TRUE);
   }
   
   do {
      value = 0;
      address = 0;
      usb_task();
      do {
         printf(usb_cdc_putc, "\r\nRead or Write: ");
         cmd=usb_cdc_getc();
         cmd=toupper(cmd);
         usb_cdc_putc(cmd);
      } while ( (cmd!='R') && (cmd!='W') );

      printf(usb_cdc_putc, "\n\rLocation: ");
     
      // 4 digits hex address
      address = gethex_usb();
      address = (address<<8)+gethex_usb();

      if(cmd=='R') {
         // Returns 0 if OK, non-zero if error
         ret = mmcsd_read_byte(address, &value);
         printf(usb_cdc_putc, "\r\nMMCSD_err: %02X\r\n", ret);
         printf(usb_cdc_putc, "\r\nValue: %X\r\n", value);
      }else if(cmd=='W') {
         printf(usb_cdc_putc, "\r\nNew value: ");
         value = gethex_usb();
         printf(usb_cdc_putc, "\n\r");
         mmcsd_write_byte(address, value);
         ret = mmcsd_read_byte(address, &value);
        printf(usb_cdc_putc, "\r\nMMCSD_err: %02X", ret);
        printf(usb_cdc_putc, "\r\nValue: %X\r\n", value);
         mmcsd_flush_buffer();
      }
   } while (TRUE);   
}


Any advice?
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Tue Nov 02, 2010 4:25 pm     Reply with quote

I'd start by testing that the comparator is actually doing what you want. Attach a pot to it's input, or a signal generator with a variable amplitude, and put a scope on the output, and test if it does switch 'high' at about 2v, and 'low' somewhere at about 0.5v. You are trying to take much too large a 'jump'.....

Best Wishes
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Tue Nov 02, 2010 5:59 pm     Reply with quote

Would it be possible to attach a voltage divider to the input pin, with some resistance going to 5V and some other resistance to the SD card output? Then the voltage on the pin would be higher than the 3.3V that the card delivers and you might get it to work. Of course, this arrangement guarantees that the low-state voltage would be higher too, and you can't allow the input to seem always high, but perhaps there's a compromise that would work.
luis.rigoni



Joined: 14 Oct 2010
Posts: 12

View user's profile Send private message

PostPosted: Tue Nov 02, 2010 8:48 pm     Reply with quote

Ttelmah wrote:
Attach a pot to it's input, or a signal generator with a variable amplitude, and put a scope on the output, and test if it does switch 'high' at about 2v, and 'low' somewhere at about 0.5v.


I put a pot before the 2K resistor of the input signal comparator in the same position would be that the SD card.

I do not have an scope at the moment so I put an LED on the pin C1OUT directly to GND, and I was measuring the voltage at the pot.

The switch to 'high' (LED on) occurs at ~1.82V and switching to 'low' (LED off) at 1.23V. I can not tell if it is within proper limits, is it?

I'm starting at the 'art' of the microcontroller, any help is welcome!
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Nov 03, 2010 12:54 am     Reply with quote

If you are planning to the the USB and the SD/MMC media at the right time and want to support the FAT file system then you need to check your application can be implemented with the amount of RAM you have on this PIC. The 4550 and variants consume a lot of RAM for the USB buffers.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Wed Nov 03, 2010 3:04 am     Reply with quote

The 'high' switch is about right, but I'd have expected the 'low' switch to be much lower than this. However I see the critical thing causing this, in your line 'I put an LED on the pin C1OUT directly to GND'. This will be overloading the output pin, and clamping it to the LED voltage. Probably something like 2.7v, when I calculated the resistors assuming the pin will swing to about 4v. You _must_ always have a series resistor when feeding an LED from a pin.

You really do need a scope. Quite good basic scopes are so cheap on places like eBay now, that it is just 'wasting time', to not have one!. My next test would be to see what the edges of the waveform look like at the running frequency. The standard MMCSD library, does not specify the baud rate for the SPI. I'd set this, and turn it down to perhaps 500KHz. Note my earlier comments about the actual speed of the comparator, given the smaller input swing.....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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