Senior Member
Регистрация: 14.06.2015
Адрес: СССР
Сообщений: 122
Вес репутации: 0
|
Re: Ускоряем работу Arduino
Всем привет, и огромное спасибо за библиотеку, как автору, так и тем кто тут принимал активное участие в тестировании и доработках.
Делал себе очень похожий вариант для Меги 2560. Только ещё вспоминаю С/С++ после 17-и летнего перерыва, так что "сильно не пинайте". Полностью всё, ещё не проверял, но может что-то сгодится для Cyberlib:
заголовки. Файл Arduino.h:
PHP код:
/**
* For smaller program to Arduino IDE
*
* @author Arhat109 at june 2015. [email protected]
*/
#ifdef _ARHAT_
# include "arhat.h"
#else
# include "Arduino_orig.h"
#endif
Оригинал переименован для того, чтобы втыкая в скетч строку
переключать компиляцию скетча с типового файла на этот:
PHP код:
/**
* Main header file for small sketchs by Arduino IDE
* version 0.1 for ATmega2560 only!
*
* @author Arhat109-20150604(started). [email protected]
* @license:
* 1. This is a free software for any using and distributing without any warranties.
* 2. You should keep author tag with any changes. May be with adding.
*
* This is free software, not any pay.
* But you may donate some money to phone +7-951-388-2793
*
* Thanks:
* More parts was taked from this authors:
*
* authors Arduino.h, avr/io.h and other distribs files.
* authors this document: http://www.nongnu.org/avr-libc/user-manual/index.html
* leshak, gregoryl -- @see arduino(dot)ru /forum/programmirovanie/eshche-raz-migaem-svetodiodom-bez-delay post #47 and above.
*/
#ifndef _ARHAT_H_
#define _ARHAT_H_
#include <avr/pgmspace.h>
/* pgmspace.h are defining macros for use programm memory for data storage. Autoincluding:
<inttypes.h> -- constants for print any integer formats
<stdint.h> -- .. .. redefining standart int types and max int constants
<stddef.h> -- standart definitions
<avr/io.h> -- io.h auto including:
<avr/sfr_defs.h> -- "Special Function Register" _SFR_xxx()
<inttypes.h>
<stdint.h>
<avr/io***.h> -- special defines for the AVR-processor: ramend, fuse ... etc.
<avr/io***.h> -- .. may be additional defines (x[A..L],n[0..7]): DDRxn,DDxn, PORTxn,Pxn, PINx,PINxn, .. see datasheet terms.
<avr/portpins.h> -- additional pin primitives: DDn,PORTn,PINn and redefines above.
<avr/common.h> -- additional SP,SREG,IND,X,Y,Z,RAMPZ .. etc. defines.
<avr/sfr_defs.h>
<inttypes.h>
<stdint.h>
<avr/version.h> -- only version for avr-gcc compiler
<avr/fuse.h> -- only typedef for fuse structs
<avr/lock.h> -- only lockmem registers and bits
*/
#include <avr/interrupt.h>
/* sei(),cli(),ISR() and so on. Including <avr/io.h> too. */
#ifdef __cplusplus
extern "C"{
#endif
/* ============= main defines for Arduino ============ */
#define SET_MASK_0 (uint8_t)1
#define SET_MASK_1 (uint8_t)2
#define SET_MASK_2 (uint8_t)4
#define SET_MASK_3 (uint8_t)8
#define SET_MASK_4 (uint8_t)16
#define SET_MASK_5 (uint8_t)32
#define SET_MASK_6 (uint8_t)64
#define SET_MASK_7 (uint8_t)128
#define CLR_MASK_0 (uint8_t)254
#define CLR_MASK_1 (uint8_t)253
#define CLR_MASK_2 (uint8_t)251
#define CLR_MASK_3 (uint8_t)247
#define CLR_MASK_4 (uint8_t)239
#define CLR_MASK_5 (uint8_t)223
#define CLR_MASK_6 (uint8_t)191
#define CLR_MASK_7 (uint8_t)127
/** timer interrupt names and flag bits */
#define TIF_OVF (uint8_t)1
#define TIF_COMPA (uint8_t)2
#define TIF_COMPB (uint8_t)4
#define TIF_COMPC (uint8_t)8
#define TIF_CAPT (uint8_t)32
#define TCLK_0 0 // timer is stopped.
#define TCLK_1 1
#define TCLK_8 2
#define TCLK_64 3
#define TCLK_256 4
#define TCLK_1024 5
#define TCLK_IN_EDGE 6
#define TLCK_IN_FRONT 7
#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2
#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
#define TWO_PI 6.283185307179586476925286766559
#define DEG_TO_RAD 0.017453292519943295769236907684886
#define RAD_TO_DEG 57.295779513082320876798154814105
#define EULER 2.718281828459045235360287471352
/* sound speed as 33500 sm./sec., 2-way: microsecs. to santimeters! */
#define SND_TO_SM 59.701492537
/* ============= specific definitions for smaller code ============ */
#ifdef __AVR_ATmega2560__
# include "arhat_pins2560.h" /** special macros for each pin Arduino Mega board */
#endif
/* private level 2. NOT USE IT in sketches !!! */
#define pinDirReg(p) (DREG##p) /** get DDRx direction register macros for pin */
#define pinOutReg(p) (OREG##p) /** get PORTx output register macros for pin */
#define pinInReg(p) (IREG##p) /** get PINx input register macros for pin */
#define pinSetMask(p) (BSET##p) /** get bit mask to set pin into HIGH */
#define pinClearMask(p) (BCLR##p) /** get bit mask to LOW pin */
#define _timerCount(p) (TCNT##p)
#define _timerControl(p,r) (TCCR##p##r)
#define _timerCompare(p,r) (OCCR##p##A)
#define _timerCapture(p) (ICR##p)
#define _timerIMask(t,v,b) ((b)? (TIMSK##t |= (TIF_##v)) : (TIMSK##t &= ~(TIF_##v)))
#define _timerIFlag(t,v) (TIFR##t & (TIF_##v))
#define _ISRtimer(t,v) (TIMER##t##_##v##_vect)
#define _prescalerMode(p) (TCLK_##p)
#define _setPWM(p) setPWM##p
#define _analogWrite(p,v) outPWM##p(v)
#define _ISRusart(t,v) (USART##t##_##v##_vect) /** t:[0,1,2,3] v:[RX,TX,UDRE]*/
/* end private macros. Public section: macros for use with constants pin numbers only! */
/* =================================================================================== */
/* may use with standart macros for registers and bits names from <avr/io.h> */
#ifndef cbi
# define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
# define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define pinModeOut(p) (pinDirReg(p) |= pinSetMask(p)) /** set pin to out */
#define pinModeIn(p) (pinDirReg(p) &= pinClearMask(p)) /** set pin to input */
//#define pinMode(p,m) ((m)==OUTPUT? pinModeOut(p) : pinModeIn(p))
#define pinOutHigh(p) (pinOutReg(p) |= pinSetMask(p)) /** out 1 (HIGH) to pin */
#define pinOutLow(p) (pinOutReg(p) &= pinClearMask(p)) /** out 0 (LOW) to pin */
#define pinOut(p,v) ((v)? pinOutHigh(p) : pinOutLow(p)) /** see digitalWrite() */
#define pinRead(p) ((*(uint8_t *)pinInReg(p)) & pinSetMask(p)) /** see digitalRead() */
#define timerCount(p) _timerCount(p) /** get/set TCNT register 8/16bit p:[0,2,[1,3,4,5]] */
#define timerControl(p,r) _timerControl(p,r) /** get/set control registers: p:[0,2,[1,3,4,5]], r:[A,B[,C]] */
#define timerCompare(p,r) _timerCompare(p,r) /** get/set compare registers: p:[0,2,[1,3,4,5]], r:[A,B[,C]] */
#define timerCapture(p) _timerCapture(p) /** get/set capture registers: p:[1,3,4,5] */
#define timerIMask(t,v,b) _timerIMask(t,v,b) /** timer mask register for interrupt b==1: set | b==0: clear */
#define timerIFlag(t,v) _timerIFlag(t,v) /** get interrupt flag t:[0,2[,1,3,4,5]] v:[OVF,COMPA,COMPB[,COMPC,CAPT]]*/
#define ISRtimer(t,v) _ISRtimer(t,v) /** make interrupt vector name */
#define prescalerMode(p) _prescalerMode(p) /** return mode for prescaler */
#define setPWM(p) _setPWM(p) /** to setup() set pin to PWM mode */
#define analogWrite(p,v) _analogWrite(p,v) /** out PWM [0..255] with 250kHz */
/* ============= See arhat_time.c (wiring_digital.c) ============ */
void turnOffPWM(uint8_t timer);
void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t val);
//int digitalRead(uint8_t pin);
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); /** WARNING: !compiler depends method! */
/* ============= time and delay. See arhat_time.c (wiring.c) ============ */
void time_init(void); // init timer with TIME_DEFAULT section in arhat_time.c
unsigned long micros(void); // microseconds upto 1.19 hour
unsigned long millis(void); // milliseconds upto 49.7 days WARNING: wiring.c: !not corrected for current microtime
void delay(unsigned long); // delay in milliseconds
/* ============= wait without delay ============ */
#define FIRST_STOP 0
#define FIRST_START 1
/**
* @param uint32_t interval; [milliseconds]
* @param EveryWorker action;
* @param int onStart; [==0: skip action at first time]
*/
#define everyMillis(interval, action) \
{ \
static unsigned long t = 0UL; \
if( millis() - t > (interval) ) \
{ \
{ action; } \
t = millis(); \
} \
}
/* ============= other interest definition ============ */
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
void setup(void);
void loop(void);
/* ============= END ============ */
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* ifdef _ARHAT_H_ */
Вся настройка ног Меги, чтобы можно было обращаться по номеру пина вынесена сюда:
PHP код:
/**
* pins definition for ATmega2560 for use in macros "Arhat.h"
*
* @author Arhat109-20150604. [email protected]
* @license:
* 1. This is a free software for any using and distributing without any warranties.
* 2. You should keep author tag with any changes. May be with adding.
*/
#ifdef __AVR_ATmega2560__
#include <stdint.h>
#define ARDUINO_MEGA_PINS 70
#define NUM_DIGITAL_PINS ARDUINO_MEGA_PINS
#define MAX_ANALOG_INPUTS 16
#define analogInputToDigitalPin(p) ((p < 16) ? (p) + 54 : -1)
#define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 13) || ((p) >= 44 && (p)<= 46))
/** pin number at Arduino Mega board */
/** second and other function on this Arduino pin */
#define pinLed 13
#define pin0 0
#define pin1 1
#define pin2 2
#define pin3 3
#define pin4 4
#define pin5 5
#define pin6 6
#define pin7 7
#define pin8 8
#define pin9 9
#define pin10 10
#define pin11 11
#define pin12 12
#define pin13 13
#define pin14 14
#define pin15 15
#define pin16 16
#define pin17 17
#define pin18 18
#define pin19 19
#define pin20 20
#define pin21 21
#define pin22 22
#define pin23 23
#define pin24 24
#define pin25 25
#define pin26 26
#define pin27 27
#define pin28 28
#define pin29 29
#define pin30 30
#define pin31 31
#define pin32 32
#define pin33 33
#define pin34 34
#define pin35 35
#define pin36 36
#define pin37 37
#define pin38 38
#define pin39 39
#define pin40 40
#define pin41 41
#define pin42 42
#define pin43 43
#define pin44 44
#define pin45 45
#define pin46 46
#define pin47 47
#define pin48 48
#define pin49 49
#define pin50 50
#define pin51 51
#define pin52 52
#define pin53 53
#define pin54 54
#define pin55 55
#define pin56 56
#define pin57 57
#define pin58 58
#define pin59 59
#define pin60 60
#define pin61 61
#define pin62 62
#define T0_IN 38
#define Tout_0A_1C 13
#define T0outA 13
#define T0outB 4
#define T2outA 10
#define T2outB 9
#define T1outA 11
#define T1outB 12
#define T1outC 13
#define T3outA 5
#define T3outB 2
#define T3outC 3
#define T4_ICP
#define T4outA 6
#define T4outB 7
#define T4outC 8
#define T5_IN 47
#define T5_ICP 48
#define T5outA 46
#define T5outC 44
#define T5outB 45
#define setPWM2 { TCCR3A |= 33; TCCR3B |= 3; } /* A: COM32B=normal, 8-bit; B: prescaler=64 */
#define setPWM3 { TCCR3A |= 9; TCCR3B |= 3; } /* A: COM32C=normal, 8-bit; B: prescaler=64 */
#define setPWM4 { TCCR0A |= 32; } /* A: COM02B=normal, not set: use for time functions! */
#define setPWM5 { TCCR3A |= 129; TCCR3B |= 3; } /* A: COM32A=normal, 8-bit; B: prescaler=64 */
#define setPWM6 { TCCR4A |= 129; TCCR3B |= 3; } /* A: COM42A=normal, 8-bit; B: prescaler=64 */
#define setPWM7 { TCCR4A |= 33; TCCR3B |= 3; } /* A: COM42B=normal, 8-bit; B: prescaler=64 */
#define setPWM8 { TCCR4A |= 9; TCCR3B |= 3; } /* A: COM42C=normal, 8-bit; B: prescaler=64 */
#define setPWM9 { TCCR2A |= 35; TCCR3B |= 3; } /* A: COM22B=normal, fastPWM; B: prescaler=64 */
#define setPWM10 { TCCR2A |= 131; TCCR3B |= 3; } /* A: COM22A=normal, fastPWM; B: prescaler=64 */
#define setPWM11 { TCCR1A |= 129; TCCR3B |= 3; } /* A: COM12A=normal, 8-bit; B: prescaler=64 */
#define setPWM12 { TCCR1A |= 33; TCCR3B |= 3; } /* A: COM12B=normal, 8-bit; B: prescaler=64 */
#define setPWM13 { TCCR0A |= 128; } /* A: COM02A=normal, not set other! */
#define setPWM13a { TCCR1A |= 9; TCCR3B |= 3; } /* A: COM12C=normal, 8-bit; B: prescaler=64 */
#define setPWM44 { TCCR5A |= 9; TCCR3B |= 3; } /* A: COM52C=normal, 8-bit; B: prescaler=64 */
#define setPWM45 { TCCR5A |= 33; TCCR3B |= 3; } /* A: COM52B=normal, 8-bit; B: prescaler=64 */
#define setPWM46 { TCCR5A |= 128; TCCR3B |= 3; } /* A: COM52A=normal, 8-bit; B: prescaler=64 */
#define outPWM2(v) { OCR3BH = (unsigned char)(v>>8); OCR3BL = (unsigned char)(v); }
#define outPWM3(v) { OCR3CH = (unsigned char)(v>>8); OCR3CL = (unsigned char)(v); }
#define outPWM4(v) OCR0B /*8 bit */
#define outPWM5(v) { OCR3AH = (unsigned char)(v>>8); OCR3AL = (unsigned char)(v); }
#define outPWM6(v) { OCR4AH = (unsigned char)(v>>8); OCR4AL = (unsigned char)(v); }
#define outPWM7(v) { OCR4BH = (unsigned char)(v>>8); OCR4BL = (unsigned char)(v); }
#define outPWM8(v) { OCR4CH = (unsigned char)(v>>8); OCR4CL = (unsigned char)(v); }
#define outPWM9(v) OCR2B /*8 bit */
#define outPWM10(v) OCR2A /*8 bit */
#define outPWM11(v) { OCR1AH = (unsigned char)(v>>8); OCR1AL = (unsigned char)(v); }
#define outPWM12(v) { OCR1BH = (unsigned char)(v>>8); OCR1BL = (unsigned char)(v); }
#define outPWM13(v) OCR0A /* 8+16 bit!*/
#define outPWM13a(v) { OCR1C /* 8+16 bit!*/
#define outPWM44(v) { OCR5CH = (unsigned char)(v>>8); OCR5CL = (unsigned char)(v); }
#define outPWM45(v) { OCR5BH = (unsigned char)(v>>8); OCR5BL = (unsigned char)(v); }
#define outPWM46(v) { OCR5AH = (unsigned char)(v>>8); OCR5AL = (unsigned char)(v); }
#define pinPWM2 T3outB
#define pinPWM3 T3outC
#define pinPWM4 T0outB /*8 bit */
#define pinPWM5 T3outA
#define pinPWM6 T4outA
#define pinPWM7 T4outB
#define pinPWM8 T4outC
#define pinPWM9 T2outB /*8 bit */
#define pinPWM10 T2outA /*8 bit */
#define pinPWM11 T1outA
#define pinPWM12 T1outB
#define pinPWM13 T1out_0A_1C /* 8+16 bit!*/
#define pinPWM44 T5outC
#define pinPWM45 T5outB
#define pinPWM46 T4outA
#define Analog0 54
#define Analog1 55
#define Analog2 56
#define Analog3 57
#define Analog4 58
#define Analog5 59
#define Analog6 60
#define Analog7 61
#define Analog8 62
#define Analog9 63
#define Analog10 64
#define Analog11 65
#define Analog12 66
#define Analog13 67
#define Analog14 68
#define Analog15 69
#define BUS_AD0 22
#define BUS_AD1 23
#define BUS_AD2 24
#define BUS_AD3 25
#define BUS_AD4 26
#define BUS_AD5 27
#define BUS_AD6 28
#define BUS_AD7 29
#define BUS_A8 37
#define BUS_A9 36
#define BUS_A10 35
#define BUS_A11 34
#define BUS_A12 33
#define BUS_A13 32
#define BUS_A14 31
#define BUS_A15 30
#define BUS_ALE 39
#define BUS_RD 40
#define BUS_WR 41
#define USART0_RX 0
#define USART0_TX 1
#define USART1_RX 19
#define USART1_TX 18
#define USART2_RX 17
#define USART2_TX 16
#define USART3_RX 15
#define USART3_TX 14
#define I2C_SDA 20
#define I2C_SCL 21
#define SPI_MISO 50
#define SPI_MOSI 51
#define SPI_SCK 52
#define SPI_SS 53
#define JTAG_TDI 61
#define JTAG_TDO 60
#define JTAG_TMS 59
#define JTAG_TCK 58
#define AIN1 5
#define INT_0 21
#define INT_1 20
#define INT_2 19
#define INT_3 18
#define INT_4 2
#define INT_5 3
#define PC_INT_0 53
#define PC_INT_1 52
#define PC_INT_2 51
#define PC_INT_3 50
#define PC_INT_4 10
#define PC_INT_5 11
#define PC_INT_6 12
#define PC_INT_7 13
#define PC_INT_8 0
#define PC_INT_9 15
#define PC_INT_10 14
#define PC_INT_16 62
#define PC_INT_17 63
#define PC_INT_18 64
#define PC_INT_19 65
#define PC_INT_20 66
#define PC_INT_21 67
#define PC_INT_22 68
#define PC_INT_23 69
/** Special reg and bit macros for autoconverting int pin number into {REG,BIT} */
#define DREG22 DDRA
#define OREG22 PORTA
#define IREG22 PINA
#define BSET22 SET_MASK_0
#define BCLR22 CLR_MASK_0
#define DREG23 DDRA
#define OREG23 PORTA
#define IREG23 PINA
#define BSET23 SET_MASK_1
#define BCLR23 CLR_MASK_1
#define DREG24 DDRA
#define OREG24 PORTA
#define IREG24 PINA
#define BSET24 SET_MASK_2
#define BCLR24 CLR_MASK_2
#define DREG25 DDRA
#define OREG25 PORTA
#define IREG25 PINA
#define BSET25 SET_MASK_3
#define BCLR25 CLR_MASK_3
#define DREG26 DDRA
#define OREG26 PORTA
#define IREG26 PINA
#define BSET26 SET_MASK_4
#define BCLR26 CLR_MASK_4
#define DREG27 DDRA
#define OREG27 PORTA
#define IREG27 PINA
#define BSET27 SET_MASK_5
#define BCLR27 CLR_MASK_5
#define DREG28 DDRA
#define OREG28 PORTA
#define IREG28 PINA
#define BSET28 SET_MASK_6
#define BCLR28 CLR_MASK_6
#define DREG29 DDRA
#define OREG29 PORTA
#define IREG29 PINA
#define BSET29 SET_MASK_7
#define BCLR29 CLR_MASK_7
#define DREG53 DDRB
#define OREG53 PORTB
#define IREG53 PINB
#define BSET53 SET_MASK_0
#define BCLR53 CLR_MASK_0
#define DREG52 DDRB
#define OREG52 PORTB
#define IREG52 PINB
#define BSET52 SET_MASK_1
#define BCLR52 CLR_MASK_1
#define DREG51 DDRB
#define OREG51 PORTB
#define IREG51 PINB
#define BSET51 SET_MASK_2
#define BCLR51 CLR_MASK_2
#define DREG50 DDRB
#define OREG50 PORTB
#define IREG50 PINB
#define BSET50 SET_MASK_3
#define BCLR50 CLR_MASK_3
#define DREG10 DDRB
#define OREG10 PORTB
#define IREG10 PINB
#define BSET10 SET_MASK_4
#define BCLR10 CLR_MASK_4
#define DREG11 DDRB
#define OREG11 PORTB
#define IREG11 PINB
#define BSET11 SET_MASK_5
#define BCLR11 CLR_MASK_5
#define DREG12 DDRB
#define OREG12 PORTB
#define IREG12 PINB
#define BSET12 SET_MASK_6
#define BCLR12 CLR_MASK_6
#define DREG13 DDRB
#define OREG13 PORTB
#define IREG13 PINB
#define BSET13 SET_MASK_7
#define BCLR13 CLR_MASK_7
#define DREG37 DDRC
#define OREG37 PORTC
#define IREG37 PINC
#define BSET37 SET_MASK_0
#define BCLR37 CLR_MASK_0
#define DREG36 DDRC
#define OREG36 PORTC
#define IREG36 PINC
#define BSET36 SET_MASK_1
#define BCLR36 CLR_MASK_1
#define DREG35 DDRC
#define OREG35 PORTC
#define IREG35 PINC
#define BSET35 SET_MASK_2
#define BCLR35 CLR_MASK_2
#define DREG34 DDRC
#define OREG34 PORTC
#define IREG34 PINC
#define BSET34 SET_MASK_3
#define BCLR34 CLR_MASK_3
#define DREG33 DDRC
#define OREG33 PORTC
#define IREG33 PINC
#define BSET33 SET_MASK_4
#define BCLR33 CLR_MASK_4
#define DREG32 DDRC
#define OREG32 PORTC
#define IREG32 PINC
#define BSET32 SET_MASK_5
#define BCLR32 CLR_MASK_5
#define DREG31 DDRC
#define OREG31 PORTC
#define IREG31 PINC
#define BSET31 SET_MASK_6
#define BCLR31 CLR_MASK_6
#define DREG30 DDRC
#define OREG30 PORTC
#define IREG30 PINC
#define BSET30 SET_MASK_7
#define BCLR30 CLR_MASK_7
#define DREG21 DDRD
#define OREG21 PORTD
#define IREG21 PIND
#define BSET21 SET_MASK_0
#define BCLR21 CLR_MASK_0
#define DREG20 DDRD
#define OREG20 PORTD
#define IREG20 PIND
#define BSET20 SET_MASK_1
#define BCLR20 CLR_MASK_1
#define DREG19 DDRD
#define OREG19 PORTD
#define IREG19 PIND
#define BSET19 SET_MASK_2
#define BCLR19 CLR_MASK_2
#define DREG18 DDRD
#define OREG18 PORTD
#define IREG18 PIND
#define BSET18 SET_MASK_3
#define BCLR18 CLR_MASK_3
#define DREG38 DDRD
#define OREG38 PORTD
#define IREG38 PIND
#define BSET38 SET_MASK_7
#define BCLR38 CLR_MASK_7
#define DREG0 DDRE
#define OREG0 PORTE
#define IREG0 PINE
#define BSET0 SET_MASK_0
#define BCLR0 CLR_MASK_0
#define DREG1 DDRE
#define OREG1 PORTE
#define IREG1 PINE
#define BSET1 SET_MASK_1
#define BCLR1 CLR_MASK_1
#define DREG5 DDRE
#define OREG5 PORTE
#define IREG5 PINE
#define BSET5 SET_MASK_3
#define BCLR5 CLR_MASK_3
#define DREG2 DDRE
#define OREG2 PORTE
#define IREG2 PINE
#define BSET2 SET_MASK_4
#define BCLR2 CLR_MASK_4
#define DREG3 DDRE
#define OREG3 PORTE
#define IREG3 PINE
#define BSET3 SET_MASK_5
#define BCLR3 CLR_MASK_5
#define DREG54 DDRF
#define OREG54 PORTF
#define IREG54 PINF
#define BSET54 SET_MASK_0
#define BCLR54 CLR_MASK_0
#define DREG55 DDRF
#define OREG55 PORTF
#define IREG55 PINF
#define BSET55 SET_MASK_1
#define BCLR55 CLR_MASK_1
#define DREG56 DDRF
#define OREG56 PORTF
#define IREG56 PINF
#define BSET56 SET_MASK_2
#define BCLR56 CLR_MASK_2
#define DREG57 DDRF
#define OREG57 PORTF
#define IREG57 PINF
#define BSET57 SET_MASK_3
#define BCLR57 CLR_MASK_3
#define DREG58 DDRF
#define OREG58 PORTF
#define IREG58 PINF
#define BSET58 SET_MASK_4
#define BCLR58 CLR_MASK_4
#define DREG59 DDRF
#define OREG59 PORTF
#define IREG59 PINF
#define BSET59 SET_MASK_5
#define BCLR59 CLR_MASK_5
#define DREG60 DDRF
#define OREG60 PORTF
#define IREG60 PINF
#define BSET60 SET_MASK_6
#define BCLR60 CLR_MASK_6
#define DREG61 DDRF
#define OREG61 PORTF
#define IREG61 PINF
#define BSET61 SET_MASK_7
#define BCLR61 CLR_MASK_7
#define DREG41 DDRG
#define OREG41 PORTG
#define IREG41 PING
#define BSET41 SET_MASK_0
#define BCLR41 CLR_MASK_0
#define DREG40 DDRG
#define OREG40 PORTG
#define IREG40 PING
#define BSET40 SET_MASK_1
#define BCLR40 CLR_MASK_1
#define DREG39 DDRG
#define OREG39 PORTG
#define IREG39 PING
#define BSET39 SET_MASK_2
#define BCLR39 CLR_MASK_2
#define DREG39 DDRG
#define OREG39 PORTG
#define IREG39 PING
#define BSET39 SET_MASK_5
#define BCLR39 CLR_MASK_5
#define DREG17 DDRH
#define OREG17 PORTH
#define IREG17 PINH
#define BSET17 SET_MASK_0
#define BCLR17 CLR_MASK_0
#define DREG16 DDRH
#define OREG16 PORTH
#define IREG16 PINH
#define BSET16 SET_MASK_1
#define BCLR16 CLR_MASK_1
#define DREG6 DDRH
#define OREG6 PORTH
#define IREG6 PINH
#define BSET6 SET_MASK_3
#define BCLR6 CLR_MASK_3
#define DREG7 DDRH
#define OREG7 PORTH
#define IREG7 PINH
#define BSET7 SET_MASK_4
#define BCLR7 CLR_MASK_4
#define DREG8 DDRH
#define OREG8 PORTH
#define IREG8 PINH
#define BSET8 SET_MASK_5
#define BCLR8 CLR_MASK_5
#define DREG9 DDRH
#define OREG9 PORTH
#define IREG9 PINH
#define BSET9 SET_MASK_6
#define BCLR9 CLR_MASK_6
#define DREG15 DDRJ
#define OREG15 PORTJ
#define IREG15 PINJ
#define BSET15 SET_MASK_0
#define BCLR15 CLR_MASK_0
#define DREG14 DDRJ
#define OREG14 PORTJ
#define IREG14 PINJ
#define BSET14 SET_MASK_1
#define BCLR14 CLR_MASK_1
#define DREG49 DDRL
#define OREG49 PORTL
#define IREG49 PINL
#define BSET49 SET_MASK_0
#define BCLR49 CLR_MASK_0
#define DREG48 DDRL
#define OREG48 PORTL
#define IREG48 PINL
#define BSET48 SET_MASK_1
#define BCLR48 CLR_MASK_1
#define DREG47 DDRL
#define OREG47 PORTL
#define IREG47 PINL
#define BSET47 SET_MASK_2
#define BCLR47 CLR_MASK_2
#define DREG46 DDRL
#define OREG46 PORTL
#define IREG46 PINL
#define BSET46 SET_MASK_3
#define BCLR46 CLR_MASK_3
#define DREG45 DDRL
#define OREG45 PORTL
#define IREG45 PINL
#define BSET45 SET_MASK_4
#define BCLR45 CLR_MASK_4
#define DREG44 DDRL
#define OREG44 PORTL
#define IREG44 PINL
#define BSET44 SET_MASK_5
#define BCLR44 CLR_MASK_5
#define DREG43 DDRL
#define OREG43 PORTL
#define IREG43 PINL
#define BSET43 SET_MASK_6
#define BCLR43 CLR_MASK_6
#define DREG42 DDRL
#define OREG42 PORTL
#define IREG42 PINL
#define BSET42 SET_MASK_7
#define BCLR42 CLR_MASK_7
#define DREG62 DDRK
#define OREG62 PORTK
#define IREG62 PINK
#define BSET62 SET_MASK_0
#define BCLR62 CLR_MASK_0
#define DREG63 DDRK
#define OREG63 PORTK
#define IREG63 PINK
#define BSET63 SET_MASK_1
#define BCLR63 CLR_MASK_1
#define DREG64 DDRK
#define OREG64 PORTK
#define IREG64 PINK
#define BSET64 SET_MASK_2
#define BCLR64 CLR_MASK_2
#define DREG65 DDRK
#define OREG65 PORTK
#define IREG65 PINK
#define BSET65 SET_MASK_3
#define BCLR65 CLR_MASK_3
#define DREG66 DDRK
#define OREG66 PORTK
#define IREG66 PINK
#define BSET66 SET_MASK_4
#define BCLR66 CLR_MASK_4
#define DREG67 DDRK
#define OREG67 PORTK
#define IREG67 PINK
#define BSET67 SET_MASK_5
#define BCLR67 CLR_MASK_5
#define DREG68 DDRK
#define OREG68 PORTK
#define IREG68 PINK
#define BSET68 SET_MASK_6
#define BCLR68 CLR_MASK_6
#define DREG66 DDRK
#define OREG66 PORTK
#define IREG66 PINK
#define BSET66 SET_MASK_7
#define BCLR66 CLR_MASK_7
/* not used in Arduino not a pin!
* If you have this pin on your board add it numbers in this correctly! */
/*
#define pin pinD4
#define T1_ICP pinD4
#define DREG DDRD
#define OREG PORTD
#define IREG PIND
#define BSET SET_MASK_4
#define BCLR CLR_MASK_4
#define pin pinD5
#define USART1_Xck pinD5
#define DREG DDRD
#define OREG PORTD
#define IREG PIND
#define BSET SET_MASK_5
#define BCLR CLR_MASK_5
#define pin pinD6
#define T1_IN pinD6
#define DREG DDRD
#define OREG PORTD
#define IREG PIND
#define BSET SET_MASK_6
#define BCLR CLR_MASK_6
#define pin pinE2
#define USART0_Xck pinE2
#define DREG DDRE
#define OREG PORTE
#define IREG PINE
#define BSET SET_MASK_2
#define BCLR CLR_MASK_2
#define pin pinE6
#define T3_IN pinE6
#define INT_6 pinE6
#define DREG DDRE
#define OREG PORTE
#define IREG PINE
#define BSET SET_MASK_6
#define BCLR CLR_MASK_6
#define pin pinE7
#define T3_ICP pinE7
#define INT_7 pinE7
#define DREG DDRE
#define OREG PORTE
#define IREG PINE
#define BSET SET_MASK_7
#define BCLR CLR_MASK_7
#define pin pinG3
#define Tosc1 pinG3
#define DREG DDRG
#define OREG PORTG
#define IREG PING
#define BSET SET_MASK_3
#define BCLR CLR_MASK_3
#define pin pinG4
#define Tosc2 pinG4
#define DREG DDRG
#define OREG PORTG
#define IREG PING
#define BSET SET_MASK_4
#define BCLR CLR_MASK_4
*
* "G6,G7" pin/reg group has NO any pin number or functions!
*
#define pin pinH2
#define USART2_Xck pinH2
#define DREG DDRH
#define OREG PORTH
#define IREG PINH
#define BSET SET_MASK_2
#define BCLR CLR_MASK_2
#define pin pinH7
#define T4_IN pinH7
#define DREG DDRH
#define OREG PORTH
#define IREG PINH
#define BSET SET_MASK_7
#define BCLR CLR_MASK_7
#define pin pinJ2
#define USART3_Xck pinJ2
#define PC_INT_11 pinJ2
#define DREG DDRJ
#define OREG PORTJ
#define IREG PINJ
#define BSET SET_MASK_2
#define BCLR CLR_MASK_2
#define pin pinJ3
#define PC_INT_12 pinJ3
#define DREG DDRJ
#define OREG PORTJ
#define IREG PINJ
#define BSET SET_MASK_3
#define BCLR CLR_MASK_3
#define pin pinJ4
#define PC_INT_13 pinJ4
#define DREG DDRJ
#define OREG PORTJ
#define IREG PINJ
#define BSET SET_MASK_4
#define BCLR CLR_MASK_4
#define pin pinJ5
#define PC_INT_14 pinJ5
#define DREG DDRJ
#define OREG PORTJ
#define IREG PINJ
#define BSET SET_MASK_5
#define BCLR CLR_MASK_5
#define pin pinJ6
#define PC_INT_15 pinJ6
#define DREG DDRJ
#define OREG PORTJ
#define IREG PINJ
#define BSET SET_MASK_6
#define BCLR CLR_MASK_6
#define pin pinJ7
#define DREG DDRJ
#define OREG PORTJ
#define IREG PINJ
#define BSET SET_MASK_7
#define BCLR CLR_MASK_7
*/
#endif /* __AVR_ATmega2560__ */
Ну и моя реализация wiring.c (таймер времени и функции задержек). Поскольку пока ещё не силен, часть выдрал из типового wiring.c (отмечены).
PHP код:
/**
* Simple function for calc time and delays.
*
* use timer0 as default and prescaler 1/64 for each 4 microseconds with 16Mhz CLK.
* By default timer0 is overflowing each 256 ticks at 1024 microseconds.
*
* For use other timer you need predefine all constants in block TIME_DEFAULT
* and recompile this file.
*
* For simple use, you may rename this file to wiring.c and replace it.
*
* @author Arhat109: [email protected]
*
* license agreement:
* You use this software on own your risks. No claims will be accepted.
* You may use this file any way, but cannot change
* or delete tag @author above (you may append your tag @author) and must keeping this rows:
*
* This is free software, not any pay. But you may donate some money to phone +7-951-388-2793
*/
#include "arhat.h"
#ifdef __AVR_ATmega2560__
# include "arhat_pins2560.h"
#else
# error "This may be not work on your board!"
#endif
#ifndef TIME_DEFAULT /* all defaults for F_CPU=16Mhz ONLY! */
# define TIME_DEFAULT 0
# define TIME_MAX_COUNTER 256 /* max conter+1 */
# define TIME_PRESCALLER 64
# define TIME_MODE 3 /* WGM10 = 1, WGM00 = 1 fast-PWM mode for timer0 */
# define TIME_TICK_MCS 4 /* 1 tick by prescaler:[0.25, 0.5, 4, 16, 64] in microseconds */
# define TIME_SHIFT 2 /* From prescaller: 1=>4, 8=>1, 64=>2, 256=>4, 1024=>6 */
# define TIME_MCS2MS 1024/1000 /* ==[16,128,1024,4096,16384]/1000 by prescaler */
# define TIME_ISR OVF /* what interrupt name using for this timer */
#endif
volatile unsigned long ovf_count = 0UL;
/**
* Timer interrupt by Overflow Flag up each (TIMER_TICK_MCS*TIMER_MAX_COUNT) microseconds.
* Simpler, faster and not use SRAM..
*/
ISR(ISRtimer(TIME_DEFAULT, TIME_ISR))
{
ovf_count++;
}
/**
* Return microseconds 0..up to 1 hours 11.4 minutes
* are identical with wiring.c
*/
unsigned long micros()
{
uint8_t oldSREG = SREG;
uint8_t timer;
uint32_t micro;
// read all into local: volatile!
cli();
micro = ovf_count;
timer = timerCount(TIME_DEFAULT);
// some bug with 255?!? Don't know, without this is not work...
if ( timerIFlag(TIME_DEFAULT,OVF) && (timer<255) ){ micro++; }
SREG = oldSREG;
if( TIME_TICK_MCS>1 ) {
return (((micro<<8)+timer)<<TIME_SHIFT)+1; // +1microsec. fot this routine
} else {
return (((micro<<8)+timer)>>TIME_SHIFT)+1;
}
}
/**
* Return current time into milliseconds upto 49.7 days
* corrected for timer counter current value and fract data
*/
unsigned long millis()
{
uint16_t timer;
uint8_t oldSREG = SREG, tov=0;
cli();
timer = timerCount(TIME_DEFAULT);
if ( timerIFlag(TIME_DEFAULT,OVF) ){ tov++; }
SREG = oldSREG;
timer = (TIME_TICK_MCS>1 ? timer<<TIME_SHIFT : timer>>TIME_SHIFT);
return (ovf_count * TIME_MCS2MS)+tov+(timer>1000? 1 : 0);
}
/**
* identical with wiring.c
*/
void delay(unsigned long interval)
{
// interval += millis();
// while( millis()<interval);
uint16_t start = (uint16_t)micros();
while (interval > 0) {
if (((uint16_t)micros() - start) >= 1000) {
interval--;
start += 1000;
}
}
}
/* getted from wiring.c */
/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
void delayMicroseconds(unsigned int us)
{
// calling avrlib's delay_us() function with low values (e.g. 1 or
// 2 microseconds) gives delays longer than desired.
//delay_us(us);
#if F_CPU >= 20000000L
// for the 20 MHz clock on rare Arduino boards
// for a one-microsecond delay, simply wait 2 cycle and return. The overhead
// of the function call yields a delay of exactly a one microsecond.
__asm__ __volatile__ (
"nop" "\n\t"
"nop"); //just waiting 2 cycle
if (--us == 0)
return;
// the following loop takes a 1/5 of a microsecond (4 cycles)
// per iteration, so execute it five times for each microsecond of
// delay requested.
us = (us<<2) + us; // x5 us
// account for the time taken in the preceeding commands.
us -= 2;
#elif F_CPU >= 16000000L
// for the 16 MHz clock on most Arduino boards
// for a one-microsecond delay, simply return. the overhead
// of the function call yields a delay of approximately 1 1/8 us.
if (--us == 0)
return;
// the following loop takes a quarter of a microsecond (4 cycles)
// per iteration, so execute it four times for each microsecond of
// delay requested.
us <<= 2;
// account for the time taken in the preceeding commands.
us -= 2;
#else
// for the 8 MHz internal clock on the ATmega168
// for a one- or two-microsecond delay, simply return. the overhead of
// the function calls takes more than two microseconds. can't just
// subtract two, since us is unsigned; we'd overflow.
if (--us == 0)
return;
if (--us == 0)
return;
// the following loop takes half of a microsecond (4 cycles)
// per iteration, so execute it twice for each microsecond of
// delay requested.
us <<= 1;
// partially compensate for the time taken by the preceeding commands.
// we can't subtract any more than this or we'd overflow w/ small delays.
us--;
#endif
// busy wait
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t" // 2 cycles
"brne 1b" : "=w" (us) : "0" (us) // 2 cycles
);
}
/**
* (re)start timer for time functions
* @see TIME_DEFAULT define section
*/
void time_init()
{
sei();
timerControl(TIME_DEFAULT, A) |= TIME_MODE;
timerControl(TIME_DEFAULT, B) |= prescalerMode(TIME_PRESCALLER);
timerIMask(TIME_DEFAULT, OVF, 1);
}
Основной недостаток, который вижу - это пересчет из тиков таймера по 1024мксек на 1000 при чтении времени. Там, насколько смотрел результат сборки подключается целочисленное деление из стандартной библиотеки. Наверное это "долго".
В общем, "на суд сообщества", ну и может будет что-то полезно. Наименования функций "придумывал сам", ещё до знакомства с Cyberlib. На этом ещё мало чего написано, можно поменять названия функций на более популярные грепом.
Идея была в том, чтобы постараться адресовать порты и биты по номеру пина платы Arduino Mega 2560. Прописаны все пины, в т.ч. сделаны заготовки под неподключенные.
|