BiNa2605
Full Member level 3
Hi everyone,
I have a trouble in SPI transmission. I knew the problem but I don't know how to fix it. I connect MCU (PIC16F151x) to EEPROM in SPI mode.
I used MikroC to coding. As normal, the SPI transmission failed. However, when I connected one probe of Oscilloscope to the MOSI pin (just touched), I received the right data in EEPROM. Does anyone know the problem I met? I appreciate for any suggestion.
MCU operates in internal Clock mode, 1Mhz.
- - - Updated - - -
This is my code.
I think the problem is the hardware not in programming.
I have a trouble in SPI transmission. I knew the problem but I don't know how to fix it. I connect MCU (PIC16F151x) to EEPROM in SPI mode.
I used MikroC to coding. As normal, the SPI transmission failed. However, when I connected one probe of Oscilloscope to the MOSI pin (just touched), I received the right data in EEPROM. Does anyone know the problem I met? I appreciate for any suggestion.
MCU operates in internal Clock mode, 1Mhz.
- - - Updated - - -
This is my code.
Code:
#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);
}
}
}
I think the problem is the hardware not in programming.