• AVR Freaks

Hot!I2C Interfacing XC8 PIC16F877A

Page: < 12345.. > >> Showing page 3 of 6
Author
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/07 13:09:01 (permalink)
+1 (1)
Look at the top bit if you must, but the most intuitive way to tell if it is negative (if stored in a signed data type) would be by simply comparing with zero:
if (value < 0)
{
...
}


And yes, that is one way to invert the sign. But again the most intuitive and simple way is:
value = -value;


Not necessarily silly questions, but certainly noob programming ones. Sorry.
You really should get yourself a copy of K&R The C Programming Language.
#41
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/07 13:12:17 (permalink)
+1 (1)
You're over thinking this. You are working in C, so you don't have to get down to the bit level.
To see if a number is negative, compare it to zero.
if (number < 0)

To convert a number form negative to positive, just subtract if from zero
new_number = 0 - number;

so there's absolutely no need to even know how it is stored internally.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#42
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/07 22:17:46 (permalink)
0
.

MPLAB X IDE v5.05
XC8 2.00
#43
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/08 18:55:33 (permalink)
0
Could you help me with my I2C function .. I am trying to read the constants .. But it always displays -127 with all the constants .. Could you give me some ideas about what should I modify? .. I have already check the code and looks fine, but I guess there is something I can't see .. 
 
post edited by delfindelfin - 2019/07/08 19:04:06

MPLAB X IDE v5.05
XC8 2.00
#44
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/08 19:28:23 (permalink)
+1 (1)
In i2c.c, in the I2C_Master_Read() function, this line is faulty
    SSPCON2bits.ACKEN = 0;  // Initiate Acknowledge sequence on SDA and SCL pins and transmit ACKDT data bit

You should write "1" to that bit, not "0" to initiate an ack cycle.
 
Also, you are NEVER checking the ACK response after a call I2C_Master_Write().
I'm surprised this hasn't been discussed before in your topics.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#45
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/08 19:36:50 (permalink)
0
But, what is the reason of having the Acknowledge or not Acknowledge in I2C? .. I don't understand why you need them .. After writing, you always have to acknowledge, right? .. So, I guess, I have to add, this code in the Master Write function at the end

SSPCON2bits.ACKDT = 0; // Acknowledge
SSPCON2bits.ACKEN = 1; // Initiate Acknowledge sequence on SDA and SCL pins and transmit ACKDT data bit

 
post edited by delfindelfin - 2019/07/08 19:41:22

MPLAB X IDE v5.05
XC8 2.00
#46
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/08 19:44:22 (permalink)
+2 (2)
The ACK cycle is to let the other end see you are responding.
When the Master sends out a Slave address, the only way it knows that a Slave responded is in the ACK response.
That way, you can quickly tell that you actually sent the correct address.
When writing to the slave, you are currently ignoring it, so wasting the opportunity to see if your code is working.
 
Conversely, when you are reading from a slave, it is expecting to see an ACK cycle. Your bug meant you were not actually performing an ACK cycle on reads at all, corrupting all following reads in the same cycle.
 
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#47
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/08 20:20:55 (permalink)
+1 (1)
n.b. the code in post#1 handled the ACKEN bit correctly.
By post #9 you had changed it from "1" to "0", but no-one noticed.
 
