View: 36010|Reply: 3

Using gpio/SPI/i2c with the Orange Pi PC

[Copy link]

1

threads

3

posts

43

credits

Novice

Rank: 1

credits
43
Published in 2016-3-20 23:19:40 | Show all floors |Read mode
Edited by t0k3-0 at 2016-3-21 07:23

Hey OrangePi community!
So, I don't know very much about SPI, i2c etc., but recently bought myself an OPi-PC. I've been a programmer for a while and been working with C and C++ in my spare time.
I couldn't get SPI or i2c working (using gpio readall) with my Armbian image installed (in february or earlier I believe) from the official Armbian site for my board, but I did get the usual IO pins to read and write data via the C library wiringPi.
I also ordered an 8x8 led matrix which came with the chip MAX7219, which I put together and tried to hook up. I believe this device should use SPI but decided to "bitbang" it since that seemed like a simpler solution for me.
Said and done I hooked it up to the 5v, ground, gpio 7 (DIN), gpio 8 (CS) and gpio 10 (CLK). I then tried to modify some code built for the MAX7219 with Raspberry Pi specifically, but cannot seem to get it to work.
As I do "gpio readall" it does read them all and they change while the program is running, so there's no question that I'm using the right pins in my code.

Also, on an earlier occasion I got the lights going, when I by mistake hooked it up in the wrong way (I think).. Now they don't do anything at all. Maybe I broke it? Ground and 5v have always been connected to the same pins though.
Here is my code: http://pastebin.com/RvDtNnnH
Compiles with: "gcc -o test thisfile.c -lwiringPi"
I would really appreciate it if someone with more knowledge would look over it quickly, or give me some insight on how to use SPI and/or i2c. I have looked for the info all over the forums for hours, but am obviously not looking hard enough (?).

Regards

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2016-4-5 18:10:06 | Show all floors
Your cord has 2 mistakes.
You have to forward 16 bits of data together like this.

  1. static void Send16bits (unsigned short output)
  2. {
  3.    
  4.   unsigned char i;

  5.   for (i=16; i>0; i--)
  6.   {
  7.     unsigned short mask = 1 << (i - 1); // calculate bitmask
  8.   
  9.     digitalWrite(CLOCK, 0);  // set clock to 0
  10.    
  11.     // Send one bit on the data pin
  12.    
  13.     if (output & mask)   
  14.       digitalWrite(DATA, 1);         
  15.     else                              
  16.       digitalWrite(DATA, 0);  
  17.       
  18.     digitalWrite(CLOCK, 1);  // set clock to 1
  19.       
  20.   }

  21. }
Copy code


CS with a bar is reversed CS.

  1. // Take a reg number and data and send to the max7219
  2. //
  3. // reg_number : Register Address of max7219 (0x00-0x0f See datasheet)
  4. // dataout    : Send Data
  5. //

  6. static void MAX7219Send (unsigned char reg_number, unsigned char dataout)
  7. {
  8.   digitalWrite(LOAD, 1);  // set LOAD 1 to start
  9.   Send16bits((reg_number << 8) + dataout);   // send 16 bits ( reg number + dataout )
  10.   digitalWrite(LOAD, 0);  // LOAD 0 to latch
  11.   digitalWrite(LOAD, 1);  // set LOAD 1 to finish
  12. }
Copy code

Example is here.
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=67382



1

threads

3

posts

43

credits

Novice

Rank: 1

credits
43
 Author| Published in 2016-4-9 20:56:29 | Show all floors
Thanks a lot for taking the time to check it out. Will try the code as soon as I get the chance and report back :]

1

threads

106

posts

633

credits

Senior member

Rank: 4

credits
633
Published in 2024-5-14 01:15:22 | Show all floors
This post was finally edited by RobertChong at 2024-6-4 00:27


I remember when I was first getting into C++ for my Orange Pi projects, the learning curve was pretty steep. Debugging issues could be so frustrating, especially when dealing with hardware-specific code. Collaborating with peers or seeking help with c++ homework help can make a huge difference. It's amazing how much clearer things become when you get a bit of guidance or see a problem from a different perspective. What specific challenges have you all faced with it, and how did you overcome them?

You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list