This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

atmel

how 2 intialize at89c51ed2 for spi communication with ds1306?

Parents
  • Thank you for your reply sir. i have return this code.
    #include <REG51xD2.H>
    #include<stdio.h>
    #include<stdlib.h>
    unsigned char SPI(unsigned char);

    void sdelay(int );
    void main(void)
    { EA=1;
    SPSTA=0x00;
    SPCON=0xd6;

    P1_7=1;//mOsi

    P1_6=0;//sck
    P1_6=1;
    P1_1=1;//ss
    sdelay(1);

    SPI(0x80);
    SPI(0x55);
    SPI(0x58);
    SPI(0x16);
    SPI(0x3);
    SPI(0x19);
    SPI(0x10);
    SPI(0x04);
    P1_1=0;
    sdelay(1);
    }

    unsigned char SPI(unsigned char mybyte)
    {

    SPDAT|=mybyte;

    while(!(SPSTA&0x80)) { ; }

    SPSTA&=0x00; return SPDAT;

    }

    void sdelay(int ms)
    { unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<135;j++);

    }

    here the value is not getting transmitted to SPDAT register.. I don't know what is the problem wiht this code

Reply
  • Thank you for your reply sir. i have return this code.
    #include <REG51xD2.H>
    #include<stdio.h>
    #include<stdlib.h>
    unsigned char SPI(unsigned char);

    void sdelay(int );
    void main(void)
    { EA=1;
    SPSTA=0x00;
    SPCON=0xd6;

    P1_7=1;//mOsi

    P1_6=0;//sck
    P1_6=1;
    P1_1=1;//ss
    sdelay(1);

    SPI(0x80);
    SPI(0x55);
    SPI(0x58);
    SPI(0x16);
    SPI(0x3);
    SPI(0x19);
    SPI(0x10);
    SPI(0x04);
    P1_1=0;
    sdelay(1);
    }

    unsigned char SPI(unsigned char mybyte)
    {

    SPDAT|=mybyte;

    while(!(SPSTA&0x80)) { ; }

    SPSTA&=0x00; return SPDAT;

    }

    void sdelay(int ms)
    { unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<135;j++);

    }

    here the value is not getting transmitted to SPDAT register.. I don't know what is the problem wiht this code