That code has never handled the ACK responses from the slave, so it is lazy programming by whoever you copied it from. Yet another reason why you should not assume everything you find online is good.
Use it for ideas, but check what it is doing yourself.
The I2C page in wikipedia is good: https://en.wikipedia.org/wiki/I%C2%B2C
followed by the actual I2C specification itself. http://www.nxp.com/documents/user_manual/UM10204.pdf
(although the specification mentions a lot of stuff you don't need, like multi-masters, and SMBUS...)
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#48
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/09 21:29:23 (permalink)
-1 (1)
Another question that is off topic .. What do you think about PIC32 Pinguino? I wanted to learn about operating systems, so I wanted to try FreeRTOS .. I read in these forums that a PIC32 is a good choice for installing FreeRTOS .. So, I wanted to give this Pinguino a try .. Do you recommend it?
post edited by delfindelfin - 2019/07/09 23:25:21

MPLAB X IDE v5.05
XC8 2.00
#49
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/12 07:27:02 (permalink)
0
So .. to handle the ACK responses of the slave, in this function:

void I2C_Master_Write(uint8_t data)
{
I2C_Master_Hold(); // Hold the program if I2C is busy
SSPBUF = data; // Write Data to SSPBUF
}

After writing to SSPBUF, I guess I have to check this bit: SSPCON2bits.ACKSTAT 
I imagine, that after writing to SSPBUF, the bit must be cleared automatically if the SLAVE responses .. I guess once is automatically cleared, must be set as "one" in software .. If the slave doesn't respond, if  SSPCON2bits.ACKSTAT is one, Do I have to assume that the device is now working or maybe is not connected at all?
post edited by delfindelfin - 2019/07/12 07:34:46

MPLAB X IDE v5.05
XC8 2.00
#50
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/12 08:17:50 (permalink)
+1 (1)
delfindelfinAfter writing to SSPBUF, I guess I have to check this bit: SSPCON2bits.ACKSTAT 
I imagine, that after writing to SSPBUF, the bit must be cleared automatically if the SLAVE responses
I guess once is automatically cleared, must be set as "one" in software

Stop guessing and imagining.  Read the datasheet.  What does it say?
#51
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/12 08:45:24 (permalink)
0
It says a lot of things .. But I am going to check
Edit.
"In Transmit mode, the ACKSTAT bit (SSPCON2<6>) is
cleared when the slave has sent an Acknowledge
(ACK = 0) and is set when the slave does Not Acknowl-
edge (ACK = 1). A slave sends an Acknowledge when
it has recognized its address (including a general call)
or when the slave has properly received its data."
post edited by delfindelfin - 2019/07/12 09:12:51

MPLAB X IDE v5.05
XC8 2.00
#52
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/12 22:06:25 (permalink)
+1 (1)
So there you have your answer.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#53
mlp
boots too small
  • Total Posts : 776
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/14 21:20:35 (permalink)
0
delfindelfin
Another question that is off topic

Stop doing that. It won't get you good answers from knowledgeable people.

Mark (this opinion available for hire)
#54
1and0
Access is Denied
  • Total Posts : 9478
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/15 00:18:06 (permalink)
0
I just like to know if OP has fixed the math problem yet. ;)  ... from before he started changing topics.
#55
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/20 20:58:44 (permalink)
0
A noob question .. I made a spreadsheet with the possible values of SSPADD Register and I notice that for some values of common oscillators, you get negative numbers or with decimal point .. So if I want to use an I2C Standard Clock, for example: Standard Mode (Sm) - 100 kbis/s .. I am not allowed to use those oscillators, right? Or is it similar to the baud rate error of UART?
 
 
post edited by delfindelfin - 2019/07/20 21:01:19

MPLAB X IDE v5.05
XC8 2.00
#56
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/20 23:28:23 (permalink)
+1 (1)
With I2C you have a data line and a separate CLOCK. Because you have a separate clock signal it does not matter if the frequency is precise like it does with a UART. The slave just uses the received clock from the master, rather than trying to create its own using the SSPADD value.

Obviously you can only program whole numbers, and not negative ones. If you have a negative answer you will need to round up to zero (or maybe 1, not sure if zero is valid - read the datasheet) and that will be the fastest achievable I2C clock at that CPU Fosc.
#57
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/21 01:54:56 (permalink)
+1 (1)
As above.
All those negative results indicate that you cannot generate that frequency.
e.g. there's no way you can generate a 400 kHz clock from a 455 kHz oscillator, which is what you're trying to do on row#4.
But, it doesn't matter. You as the Master have total control of the clock. You can run it at whatever speed you like, so long as it does not exceed the max your devices can do (400kHz or 100kHz), and they have to follow.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#58
delfindelfin
Super Member
  • Total Posts : 374
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/21 23:09:20 (permalink)
0
When using Logic Operators, when a Byte is true, when all its bits are one?

MPLAB X IDE v5.05
XC8 2.00
#59
ric
Super Member
  • Total Posts : 23172
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/21 23:34:36 (permalink)
0
Only zero is treated as "false", so a byte is "true" when ANY bits are set.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#60
Page: < 12345.. > >> Showing page 3 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5