epideath
Joined: 07 Jun 2006 Posts: 47
|
MMC SPI with 18F4520 |
Posted: Sat Jun 10, 2006 6:18 pm |
|
|
EDIT: I got it working. phew. so you may disregard now. Thanks
OK its me again. sorry for all the questions. but I'm down to my last major problem (I think ) I have SPI working to my ADC and my EEPROM now I just need to get it working with the MMC. BTW if I load my code from MCC18 complier all the hardware works wonderfully.
I'm just not sure what I could be doing wrong. I have been playing with this for 2 days now. I have tried the mmc_spi.c driver code. Modified for use with my chip. I turned off the SPI as the driver really doesn't use SPI it just drives the IO lines directly. I couldn't even get that one working at all with a new project with just the mmc_spi.c and mnimum code to drive the init. It failed always. what I'm hoping for is that someone can tell me why there is a difference in the two code snippets that follow. one is from my working software from MCC18. There I use the code found all over the web and it basically loads the SSPBUF directly and reads the SSPSTAT. In the new code with PICC I decide to try and use the spi_write and spi_read commands instead. These work to my EEPROM and ADC chip.
As far as I can tell the codes are equivalent. I have played with all kinds of settings on the spi_setup(). I will show the latest settings at the moment and the working settings from the MCC18 code. I have tried it with interrupts on or off on both projects. Doesn't seem to matter or help. But they are off for the time being. Ive tried making a simple projcect with jsut the code for the MMC but it didn't help either. So I assume that I'm not doing anything bad in my code else where. I pretty much call MMC Init right after the pre-built PICC setup functions. Mainly I jsut need to init my LCD display so I have something to see while I debug.
I really appreciate any help that can be given. I'm really starting to pull my hair out on this conversion.
Oh and BTW on my earlier problem with the SPI to EEPROM. Thanks for the help there. A major part of the problem was the missing variable on the spi_Read(0) command. I did see it in the manual. I guess I missed that first time around.
Well here is the code. Thanks again for the help.
MCC18 working code:
Results on r1-r8 for cmd 0x40 and cmd 0x41 are:
01 FF FF FF FF FF FF ... for cmd 0x40
00 FF FF FF FF FF FF ... for cmd 0x41
so those are normal and work. oh and BTW the code for writing and reading is working on this code. I just haven't got to that pooint yet with the converted code.
Code: |
spi setup --> OpenSPI(SPI_FOSC_64, MODE_00, SMPEND);
char SPI(char d)
{
SSPBUF = d;
while(!SSPSTATbits.BF);
return SSPBUF;
}
char Command(char befF,unsigned int AdrH,unsigned int AdrL,char befH )
{ // sends a command to the MMC
char r1[7];
char hex_str[3] = " ";
char i;
SPI(0xFF);
SPI(befF);
SPI(AdrH>>8);
SPI(AdrH & 0xFF);
SPI(AdrL>>8);
SPI(AdrL & 0xFF);
SPI(befH);
SPI(0xFF);
for(i=0; i<7; i++)
r1[i] = SPI(0xFF);
for(i=0; i<7; i++)
{
conv_char2Hex(r1[i], hex_str);
LCD_puts(0,2, NORMAL, hex_str);
}
return r1[0];
}
char InitMMC(void)
{
int i;
char r1;
char str[10] = " ";
PORTCbits.RC7 = 1; // MMC-Disabled
for(i=0; i < 10; i++)SPI(0xFF); // send 10*8=80 clock pulses
PORTCbits.RC7 = 0; // MMC-Enabled
if (Command(0x40,0,0,0x95) !=1) goto mmcerror;
st:
if(Command(0x41,0,0,0xFF) != 0) goto st;
SPI(0xFF);
//Delay10KTCYx(0);
PORTCbits.RC7 = 1; // MMC-Disabled
SPI(0xFF);
return 1;
mmcerror:
PORTCbits.RC7 = 1; // MMC-Disabled
SPI(0xFF);
return 0;
}
|
Code for the PICC conversion code
The results for the r1-r8 values are as below.
01 FF FF FF FF FF FF... for cmd 0x40
F0 5F FF FF FF FF FF... for cmd 0x41
so the first command seems to be right. but then i get some weird values for the next command.
Code: |
spi_setup code-->setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_XMIT_L_TO_H|SPI_CLK_DIV_64|SPI_SAMPLE_AT_END);
int8 MMC_Command(int8 befF, int16 AdrH, int16 AdrL, int8 befH,
int8 valid, int8 invalid)
{
int8 i;
int8 r1[7];
spi_write(0xFF);
spi_write(befF);
spi_write(AdrH>>8);
spi_write(AdrH & 0xFF);
spi_write(AdrL>>8);
spi_write(AdrL & 0xFF);
spi_write(befH);
spi_read(0xFF);
for(i=0; i<7; i++)
r1[i] = spi_read(0xFF);
for(i=0; i<7; i++)
{
LCD_SetCursor(i*3,valid);
printf(LCD_putc, "%X", r1[i]);
}
delay_ms(4000);
return r1[0];
}
int1 MMC_Initialize(void)
{
int8 i, r1;
output_high(PIN_C7); //MMC select off
for(i=0; i < 20; i++)
spi_write(0xFF);
output_low(PIN_C7);
//delay_ms(20);
if(MMC_Command(0x40,0,0,0x95, 1, 99) != 1) goto ErrorMMC;
MMCLoop:
r1 = MMC_Command(0x41, 0, 0, 0xFF, 0, 0x40);
//LCD_SetCursor(0,6);
//printf(LCD_putc, "41_R1: %X", r1);
if(r1 != 0) {
goto MMCLoop;
}
spi_write(0xFF);
output_high(PIN_C7);
spi_write(0xFF);
return TRUE;
ErrorMMC:
output_high(PIN_C7);
spi_write(0xFF);
return FALSE;
}
|
and just for reference, here is my code for setting the analog values to my SPI ADC chip. This is working great, even though it is much less complex than the MMC.
Code: |
void SetAnalogOutput(int8 channel, float value)
{
float v;
int16 setpoint;
int8 lsb, msb;
output_low(PIN_C6);
v = 1024.0 * (value / 1000.0);
setpoint = channel;
setpoint = setpoint<<2;
setpoint += 3;
setpoint = setpoint<<10;
setpoint += (int16) v;
setpoint = setpoint<<2;
setpoint &= 0xFFFC;
lsb = setpoint &0xFF;
msb = setpoint>>8;
spi_write(msb);
spi_write(lsb);
output_high(PIN_C6);
}
|
|
|