login|Register
Forum > C/C++
Post|
看31734|回3|Favorite
t0k3-0 看全部
2016-3-20 23:19:40
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
nopnop2002 看全部
2016-4-5 18:10:06
Your cord has 2 mistakes.
You have to forward 16 bits of data together like this.

code:

  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. }


CS with a bar is reversed CS.

code:

  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. }

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



t0k3-0 看全部
2016-4-9 20:56:29
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 :]

OrangePi En

Powered by Discuz! X3.4

homepage|Simple edition|Touch edition|PC