Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Propeller C – Simple Protocols

Propeller C – Simple Protocols

More Data Types and Devices

Now that we have done some basic communication with simple byte data and one I2C device, let’s expand on that. First, let’s modify the original code to work with other data types besides bytes. Then, if you are interested in going a bit further, the Advanced Topic section will show you a method you could use to put two additional EEPROM DIP chips (plugged into your breadboard) on a second I2C bus.

 

Try This – Data Types

Data is written to and read from I2C devices in the form of one or more eight-bit bytes.  In many cases the data written to an I2C chip originated as a different data type, like maybe a 32-bit int or even a float.

Likewise, data bytes read from I2C chips may need to be reassembled back into the data those bytes actually represent.  For example, a sensor might be returning a signed 16-bit short value with two bytes, or an EEPROM or other memory device might be storing and returning ints, floats, or some other type. 

So, here is an example that shows how an int variable can be converted to bytes, stored in EEPROM, retrieved from EEPROM, and reassembled back into an int value.  It does this process with the number 5280, stored in an int variable.

Along with storing and retrieving an int value, this program gives names to some of the constant values from the previous example.  The main trick for the variable-to-bytes conversion is to pass the address of the variable in question, cast as a char pointer,  and make sure to specify the correct number of bytes for the data type. 

So, this program starts with int val = 5280.  Notice how i2c_out(eeBus, eeAddr, memAddr, 2, (char*) &val, 4) uses (char*) &val to cast the address of the val int variable as a pointer to a character variable.  The dataCount parameter following it is also set to 4, since an int variable has four bytes.  The reverse of this process is accomplished with i2c_in(eeBus, eeAddr, memAddr, 2, (char*) &val, 4).   

  • Use the Save Project As button to save a copy of your project in …DocumentsSimpleIDEMy Projects.
  • Modify the main function as shown below.
  • Use Run with Terminal to verify the output.

 

Advanced Topic: Adding a Second I2C Bus

This is meant to be a theoretical exercise, not a hands-on activity.  If you have the two additional EEPROMs required for this example and wish to try adding a second bus to your board, then the information provided here can be used as a guide.

Below is an example of an I2C bus you could add to your Activity Board’s breadboard (original or WX version).  It has two more I2C EEPROMs just like the one on the Activity Board’s P28/P29 bus.

Please Note: The top 24LC512 shown in the schematic has its A0, A1 and A2 pins set the same as the one on P28 and P29.  But keep in mind, it’s on a completely different bus, so it’s okay for it to have the same address.  Since the bottom 24LC512 is on the same bus, it has to have a different address.

Additional Parts

(2) 24LC512 EEPROM

It’s easy to expand your program in order to communicate with all three EEPROMs; these two and the one built into your Propeller board.  The steps are pretty much the same:

  1. Declare a second bus.
i2c *eeBus2;
  1. Intialize a second bus.
eeBus2 = i2c_newbus(7, 6, 0); 
  1. Communicate with devices on the second bus.
// Write to upper EEPROM on second bus

i2c_out(eeBus2, 0b1010000, cmd, 2, "mnop", 5);

// Write to lower EEPROM on second bus

i2c_out(eeBus2, 0b1010001, cmd, 2, "qrstuvw", 8);

...

Printer-friendly version
Did You Know?
Prev

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress