??? 09/05/06 16:53 Modified: 09/05/06 16:54 Read: times |
#123686 - RL & RLC inst in C |
Hello all,
I'm trying to rewrite I2C routines in C from assembly to practice C programming, I run into a problem and I'm having hardtime to figure out how to write these instructions RL, RLC in C. So far Start, Stop, Write, Repeat Start routines are working, but the Read routine doesn't work. (Received data is not correct as reading variable Data. I'm using Ceibo emulator and Keil IDE. However data that is displayed on oscilloscope is correct). So I think that there is something wrong with the way I manipulate data in the routine. Here is what I did: unsigned char I2C_Read (unsigned char Data) { unsigned char Bit_Cnt; Data = 0x00; for (Bit_Cnt = 0; Bit_Cnt < 8; Bit_Cnt++) { Data |= I2DAT; // Receive MSB first Data = Data << 1 ; // Rotate left while (!ATN){} // Pool ATN flag if (!DRDY) { Fault = 1; break; } } Data |= I2CON << 8; // Get last bit in the I2CON reg I2DAT = 0x80; // Send Ack bit while (!ATN){} if (!DRDY) { Fault = 1; } return (Data); } Here is the assembly routine that works R_Loop: mov BitCnt,#08h ; Set BitCnt = 8 (receive 1 Byte at a time) RBit: orl A,I2DAT ; Read I2DAT.Get 1st bit (MSB). Clear DRDY rl A ; Shift left received data jnb ATN,$ ; Wait for new data jnb DRDY,RDErr ; Wait for DRDY = 1 (Detect rising edge of SCL) djnz BitCnt,RBit ; Loop until the last bit jnb ATN,$ ; mov C,RDAT ; Read last bit from I2CON register rlc A ; Get full data byte Any inputs would be greatly appreciated. Have a great day. Best Regards, T.L PS: My MCU is 87C752 |
Topic | Author | Date |
RL & RLC inst in C | 01/01/70 00:00 | |
yes, they are | 01/01/70 00:00 | |
yes, they are | 01/01/70 00:00 | |
mistype | 01/01/70 00:00 | |
Choose the best tool for the job. | 01/01/70 00:00 | |
another option (without if) | 01/01/70 00:00 | |
C style | 01/01/70 00:00 | |
May Be This Way | 01/01/70 00:00 | |
No, that won't work! | 01/01/70 00:00 | |
ways to skin the cat | 01/01/70 00:00 | |
Compiler dependant | 01/01/70 00:00 | |
compiler independent, but still very ugly version | 01/01/70 00:00 | |
Thank You | 01/01/70 00:00 | |
A very important point! | 01/01/70 00:00 | |
It is nice C | 01/01/70 00:00 | |
Not necessarily | 01/01/70 00:00 | |
Problem solved | 01/01/70 00:00 | |
Keil-specific | 01/01/70 00:00 | |
Thank you![]() | 01/01/70 00:00 |