Hello, I would like to know, if it is possible to communicate (for example with Leddriver Max7219), using only digitalWrite function. To simulate SPI protocol only by digitalWrite function (+ eventually delayMicroseconds function). Thank you very much for the response.
Yes.
The name you need to search for to get examples is
Bit banging SPI
The Arduino shiftOut() function (source code in wiring_shift.c) does what you appear to want and uses digitalWrite() to achieve it.
Thank you very much for the response. It is great. Now I would like to write code (for example to light up one diode on matrix display with Max7219). The issue will be probably to synchronize all necessary delays). Have a nice day.
May I ask, why would you like to do that? Most probably it will be much slower than the hardware version.
You shouldn't need to use any delays at all. And there is no need to synchronise any you feel are necessary because delay is a blocking piece of code and nothing happens until it is over.
Max7219 doesn't use SPI to work.
I would like to understand serial communication. The idea was: "how should I light some character on the matrix display using Max7219", it is made, using corresponding packet send from Arduino, if I want to see the packet in detail (not to use library) am I able to generate this packet using corresponding pins high and low(to use digitalWrite function)? This was the initial idea.
I would recommend you to inspect in detail the arduino shiftOut()
function.
I wrote code, using shiftOut() and it works fine (I send 8 bits to Max7219 - address, then I send 8 bits - which leds to light up). The code is something like this:
- CLK, DIN, CS as outputs
- CS low
- shiftOut (DIN, CLK, MSBFIRST, b0000 0001) //Address
- shiftOut (DIN, CLK, MSBFIRST, b1111 1111) //light the whole first line of leds
- CS high
In another case I wanted to use only digitalWrite function. The code like this: - CLK, DIN, CS as outputs
- CS low
- DIN low
- CLK high
- CLK low // first bit of 16 bit packet (first 8 bits for address, then 8 bits for leds)
... - DIN high
- CLK high
- DIN low
- CLK low // last bit of address
... - eight times same procedure for DIN high
- CS high
And the matrix display remains dark. If someone has an idea, I would appreciate it.
In principle, you start with the clock LOW. You set DIN for the first bit then you pulse the clock HIGH then LOW (SPI mode 0 . data shifted out on rising clock edge). Set the DIN for the second bit and pulse the clock and continue. You appear to be setting DIN on both the HIGH and the LOW side of the clock pulse.
Why not show the code itself instead of retelling it in words?
Now it works great. Many thanks to everyone. I appreciate your deep knowledge. Please see bellow the final code:
const int DIN_PIN=11;
const int CLK_PIN=13;
const int CS_PIN=10;
void sendData(byte address,byte data){
digitalWrite(CS_PIN,LOW);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, address);
shiftOut(DIN_PIN, CLK_PIN, MSBFIRST, data);
digitalWrite(CS_PIN,HIGH);
}
void setup() {
pinMode(DIN_PIN, OUTPUT);
pinMode(CLK_PIN, OUTPUT);
pinMode(CS_PIN, OUTPUT);
sendData(0x0f,0x00);
sendData(0x0c,0x01);
sendData(0x0b,0x07);
sendData(0x0a,0x0f);
sendData(0x09,0x00); //set display parameters
sendData(B00000001,B00000000);
sendData(B00000010,B00000000);
sendData(B00000011,B00000000);
sendData(B00000100,B00000000);
sendData(B00000101,B00000000);
sendData(B00000110,B00000000);
sendData(B00000111,B00000000);
sendData(B00001000,B00000000); //set all leds low
digitalWrite(CS_PIN,LOW);
digitalWrite(DIN_PIN,0); //D15
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D14
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D13
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D12
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D11
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D10
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D9
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D8
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D7
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D6
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D5
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D4
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D3
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D2
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,0); //D1
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(DIN_PIN,1); //D0
digitalWrite(CLK_PIN,HIGH);
digitalWrite(CLK_PIN,LOW);
digitalWrite(CS_PIN,HIGH); //set display character
}
void loop() {
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.