SPI Controller with PSoC

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello everyone. I just order a ADC chip from analog devices. Which is ADE7763. This is classical ADC unit which is working on MODE2.

I started to write program and i will check is it works, or not next week. But it is my first time to use SPI communication so i want to if you guys check it and tell me its okay?

I added just one digital clock SPIM. MOSI=P0[1], SCLK=P0[3], MISO=P0[0], CS(SS)=P1[0] and MISO is set to "async".

In ADE7763, first of all I need to send 3x8bit data for write. After that master send 8bit data and ADE7763 answers 2x8bit data (MSB-LSB) (As i understand)
ADE7763:     http://www.analog.com/static/imported-f ... DE7763.pdf      on page 5.

So i wrote that program;

 BYTE Port1Shadow;
int i,k;
int VoltMSB, VoltLSB;
int VoltageResult;
int myInt = 10;
char str[7]="";  // String to hold the ascii result
BYTE data=0x80;
BYTE data2=0x00;
BYTE data3=0x0C;
BYTE x;

void GetVoltage()
{


   Port1Shadow &= ~0x01;
   PRT1DR = Port1Shadow;

   SPIM_Start(SPIM_SPIM_MODE_2 | SPIM_SPIM_MSB_FIRST);
   SPIM_SendTxData(0x16);
   
   
   LCD_Position(0,7); // Place LCD cursor at row 0, col 5.
   x=SPIM_bReadRxData();
   LCD_PrCString(" U=   V"); // Print "PSoC LCD" on the LCD
   
   SPIM_SendTxData(0x00);
   LCD_Position(0,10); // Place LCD cursor at row 0, col 5.
   VoltMSB=SPIM_bReadRxData();

   SPIM_SendTxData(0x36);
   LCD_Position(0,10); // Place LCD cursor at row 0, col 5.
   VoltLSB=SPIM_bReadRxData();
   
   
   VoltageResult= (256*VoltMSB)+VoltLSB;
   itoa(str, VoltageResult, myInt);
   LCD_PrString(str); // Print "PSoC LCD" on the LCD
   
   
   SPIM_Stop();
   Port1Shadow = 0x01;
   PRT1DR = Port1Shadow;

   
}

