1
\$\begingroup\$

I am from a programming background, but very new to microcontrollers. Pardon me if this question is very basic, or doesn't make complete sense. I'm trying to bootstrap my knowledge, and the learning curve appears to be steep in some areas.

I am trying to get I2C working with a new IC I purchased, the PIC12LF1552. This IC comes with an I2C module in it, so I shouldn't have to bit-bang. However, It seems like XC8 doesn't support I2C on the PIC12 series. MPLAB X can't seem to find the ic2.h include file (this may not actually be a problem, it could be paths are not setup correctly), so I linked the file with a full path. i2c.h includes pconfig.h on line 32, and pconfig.h includes p18cxxx.h on line 16, which seems like it won't work as it's for a different chip series.

So my question is, what support is there for running native I2C on PIC12 series ICs?

\$\endgroup\$
1
\$\begingroup\$

It depends on what you mean by "support". Just because there may not be a canned library doesn't mean you can't use the peripheral.

The XC compilers offer direct access to the special function registers and interrupt capabilities of the target device. You can just as easily configure the I2C peripherals and handling routines in C as you could in assembly language. The device datasheet will include all the details for setting up these peripherals. Just read the datasheet, write the necessary values to the necessary SFRs and poll/interrupt as needed.

(It's often easier to code this way, as you're not reliant on closed-source libraries that may not do exactly what the advertise.)

\$\endgroup\$
4
  • \$\begingroup\$ There is a significant section of the data sheet dedicated to I2C (and SPI) communications. I understand how I2C works, and the poll/interrupt code should be pretty straight forward, especially with clock stretching enabled. I think I'm a bit lost as to how a write to a register will generate the I2C output, and how that all works. I'm going to read the rest of the datasheet and see if I can make heads or tails of it. \$\endgroup\$
    – Beachhouse
    Dec 17 '13 at 1:46
  • \$\begingroup\$ It's pretty simple when you realize some things, such as: one of the locations you write to is the transmit buffer, and one of the locations you read from is the receive buffer... \$\endgroup\$ Dec 17 '13 at 2:10
  • \$\begingroup\$ Yeah, I can see where it has to go with that. It seems interesting that this is the way to do it though. It's the desktop programming equivalent of to having a global variable that is written to and read from by multiple threads. How do you guarantee thread safety? Manually based on other registers I guess? I don't think there is really threading though, so how does the register ever get processed if we're not calling a function? \$\endgroup\$
    – Beachhouse
    Dec 17 '13 at 6:34
  • 1
    \$\begingroup\$ PICs aren't multithreaded per se, but you generally do need to keep track of state when the code jumps into and out of ISRs (volatile variable declarations, register clobbering, etc.) Indeed, SFRs are very much like globals in that they're globally accessible. The PIC hardware takes care of 'what to do' when these registers get written by software. \$\endgroup\$ Dec 17 '13 at 17:06
1
\$\begingroup\$

The Microchip XC8 compiler has integrated peripheral libraries (including I2C libraries), but only for the PIC18F family of micros. The other 8-bit micros (PIC10F, PIC12F, PIC16F) don't have a peripheral library. You'll have to write your own implementation/drivers for the I2C peripheral by directly manipulating the registers.

\$\endgroup\$
2
  • \$\begingroup\$ I've looked at the data sheet and they do tell you what SFR registers are for I2C. I am still having trouble finding more information about this, for one, what is this process called? It's not bit banging, because the registers are doing some of the heavy lifting for you. I'm guessing I'm not the first person to have run into this, so I was hoping I could find an example. \$\endgroup\$
    – Beachhouse
    Dec 17 '13 at 1:36
  • 1
    \$\begingroup\$ No, it's not bit-banging. I guess you can call it 'writing your own driver for the I2C peripheral' :-) Here are some code examples that might help you.. \$\endgroup\$
    – m.Alin
    Dec 17 '13 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.