Please select To the mobile version | Continue to access the desktop computer version

OrangePi

 Login            
 Register            
Search
Hot search: orangepi
View: 20559|Reply: 2

Using gpio/SPI/i2c with the Orange Pi PC

[Copy link]

1

Threads

3

Posts

43

Credits

Novice

Rank: 1

Credits
43
Post time 2016-3-20 23:19:40 | Show all posts |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

219

Posts

1974

Credits

Gold member

Rank: 6Rank: 6

Credits
1974
Post time 2016-4-5 18:10:06 | Show all posts
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 the 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 the 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| Post time 2016-4-9 20:56:29 | Show all posts
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 :]
You have to log in before you can reply Login | Register

Points Rules

Archiver|Mobile edition|Darkroom|OrangePi En ( 粤ICP备14086627号-2

2019-8-24 12:15 GMT+8 , Processed in 0.027005 second(s), 23 queries .

Powered by Discuz! X3.2

© 2014-2015 orangepibbs en.

Quick Reply To Top Return to the list