Hello Jim,
I can see a couple of obvious problems with your code -
Firstly the data_read() function does not return a value - I assume it should do so, and the following code would return a value.
unsigned char data_read (void)
{
DDRE = 0x00;
return (PTE & 0x01);
}
There is also a problem with the following function -
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
data1(); // release DATA line
for (i = 0x80; i > 0; i /= 2) { // shift bit for masking
sck1(); // clk for SENSIBUS
input();
if (PTE)
val = (val|i); //read bit
sck0();
}
if ((ack & 0x01) == 1)
data0(); // in case of "ack==1" pull down DATA Line
sck1(); // clk #9 for ack
delayus(6); // pulswith approx. 5 us
sck0();
data1(); //release DATA line
return val;
}
You are actually testing the state of all bits of PTE - I don't think this what you want. To test only the bit used for the serial data, the line should read -
if (PTE & 0x01)
I will also make some other comments about the code, although these will not affect the present operation. In many of your functions you require to set or clear a single bit associated with a port or its DDR. However, you are actually affecting all eight bits within the port. Unless specifically a function to initialise all bits within a port, generally a function should only alter those bits that it uses.
Consider if you decided to use another bit of the same port for a different purpose - you would have to find all instances where that bit was affected in the existing code. This would involve a lot of modification, and would be prone to error, or perhaps a missed occurrance.
The approach I might usually take is to initialise all bits within a port, its associated DDR, and perhaps the other registers associated with that port. This code would reside in an initialisation function that is called only once whenever the program executes (for example your init() function). This can easily be edited as circumstances change. The various functions called would then set or clear only the bits they were concerned with, and not affect any other bit.
The following macros for example -
#define DATA_0 (DDRE |= 0x01) // Set DDRE0 bit
#define DATA_1 (DDRE &= 0xFE) // Clear DDRE0 bit
#define SCLK_0 (PTA &= 0x7F) // Clear PTA7 bit
#define SCLK_1 (PTA |= 0x80) // Set PTA7 bit
Regards,
Mac