I have a program that reads a data from its GPIO pins, converting them into hexadecimal, and printing them.
the program works fine until the frequency that i'm reading is increased. I have an SR latch connected and it seems like when the data input is increased the clear/klaro pin of the SR latch can't keep up in resetting the latch, so I would like to try optimizing the speed of my code
any suggestions to speed up my code?
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define data_ready 17
#define chipselect 27
#define klaro 18
#define D0 6
#define D1 13
#define D2 19
#define D3 26
#define D4 12
#define D5 16
#define D6 20
#define D7 21
int readydata;
int prevdata;
int i;
int chip_select;
int chunk;
int chunk2;
int chunk3;
char storage[3][9];
void chipselectInt (void){
chunk = strtol(&storage[0][0], 0, 2) ;
chunk2 = strtol(&storage[1][0], 0, 2) ;
chunk3 = strtol(&storage[2][0], 0, 2) ;
printf("%d",i) ;
printf(",0x%lx", chunk);
printf(",0x%lx", chunk2);
printf(",0x%lx", chunk3);
printf("\n");
i=0;
}
int main(){
wiringPiSetupGpio();
pinMode(data_ready, INPUT);
pinMode(chipselect, INPUT);
pinMode(D0, INPUT);
pinMode(D1, INPUT);
pinMode(D2, INPUT);
pinMode(D3, INPUT);
pinMode(D4, INPUT);
pinMode(D5, INPUT);
pinMode(D6, INPUT);
pinMode(D7, INPUT);
pinMode(klaro, OUTPUT);
(wiringPiISR (27, INT_EDGE_RISING , chipselectInt ));
while(1){
if(chip_select == LOW){
readydata = digitalRead(data_ready);
digitalWrite(klaro, LOW);
switch(i){
case 0:
storage[0][0] = digitalRead(D0) 48;
storage[0][1] = digitalRead(D1) 48;
storage[0][2] = digitalRead(D2) 48;
storage[0][3] = digitalRead(D3) 48;
storage[0][4] = digitalRead(D4) 48;
storage[0][5] = digitalRead(D5) 48;
storage[0][6] = digitalRead(D6) 48;
storage[0][7] = digitalRead(D7) 48;
break;
case 1:
storage[1][0] = digitalRead(D0) 48;
storage[1][1] = digitalRead(D1) 48;
storage[1][2] = digitalRead(D2) 48;
storage[1][3] = digitalRead(D3) 48;
storage[1][4] = digitalRead(D4) 48;
storage[1][5] = digitalRead(D5) 48;
storage[1][6] = digitalRead(D6) 48;
storage[1][7] = digitalRead(D7) 48;
break;
case 2:
storage[2][0] = digitalRead(D0) 48;
storage[2][1] = digitalRead(D1) 48;
storage[2][2] = digitalRead(D2) 48;
storage[2][3] = digitalRead(D3) 48;
storage[2][4] = digitalRead(D4) 48;
storage[2][5] = digitalRead(D5) 48;
storage[2][6] = digitalRead(D6) 48;
storage[2][7]= digitalRead(D7) 48;
break;
}
if(readydata == HIGH && prevdata == LOW){
i ;
digitalWrite(klaro, HIGH);
}
}
prevdata == readydata;
}
return 0;
}
CodePudding user response:
This is probably not an answer unless @ulix's point of prevdata = readydata;
is the source of your trouble. It is the best way to show you the changes I mentioned above, though. I don't know where you want to move the printf()
statements (as that seems to to be the point of that interrupt handler). Usually, you set a flag, then check for that flag in non-interrupt handling code. i
is particular bad global variable name so I suggest you use something more descriptive.
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define data_ready 17
#define chipselect 27
#define klaro 18
int i;
char storage[3][9];
void chipselectInt (void){
printf("%d,0x%lx,0x%lx,0x%lx\n",
i,
(int) strtol(&storage[0][0], 0, 2),
(int) strtol(&storage[1][0], 0, 2),
(int) strtol(&storage[2][0], 0, 2)
);
i=0;
}
int main(){
int D[] = {6, 13, 19, 26, 12, 16, 20, 21};
wiringPiSetupGpio();
pinMode(data_ready, INPUT);
pinMode(chipselect, INPUT);
for(int j=0; j < sizeof D / sizeof *D; j )
pinMode(D[j], INPUT);
pinMode(klaro, OUTPUT);
(wiringPiISR (27, INT_EDGE_RISING , chipselectInt ));
int readydata = 0; // LOW?
int prevdata = 0; // LOW?
int chip_select = 0;
for(;;) {
if(chip_select == LOW){
readydata = digitalRead(data_ready);
digitalWrite(klaro, LOW);
for(int j=0; j < sizeof D / sizeof *D; j )
storage[i][j] = digitalRead(D[j]) 48;
if(readydata == HIGH && prevdata == LOW){
i ;
digitalWrite(klaro, HIGH);
}
}
prevdata = readydata;
}
return 0;
}