hi in my recent project i'm doing bit banging SPI in AT89S52. i am using a self modified library and its not working properly. i cant remember the original page where i found the code, though it was for the PIC's.
The data is sending correctly The problem is i am receiving same data which i am sending. take a look at the code.
#ifndef buffer #define buffer buf unsigned char bdata buffer; sbit Dbit0=buffer^0; sbit Dbit1=buffer^1; sbit Dbit2=buffer^2; sbit Dbit3=buffer^3; sbit Dbit4=buffer^4; sbit Dbit5=buffer^5; sbit Dbit6=buffer^6; sbit Dbit7=buffer^7; #endif // CONSTANTS ---------------------------------------------- #define CPOL 0 // Set CPOL to 1 or 0 #define CPHA 0 // Set CPHA to 1 or 0 // MACROS ------------------------------------------------- #if CPHA #define SCK_POST #if CPOL #define SCK_INIT 1 #define SCK_PRE SCK=0 #define SCK_MID SCK=1 #else #define SCK_INIT 0 #define SCK_PRE SCK=1 #define SCK_MID SCK=0 #endif #else #define SCK_PRE #if CPOL #define SCK_INIT 1 #define SCK_MID SCK=0 #define SCK_POST SCK=1 #else #define SCK_INIT 0 #define SCK_MID SCK=1 #define SCK_POST SCK=0 #endif #endif void SPI_begin() { MOSI=1; MISO=1; SCK=SCK_INIT; } unsigned char SPI_rw(unsigned char Data) { buffer=Data; SCK_PRE; MOSI = Dbit7; SCK_MID; Dbit7 = MISO; SCK_POST; // bit 7 SCK_PRE; MOSI = Dbit6; SCK_MID; Dbit6 = MISO; SCK_POST; // bit 6 SCK_PRE; MOSI = Dbit5; SCK_MID; Dbit5 = MISO; SCK_POST; // bit 5 SCK_PRE; MOSI = Dbit4; SCK_MID; Dbit4 = MISO; SCK_POST; // bit 4 SCK_PRE; MOSI = Dbit3; SCK_MID; Dbit3 = MISO; SCK_POST; // bit 3 SCK_PRE; MOSI = Dbit2; SCK_MID; Dbit2 = MISO; SCK_POST; // bit 2 SCK_PRE; MOSI = Dbit1; SCK_MID; Dbit1 = MISO; SCK_POST; // bit 1 SCK_PRE; MOSI = Dbit0; SCK_MID; Dbit0 = MISO; SCK_POST; // bit 0 return buffer; }
Please note: if i remove the Dbit'x' = MISO; and replae with Pbit'x' = MISO; other variable (pbit will be second bdata variable same as above) then it works. But logically using same buffer should work as i am only updating the bits.
My question is why the above code is not working?? all other methods are working correctly. why not this. what am i missing?????
and please don't tell me to use other methods. i have plenty of methods. i just want to know whats wrong in my code?
Thank you.
Thank you everyone the problem is finally solved.I just lowered the optimization level only for this function and now the code is working as it should.
#pragma ot(3, SIZE) unsigned char SPI_rw(unsigned char Data) { buffer=Data; SCK_PRE; MOSI = Dbit7; SCK_MID; Dbit7 = MISO; SCK_POST; // bit 7 SCK_PRE; MOSI = Dbit6; SCK_MID; Dbit6 = MISO; SCK_POST; // bit 6 SCK_PRE; MOSI = Dbit5; SCK_MID; Dbit5 = MISO; SCK_POST; // bit 5 SCK_PRE; MOSI = Dbit4; SCK_MID; Dbit4 = MISO; SCK_POST; // bit 4 SCK_PRE; MOSI = Dbit3; SCK_MID; Dbit3 = MISO; SCK_POST; // bit 3 SCK_PRE; MOSI = Dbit2; SCK_MID; Dbit2 = MISO; SCK_POST; // bit 2 SCK_PRE; MOSI = Dbit1; SCK_MID; Dbit1 = MISO; SCK_POST; // bit 1 SCK_PRE; MOSI = Dbit0; SCK_MID; Dbit0 = MISO; SCK_POST; // bit 0 return buffer; }
Sorry for asking, but I want to learn something. What exactly was wrong? The high optimization may create a common subroutine which makes things slower; but that is not wrong code.
So what exactly was wrong with the optimization level?
missing 'volatile', probably.
wow. you are a genius. thank you very much. now i know the EXACT Problem of my code. thank you once again.
your welcome
plz donate me sum bitcoins if u really want 2 show apreciation