Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

Problem in receiving clock from slave

$
0
0

Master Code

#include <msp430x22x4.h>

#define SDA BIT0 //Serial data line
#define SCL BIT2 //Serail Clock line

void delay(void);
void SDA_High();
void SDA_High();
void SCL_Low();
void SCL_High();
void start(void);
void stop(void);
void Init();
unsigned char write(char c);
char read (unsigned char ack);


void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALBC1_1MHZ;

Init();
while(1)
{
start();
write(0x50);
//write(0xF9);
stop();
}
}

// To create the delay
void delay(void)
{
for(int i=0;i<0x66;i++);
}

void SDA_High()
{
P2DIR &= ~SDA; // P2.0 as input
delay();
}
void SDA_Low()
{
P2DIR |= SDA; // P2.0 as output
delay();
}

void SCL_Low()
{
P2OUT &= ~SCL; // P2.3 == 00000000;
delay();
}
void SCL_High()
{
P2OUT |= SCL; // P2.3 = 00001000;
delay();
}


void Init()
{
P2SEL &= ~SDA;
P2SEL &= ~SCL;

P2OUT &= ~SCL;
P2OUT &= ~SDA;

P2DIR |= SCL;
P2DIR &= ~SDA;

SCL_High();
SDA_Low();
SDA_High();
}

// Start the Sequence
void start(void)
{
SCL_High();
SDA_High();

SCL_High();
SDA_Low();

SCL_Low();
SDA_High();
}

// Stop the Sequence

void stop(void)
{
SCL_Low();
SDA_Low();

SCL_High();
SDA_Low();

SCL_High();
SDA_High();
}

// Write the data

unsigned char write(char c)
{
unsigned char ack;
for(int i=0;i<8;i++)
{
SCL_Low();
if(c & 0x80)
SDA_High();
else
SDA_Low();

c= c<<1;
SCL_High();
}
SCL_Low();

SDA_High();

SCL_High();
if(P2IN & SDA)
{
ack = 1; // No acknowledgement is there
}
else
{
ack = 0; // Acknowledgement is there
}

SCL_Low();
return(ack);
}

//Read the data

char read (unsigned char ack)
{
SCL_Low();
SDA_High();
char c;
for(int i=0;i<8;i++)
{
c = c<<1;
SCL_High();
if(P2IN & SDA)
c |= 0x01;
else
c &= ~0x01;

SCL_Low();

}

if(ack)
{
SDA_Low();
}
else
{
SDA_High();
}
SCL_High();
SCL_Low();

return(c);
}

// Slave Code

#include <msp430x22x4.h>

#define SDA BIT0 //Serial data line
#define SCL BIT2 //Serail Clock line

void delay(void);
void SDA_High();
void SDA_Low();
unsigned char write(char c);
char read (unsigned char ack);
char read_addr();

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALBC1_1MHZ;
P2SEL &= ~SDA;
P2SEL &= ~SCL;
P2DIR &= ~SDA;
P2DIR &= ~SCL;
/*P1SEL &= ~0x01;
P1DIR |= 0x01;
P1OUT &= 0x00;*/

//char cmd;
//delay();
while(1)
{

//while((!(P2IN & 0x01))&&(P2IN & SCL));
read_addr();
/*{
cmd = read(1);
if( cmd == 0x00)
P1OUT |= 0x01;
else
P1OUT &= ~0x01;
for(int i=0;i<0xFF;i++)
for(int j=0;j<0xFF;j++);
}*/


}
}

// To create the delay
void delay(void)
{
for(int i=0;i<0x30;i++);
}

void SDA_High()
{
P2DIR &= ~SDA;
delay();
}
void SDA_Low()
{
P2DIR |= SDA;
delay();
}

// Write the data

unsigned char write(char c)
{
unsigned char ack;
P2DIR |= SDA;
for(int i=0;i<8;i++)
{
if((P2IN & SCL) == 0x00)
{
if(c & 0x80)
SDA_High();
else
SDA_Low();

c= c<<1;
}
//delay();
}
P2DIR &= ~SDA;
delay();
if(P2IN & SDA)
{
if((P2IN & SCL) == 0x08)
ack = 1; // No acknowledgement is there
}
else
{
if((P2IN & SCL) == 0x08)
ack = 0; // Acknowledgement is there
}

P2OUT &= ~SDA;
return(ack);
}

//Read the data

char read(unsigned char ack)
{
P2DIR &= ~SDA;
char c= 0x00;
for(int i=0;i<8;i++)
{
//while((P2IN & SCL) == 0x00);
{
c = c<<1;
if(P2IN & SDA)
c |= 0x01;
else
c &= ~0x01;

}
}
//P2OUT &= ~SDA;

//while((P2IN & SCL)== 0x08);
if(ack)
{
P2DIR |= SDA;
SDA_Low();
}
else
{
P2DIR |= SDA;
SDA_High();
}
//while((P2IN & SCL)== 0x00);
P2DIR &= ~SDA;
return(c);
}

char read_addr()
{
char a=0x01;
char c= 0x00;
for(int i=0;i<8;i++)
{
while((P2IN & SCL) ==0x00);
c = c<<1;
if(P2IN & SDA)
c |= 0x01;
else
c &= ~0x01;
}
//while((P2IN & SCL) == 0x50);
if(c == 0xF0)
{
SDA_Low();
a=0x00;
}
else
SDA_High();
return(a);
}

I am able to write butnot able to receive ack from slave..Please if any one can help me .


Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>