• AVR Freaks

Helpful ReplyHot!I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board

Page: 12 > Showing page 1 of 2
Author
malcolm.duckett@btopenworld.com
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/10/16 10:44:35
  • Location: 0
  • Status: offline
2020/01/24 05:47:00 (permalink)
0

I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board

Hi there, I am trying to prototype a PIC16F data collection device on a Curiosity board - and am a  newbie C programmer (learnt my programming in Assembler in the 70's! I have done some Assembler programming on PIC, but decided to go to C for this project....so think "first timer"!).
 
I am using MPLAB X IDE (current version) and utilising MCC to create drivers/headers/config. (windows 10 Pro).
 
The plan is to configure board as I2C Slave to pass collected data to Pi data processor. (I2C Master).
 
The application is (obvs) real-time, so I am wanting to use Interrupt based solution.
 
The problem I am having is trying to understand the API provided by the I2C drivers. e.g. MCC provides files under the headings "i2c_driver" and "i2c_slave".. there are many entrypoints, but no apparent guidance as to which to use. I have spent some days (weeks?) searching, and while I have found some examples and tutorials they do not seem compatible with the code generated by MCC...
 
A steer, code example, guidance, pointer to docs or even sympathy would help... I do not want to go bit-banging, or attempting to write my own libraries, and would prefer to utilise the Microchip generated ones...
 
thanks in advance :-/
 
Malcolm
 
#1
malcolm.duckett@btopenworld.com
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/10/16 10:44:35
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 05:49:35 (permalink)
0
PS... AN734 has a link to code examples which appear to be exactly what I need - but the link is broken - <sad face>
#2
Mysil
Super Member
  • Total Posts : 3678
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 07:16:29 (permalink)
+2 (2)
Hi,
Although AN734, AN735 and AN736  are about the subject, and contain explainations and example code,
They are Not about MCC, and code is very different from what is generated by MCC.
The application notes are here:
http://www.microchip.com/....aspx?appnote=en011798
http://www.microchip.com/....aspx?appnote=en011802
https://www.microchip.com....aspx?appnote=en011810
 
The rest of this message is about Master mode, so may be beside the original question.
 
There is a lot of example code in the Click Library collection in MCC,
using the Foundation Services library in MCC.
The Foundation Services driver code in MCC is the forerunner for the current code,
generated by Device library PIC10/PIC12/PIC16/PIC18 v1.78.1
in the current release of MCC.
 
While there is a lot of example code hidden around, there is little in the way of description and documentation.
There are some comments and example/test code in this thread:
https://www.microchip.com/forums/FindPost/1117381
If you skip the EEPROM example and test code in projects in that thread,
there is also example code of a I2C Bus scan, reporting which slave devices respond when addressed.
The thread is however not a tutorial, instead it is mostly pointing out areas that could be improved.
 
Here is another thread that may be relevant:
https://www.microchip.com/forums/FindPost/1118405
 
Regards,
    Mysil
post edited by Mysil - 2020/01/24 07:36:14
#3
katela
Super Member
  • Total Posts : 1833
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 07:35:44 (permalink) ☄ Helpfulby malcolm.duckett@btopenworld.com 2020/01/30 06:36:22
+1 (1)
Try to use use the I2C PIC10, PIC12, PIC16 PIC18 library instead of the foundation library, the i2c.h file has some short descriptions how to use those generated functions.
Microchip developer help page has some few tutorials you could start with. MPLAB Xpress example page.
You could also check this tutorial.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#4
malcolm.duckett@btopenworld.com
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/10/16 10:44:35
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 08:05:54 (permalink)
0
Mysil, thank you very for your attempt to provide assistance....
 
But I am not managing to unravel this...
 
For example, your memory test examples call routines like EEPROM_ReadOneByte  which don't exist in the I2C Slave implementations provided by MCC (and I understand that is obvious), but I can't decide which routines in the drivers MCC provides for the MSSP Slave implementation. should be used..
 
 
For example I was expecting a function to be available of the form "I2C_Initialise()", but there is not - I can find "I2C_driver_open()" and "i2c_slave_open()" but which do I use, and are these really replacements for an "Initialize" function?
 
It seems most examples are Master, and not Interrupt driven and are focussed on using EEProms, so they are just too far away from what I am trying to do.
 
I was expecting to provide a main that did something like this:-
void main(void)
{
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
 
char data;
data= 0;
i2c_initialise();
i2c_Slave_Address=0x5;
while(something);  //wait for master to try to read me
i2c_WriteByte(data);
}
... but how do I declare and write the ISR (or is this provided somewhere)....
 
FYI I am attempting to use the "MSSP (Foundation Services Library by Microchip)".
 
Maybe I am just missing too much here, and should slope off and do something else with my time,  but it would be good to get started :-(
 
Thanks again,
Malcolm
#5
malcolm.duckett@btopenworld.com
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/10/16 10:44:35
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 08:08:23 (permalink)
+1 (1)
katela
Try to use use the I2C PIC10, PIC12, PIC16 PIC18 library instead of the foundation library, the i2c.h file has some short descriptions how to use those generated functions.
Microchip developer help page has some few tutorials you could start with. MPLAB Xpress example page.
You could also check this tutorial.




Katela, thank you for this suggestion, I will give that a try....
 
#6
Mysil
Super Member
  • Total Posts : 3678
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 13:37:53 (permalink)
+1 (1)
To katela,
You will have to be more precise about what version of
MCC Device Library for PIC10, PIC12, PIC16, PIC18 MCUs 
that you suggest to use.
 
There is a big rewrite going on for the MSSP drivers for I2C and SPI in device library 1.78.0 and 1.78.1,
and generated code is wery different from previous versions of the library.
 
EEPROM_...     functions  are generated by memoryProductsLibrary_v2.10.jar,
as is pointed out several places in the thread, and in source code in the MemoryLibraryTest_3.zip
Yes,  example code generated by memoryProductsLibrary_v2.10  
make calls to functions that don't exist in EEPROM_driver.c 
But  memoryProductsLibrary_v2.10    is all Master code, and doesn't answer your call for Slave code example.
 
I have seen the  I2C Slave code generated by device library 1.78.0
and did not understand how to use it either,
so continue to use code based on code generated by previous versions of MCC device library.
 
    Mysil
#7
katela
Super Member
  • Total Posts : 1833
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/24 14:12:43 (permalink)
+1 (1)
You are correct Mysil, the examples I am referring to would work with the MCC Device Library PIC10, PIC12, PIC16, PIC18 version 1.77
I have realized from 1.78 it's a different beast. 
If the OP doesn't mind, who could load the older version (in MCC versions (bottom left) just expand the required library you will see a number of versions. Right-click and select mark for load), it's less obscure than the foundation library and at least there are some few examples online he can get started with while learning the new version. 
 

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#8
malcolm.duckett@btopenworld.com
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/10/16 10:44:35
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/25 09:45:11 (permalink)
0
thanks guys - I will make sure I have the right libraries version included - while I haven't got it working yet, at least the MCC files and examples made some sense...
 
I also realised that in my "tinkering" I had also manually loaded a second version of the libraries, hence the presence of i2c_slave and i2c_driver H and C files, and that was further compounding my confusion :-)
 
 
#9
malcolm.duckett@btopenworld.com
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2018/10/16 10:44:35
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/29 06:15:43 (permalink)
0
Just a note to say that Mysil and Katela's support got me going - working with the older libraries (as opposed to Foundation Services) provided enough examples and insight to get me going. Thank you both.
 
I think part of the problem I was suffering was electrical. I had the Curiosity board connected to my Windows PC and powered from its usb cable. Whereas the Raspberry Pi I was using as I2C Master was powered from a separate Hub and powerbrick, and I was only connecting clock and data pins. I am guessing that there was no ground reference - in fact I think I blew the IF pins on the PI as a result... reconnecting things so that everything was powered from the Hub (and a new PI) seemed to get things working.... 
 
I am left with two mysteries:
 1: i2c-tools on the Pi reported two slave devices at address 0x04 and 0x044! Whereas the default PIC slave address seemed to be set to "#define I2C_SLAVE_DEFAULT_ADDRESS          0x08" ...even more wierdly I can do an i2cget() on either 04 or 044 and it works!!! Docs seem to indicate that the PI left-shifts the 7-bit address, so maybe thats related....
 
2: is it acceptable to link the grnd lines on the PIC to those on the PI to attempt remove the grounding problem?
#10
Mysil
Super Member
  • Total Posts : 3678
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/29 08:36:35 (permalink) ☄ Helpfulby malcolm.duckett@btopenworld.com 2020/01/30 06:35:34
+2 (2)
Hi,
It is a recurring source of confusion, that I2C device address may be reported either as a 7 bit field value,
or as a 8 bit Byte value, as transmitted on the Bus.
So much that documentation and datasheet for many I2C devices,
show how address is transmitted on the bus as a figure showing bit pattern and R/W bit together.
Or as a signal graph showing both Clock and Data signal sequence.
 
In general, Microchip have thousands of pages of datasheets and documentation,
with specifications of bits and fields in SFR registers, where bit and field values are documented as such,
and Not as combined register value, in the register where the field value is inserted.
This is usually correctly understood and used.
I2C  7-bit address field value is no different from any other field value,
but is still a common source of confusion and mistakes.
 
Thus, when specifying I2C address values,  or function call arguments, it is nessesary to be clear whether a 7-bit address field value, or a 8-bit byte value is meant.
Otherwise, there will be  4 * 2 = 8 mistakes beeing made.
 
I have no detailed explaination why you get response for both address 04 and 044.
Note, that it is possible to set up I2C slave to respond to multiple I2C addresses,
by use of SSPMSK register.
In fact, if you make a mistake with Mask register value, I2C Slave hardware will accept any address.
 
Also be aware that according to: I2C User Manual and Specification, UM10204 on NXP website,
I2C 7-bit addresses 0 thru 7, and 120 thru 128, are reserved for special purposes.
 
2:
Ground lines Should be connected in parallell with signal lines, when ever signal lines are connected.
Recommended practice would be to use a twisted pair to connect SCL signal and it's ground line between I2C Master and I2C Slave. Then another twisted pair for SDA signal and it's own  ground line.
 
If there are separate power supplies, then it is good practice to ensure that power supply Grounds are connected
before any signal connections are made.
 
    Mysil
 
 
#11
vzaka
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2007/11/12 12:53:26
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/30 12:52:31 (permalink)
0
Hello Mysil,
I am not sure if I am in the right forum
Please, I have a question  regarding setting PIC 24 in I2C-Slave mode.
So far I have used PIC18, setting I2C-slave mode by means of SSPCON1 register (SSPCON1=0x36).
I am new in PIC24, and I am not figuring out how to set up the micro (PIC24) in I2C-slave mode.
You help will be highly appreciated.
Thank you.
 vzaka
 
 
 
#12
ric
Super Member
  • Total Posts : 28009
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/01/30 20:32:32 (permalink)
0
No, this is not the correct forum.
You'll get much better help if you start your own topic in the PIC24 forum.
https://www.microchip.com/forums/f179.aspx
 

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!
#13
Moonwalker
Starting Member
  • Total Posts : 52
  • Reward points : 0
  • Joined: 2010/05/07 02:21:15
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/15 17:22:06 (permalink)
0
@Mysil -  I am using MCC generated i2c_simple_master driver on a PIC16F18326.
I have a I2C sensor over 25m cable using CAT6 connected to a 4 pin plug.
All works fine except when the sensor is disconnected, sometimes the program stops and still does not continue when reconnected.
   Is there a way around this - maybe a timeout in the I2C read/write?
#14
Mysil
Super Member
  • Total Posts : 3678
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/17 04:42:00 (permalink)
0
@Moonwalker
Some of Microchip and MCC code is example code that is weak on error checking and recovery.
 
If a I2C sensor slave is disconnected while a Read transfer is ongoing,
then slave hardware may be holding SDA line Low, while waiting for SCL line to provide clock pulses.
You may experience Bus Collision status that will lock up the I2C master peripheral.
Just reconnecting I2C wiring is not likely to resolve this,
You may either:
1:  Power cycle the slave sensor to reset the slave I2C logic.
2:  Disable I2C master peripheral, and toggle SCL line as GPIO to provide missing clock pulses,
     and then Stop signal sequence. Then reenable I2C master hardware and retry communication.
3:  Use SMBus sensor slave devices. These have hardware timeout that will reset the I2C slave logic
     if communication is interrupted or disconnected.
     Note, that this may make debugging master code more complicated,
     since slave devices will reset communication if you breakpoint master logic for debugging.
 
    Mysil
#15
Moonwalker
Starting Member
  • Total Posts : 52
  • Reward points : 0
  • Joined: 2010/05/07 02:21:15
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/18 15:03:45 (permalink)
0
Thanks for answering. Could you use I2C_driver directly to manipulate the bits as required? I think some kind of timeout should be implemented in the I2C_simple_master read/write routines for better error handling.
 
Anyway looks like I2C is not intended for long line transmission. Although the project (long I2C cable) worked in the lab, when tested in the field (with some heavy duty VFD motors nearby), it was picking all sort of noise and system was hanging quite often.
I will go for a basic analogue sensor now.
#16
NKurzman
A Guy on the Net
  • Total Posts : 18858
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/18 15:22:37 (permalink)
0
No I2C in not designed for long communication lines. And are not good in noisy environments.
I2C = Inter-Chip Communication.
#17
ric
Super Member
  • Total Posts : 28009
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/18 15:40:38 (permalink)
0
NKurzman
No I2C in not designed for long communication lines. And are not good in noisy environments.

Agree. You can improve it with stronger pullup resistors, slower baudrate, and some filtering, but it's really not intended for "off board" communication.

I2C = Inter-Chip Communication.

I2C = I²C = IIC = Inter-Integrated Circuit
https://en.wikipedia.org/wiki/I%C2%B2C
 

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!
#18
Moonwalker
Starting Member
  • Total Posts : 52
  • Reward points : 0
  • Joined: 2010/05/07 02:21:15
  • Location: 0
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/18 16:12:55 (permalink)
0
Yes I had 2k2 pullups on each side and using CAT6 cable with signal and ground pairs. Baudrate was at 20kHz. Maybe could have worked with shielded cable but without good error handling in the driver was not worth risking.
#19
ric
Super Member
  • Total Posts : 28009
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: I2C Slave: support/docs C for MCC generated drivers on PIC16Fxxxx/ Curiosity board 2020/06/18 16:45:37 (permalink)
0
Moonwalker
... and using CAT6 cable with signal and ground pairs.



How exactly were the signals arranged on the wires?
I hope you didn't put SCL and SDA on a pair together!
 

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!
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5