Children
  • HI.

    b4 u go on u must put a change in ur code to stop reset.

    like;

    void main(void)
    {
      .
      .
      .
      sdelay(1);
    
      for (;;)
        ;
    }
    

    Always yo're freind.

    Zeusti.

  • Thank you for your reply sir. i have return this code.
    i do not understand, code has comments, I see none.

    Please post your code, not some scribbles

    Erik

  • "i do not understand, code has comments, I see none."

    As a sentence, that one makes no sense.

  • "i do not understand that you state "here is my code", code has comments, I see none."

    is that better?

  • "is that better?"

    A little better ;)

    This may be considered better:


    I do not think you are right when you say "here is my code". Code has comments. I see no comments in your post. It could therefore be deduced that you have not actually posted any code for us to view.

    I do admit, however, that mine is way longer than yours.

    ps I totally agree with the sentiments of your original post.

  • I have written the comments plz go through it
    #include <REG51xD2.H>

    void SPI(unsigned char);

    void sdelay(int );
    void main(void)
    {

    EA=1;
    SPSTA=0x00;
    SPCON=0xd6; /*MSTR(4th bit),SPEN(7th bit),CPHA (2nd bit),7th,0th & 1st bits are enbled. the 7th ,0th & 1st bits are enbled for Fclk Periph/128*/

    IEN1=0x04; /* enable spi interrupt */

    P1_7=1;/*mOsi enable*/
    P1_6=1;/*sck enable*/
    P1_1=1;/*slave select*/
    sdelay(1);
    SPI(0x80);/* send an example data */
    sdelay(1);
    while(1);
    }

    void SPI(unsigned char mybyte)
    {

    SPDAT=mybyte;/* read receive data */

    while(!(SPSTA&0x80))/* set software flag */ { ; }

    SPSTA&=0x7f;

    }

    void sdelay(int ms)
    { unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<135;j++);

    }

  • I hav written the comments plz go through it.
    #include <REG51xD2.H>

    void SPI(unsigned char);

    void sdelay(int );
    void main(void)
    {

    EA=1;
    SPSTA=0x00;
    SPCON=0xd6; /*MSTR(4th bit),SPEN(7th bit),CPHA(2nd bit),7th,0th & 1st bits are enbled. the 7th ,0th & 1st bits are enbled for Fclk Periph/128*/

    IEN1=0x04; /* enable spi interrupt */

    P1_7=1;/*mOsi enable*/
    P1_6=1;/*sck enable*/
    P1_1=1;/*slave select*/
    sdelay(1);
    SPI(0x80);/* send an example data */

    sdelay(1);
    while(1);
    }

    void SPI(unsigned char mybyte)
    {

    SPDAT=mybyte;/* read receive data */

    while(!(SPSTA&0x80))/* set software flag */ { ; }

    SPSTA&=0x7f;

    }

    void sdelay(int ms)
    { unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<135;j++);

    }

  • I hav written the comments plz go through it.
    #include <REG51xD2.H>

    void SPI(unsigned char);

    void sdelay(int );
    void main(void)
    {

    EA=1;
    SPSTA=0x00;
    SPCON=0xd6; /*MSTR(4th bit),SPEN(7th bit),CPHA(2nd bit),7th,0th & 1st bits are enbled. the 7th ,0th & 1st bits are enbled for Fclk Periph/128*/

    IEN1=0x04; /* enable spi interrupt */

    P1_7=1;/*mOsi enable*/
    P1_6=1;/*sck enable*/
    P1_1=1;/*slave select*/
    sdelay(1);
    SPI(0x80);/* send an example data */

    sdelay(1);
    while(1);
    }

    void SPI(unsigned char mybyte)
    {

    SPDAT=mybyte;/* read receive data */

    while(!(SPSTA&0x80))/* set software flag */ { ; }

    SPSTA&=0x7f;

    }

    void sdelay(int ms)
    { unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<135;j++);

    }

  • Why did you post your code THREE times?

    Why did you not place your code in 'pre' tags so we could read it more easily?

    Comments ... Hmmmm.

  • Sausage man,
    Have you noticed the widespread of C based delay loops (here and in other threads)? what is it with teachers today? even they can't get their act together?!?!?!

  • "Have you noticed the widespread of C based delay loops..."

    Unfortunately, I don't think it's just teachers who are to blame for this.

    There are a number of manufacturer examples that do the very same thing. I've certainly seen ST and PIC code that contains it.

    When I was getting my (precious few) qualifications, I was frequently told by the lecturers that we shouldn't concern ourselves with code efficiency because "tomorrows processors will be faster".

    At the time I thought that was b#@@sh1t - And I still do!

  • When I was getting my (precious few) qualifications, I was frequently told by the lecturers that we shouldn't concern ourselves with code efficiency because "tomorrows processors will be faster".

    At the time I thought that was b#@@sh1t - And I still do!
    1) I agree this is male cow manure
    2) you did not mention that when "tomorrows processors" are inserted the delay loops will be faster too, OOPS

    Erik

  • SPI(0x80);/* send an example data */
    .....
    SPDAT=mybyte;/* read receive data */

  • but they will also be given more to do - so there is no end to the challenge of fitting everything the customer wants into the resources that he's prepared to pay for...

  • Another factor is that more and more equipment will be driven on batteries at the same time as they are expected to be smaller and smaller.

    Not needing the extra speed of that new processor, and instead downclocking it or putting it to sleep means the battery can be smaller, or the unit can run a longer time.

    Isn't it enough that Windows and a lot of application developers increases the power consumption of our PC:s with 20-100W just because they like nice graphic screen savers and requires everything to be animated?

    I recently noticed that some of my Blu-ray disks overrides the players "screen saver" which means that the player will consume a lot more power, and - even more importantly - will not power down itself and the projector if the movie is left on pause too long...