void main(void)
{
    Port1Shadow = 0x01;
    PRT1DR = Port1Shadow;
    LCD_Start(); // Initialize LCD
   /*Port1Shadow &= ~0x01;
   PRT1DR = Port1Shadow;
   if(PRT1DR == Port1Shadow)
   {
   SPIM_Start(SPIM_SPIM_MODE_0 | SPIM_SPIM_MSB_FIRST);
   SPIM_SendTxData(data);
   x=SPIM_bReadRxData();
   SPIM_SendTxData(data2);
   x=SPIM_bReadRxData();
   SPIM_SendTxData(data3);
   x=SPIM_bReadRxData();
   SPIM_Stop();
   Port1Shadow = 0x01;
   PRT1DR = Port1Shadow;
   }    While(1)
{
GetVoltage();
}



What do you think about that program or what will you suggest ?

 

0 Likes
1 Solution
Anonymous
Not applicable

Hey,I feel you should implement your basic SPI functions of Tx and Rx of a byte as I've suggested below:

   


uint8 spiTx(uint8 bDataSend)//Send a Byte.
{   
    while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); // Wait for existing data to go out.
    SPIM_SendTxData(bDataSend); //Write the Byte onto the TX FIFO
    while (!( SPIM_bReadStatus() & SPIM_SPI_COMPLETE  ));//Wait till completely sent.
     while (!( SPIM_bReadStatus() & SPIM_RX_BUFFER_FULL ));//Wait for a byte to come in
    return(SPIM_bReadRxData());
}

uint8 spiRx(void){//Receive a Byte.
    CyDelay(1);
    return(spiTx(0x00));
}

   

 

   

The issue in your code is,you arent waiting for a byte to come in,or to go out.

   

 

   

Hope this helps.

View solution in original post

0 Likes
6 Replies
Anonymous
Not applicable

Hey,I feel you should implement your basic SPI functions of Tx and Rx of a byte as I've suggested below:

   


uint8 spiTx(uint8 bDataSend)//Send a Byte.
{   
    while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); // Wait for existing data to go out.
    SPIM_SendTxData(bDataSend); //Write the Byte onto the TX FIFO
    while (!( SPIM_bReadStatus() & SPIM_SPI_COMPLETE  ));//Wait till completely sent.
     while (!( SPIM_bReadStatus() & SPIM_RX_BUFFER_FULL ));//Wait for a byte to come in
    return(SPIM_bReadRxData());
}

uint8 spiRx(void){//Receive a Byte.
    CyDelay(1);
    return(spiTx(0x00));
}

   

 

   

The issue in your code is,you arent waiting for a byte to come in,or to go out.

   

 

   

Hope this helps.

0 Likes
Anonymous
Not applicable

So you say i should change it this way?

   

....

   

void GetVoltage()
{


   Port1Shadow &= ~0x01;
   PRT1DR = Port1Shadow;

   SPIM_Start(SPIM_SPIM_MODE_2 | SPIM_SPIM_MSB_FIRST);
       while( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); // Wait for existing data to go out.
   

   

   SPIM_SendTxData(0x16);
   
     
   LCD_Position(0,7); // Place LCD cursor at row 0, col 5.
   x=SPIM_bReadRxData();
   LCD_PrCString(" U=   V"); // Print "PSoC LCD" on the LCD
   
   SPIM_SendTxData(0x00);
   LCD_Position(0,10); // Place LCD cursor at row 0, col 5.
   VoltMSB=SPIM_bReadRxData();

   SPIM_SendTxData(0x36);
   LCD_Position(0,10); // Place LCD cursor at row 0, col 5.
   VoltLSB=SPIM_bReadRxData();
   while (!( SPIM_bReadStatus() & SPIM_SPI_COMPLETE  ));//Wait till completely sent.
      while (!( SPIM_bReadStatus() & SPIM_RX_BUFFER_FULL ));//Wait for a byte to come in
   

   
   VoltageResult= (256*VoltMSB)+VoltLSB;
   itoa(str, VoltageResult, myInt);
   LCD_PrString(str); // Print "PSoC LCD" on the LCD
   
   
   SPIM_Stop();
   Port1Shadow = 0x01;
   PRT1DR = Port1Shadow;

   
}

 ....

0 Likes
Anonymous
Not applicable

Well i changed program this way and when i checked it on osillascope it seems nothing to changed. But change might not be seems very well on scope. After i tried with ADE7763 i can say it's works or not. Thanks  anyway 😉

   

 

   

   

Port1Shadow &= ~

       

   

0x01;if( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); 0x17);0,3); if (!( SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ));"V"); if( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); 0x00);0,0); if (!( SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ));if( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) ); 0xDC);0,0); if (!( SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ));256*VoltMSB)+VoltLSB;0x01;

   

PRT1DR = Port1Shadow;

   

 

   

SPIM_Start(SPIM_SPIM_MODE_2 | SPIM_SPIM_MSB_FIRST);

   

 

   

{

   

SPIM_SendTxData(

   

}

   

LCD_Position(

   

 

   

{

   

x=SPIM_bReadRxData();

   

}

   

LCD_PrCString(

   

 

   

{

   

SPIM_SendTxData(

   

}

   

LCD_Position(

   

 

   

{

   

VoltMSB=SPIM_bReadRxData();

   

}

   

 

   

{

   

SPIM_SendTxData(

   

}

   

LCD_Position(

   

 

   

{

   

VoltLSB=SPIM_bReadRxData();

   

}

   

VoltageResult= (

   

itoa(str, VoltageResult, myInt);

   

LCD_PrString(str);

   

SPIM_Stop();

   

Port1Shadow =

   

PRT1DR = Port1Shadow;

0 Likes
Anonymous
Not applicable

 Port1Shadow &= ~0x01;
 PRT1DR = Port1Shadow;
 
 SPIM_Start(SPIM_SPIM_MODE_2 | SPIM_SPIM_MSB_FIRST);
 if( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
 {
 SPIM_SendTxData(0x17);
 }
 LCD_Position(0,3);
 if (!( SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ));
 {
 x=SPIM_bReadRxData();
 }
 LCD_PrCString("V");
 if( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
 {
 SPIM_SendTxData(0x00);
 }
 LCD_Position(0,0);
 if (!( SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ));
 {
 VoltMSB=SPIM_bReadRxData();
 }
 if( ! (SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY ) );
 {
 SPIM_SendTxData(0xDC);
 }
 LCD_Position(0,0);
 if (!( SPIM_bReadStatus() & SPIM_SPIM_RX_BUFFER_FULL ));
 {
 VoltLSB=SPIM_bReadRxData();
 }

   

 VoltageResult= (256*VoltMSB)+VoltLSB;
 itoa(str, VoltageResult, myInt);
 LCD_PrString(str);

   

 SPIM_Stop(); 
 Port1Shadow = 0x01;
 PRT1DR = Port1Shadow;

0 Likes
Anonymous
Not applicable

So,did this work or not?

0 Likes
Anonymous
Not applicable

I can't understand for now. I can tell u that next week. But i checked on ossilascope and it seems they are same and nothing is changed. Anyway thank you 😉

0 Likes