#define SEN PORTC.B1
#define PAGE_WR 0x0800
#define WRITE 0x0000
#define READ 0x4000
#define COMMANDMODE 0XC0
unsigned int uiTemp, uiTemp1, uiMo;
unsigned char ucwrData,ucKQ;
unsigned char Data[6] = {0x1C,0x2C,0x3C,0x4C,0x5C,0x6C};
//Command Mode for SL900A SPI interface
void SL900A_CommandMode(unsigned char ucData_FIFO){
unsigned char sr; //Status Register
//Check the FIFO status
do{
SEN = 1;
delay_us(200);
SPI1_WRITE(0xE7); //Write the command to read FIFO Status register
sr = SPI1_READ(0x00);
delay_us(50);
SEN = 0;
}while(!sr & 0x80);
//Start SPI transfer
SEN = 1;
delay_us(500);
//Transfer Data_IN
SPI1_WRITE(0xE1); //Write WRITE_FIFO command
//delay_us(100);
SPI1_WRITE(ucData_FIFO);
delay_us(500);
SEN = 0;
}
unsigned char SL900A_Read(unsigned int uirMode, unsigned int uirAddress){
unsigned char ucResult;
SEN = 1;
delay_ms(1);
uiTemp = uirMode | uirAddress;
SPI1_Write(uiTemp >> 8);
SPI1_Write(uirAddress & 0x00FF);
//delay_ms(1);
ucResult= SPI1_Read(0);
delay_ms(1);
SEN = 0;
return ucResult;
}
//Write function for the SPI interface
void SL900A_Write(unsigned int uiMode, unsigned int uiAddress, unsigned char ucData){
//SPI1_Init();
SEN = 1; //Enable Slave select
delay_ms(1); //SEN to first SCLK rising edge setup time
//Transmit 16 bit data
uiTemp = uiMode | uiAddress;
uiTemp1 = uiTemp & 0xFF00; //Lay 8 bit cao
//SPI1_Write(ucMode >> 6); //Send WRITE Mode (two highest bits)
SPI1_Write(uiTemp >> 8); //Transmit higher 8 bit
SPI1_Write(uiAddress & 0x00FF); //Transmit lower 8 bit
SPI1_Write(ucData); //Data need to transmit
delay_ms(1);
SEN = 0; //Stop
uiTemp = 0;
}
void main()
{
unsigned int uiAdd,i,uirMo;
//FOSC = 0b110; INTOSC oscillator
OSCCON = 0b01011011; //Internal Oscillator 1Mhz --- SL900A -> 1.5v
//OSCCON = 0b01101010; //Internal Oscillator 4Mhz
//OSCCON = 0b01110010; //Internal Oscillator 8Mhz
//OSCCON = 0b01111010; //Internal Oscillator 16Mhz
ANSELC = 0; //PortC is a digital output
TRISC.B1 = 0; //Chip Select
TRISB.B4 = 0; //RB4 output test led
//PORTB.B4 = 0;
//Initiate SPI
//SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_LOW_2_HIGH);
SPI1_Init();
//Choose the Mode here
//uirMo = READ;
//uiAdd = 0x0050; //The first physical address of SL900A User_Memory
uiAdd = 0x0064; //the last physical address of SL900A User_Memory
//ucwrData = 0x29;
uiMo = WRITE;
SEN = 0;
while(1){
for(i = 0; i < 6; i++)
{
SL900A_Write(uiMo, uiAdd+i, Data[i]);
PORTB.B4 = 0;
delay_ms(20);
PORTB.B4 = 1;
delay_ms(20);
}
}
}
The EEPROM is inside SL900A (RFID IC tag). I read the value of EEPROM using RFID reader and know exactly why the SPI transmission failed.
I expect one scope picture with all 4 signlas (maybe MISO is not necessary). But i need to be able to see each bit. Timing of MOSI to SCK. and SS to SCK.show us one scope picture with MOSI, MISO, SCK and SS of a one byte transfer.
Either the mikroC manual description of SPI1_Init() quoted in post #5 is wrong or you are setting the mode somewhere in your code,--> Select mode0: both CKP and CKE = 0
(I can't find this in your code)
Either the mikroC manual description of SPI1_Init() quoted in post #5 is wrong or you are setting the mode somewhere in your code,
The pink signal is SCK and is raising.In post#3, second picture you see that with rising SCK edge MOSI = EEPROM_DIN changes.
But in your scope picture (sending 0x64) you see that this takes place at the falling edge of SCK.
It's include in SPI1_Init(); That I presented in post #5--> Select mode0: both CKP and CKE = 0
(I can't find this in your code)
Now I think I see what you mean. I will try to fix that.In post#3, second picture you see that with rising SCK edge MOSI = EEPROM_DIN changes.
But in your scope picture (sending 0x64) you see that this takes place at the falling edge of SCK.
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
SSPSTAT.SMP=0; //Master Mode, Input data sampled at middle of data output time
SSPSTAT.CKE=0; //Transmit occurs on transition from Idle to active clock state
SSPCON.CKP=0; //Idle state for clock is a low level
//OSC_DIV4
SSPCON1.SSPM3=0;
SSPCON1.SSPM2=0;
SSPCON1.SSPM1=0;
SSPCON1.SSPM0=0;
SSPCON1.SSPEN=1;
Apparently you don't compare the actual waveform with the datasheet specification as shown in post #3. According to it, DIN (= MOSI) should be set on the rising clock edge but it's actually set on the falling which could well explain the observed problems.I just used SPI1_Init(). Why is it wrong? I don't understand.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?