Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

What is wrong with my code ??

Status
Not open for further replies.

afesheir

New Member
hi everybody ..
I hope I find my needs in this great board ..
Now I am trying to initialize MMC SanDisk 1GB using my own code (without using mikroC library) with PIC16F877A ..

When the card is initialized, I want my GLCD to display the word "Ready", otherwise I want to display "Initializing ..." ..

before anything, I want to display "WELCOME" ..

the GLCD is functioning properly alone, but with my code, the glcd displays nothing ..

here is my code:

Code:


//module connection

char GLCD_DataPort at PORTD;
sbit GLCD_CS1 at RB0_bit;
sbit GLCD_CS2 at RB1_bit;
sbit GLCD_RS at RB2_bit;
sbit GLCD_RW at RB3_bit;
sbit GLCD_EN at RB4_bit;
sbit GLCD_RST at RB5_bit;
sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;


// Software SPI module connections
sbit SoftSpi_SDI at RC4_bit;
sbit SoftSpi_SDO at RC5_bit;
sbit SoftSpi_CLK at RC3_bit;

sbit SoftSpi_SDI_Direction at TRISC4_bit;
sbit SoftSpi_SDO_Direction at TRISC5_bit;
sbit SoftSpi_CLK_Direction at TRISC3_bit;
// End Software SPI module connections


void sendCommand(int cmdIndex); //function prototype

void main() {
unsigned char Response;


Soft_SPI_Init(); // Init Soft_SPI
Glcd_Init(); //Init. GLCD
// Glcd_Set_Font(font5x7 , 5, 7, 32);// Change font
Glcd_Fill(0x00); //Clear GLCD
Glcd_Write_Text("WELCOME",0,0,1);


TRISC.B1=0; //Make RC2 output pin (CS or SS Pin)
PORTC.B1=0; //Hold CC=0
delay_ms(4); //Delay about 75 clock pulses at 20KHz

sendCommand(0x00); //CMD0 for S/W reset
Init:sendCommand(0x37); //CMD55 to tell that the next command is Application Specific
sendCommand(0x29); //ACMD41 to Initialize the SD Card
Response= Soft_Spi_Read(0x00);

if (Response==0x00)
{
Glcd_Fill(0x00); //Clear GLCD
Glcd_Write_Text("Ready !!",20,4,1);

}
else
{
Glcd_Fill(0x00);
Glcd_Write_Text("Initializing ...",20,4,1);
goto Init;
}

}

void sendCommand(int cmdIndex)
{
Soft_Spi_Write(0x40+cmdIndex); //Send command of index cmdIndex
Soft_Spi_Write(0x00); //Send 4
Soft_Spi_Write(0x00); //Clear Bytes
Soft_Spi_Write(0x00); //As
Soft_Spi_Write(0x00); //Argument
Soft_Spi_Write(0x95); //CRC = 0x95 for CMD0 and not important for SPI mode
}


also I am asking about the red number, this function is to read data from spi bus, why do we need to assign data to send ..?

Great thanks to everybody ..
 
Do not know a thing about microcontrollers, but if you place a breakpoint after your line of code

Glcd_Write_Text("WELCOME",0,0,1);

Do you see the word welcome?
 
Why are you trying to debug too many steps at once? You've got 3 things happening before it outputs "welcome". Does your code reach and execute each one?
 
yes upand ..
now the GLCD works fine and display texts without problems .. but I am seeking the SD initialization now .. I got "Initializing" on the screen all the time and the SD never gets ready .. why ?
 
Sorry for misunderstanding your problem.

This isn't the same programming language, but it's a sample of the code I've used for using an SD card with a PIC. It's using the SPI mode, not MMC. It's been so long since I've used this that I'm not much help in explaining it, only that it does work.
Code:
function MMC_Reset return bit is
	
	CS = high
	
	for 10 loop
		SPI(0xFF)
	end loop
		
	CS = low
	
	MMC_Command(0x40, 0, 0, 0, 0, 0x95)
	
	if ! MMC_Response(0x01) Then
		return false
	end if
	
	for 255 loop
		MMC_Command(0x41, 0, 0, 0, 0, 0xFF)
		if MMC_Response(0x00) Then
			-- exit with success
			CS = high
			SPI(0xFF)
			return true
		end if
	end loop
	
	return false
	
end function
 
nevermind, delete
 
Last edited:
what i understand by reading mikroC help...you should provide a variable to Soft_SPI_Read() function which will store the data which read from spi bus instead of a constant....that's how it should work..
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top