 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
|
Posted: Fri Mar 02, 2012 12:23 am |
|
|
80 = 0xA0 right? Where does it come from? Do I need to change this value if I use different address? If yes then how does it get calculated?
I imagine that 80 is A0 and if it is then I do not need this:
| Quote: |
Above question is irrelevant now:
0xA0 = 10100000 -> i2C makes from it 01010000 =>0x50 => 80
so state 1 should be a command and double check of my address is not required anymore. If I use different address then I need to adjust my bits and change 80 into appropriate address -THNX 4 ALL HELP. |
| Code: | if (state == 0) { //address
c = i2c_read();
buffer[0] = c;
print = 1;
if (c == 0xA0) {
print = 1;
}
} |
below is working code:
| Code: |
void send_to_i2c(BYTE destinationi2c, BYTE command, BYTE data1, BYTE data2, BYTE data3, BYTE data4, BYTE data5, BYTE data6, BYTE data7, BYTE data8) {
i2c_start(); //begin transmission
i2c_write(destinationi2c); //select address of device to communicate with
i2c_write(command); //send actual command
i2c_write(data1); //send actual data
i2c_write(data2); //send actual data
i2c_write(data3); //send actual data
i2c_write(data4); //send actual data
i2c_write(data5); //send actual data
i2c_write(data6); //send actual data
i2c_write(data7); //send actual data
i2c_write(data8); //send actual data
i2c_stop(); //terminate communication
}
void read_i2c(BYTE destinationi2c, BYTE rcommand) {
i2c_start();
i2c_write(destinationi2c);
i2c_write(rcommand);
i2c_write(capture[0]);
i2c_write(capture[1]);
i2c_write(capture[2]);
i2c_write(capture[3]);
i2c_write(capture[4]);
i2c_write(capture[5]);
i2c_write(capture[6]);
i2c_write(capture[7]);
i2c_start();
i2c_write(destinationi2c+1);
i2cread[0]=i2c_read(0);
i2c_stop();
} |
| Code: | void ssp_interupt(void) {
state = i2c_isr_state();
if(state < 0x80) { //master is sending data
if (state == 0) { //address
c = i2c_read();
buffer[0] = c;
print = 1;
if (c == 0xA0) {
print = 1;
}
}
if (state == 1) { //command
c = i2c_read();
buffer[1] = c;
}
if (state == 2) { //data0
c = i2c_read();
buffer[2] = c;
}
if (state == 3) { //data1
c = i2c_read();
buffer[3] = c;
}
if (state == 4) { //data2
c = i2c_read();
buffer[4] = c;
}
if (state == 5) { //data3
c = i2c_read();
buffer[5] = c;
}
if (state == 6) { //data4
c = i2c_read();
buffer[6] = c;
}
if (state == 7) { //data5
c = i2c_read();
buffer[7] = c;
}
if (state == 8) { //data6
c = i2c_read();
buffer[8] = c;
}
if (state == 9) { //data7
c = i2c_read();
buffer[9] = c;
}
if (state > 9) { // safety switch -> kill IRQ if msg too long
clear_interrupt(int_SSP);
}
}
if(state == 0x80) { //master is requesting data
i2c_write(buffer[8]); //send requested data
}
} |
_________________ Help "d" others and then you shell receive some help from "d" others. |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Mar 02, 2012 5:43 am |
|
|
Your #INT_SSP code does not work according to the suggestions in the compiler manual respectively the necessary operation scheme described in the Microchip datasheet.
When receiveicing i2c_isr_state() == 0x80, you need to 1. read and 2. write the I2C data register:
| Code: | #INT_SSP
void i2c_isr() {
state = i2c_isr_state();
if((state== 0 ) || (state== 0x80))
i@c_read();
if(state >= 0x80)
i2c_write(send_buffer[state - 0x80]);
else if(state > 0)
rcv_buffer[state - 1] = i2c_read();
} |
|
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1426
|
|
Posted: Fri Mar 02, 2012 8:18 am |
|
|
Linuxbuilders,
It is important for you to understand that the state value from i2c_isr_state() is not a hardware value directly. This is a utility provided to you by the compiler and completely generated by the compiler. It is meant to give you a single value to help you discern 2 things:
1. Is the value received data or the address+r/w bit?
2. Is the master sending data or expecting data back?
#1 is handled by using the state values 0x00 and 0x80 as indicators that an address with r/w set to 0 is used (0x00) or an address with r/w set to 1 is used (0x80). The state value of 0x80 has absolutely nothing to do with the actual address used. It just says "hey, someone sent an address byte with r/w set to 1". If the slave address was 0x42, you would still get an 0x80 for the state when 0x43 was sent. State values of 0x01-0x7f indicate incoming data, and values of 0x81-0xff indicate data requests
#2 is handled by the range of values. 0x00-0x80 indicates that data (or the address) was sent to the slave from the master and MUST be read (usually using i2c_read() ). 0x80-0xFF indicates that the master wants to receive data from the slave, so an i2c_write() should be done. NOTICE how the 0x80 appears in both. That means when the state is 0x80, you need to do both a read and a write (and the read must come first). Even if it appears to work sometimes when you don't read for 0x80, it can cause big trouble in the hardware. The hardware expects certain things to happen for its control signals to function correctly. Not following those rules can lead to buggy i2c behavior or even failure.
You can actually do i2c code without ever using the state or ever calling i2c_isr_state() once. It is there for your convenience, but you need to follow the i2c "rules", especially when you use it or you can get unintended behavior from both your hardware and software. |
|
 |
Linuxbuilders
Joined: 20 Mar 2010 Posts: 193 Location: Auckland NZ
|
|
Posted: Fri Mar 02, 2012 2:07 pm |
|
|
thnx guys, there i more light in my head now than b4  _________________ Help "d" others and then you shell receive some help from "d" others. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|