|
|
View previous topic :: View next topic |
Author |
Message |
gukevin1984
Joined: 20 May 2011 Posts: 1
|
is that the source problem? |
Posted: Fri May 20, 2011 11:24 pm |
|
|
i am going to use this code for sensor data transfer to SD card by SPI mode
but when i built it, there is some problem, failed~~~
help~~~~~~pls, im a new guy
#include <htc.h>
#define SPI_OUT RC5_bit /* define SPI SDO signal to be PIC port RC5 */
#define SPI_IN RC4_bit /* Define SPI SDI signal to be PIC port RC4 */
#define SPI_CLK RC3_bit /* Define SPI CLK/SCK signal to be PIC port RC3 */
#define SPI_CS RC2_bit /* Define SPI CS/SS signal to be PIC port RC2 */
int mmc_init();
int mmc_response(unsigned char response);
int mmc_read_block(unsigned long block_number);
int mmc_write_block(unsigned long block_number);
int mmc_get_status();
/************************** MMC Init **************************************/
/* Initialises the MMC into SPI mode and sets block size, returns 0 on success */
int mmc_init()
{
int i;
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_SS_DISABLED);
*0x94 |= 0x00; // set CKE = 0 - from idle to active clock state
*0x14 &= 0xEF; // set CKP = 0 - Idle state for clock is a low level
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
for(i=0;i<10;i++) // initialise the MMC card into SPI mode by sending clks on (11)
{
SPI_WRITE(0x0B);
}
OUTPUT_LOW(PIN_C2); // set SS = 0 (on) tells card to go to spi mode when it receives reset
SPI_WRITE(0x40); // send reset command
SPI_WRITE(0x00); // all the arguments are 0x00 for the reset command
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x95); // precalculated checksum as we are still in MMC mode
puts("Sent go to SPI\n\r");
if(mmc_response(0x01) == 1) return 1; // if = 1 then there was a timeout waiting for 0x01 from the mmc
puts("Got response from MMC\n\r");
i = 0;
while((i < 255) && (mmc_response(0x00) == 1)) // must keep sending command if response
{
SPI_WRITE(0x41); // send mmc command one to bring out of idle state
SPI_WRITE(0x00); // all the arguments are 0x00 for command one
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
i++;
}
if(i >= 254) return 1; // if >= 254 then there was a timeout waiting for 0x00 from the mmc
puts("Got out of idle response from MMC\n\r");
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
SPI_WRITE(0xFF); // extra clocks to allow mmc to finish off what it is doing
OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
SPI_WRITE(0x50); // send mmc command one to bring out of idle state
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x02); // high block length bits - 512 bytes
SPI_WRITE(0x00); // low block length bits
SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
if((mmc_response(0x00)) == 1) return 1;
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
puts("Got set block length response from MMC\n\r");
return 0;
}
/************************** MMC Get Status **************************************/
/* Get the status register of the MMC, for debugging purposes */
int mmc_get_status()
{
OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
SPI_WRITE(0x58); // send mmc command one to bring out of idle state
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00); //
SPI_WRITE(0x00); // always zero as mulitples of 512
SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
return 0;
}
/************************** MMC Write Block **************************************/
int mmc_write_block(unsigned long block_number)
{
unsigned long i;
unsigned long varh,varl;
varl=((block_number&0x003F)<<9);
varh=((block_number&0xFFC0)>>7);
puts("Write block\n\r"); // block size has been set in mmc_init()
OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
SPI_WRITE(0x58); // send mmc write block
SPI_WRITE(HIGH(varh));
SPI_WRITE(LOW(varh));
SPI_WRITE(HIGH(varl));
SPI_WRITE(0x00); // always zero as mulitples of 512
SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
if((mmc_response(0x00))==1) return 1;
puts("Got response to write block\n\r");
SPI_WRITE(0xFE); // send data token
for(i=0;i<512;i++)
{
SPI_WRITE(i2c_eeprom_read(HIGH(i),LOW(i))); // send data
}
SPI_WRITE(0xFF); // dummy CRC
SPI_WRITE(0xFF);
if((SPI_READ(0xFF)&0x0F)!=0x05) return 1;
puts("Got data response to write block\n\r");
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
return 0;
}
/************************** MMC Read Block **************************************/
/**** Reads a 512 Byte block from the MMC and outputs each byte to RS232 ****/
int mmc_read_block(unsigned long block_number)
{
unsigned long i;
unsigned long varh,varl;
varl=((block_number&0x003F)<<9);
varh=((block_number&0xFFC0)>>7);
OUTPUT_LOW(PIN_C2); // set SS = 0 (on)
SPI_WRITE(0x51); // send mmc read single block command
SPI_WRITE(HIGH(varh)); // arguments are address
SPI_WRITE(LOW(varh));
SPI_WRITE(HIGH(varl));
SPI_WRITE(0x00);
SPI_WRITE(0xFF); // checksum is no longer required but we always send 0xFF
if((mmc_response(0x00))==1) return 1; // if mmc_response returns 1 then we failed to get a 0x00 response (affirmative)
puts("Got response to read block command\n\r");
if((mmc_response(0xFE))==1) return 1; // wait for data token
puts("Got data token\n\r");
for(i=0;i<512;i++)
{
putc(SPI_READ(0xFF)); // we should now receive 512 bytes
}
SPI_READ(0xFF); // CRC bytes that are not needed
SPI_READ(0xFF);
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
SPI_WRITE(0xFF); // give mmc the clocks it needs to finish off
puts("\n\rEnd of read block\n\r");
return 0;
}
/************************** MMC get response **************************************/
/**** Repeatedly reads the MMC until we get the response we want or timeout ****/
int mmc_response(unsigned char response)
{
unsigned long count = 0xFFFF; // 16bit repeat, it may be possible to shrink this to 8 bit but there is not much point
while(SPI_READ(0xFF) != response && --count > 0);
if(count==0) return 1; // loop was exited due to timeout
else return 0; // loop was exited before timeout
}
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 25.1 function declared implicit int
Error [192] C:\Users\kevin\Desktop\Untitled.c; 25.11 undefined identifier "SPI_MASTER"
Error [192] C:\Users\kevin\Desktop\Untitled.c; 25.24 undefined identifier "SPI_H_TO_L"
Error [192] C:\Users\kevin\Desktop\Untitled.c; 25.37 undefined identifier "SPI_CLK_DIV_4"
Error [192] C:\Users\kevin\Desktop\Untitled.c; 25.53 undefined identifier "SPI_SS_DISABLED"
Error [981] C:\Users\kevin\Desktop\Untitled.c; 27.7 pointer required
Error [981] C:\Users\kevin\Desktop\Untitled.c; 27.7 pointer required
Error [981] C:\Users\kevin\Desktop\Untitled.c; 28.7 pointer required
Error [981] C:\Users\kevin\Desktop\Untitled.c; 28.7 pointer required
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 30.1 function declared implicit int
Error [192] C:\Users\kevin\Desktop\Untitled.c; 30.13 undefined identifier "PIN_C2"
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 34.1 function declared implicit int
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 37.1 function declared implicit int
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 46.1 function declared implicit int
Error [192] C:\Users\kevin\Desktop\Untitled.c; 93.12 undefined identifier "PIN_C2"
Error [192] C:\Users\kevin\Desktop\Untitled.c; 118.12 undefined identifier "PIN_C2"
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 121.11 function declared implicit int
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 122.11 function declared implicit int
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 135.11 function declared implicit int
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 142.5 function declared implicit int
Error [192] C:\Users\kevin\Desktop\Untitled.c; 163.12 undefined identifier "PIN_C2"
Warning [361] C:\Users\kevin\Desktop\Untitled.c; 182.1 function declared implicit int
********** Build failed! ********** |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 21, 2011 12:16 am |
|
|
Quote: |
#include <htc.h>
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_SS_DISABLED);
*0x94 |= 0x00; // set CKE = 0 - from idle to active clock state
*0x14 &= 0xEF; // set CKP = 0 - Idle state for clock is a low level
OUTPUT_HIGH(PIN_C2); // set SS = 1 (off)
|
You're using the Hi-Tech C compiler to compile CCS C source code.
That's not going to work. They are not compatible.
Decide which compiler you want to use, Hi-Tech or CCS.
Then find source code which is written for the compiler you choose.
If you choose Hi-Tech, here are the forums for that compiler:
http://forum.htsoft.com/all/categories.php
http://www.microchip.com/forums/f231.aspx |
|
|
|
|
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
|