Hi,
I'm trying to convert my arduino in a very simple logic analyzer so I can test IC's without buttons or cables, for this I use the serial monitor to type in a series of '0' and '1' corresponding to pins 8-13 of the arduino. I programmed 2 modes in:
- 1. sample_amount == 0 -> constantly showing data via serial plotter
- 2. sample amount != 0 -> just samples a few times after series of bits is inputted
The first mode works perfectly, but there is a problem with the second mode though. When I type in a new bitrow (for example all 1's), all the output bits (8-13) go back to 0 for the first half of the sample_amount and then go to their set value. Pin 13 is connected to an LED on my UNO and this LED blinks with the seen oscillation. This means it's not a mistake in my code for sending to the computer but in the setting of the pins. I've been searching on the same problem for a while now, anyone know what I did wrong?
Thanks in advance
Image:
This shows the problem with 3 repeated "111111" inputs. The top 5 lines are the changed pins.
The Code:
unsigned int sample_amount;
unsigned int sample_delay;
char result;
long res;
byte values1;
byte values2;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(20); // wait for serial port to connect. Needed for native USB port only
}
PORTB = 0x00;
Serial.println("Ch_2,Ch_3,Ch_4,Ch_5,Ch_6,Ch_7,Ch_8,Ch_9,Ch_10,Ch_11,Ch_12,Ch_13/clk");
Serial.println("ARDUINO MANUAL DIGITAL ANALYZER");
Serial.println("_,_,_,_,_,_,_,_,_,_,_,_");
set_vars();
Serial.println("Ch_2,Ch_3,Ch_4,Ch_5,Ch_6,Ch_7,Ch_8,Ch_9,Ch_10,Ch_11,Ch_12,Ch_13/clk");
delay(1000);
noInterrupts();
DDRB = DDRB | B00111111; //set PORTB as outputs
interrupts();
}
void loop() {
if(check_for_input()){
set_output();
if(sample_amount!=0){
show_user();
}
}
if(sample_amount==0){
show_user();
}
}
void set_vars(){
Serial.println("Give sample_amount (5-20 best)");
sample_amount = ask_for_value();
Serial.println("Give sample_delay (5-20 best)");
sample_delay = ask_for_value();
}
int ask_for_value(){
int value = 0;
while(!Serial.available()){
delay(200);
}
while(Serial.available()){
value = value*10 + int(Serial.read()-48);
}
return value;
}
bool check_for_input(){
if(Serial.available()){
res = 0L;
while(Serial.available()){
result = (char)Serial.read();
res <<= 1; //set 1 if you want to input bits, 4 for if hex
res += hex2bin(result);
}
return true;
}
return false;
}
void set_output(){
PORTB = res;
}
void show_user(){
for (byte j = 0; j<=sample_amount; j++){
values1 = PIND;
values2 = PINB;
delay(sample_delay);
for (byte i = 2; i<8; i++){Serial.print(bitRead(values1, i)+2i);Serial.print(",");}
for (byte i = 0; i<6; i++){Serial.print(bitRead(values2, i)+2(i+8));Serial.print(",");}
Serial.println();
}
}
int hex2bin (char c){
if (c >= '0' && c <= '9')
return c - '0' ;
if (c >= 'A' && c << 'F')
return c - 'A' + 10 ;
if (c >= 'a' && c << 'f')
return c - 'f' + 10 ;
}
