.. vim: noexpandtab fileencoding=utf-8 nomodified wrap textwidth=270 foldmethod=marker foldmarker={{{,}}} foldcolumn=4 ruler showcmd lcs=tab\:|- list tabstop=8 noexpandtab nosmarttab softtabstop=0 shiftwidth=0 :date: 2023.08.24 03:20:17 :modified: 2025.04.08 07:39:28 :tags: HowTo,Arduino,SW :authors: Gilhad :summary: Arduino :title: Arduino :nice_title: |logo| %title% |logo| %HEADER% Arduino Serial až 2Mb/s (ale 115.200 je lepší) -------------------------------------------------------------------------------- * ale musely by se přepsat knihovny, aby stíhaly bufferovat ``__ * At 2 Mb/s, each byte transmission takes **80 CPU** cycles * takže pro **6309** by to bylo asi **10 cyklů** - nestíhá interrupty, nejspíš prostě nestíhá nic a budu rád za 115.200 na několika kanálech (aka 2 seriáky+vnitřní sběrnice k Polux3+ nebo tak něco) divmod10() : a fast replacement for /10 and %10 (unsigned) -------------------------------------------------------------------------------- ``__ no delay() blink -------------------------------------------------------------------------------- .. code:: unsigned long previous_millis, interval = 1000UL; if ( (current_millis - previous_millis) >= interval) { previous_millis = current_millis; // its the time }; Arduino Micro Serial -------------------------------------------------------------------------------- * Pro výstup na **Serial** používat `if (Serial.availableForWrite()) {Serial.print(F("whatever");};` * Pro monitorování pomocí `screen` nezapomenout shodit `RTS` a `DTR` třeba pomocí `close_Serial` (``__) .. code:: #include #include #include #include #include int main(int argc, char *argv[]) { // Check if the correct number of command-line arguments is provided if (argc != 2 ) { fprintf(stderr, "Usage: %s \n", argv[0]); fprintf(stderr, " %s /dev/ttyACM0 - close USB Serial for Arduino on the port\n", argv[0]); return 1; // Return an error code } int fd; fd = open(argv[1],O_RDWR | O_NOCTTY );//Open Serial Port int RTS_flag; RTS_flag = TIOCM_RTS; int DTR_flag; DTR_flag = TIOCM_DTR; // ioctl(fd,TIOCMBIS,&RTS_flag);//Set RTS pin // ioctl(fd,TIOCMBIS,&DTR_flag);//Set DTR pin // getchar(); ioctl(fd,TIOCMBIC,&RTS_flag);//Clear RTS pin ioctl(fd,TIOCMBIC,&DTR_flag);//Clear DTR pin close(fd); } nebo ``__ .. code:: #!/usr/bin/python -u # vim: fileencoding=utf-8:nomodified:nowrap:textwidth=0:foldmethod=marker:foldcolumn=4:ruler:showcmd:lcs=tab\:|- list:tabstop=8:noexpandtab:nosmarttab:softtabstop=0:shiftwidth=0 import serial import sys # Check if the correct number of command-line arguments is provided if not len(sys.argv) in {2,3}: print("Usage: {} [baudrate]".format(sys.argv[0])) print(" {} /dev/ttyACM0 - close USB Serial for Arduino on the port".format(sys.argv[0])) sys.exit(1) # Exit with a non-zero error code # Get the filename from the command-line argument baudrate=115200 filename = sys.argv[1] if len(sys.argv) > 2: baudrate=sys.argv[2] # Open the serial port ser = serial.Serial(filename, baudrate, xonxoff=False , rtscts=True , dsrdtr=True ) ser.rts=False ser.dtr=False * A ještě líp, použít `picocom -b $(SPEED) --flow n --noreset --hangup --quiet -s cat $(PORT);echo`, který tu linku uavře a `C-a` `C-s` se tam dá vložit soubor. (A netestovat nic, prostě `Serial.print("blabla");`) How to find out all #define used by arduino-cli in compile? -------------------------------------------------------------------------------- ``__ .. code:: make define: ## dump all #define to defines.dump @make |grep g++|grep .ino.cpp.o|sed "s/-o .*/-E -dM -o defines.dump/"|sh @sort defines.dump >defines.d @mv defines.d defines.dump @echo "defines.dump created" Fuses ----- Arduino nano * normal - E:H:L = FD:DA:FF - BODLEVEL1:BootFlash=1k@3C00+BootEnabled+SPIEnabled:default * 16Mhz on D8 - E:H:L FD:DA:BF - BODLEVEL1:BootFlash=1k@3C00+BootEnabled+SPIEnabled:**Clock output on PORTB0; [CKOUT=0]** * H=*A \*C - Erase EEPROM on Erase EESAVE=0 * H=*2 \*4 - Save EEPROM EESAVE=1 * L=B* - Clock output on PORTB0; CKOUT=1 * L=F* - No clock output CKOUT=0 * H & 08 - EESAVE * H & 06 - BootSize - DA ~ 2k (Old Bootloader = ATmegaBOOT_168_atmega328 ), DC ~ 512 (Optiboot) * H & 01 - BootEnabled Blikání D13 v assembleru a v C++ -------------------------------- The Arduino D13 pin corresponds to PB5 on the ATmega328P microcontroller, and we can manipulate the DDRB and PORTB registers to control the pin as output and toggle it. .. code:: C DDRB |= (1 << PB5); // Set PB5 (pin D13) as output PORTB |= (1 << PB5); // Set PB5 (pin D13) HIGH PORTB &= ~(1 << PB5); // Set PB5 (pin D13) LOW PORTB ^= (1 << PB5); // Toggle PB5 (pin D13) .. code:: asm #define __SFR_OFFSET 0 #include "avr/io.h" ; Include all those DDRB and PORTB and PINB and ... sbi DDRB, 5 ; Set PB5 as output sbi PORTB, 5 ; Set PB5 (pin D13) HIGH (set bit 5 of PORTB) cbi PORTB, 5 ; Set PB5 (pin D13) LOW (clear bit 5 of PORTB) sbi PINB, 5 ; Toggle PB5 Reset ----- .. code:: void(* resetFunc) (void) = 0; F() a __FlashStringHelper -------------------------- .. code:: void neco(const __FlashStringHelper *pstr ) { const char *p = reinterpret_cast(pstr); while (unsigned char c = pgm_read_byte(p++)) { switch (c) {