mirror of
https://github.com/aster94/logic-analyzer.git
synced 2026-05-01 15:10:43 +03:00
1312
Computer_Interface/Computer_Interface.pde
Normal file
1312
Computer_Interface/Computer_Interface.pde
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,471 +0,0 @@
|
||||
import processing.serial.*;
|
||||
import java.util.Arrays;
|
||||
Serial p;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/*--------------------SETUP-------------------*/
|
||||
|
||||
//uncomment the line where your arduino/STM32 is connected
|
||||
//String LA_port = "/dev/ttyACM0"; //linux DFU
|
||||
//String LA_port = "/dev/ttyUSB0"; //linux Serial
|
||||
String LA_port = "COM10"; //windows
|
||||
|
||||
final int baudrate = 115200; //check if it is the same in arduino
|
||||
|
||||
//change it to true if you are using a STM32 instead of arduino UNO, MEGA or ESP8266
|
||||
final boolean STM32 = false;
|
||||
|
||||
/*------------------END SETUP-----------------*/
|
||||
////////////////////////////////////////////////
|
||||
|
||||
|
||||
//colors:
|
||||
int white = 255;
|
||||
int black = 0;
|
||||
int green = #00FF00;
|
||||
int grey = 150;
|
||||
|
||||
|
||||
// shift, reducer and millisecond view
|
||||
float reducer = 1.0;
|
||||
boolean milliseconds = false;
|
||||
float xShift;
|
||||
|
||||
|
||||
// start point in the processing window
|
||||
int xEdge = 60;
|
||||
int yEdge = 30;
|
||||
int xEnd;
|
||||
float[] xPos = {0, 0, 0, 0, 0, 0};
|
||||
int yBottom;
|
||||
int yDiff;
|
||||
int yPos = yEdge;
|
||||
int ySave = yEdge;
|
||||
boolean textCovered;
|
||||
boolean drawTimes = true;
|
||||
|
||||
|
||||
//Serial from mcu
|
||||
//initial data
|
||||
int samples;
|
||||
int event;
|
||||
int initialState;
|
||||
boolean first = false;
|
||||
boolean dataComplete = false;
|
||||
//following data
|
||||
boolean [][] state;
|
||||
boolean [] isLow = new boolean[6];
|
||||
float[] usTime;
|
||||
float[] xTime;
|
||||
int[] pinChanged;
|
||||
|
||||
|
||||
//buttons and others
|
||||
int button1X = 8;
|
||||
int button2X = 8;
|
||||
int button3X = 80;
|
||||
int button4X = 200;
|
||||
int button5X = 270;
|
||||
int buttonY;
|
||||
int buttonH = 20;
|
||||
int smallButtonW = 50;
|
||||
int bigButtonW = 100;
|
||||
int graphBoxH;
|
||||
int textBoxH;
|
||||
int immage = 1;
|
||||
int corner = 10;
|
||||
|
||||
|
||||
// bar scroll
|
||||
int handleFill = grey;
|
||||
float handleX;
|
||||
float handleY;
|
||||
float handleW = 20;
|
||||
float handleH = 15;
|
||||
boolean isDraggable = false;
|
||||
|
||||
|
||||
|
||||
void setup () {
|
||||
//p = new Serial(this, Serial.list()[0], 115200);
|
||||
p = new Serial(this, LA_port, baudrate);
|
||||
p.bufferUntil('\n');
|
||||
|
||||
size(1000, 460);
|
||||
background(black);
|
||||
smooth(4);
|
||||
|
||||
graphBoxH = height -50;
|
||||
textBoxH = height - 35;
|
||||
yBottom = graphBoxH-20;
|
||||
buttonY = textBoxH +8;
|
||||
handleX = xEdge;
|
||||
handleY = graphBoxH;
|
||||
}
|
||||
|
||||
|
||||
void cleanGraph() {
|
||||
noStroke(); //no borders
|
||||
fill(black);
|
||||
rect(xEdge, 0, width, graphBoxH); //cancel the graph
|
||||
stroke(green); //green lines
|
||||
Arrays.fill(xPos, 0); //reset start point of the graph
|
||||
textCovered = false;
|
||||
}
|
||||
|
||||
|
||||
void draw () {
|
||||
|
||||
if (dataComplete==true) {
|
||||
cleanGraph();
|
||||
pushMatrix(); //move the coordinate reference
|
||||
translate(xEdge, 0);
|
||||
for (int i=0; i<samples; i++) {
|
||||
yPos = yEdge; //start a new cicle
|
||||
for (int n=0; n<6; n++) {
|
||||
if (state[i][n]==true) {
|
||||
ySave = yPos; //save y value
|
||||
if (isLow[n]==true) { //pin high else low
|
||||
yDiff=yPos;
|
||||
yPos+=30;
|
||||
isLow[n]=false;
|
||||
} else {
|
||||
yDiff=yPos+30;
|
||||
isLow[n]=true;
|
||||
}
|
||||
|
||||
// Text times
|
||||
if (drawTimes == true) {
|
||||
stroke(grey);
|
||||
fill(grey);
|
||||
textSize(10);
|
||||
textCovered=!textCovered;
|
||||
dashline(xTime[i]+xShift, yPos, xTime[i]+xShift, yBottom, spacing);
|
||||
text(round(usTime[i]), xTime[i]+xShift+2, (textCovered==true) ? yBottom : yBottom+10); //write on different height
|
||||
stroke(green);
|
||||
}
|
||||
|
||||
// Graph lines
|
||||
line(xPos[n]+xShift, yPos, xTime[i]+xShift, yPos); // straight line
|
||||
line(xTime[i]+xShift, yPos, xTime[i]+xShift, yDiff); // vertical line
|
||||
|
||||
xPos[n]=xTime[i]; //save last position of the line for the pin
|
||||
yPos = ySave; //load the initial value of the y
|
||||
}
|
||||
yPos+=60; //go to the next pin
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (samples!=0) {
|
||||
xEnd = int (xTime[samples-1]) +10;
|
||||
} else {
|
||||
xEnd = 0;
|
||||
}
|
||||
|
||||
yPos = yEdge;
|
||||
for (int n = 0; n < 6; n++) {
|
||||
if (xPos[n]!=0) { //draw only the pin which are active
|
||||
if (isLow[n]==true) line(xPos[n]+xShift, yPos+30, xEnd+xShift, yPos+30);
|
||||
else line(xPos[n]+xShift, yPos, xEnd+xShift, yPos);
|
||||
}
|
||||
yPos+=60;
|
||||
}
|
||||
|
||||
dataComplete = false;
|
||||
popMatrix();
|
||||
}
|
||||
drawText();
|
||||
}
|
||||
|
||||
|
||||
void drawText() {
|
||||
|
||||
stroke(white); //white borders
|
||||
fill(black);
|
||||
rect(0, 0, xEdge, graphBoxH); //clean left side
|
||||
rect(xEdge, graphBoxH, width, handleH); //clean bar scroll
|
||||
rect(0, textBoxH, width, height); //clean bottom side
|
||||
|
||||
|
||||
// write name of the pins
|
||||
fill(white);
|
||||
textSize(14);
|
||||
|
||||
int x=10;
|
||||
int y=50;
|
||||
|
||||
if (STM32) {
|
||||
|
||||
for (byte i = 12; i<=15; i++) {
|
||||
line(x, y-20, xEdge, y-20);
|
||||
line(x, y+10, xEdge, y+10);
|
||||
text ("PB"+i, x, y);
|
||||
y+=60;
|
||||
}
|
||||
} else {
|
||||
|
||||
for (byte i = 8; i<=13; i++) {
|
||||
line(x, y-20, xEdge, y-20);
|
||||
line(x, y+10, xEdge, y+10);
|
||||
text ("Pin "+i, x, y);
|
||||
y+=60;
|
||||
}
|
||||
}
|
||||
|
||||
// draw buttons
|
||||
fill(grey);
|
||||
|
||||
rect(button1X, yBottom-15, smallButtonW, buttonH, corner);
|
||||
rect(button2X, buttonY, smallButtonW, buttonH, corner);
|
||||
rect(button3X, buttonY, bigButtonW, buttonH, corner);
|
||||
rect(button4X, buttonY, smallButtonW, buttonH, corner);
|
||||
rect(button5X, buttonY, smallButtonW, buttonH, corner);
|
||||
fill(white);
|
||||
text("T:"+ str (drawTimes), button1X+3, yBottom);
|
||||
text("Start", button2X+3, buttonY+14);
|
||||
text(milliseconds == true ? "milliseconds" : "microseconds", button3X+3, buttonY+14);
|
||||
text(reducer, button4X+3, buttonY+14);
|
||||
text("Save", button5X+3, buttonY+14);
|
||||
|
||||
|
||||
//bar scroll
|
||||
fill(handleFill);
|
||||
rect(handleX, handleY, handleW, handleH);
|
||||
|
||||
if (isDraggable) {
|
||||
handleX = mouseX-(handleW/2);
|
||||
if (handleX<xEdge) handleX = xEdge;
|
||||
if (handleX>width-handleW) handleX = width-handleW;
|
||||
|
||||
getData();
|
||||
xShift = -map(handleX, xEdge, width-handleW, 0, xEnd-900);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mousePressed() {
|
||||
if (mouseX>handleX && mouseX<handleX+handleW &&
|
||||
mouseY>handleY && mouseY<handleY+handleH) {
|
||||
isDraggable = true;
|
||||
handleFill = color(100, 200, 255);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mouseReleased() {
|
||||
isDraggable = false;
|
||||
handleFill = grey;
|
||||
}
|
||||
|
||||
|
||||
void mouseClicked() {
|
||||
|
||||
// draw times
|
||||
if (mouseY>yBottom-15 && mouseY <yBottom-15+buttonH &&
|
||||
mouseX>button1X && mouseX <button1X+smallButtonW) {
|
||||
drawTimes = !drawTimes;
|
||||
getData();
|
||||
}
|
||||
|
||||
// new read
|
||||
if (mouseY>buttonY && mouseY <buttonY+buttonH &&
|
||||
mouseX>button2X && mouseX <button2X+smallButtonW) {
|
||||
p.write('G');
|
||||
println("new data coming");
|
||||
p.clear();
|
||||
xShift = 0;
|
||||
handleX = xEdge;
|
||||
}
|
||||
|
||||
// micro or millis
|
||||
if (mouseY>buttonY && mouseY <buttonY+buttonH &&
|
||||
mouseX>button3X && mouseX <button3X+bigButtonW) {
|
||||
|
||||
milliseconds = !milliseconds;
|
||||
|
||||
if (milliseconds == true) {
|
||||
for (int i=0; i< samples; i++) usTime[i] /= 1000.0;
|
||||
}
|
||||
if (milliseconds== false) {
|
||||
for (int i=0; i< samples; i++) usTime[i] *= 1000.0;
|
||||
}
|
||||
|
||||
getData();
|
||||
xShift = 0;
|
||||
handleX = xEdge;
|
||||
}
|
||||
|
||||
//save frame
|
||||
if (mouseY>buttonY && mouseY <buttonY+buttonH &&
|
||||
mouseX>button5X && mouseX <button5X+smallButtonW) {
|
||||
String a = "la_capture-"+immage; //+".jpg"; //if you prefer this format, default .tif
|
||||
save(a);
|
||||
immage++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mouseWheel(MouseEvent event) {
|
||||
float wheel = event.getCount();
|
||||
|
||||
if (mouseY>buttonY && mouseY <buttonY+buttonH &&
|
||||
mouseX>button4X && mouseX <button4X+smallButtonW) {
|
||||
//it is over the reducer button
|
||||
xShift *= reducer;
|
||||
reducer-= wheel/10;
|
||||
reducer = constrain(reducer, 0.1, 9.9);
|
||||
xShift /= reducer; // preserve scroll position
|
||||
getData();
|
||||
} else { //move the graph
|
||||
xShift-=wheel*50;
|
||||
getData();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mouseMoved() {
|
||||
if (mouseY>buttonY && mouseY <buttonY+buttonH && mouseX>button2X && mouseX <button2X+smallButtonW) {
|
||||
cursor(HAND);
|
||||
} else if (mouseY>buttonY && mouseY <buttonY+buttonH && mouseX>button3X && mouseX <button3X+bigButtonW) {
|
||||
cursor(HAND);
|
||||
} else if (mouseY>buttonY && mouseY <buttonY+buttonH && mouseX>button5X && mouseX <button5X+smallButtonW) {
|
||||
cursor(HAND);
|
||||
} else if (mouseY>yBottom-15 && mouseY <yBottom-15+buttonH && mouseX>button1X && mouseX <button1X+smallButtonW) {
|
||||
cursor(HAND);
|
||||
} else if (mouseX>handleX && mouseX<handleX+handleW && mouseY>handleY && mouseY<handleY+handleH) {
|
||||
cursor(HAND);
|
||||
} else {
|
||||
cursor(ARROW);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void serialEvent (Serial p) {
|
||||
|
||||
String inString = p.readStringUntil('\n');
|
||||
inString = trim(inString);
|
||||
println("incoming: "+inString);
|
||||
|
||||
if (inString.equals("S") == true) {
|
||||
|
||||
initialState=0;
|
||||
samples=0;
|
||||
event=-2;
|
||||
|
||||
first = true;
|
||||
} else {
|
||||
|
||||
String list [] = split(inString, ':');
|
||||
|
||||
if (first == true) {
|
||||
|
||||
initialState = int (list[0]);
|
||||
samples = int (list[1]);
|
||||
|
||||
pinChanged = new int[samples];
|
||||
usTime = new float[samples];
|
||||
xTime = new float[samples];
|
||||
state = new boolean[samples][6];
|
||||
|
||||
first = false;
|
||||
} else {
|
||||
pinChanged[event] = int (list[0]);
|
||||
usTime[event] = float (list[1]);
|
||||
}
|
||||
}
|
||||
|
||||
event++;
|
||||
|
||||
if (event == samples) {
|
||||
getData();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void getData () {
|
||||
|
||||
//check data:
|
||||
println("event: "+event);
|
||||
println("initial: "+initialState);
|
||||
println("samples: "+samples);
|
||||
//println("pin"+changed[0]);
|
||||
//println("time"+usTime[0]);
|
||||
printArray(usTime);
|
||||
printArray(xTime);
|
||||
//println("pin: "+binary(changed[0], 6));
|
||||
|
||||
for (int i = 0; i < samples; i++) {
|
||||
xTime[i] = usTime[i] / reducer; //better to reduce the lenght of the x
|
||||
}
|
||||
|
||||
int b;
|
||||
int mask = 1;
|
||||
|
||||
// initial state
|
||||
for (int n=0; n<6; n++) {
|
||||
b = initialState & mask;
|
||||
isLow[n] = !boolean (b);
|
||||
mask <<= 1;
|
||||
//println("islow: "+isLow[n]);
|
||||
}
|
||||
|
||||
// changes
|
||||
for (int i=0; i<samples; i++) {
|
||||
mask = 1;
|
||||
//println("i:"+i);
|
||||
//println(binary(changed[i], 6));
|
||||
for (int n=0; n<6; n++) {
|
||||
b= pinChanged[i] & mask;
|
||||
state[i][n]= boolean (b);
|
||||
mask <<= 1;
|
||||
//println(state[i][n]);
|
||||
}
|
||||
}
|
||||
dataComplete = true;
|
||||
}
|
||||
|
||||
|
||||
float[] spacing = {5, 8}; //used for the dashline function, pixels
|
||||
|
||||
void dashline(float x0, float y0, float x1, float y1, float[] spacing) {
|
||||
|
||||
float distance = dist(x0, y0, x1, y1);
|
||||
float [ ] xSpacing = new float[spacing.length];
|
||||
float [ ] ySpacing = new float[spacing.length];
|
||||
float drawn = 0.0; // amount of distance drawn
|
||||
|
||||
if (distance > 0)
|
||||
{
|
||||
int i;
|
||||
boolean drawLine = true; // alternate between dashes and gaps
|
||||
|
||||
/*
|
||||
Figure out x and y distances for each of the spacing values
|
||||
I decided to trade memory for time; I'd rather allocate
|
||||
a few dozen bytes than have to do a calculation every time
|
||||
I draw.
|
||||
*/
|
||||
|
||||
for (i = 0; i < spacing.length; i++)
|
||||
{
|
||||
xSpacing[i] = lerp(0, (x1 - x0), spacing[i] / distance);
|
||||
ySpacing[i] = lerp(0, (y1 - y0), spacing[i] / distance);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
while (drawn < distance)
|
||||
{
|
||||
if (drawLine)
|
||||
{
|
||||
line(x0, y0, x0 + xSpacing[i], y0 + ySpacing[i]);
|
||||
}
|
||||
x0 += xSpacing[i];
|
||||
y0 += ySpacing[i];
|
||||
/* Add distance "drawn" by this line or gap */
|
||||
drawn = drawn + mag(xSpacing[i], ySpacing[i]);
|
||||
i = (i + 1) % spacing.length; // cycle through array
|
||||
drawLine = !drawLine; // switch between dash and gap
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,23 +5,23 @@
|
||||
* Test your logic analyzer with another arduino
|
||||
*/
|
||||
|
||||
#define led A5
|
||||
#define led2 13
|
||||
#define pin1 A5
|
||||
#define pin2 13
|
||||
|
||||
void setup() {
|
||||
pinMode(led, OUTPUT);
|
||||
pinMode(led2, OUTPUT);
|
||||
pinMode(pin1, OUTPUT);
|
||||
pinMode(pin2, OUTPUT);
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
digitalWrite(led, HIGH);
|
||||
digitalWrite(pin1, HIGH);
|
||||
delayMicroseconds(random(200));
|
||||
digitalWrite(led2, HIGH);
|
||||
digitalWrite(pin2, HIGH);
|
||||
delayMicroseconds(random(200));
|
||||
digitalWrite(led2, LOW);
|
||||
digitalWrite(pin2, LOW);
|
||||
delayMicroseconds(random(200));
|
||||
digitalWrite(led, LOW);
|
||||
digitalWrite(pin1, LOW);
|
||||
delayMicroseconds(random(200));
|
||||
}
|
||||
@@ -2,13 +2,13 @@
|
||||
* ESP8266.ino
|
||||
*
|
||||
* Author : yoursunny
|
||||
* Led: LED_BUILTIN
|
||||
*/
|
||||
|
||||
#include <c_types.h>
|
||||
|
||||
#define baudrate 115200 // check if it is the same in processing
|
||||
// number of samples to collect
|
||||
#define timezerooffset 125
|
||||
static const int N_SAMPLES = 300;
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ void setup() {
|
||||
Serial.begin(baudrate);
|
||||
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
pinMode(PIN0, INPUT_PULLUP);
|
||||
pinMode(PIN1, INPUT_PULLUP);
|
||||
@@ -40,6 +40,7 @@ void setup() {
|
||||
|
||||
unsigned long times[N_SAMPLES]; // when did change happen
|
||||
uint32_t values[N_SAMPLES]; // GPI value at time
|
||||
uint32_t timefix;
|
||||
|
||||
extern void ICACHE_RAM_ATTR collect() {
|
||||
times[0] = micros();
|
||||
@@ -73,15 +74,20 @@ int compactValue(uint32_t value) {
|
||||
|
||||
void report() {
|
||||
Serial.println("S");
|
||||
Serial.print(compactValue(values[0]));
|
||||
Serial.print(":");
|
||||
Serial.println(N_SAMPLES - 1);
|
||||
|
||||
for (int i = 1; i < N_SAMPLES; ++i) {
|
||||
Serial.print(compactValue(values[i] ^ values[i - 1]));
|
||||
Serial.print(":");
|
||||
Serial.println(times[i] - times[0]);
|
||||
Serial.print(compactValue(values[0])); Serial.print(','); Serial.print(B00000000); Serial.print(','); Serial.print(B00000000); Serial.print(":");
|
||||
Serial.println(N_SAMPLES +1);
|
||||
timefix = -times[0]+timezerooffset;
|
||||
for (int i = 0; i < N_SAMPLES; i++) {
|
||||
times[i]=times[i]+timefix;
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");//Este segmento de codigo introduce un cambio en todos los
|
||||
Serial.println(0);
|
||||
for (int i = 1; i < N_SAMPLES; ++i) {
|
||||
Serial.print(compactValue(values[i] ^ values[i - 1])); Serial.print(','); Serial.print(B00000000); Serial.print(','); Serial.print(B00000000); Serial.print(":");
|
||||
Serial.println(times[i]);
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");
|
||||
Serial.println((times[N_SAMPLES-1]+400));
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
@@ -1,22 +1,12 @@
|
||||
/*
|
||||
* MEGA.ino
|
||||
* IMPORTANT: Please to use this board use this fork https://github.com/sancho11/logic-analyzer
|
||||
*
|
||||
* Created: 11/12/2016 19.35.51
|
||||
* Author : Vincenzo / sancho
|
||||
* Modificaciones agregadas para funcionar con ArduinoMega2560 por Enmanuel Sancho Quintanilla
|
||||
* La unidad minima en tiempo para este sistema es de 8 micro segundos lo que idealmente permitiria
|
||||
* observar clocks con periodos de 62 kHz sin embargo para poder apreciar las señales logicas con suficiente
|
||||
* resolucion se recomienda no superar los 30 kHz en el clock del sistema.
|
||||
*/
|
||||
* Author : sancho / Vincenzo
|
||||
*/
|
||||
|
||||
#define baudrate 115200 // check if it is the same in processing
|
||||
#define samples 200 // the number of samples you want to take
|
||||
|
||||
|
||||
#define pin_used
|
||||
#define baudrate 115200 //check if it is the same in processing
|
||||
#define samples 500
|
||||
#define timezerooffset 125 //microsegundos
|
||||
#define PULLUP true //Si queremos entradas con PULLUP lo dejamos activado(true), si queremos dejarlas al "aire" (false), en caso de desactivarlo deberemos aterrizar todos los pines que no utilizemos.
|
||||
#define PULLUP true//Si queremos entradas con PULLUP lo dejamos activado(true), si queremos dejarlas al "aire" (false), en caso de desactivarlo deberemos aterrizar todos los pines que no utilizemos.
|
||||
#define F_CPU 16000000UL
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
@@ -31,32 +21,28 @@ uint8_t pinChanged3[samples];
|
||||
uint32_t timer[samples];
|
||||
uint32_t timefix;
|
||||
uint16_t event = 0;
|
||||
uint8_t cambio = 0;
|
||||
void init_board()
|
||||
{
|
||||
|
||||
// PORTC = (0 << 0); DDRC |= (1 << 0); // led A0
|
||||
|
||||
DDRB = 0x00;
|
||||
DDRC = 0x00;
|
||||
uint8_t cambio=0;
|
||||
void init_board() {
|
||||
|
||||
DDRB = 0x00;
|
||||
DDRC = 0x00;
|
||||
DDRL = 0x00;
|
||||
|
||||
if (PULLUP)
|
||||
{
|
||||
PORTA = B11111111; // pull-up
|
||||
PORTC = B11111111; // Activamos el pull-up para que de no conectarse nada a puerto lea un uno siempre
|
||||
PORTL = B11111111;
|
||||
if (PULLUP){ // Activamos el pull-up para que de no conectarse nada a puerto lea un uno siempre
|
||||
for (uint8_t p = 22; p <= 49; p++){
|
||||
pinMode(p, INPUT_PULLUP);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PORTA = B00000000;
|
||||
PORTC = B00000000;
|
||||
PORTL = B00000000;
|
||||
else{
|
||||
for (uint8_t p = 22; p <= 49; p++){
|
||||
pinMode(p, INPUT);
|
||||
}
|
||||
}
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
}
|
||||
|
||||
void init_timer()
|
||||
{
|
||||
void init_timer() {
|
||||
|
||||
//clear
|
||||
TCCR1A = 0b00000000;
|
||||
TCCR1B = 0b00000000;
|
||||
@@ -64,105 +50,92 @@ void init_timer()
|
||||
|
||||
//settings
|
||||
TCCR1A |= (0 << COM1A1) | (0 << COM1A0) | (0 << COM1B1) | (0 << COM1B0); //normal port operation
|
||||
TCCR1A |= (0 << WGM11) | (0 << WGM10); //normal operation
|
||||
TCCR1B |= (0 << WGM13) | (0 << WGM12); //normal operation
|
||||
TCCR1B |= prescaler; //(0 << CS12) | (0 << CS11) | (1 << CS10); //clock prescaler
|
||||
sei(); //enable interrupts
|
||||
TIMSK1 |= (1 << TOIE1); // enable overflow interrupt
|
||||
TCCR1A |= (0 << WGM11) | (0 << WGM10); //normal operation
|
||||
TCCR1B |= (0 << WGM13) | (0 << WGM12); //normal operation
|
||||
TCCR1B |= prescaler; //(0 << CS12) | (0 << CS11) | (1 << CS10); //clock prescaler
|
||||
|
||||
sei(); //enable interrupts
|
||||
TIMSK1 |= (1 << TOIE1); // enable overflow interrupt
|
||||
|
||||
}
|
||||
|
||||
ISR(TIMER1_OVF_vect)
|
||||
{
|
||||
ISR(TIMER1_OVF_vect) {
|
||||
timer1_overflow_count++;
|
||||
}
|
||||
|
||||
void reset_timer1()
|
||||
{
|
||||
void reset_timer1 () {
|
||||
TCNT1 = 0;
|
||||
timer1_overflow_count = 0;
|
||||
}
|
||||
|
||||
uint32_t myMicros()
|
||||
{
|
||||
uint32_t myMicros () {
|
||||
cli();
|
||||
|
||||
if (TIFR1 & (1 << TOV1))
|
||||
{
|
||||
if (TIFR1 & (1 << TOV1)) {
|
||||
TIFR1 = (0 << TOV1);
|
||||
timer1_overflow_count++;
|
||||
}
|
||||
|
||||
|
||||
uint32_t total_time = (65536 * timer1_overflow_count + TCNT1) / 2;
|
||||
sei();
|
||||
return total_time;
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
void start() {
|
||||
_delay_ms(1000);
|
||||
// Serial.print("hi");
|
||||
// Serial.print("hi");
|
||||
reset_timer1();
|
||||
event = 0;
|
||||
|
||||
//PORTC = (1 << 0);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
initial1 = PINA;
|
||||
initial2 = PINL;
|
||||
initial3 = PINC;
|
||||
state1 = initial1;
|
||||
state2 = initial2;
|
||||
state3 = initial3;
|
||||
for (int i = 0; i < samples; i++)
|
||||
{
|
||||
pinChanged1[i] = 0;
|
||||
pinChanged2[i] = 0;
|
||||
pinChanged3[i] = 0;
|
||||
//Serial.print(pinChanged1[i]); Serial.print(','); Serial.print(pinChanged2[i]); Serial.print(','); Serial.println(pinChanged3[i]); //debug
|
||||
}
|
||||
for (int i=0; i < samples; i++) {
|
||||
pinChanged1[i]=0;
|
||||
pinChanged2[i]=0;
|
||||
pinChanged3[i]=0;
|
||||
//Serial.print(pinChanged1[i]); Serial.print(','); Serial.print(pinChanged2[i]); Serial.print(','); Serial.println(pinChanged3[i]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void sendData()
|
||||
{
|
||||
//PORTC = (0 << 0); //turn off led
|
||||
|
||||
void sendData() {
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
//initial data
|
||||
Serial.println("S");
|
||||
Serial.print(initial1);
|
||||
Serial.print(',');
|
||||
Serial.print(initial2);
|
||||
Serial.print(',');
|
||||
Serial.print(initial3);
|
||||
Serial.print(":");
|
||||
Serial.println(samples);
|
||||
|
||||
timefix = -timer[0] + timezerooffset;
|
||||
|
||||
for (int i = 0; i < samples; i++)
|
||||
{
|
||||
timer[i] = timer[i] + timefix;
|
||||
Serial.print(initial1); Serial.print(','); Serial.print(initial2); Serial.print(','); Serial.print(initial3); Serial.print(":");
|
||||
Serial.println(samples+2);
|
||||
timefix = -timer[0]+timezerooffset;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
timer[i]=timer[i]+timefix;
|
||||
}
|
||||
//data
|
||||
for (int i = 0; i < samples; i++)
|
||||
{
|
||||
Serial.print(pinChanged1[i]);
|
||||
Serial.print(',');
|
||||
Serial.print(pinChanged2[i]);
|
||||
Serial.print(',');
|
||||
Serial.print(pinChanged3[i]);
|
||||
Serial.print(":");
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");//Este segmento de codigo introduce un cambio en todos los
|
||||
Serial.println(0); //canales, lo que soluciona un error en el codigo en processing
|
||||
//al final se hace un cambio en todos los canales, lo que soluciona //otro pequeño fallo visual.
|
||||
//data
|
||||
for (int i = 0; i < samples; i++) {
|
||||
Serial.print(pinChanged1[i]);Serial.print(','); Serial.print(pinChanged2[i]); Serial.print(','); Serial.print(pinChanged3[i]); Serial.print(":");
|
||||
Serial.println(timer[i]);
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");
|
||||
Serial.println((timer[samples-1]+400));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
int main(void) {
|
||||
Serial.begin(baudrate);
|
||||
init_board();
|
||||
init_timer();
|
||||
|
||||
start();
|
||||
|
||||
while (1)
|
||||
{
|
||||
cambio = 0;
|
||||
while (1) {
|
||||
cambio=0;
|
||||
old_state1 = state1;
|
||||
old_state2 = state2;
|
||||
old_state3 = state3;
|
||||
@@ -170,36 +143,28 @@ int main(void)
|
||||
state2 = PINL;
|
||||
state3 = PINC;
|
||||
//Serial.print(state1);Serial.print(" , ");Serial.print(state2);Serial.print(" , ");Serial.print(state3);Serial.print(" : ");Serial.println(event);
|
||||
|
||||
if (old_state1 != state1)
|
||||
{
|
||||
pinChanged1[event] = state1 ^ old_state1;
|
||||
cambio = 1;
|
||||
|
||||
if (old_state1 != state1 ) {
|
||||
pinChanged1[event] = state1 ^ old_state1;
|
||||
cambio=1;
|
||||
}
|
||||
if (old_state2 != state2)
|
||||
{
|
||||
pinChanged2[event] = state2 ^ old_state2;
|
||||
cambio = 1;
|
||||
if (old_state2 != state2 ) {
|
||||
pinChanged2[event] = state2 ^ old_state2;
|
||||
cambio=1;
|
||||
}
|
||||
if (old_state3 != state3)
|
||||
{
|
||||
pinChanged3[event] = state3 ^ old_state3;
|
||||
cambio = 1;
|
||||
if (old_state3 != state3 ) {
|
||||
pinChanged3[event] = state3 ^ old_state3;
|
||||
cambio = 1;
|
||||
}
|
||||
|
||||
if (cambio == 1)
|
||||
{
|
||||
timer[event] = myMicros();
|
||||
event++;
|
||||
}
|
||||
if (event == samples)
|
||||
{
|
||||
if (cambio == 1) {
|
||||
timer[event] = myMicros();
|
||||
event++;
|
||||
}
|
||||
if (event == samples) {
|
||||
sendData();
|
||||
while (Serial.read() != 'G')
|
||||
{
|
||||
;
|
||||
} //wait for the "go"
|
||||
while (Serial.read() != 'G') ; //wait for the "go"
|
||||
start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,15 @@
|
||||
*
|
||||
* Author : Vincenzo
|
||||
* this works using the unofficial STM32 core, more info: https://github.com/rogerclarkmelbourne/Arduino_STM32
|
||||
* Led: PB1
|
||||
*/
|
||||
|
||||
|
||||
#define baudrate 115200 // check if it is the same in processing
|
||||
#define samples 200 // the number of samples you want to take
|
||||
#define boardLed PB1
|
||||
|
||||
|
||||
|
||||
uint8_t initial, state, old_state;
|
||||
uint8_t pinChanged[samples];
|
||||
uint16_t initial, state, old_state;
|
||||
uint16_t pinChanged[samples];
|
||||
uint8_t initial1, initial2, pinChanged1, pinChanged2;
|
||||
uint32_t timer[samples];
|
||||
uint16_t event = 0;
|
||||
|
||||
@@ -25,9 +22,20 @@ void setup() {
|
||||
|
||||
Serial.begin(baudrate);
|
||||
|
||||
pinMode (boardLed, OUTPUT);
|
||||
digitalWrite(boardLed, LOW);
|
||||
|
||||
pinMode (LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
pinMode(PB0, INPUT_PULLUP);
|
||||
pinMode(PB1, INPUT_PULLUP);
|
||||
pinMode(PB2, INPUT_PULLUP);
|
||||
pinMode(PB3, INPUT_PULLUP);
|
||||
pinMode(PB4, INPUT_PULLUP);
|
||||
pinMode(PB5, INPUT_PULLUP);
|
||||
pinMode(PB6, INPUT_PULLUP);
|
||||
pinMode(PB7, INPUT_PULLUP);
|
||||
pinMode(PB8, INPUT_PULLUP);
|
||||
pinMode(PB9, INPUT_PULLUP);
|
||||
pinMode(PB10, INPUT_PULLUP);
|
||||
pinMode(PB11, INPUT_PULLUP);
|
||||
pinMode(PB12, INPUT_PULLUP);
|
||||
pinMode(PB13, INPUT_PULLUP);
|
||||
pinMode(PB14, INPUT_PULLUP);
|
||||
@@ -42,18 +50,23 @@ void startLA() {
|
||||
//delay(1000);
|
||||
|
||||
event = 0;
|
||||
digitalWrite(boardLed, HIGH);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
reset_timer();
|
||||
initial = GPIOB->regs->IDR >> 12;
|
||||
initial = GPIOB->regs->IDR;
|
||||
state = initial;
|
||||
for (int i=0; i < samples; i++) {
|
||||
pinChanged[i]=0;
|
||||
//Serial.print(pinChanged1[i]); Serial.print(','); Serial.print(pinChanged2[i]); Serial.print(','); Serial.println(pinChanged3[i]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
old_state = state;
|
||||
state = GPIOB->regs->IDR >> 12;
|
||||
state = GPIOB->regs->IDR;
|
||||
|
||||
if (old_state != state) {
|
||||
timer[event] = micros();
|
||||
@@ -69,18 +82,30 @@ void loop() {
|
||||
}
|
||||
|
||||
void sendData() {
|
||||
digitalWrite(boardLed, LOW);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
//initial data
|
||||
initial1=initial;
|
||||
initial2=initial>>8;
|
||||
Serial.println("S");
|
||||
Serial.print(initial); Serial.print(":");
|
||||
Serial.println(samples);
|
||||
|
||||
//data
|
||||
Serial.print(initial1); Serial.print(','); Serial.print(initial2); Serial.print(','); Serial.print(B00000000); Serial.print(":");
|
||||
Serial.println(samples+2);
|
||||
timefix = -timer[0]+timezerooffset;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
Serial.print(pinChanged[i]); Serial.print(":");
|
||||
timer[i]=timer[i]+timefix;
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");//Este segmento de codigo introduce un cambio en todos los
|
||||
Serial.println(0); //canales, lo que soluciona un error en el codigo en processing
|
||||
//al final se hace un cambio en todos los canales, lo que soluciona //otro pequeño fallo visual.
|
||||
//data
|
||||
for (int i = 0; i < samples; i++) {
|
||||
pinChanged1=pinChanged;
|
||||
pinChanged2=pinChanged>>8;
|
||||
Serial.print(pinChanged1);Serial.print(','); Serial.print(pinChanged2); Serial.print(','); Serial.print(B00000000); Serial.print(":");
|
||||
Serial.println(timer[i]);
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");
|
||||
Serial.println((timer[samples-1]+400));
|
||||
}
|
||||
|
||||
void reset_timer() {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#define baudrate 115200 // check if it is the same in processing
|
||||
#define samples 200 // the number of samples you want to take
|
||||
|
||||
#define timezerooffset 125 //microsegundos
|
||||
#define F_CPU 16000000UL
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
@@ -19,6 +19,7 @@ volatile uint16_t timer1_overflow_count;
|
||||
uint8_t initial, state, old_state;
|
||||
uint8_t pinChanged[samples];
|
||||
uint32_t timer[samples];
|
||||
uint32_t timefix;
|
||||
uint16_t event = 0;
|
||||
|
||||
void init_board() {
|
||||
@@ -78,7 +79,10 @@ void start() {
|
||||
PORTC = (1 << 0);
|
||||
initial = PINB;
|
||||
state = initial;
|
||||
|
||||
for (int i=0; i < samples; i++) {
|
||||
pinChanged[i]=0;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,15 +90,22 @@ void sendData() {
|
||||
PORTC = (0 << 0); //turn off led
|
||||
|
||||
//initial data
|
||||
Serial.println("S");
|
||||
Serial.print(initial); Serial.print(":");
|
||||
Serial.println(samples);
|
||||
|
||||
Serial.println("S");
|
||||
Serial.print(initial); Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");
|
||||
Serial.println(samples+2);
|
||||
timefix = -timer[0]+timezerooffset;
|
||||
for (int i = 0; i < samples; i++) {
|
||||
timer[i]=timer[i]+timefix;
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");//Este segmento de codigo introduce un cambio en todos los
|
||||
Serial.println(0); //canales evita fallo visual.
|
||||
//data
|
||||
for (int i = 0; i < samples; i++) {
|
||||
Serial.print(pinChanged[i]); Serial.print(":");
|
||||
Serial.print(pinChanged[i]);Serial.print(','); Serial.print(B00000000); Serial.print(','); Serial.print(B00000000); Serial.print(":");
|
||||
Serial.println(timer[i]);
|
||||
}
|
||||
Serial.print(B11111111);Serial.print(','); Serial.print(B11111111); Serial.print(','); Serial.print(B11111111); Serial.print(":");
|
||||
Serial.println((timer[samples-1]+400));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user