Skip to content

Instantly share code, notes, and snippets.

@ximeg
Last active August 9, 2022 20:10
Show Gist options
  • Save ximeg/f8dac451274923e396ccddd89ee50b35 to your computer and use it in GitHub Desktop.
Save ximeg/f8dac451274923e396ccddd89ee50b35 to your computer and use it in GitHub Desktop.
Arduino debugging - external timer clock
// This creates an external pulse source for an Arduino board - useful for timer debugging!
/* python
from time import sleep
from serial import Serial
from ctypes import c_uint32
from avrpy.mega328P import Mega328P
avr = Mega328P("COM6")
def cu32(value):
assert value < 2**32, "value exceeds 32 bit"
return c_uint32(value)
c = Serial("COM8", baudrate=2000000)
def q():
avr.com.write(b"?xxxx")
sleep(0.02)
print(avr.com.read_all().decode())
def p(N):
c.write(cu32(N))
sleep(1.4e-6 * (N + 100))
q()
*/
#include <stdint.h>
void setup_IO_ports()
{
DDRC = 0xff;
DDRB = 0xFF;
}
void setup_UART()
{
Serial.begin(2000000);
Serial.setTimeout(10); // ms
// Wait until the serial port is ready
while (!Serial)
{
}
Serial.flush();
}
void setup()
{
setup_IO_ports();
setup_UART();
}
void loop() {
uint32_t N;
if (Serial.available())
{
if (Serial.readBytes((char *) &N, 4) == 4)
{
Serial.println(N);
for (uint32_t i = 0; i < N; i++){
PORTC = 0xFF;
PORTC = 0;
}
}
}
}
/// On the other board run something like this:
/*
// Stop image acquisition
case '?':
Serial.print("ICR1=");
Serial.println(ICR1);
Serial.print("OCR1A=");
Serial.println(OCR1A);
Serial.print("OCR1B=");
Serial.println(OCR1B);
Serial.print("TCNT1=");
Serial.println(TCNT1);
Serial.print("sys.time=");
Serial.println((uint32_t)sys.time);
Serial.println("");
break;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment