Hello everyone,
I just got in touch with C and Arduino, now I need to use Arduino UNO digital pointer to read / write SPI flash (using GPIO mode, can't use SPI pins). At present, I have successfully sent 9Fh and got the correct response from DO pin.
But now I need to display the result in "Serial Monitor", how can I achieve it?
The following is my current code. Please help me, thank you all.
int CS = 2;
int DI = 3;
int DO = 4;
int CLK = 5;
//int HOLD = 6;
//int WP = 7;
int VCC = 8;
int val = 0;
int i;
int CLK1(int a) {
for(i=0;i<a;i++) {
digitalWrite (CLK, LOW); /*set clock*/
delayMicroseconds(200);
digitalWrite (CLK, HIGH);
delayMicroseconds(200);
}
digitalWrite (CLK, LOW);
delayMicroseconds(100);
}
void CLK_H(){
delayMicroseconds(100);
digitalWrite (CLK, HIGH);
delayMicroseconds(200);
}
void CLK_L(){
digitalWrite (CLK, LOW);
delayMicroseconds(100);
}
void instruction(int b, int c){ //checking bit is high or low, using % to achieve.
int a[4], i;
for(i = 0; i < 4; i++){
a[i] = b % 2;
b = b / 2;
}
for(i = 3; i >= 0; i--){
if(a[i] == 1){
digitalWrite(DI, HIGH);
CLK_H();
CLK_L();
}
else{
digitalWrite(DI, LOW);
CLK_H();
CLK_L();
}
}
for(i = 0; i < 4; i++){
a[i] = c % 2;
c = c / 2;
}
for(i = 3; i >= 0; i--){
if(a[i] == 1){
digitalWrite(DI, HIGH);
CLK_H();
CLK_L();
}
else{
digitalWrite(DI, LOW);
CLK_H();
CLK_L();
}
}
}
void setup() {
Serial.begin(2400);
pinMode (CS, OUTPUT);
pinMode (DI, OUTPUT);
pinMode (DO, INPUT);
pinMode (CLK, OUTPUT);
//pinMode (HOLD, OUTPUT);
//pinMode (WP, OUTPUT);
pinMode (VCC, OUTPUT);
}
void loop() {
int a, b;
digitalWrite(VCC, HIGH);
//digitalWrite(HOLD, HIGH);
//digitalWrite(WP, HIGH);
digitalWrite(CS, LOW);
a = 0x9;
b = 0xF;
instruction(a, b);
for (i = 0; i < 24; i++) {
CLK_H();
CLK_L();
val = digitalRead(DO); //this is my current questions, it can not print any values in Serial Monitor.
Serial.print(" ");
}
digitalWrite(CS, HIGH);
digitalWrite(VCC, LOW);
delay(100);
}