Arduino-Lite Development Reference Manual

Please refer to the following documents for the introduction and usage of Arduino-Lite:

Arduino-Lite Lightweight AVR library(1)

Arduino-Lite Lightweight AVR library(2)

Arduino-Lite keeps most interfaces provided by Arduino. For the reference manual of these interfaces, please refer to the Doc on Arduino Official website: http://arduino.cc/en/Reference/HomePage

Besides, you can use all the functions provided by avr-gcc, e.g. _delay_ms() (make sure to  include related head files).

Index:

Functions/Macros added by Arduino-Lite

Basic IO

PWM output & control

Analog Signal Capturing(ADC)

Sleep & Delay

Interrupt handling & Control

Serial Port Communication

Debugging support

Text Formatting

1 Interfaces added by Arduino-Lite

Now we introduce the interfaces newly added by Arduino-lite and their related Arduino interfaces with the similar behavior:

Basic IO controlling

PIN_MODE(pin, mode)

Set the working mode of an AVR IO pin.

This is the high performance version of pinMode(), only ONE AVR instruction will be emitted.

pin:

The Arduino-Lite PIN number of the target digital IO pin,digital constant\Macro are accepted,variables cannot be used.

mode:

The desired working mode of the IO pin,valid input range: INPUT, OUTPUT

E.g.: Set the IO pin 9 to the OUTPUT mode:

1
2
#define PIN_ID 9
PIN_MODE( PIN_ID, OUTPUT );
DIGITAL_WRITE( pin, value )

Write the output level (high or low) to the given IO pin.

It’s the high performance version of digitalWrite(), only ONE AVR instruction will be emitted.

pin:

The Arduino-Lite PIN number of the target digital IO pin,digital constant\Macro are accepted,variables cannot be used.

value:

The output level of the IO pin,accept range: 0, 1 or LOW, HIGH

E.g.: make the IO pin 9 to output HIGH value

1
2
3
#define PIN_ID 9
PIN_MODE( PIN_ID, OUTPUT );
DIGITAL_WRITE( PIN_ID, HIGH );

DIGITAL_READ( pin )

Read the value from the given IO pin.

It’s the high performance version of digitalRead(), only ONE AVR instruction will be emitted.

pin:

The Arduino-Lite PIN number of the target digital IO pin,digital constant\Macro are accepted,variables cannot be used.

return value:

0 – the current PIN’s voltage level is low

none Zero – the current PIN’s voltage level is high

E.g.: Read the value from Pin 10

1
2
3
4
5
6
7
8
9
#define PIN_ID 10
unsigned
char
input_level;
PIN_MODE( PIN_ID, INPUT );
input_level = DIGITAL_READ( PIN_ID );
if
(input_level) {
 
PRINT(
"HIGH\n"
);
}
else
{
 
PRINT(
"LOW\n"
);
}

PWM output & control

ANALOG_WRITE( pin, pwm_value )

make the given pin to output PWM signal and set its duty cycle. PWM output will be enabled automatically.

It’s the high performance version of analogWrite(). The equivalent code is:

1
2
PWM_ENABLE(pin);
PWM_SET( pin, pwm_value )

pin:

The Arduino-lite PIN number of  the target PWM IO pin. The given IO pin should contain PWM output capability, otherwise the compilation will fail.

Digital constant\Macro are accepted,variables cannot be used.

pwmvalue

The duty cycle of the PWM signal, ranges from 0 to 255, integer constantvariable and macro can be used.

E.g.: make the PIN 9 (PB1) to output a PWM signal with duty cycle at 100:

1
2
3
#define PWM_PIN 9
PIN_MODE( PWM_PIN, OUTPUT );
ANALOG_WRITE( PWM_PIN, 100 );
PWM_SET( pin, pwm_value )

Set the duty cycle of the PWM signal on the given PIN. Only ONE AVR instruction will be emitted. The given pin should enable PWM output in advance(PWM_ENABLE)

pin:

The Arduino-lite PIN number of  the target PWM IO pin. The given IO pin should contain PWM output capability, otherwise the compilation will fail.

Digital constant\Macro are accepted,variables cannot be used.

pwmvalue

The duty cycle of the PWM signal, ranges from 0 to 255, integer constantvariable and macro can be used.

E.g.: set the PWM duty cycle to 90 on Pin 3 (PD3)

1
2
3
4
#define PWM_PIN 3
PIN_MODE( PWM_PIN, OUTPUT );
PWM_ENABLE( PWM_PIN );
PWM_SET( PWM_PIN, 90 );
PWM_SET_SAFE( pin, pwm_value )

Set the duty cycle of the PWM signal on the given PIN. Only ONE AVR instruction will be emitted.

The the pwm_value is 0,the PWM output will be disabled and the pin will be set to INPUT mode

The given pin should enable PWM output in advance(PWM_ENABLE)

pin:

The Arduino-lite PIN number of  the target PWM IO pin. The given IO pin should contain PWM output capability, otherwise the compilation will fail.

Digital constant\Macro are accepted,variables cannot be used.

pwmvalue

The duty cycle of the PWM signal, ranges from 0 to 255, integer constantvariable and macro can be used.

E.g.: Set the PWM duty cycle to the value of variable pwm_val on PIN3 (PD3)

1
2
3
4
5
#define PWM_PIN 3
unsigned
char
pwm_value = 10;
PIN_MODE( PWM_PIN, OUTPUT );
PWM_ENABLE( PWM_PIN );
PWM_SET( PWM_PIN, pwm_value );
PWM_ENABLE( pin )

enable the PWM signal output for the given PIN.

High performance, only ONE AVR instruction will be emitted.

pin:

The Arduino-lite PIN number of  the target PWM IO pin. The given IO pin should contain PWM output capability, otherwise the compilation will fail.

Digital constant\Macro are accepted,variables cannot be used.

E.g.:  Enable the PWM ouput feature on Pin 10

1
2
3
#define PWM_PIN 10
PIN_MODE( PWM_PIN, OUTPUT );
PWM_ENABLE( PWM_PIN );
PWM_DISABLE( pin )

disable the PWM signal output for the given PIN.

High performance, only ONE AVR instruction will be emitted.

pin:

The Arduino-lite PIN number of  the target PWM IO pin. The given IO pin should contain PWM output capability, otherwise the compilation will fail.

Digital constant\Macro are accepted,variables cannot be used.

E.g.: Disable the PWM output feature on PIN 10

1
2
#define PWM_PIN 10
PWM_DISABLE( PWM_PIN );

Analog Signal Capturing(ADC)

Arduino-Lite inherits the analogRead function in arduino and provides the following ADC controlling features:

enable_adc()

enabled the ADC module inside the AVR chip, this function will be invoked by default during the Arduino-Lite initialization stage.

E.g.: enable the ADC module and start ADC sampling

1
2
3
4
#define ADC_PIN 1
PIN_MODE( ADC_PIN, INPUT );
enable_adc();
unsigned
int
adc_value = analogRead(ADC_PIN);
disable_adc()

disable the ADC module inside the AVR chip. This operation can be used to save power.

Interrupt handling and control

ENABLE_INT(vector, mode)

Enable the external interrupts and set the interrupt trigger type.

Similar to the attachInterrupt() function. The difference is that the ISR() macro provided by avr-libc should be used here.

It has better performance.

vector:

The external interrupt ID, starts from 0

mode

The interrupt trigger type:

  • LOW – trigger by LOW input value
  • CHANGE – triggered when the input value has changed
  • RISING – triggered when the input has changed from LOW to HIGH
  • FALLING – triggered when the input has changed from HIGH to LOW

Enable the external interrupt #0, using the RISING trigger type. The interrupt handling routine is INT0_vect

1
2
3
4
5
6
7
8
9
10
11
12
#define EXT_INT 0
#define EXT_INT_PIN 2 //PD2
void
main()
{
 
PIN_MODE( EXT_INT_PIN, INPUT );
 
ENABLE_INT( EXT_INT, RISING );
}
ISR(INT0_vect)
{
 
PRINT(
"External Interrrupt invoked\n"
);
}
DISABLE_INT( vector )
Disable the external interrupt specified by vector. The related interrupt handling routine will no longer been invoked.

Serial Port communication

The Arduino’s Serial Class has been removed in Arduino-Lite for the performance and code size reason. Arduino-Lite provide the following functions to replace it

SERIAL_BEGIN()

Enable the serial port module and set the Baud Rate to 19200bps. The Baud Rate can be changed by defining the BAUD macro.

Similar to the arduino code Serial.begin(19200) but this macro is much faster

E.g.:  enable the serial port and make it work at 19200bps

1
2
SERIAL_BEIGN();
PRINT(
"Hello AVR\n"
);
void serial_begin(unsigned long baud)

Enable the serial port module of the AVR chip and set the baud rate. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.begin(baud) but this function has better performance

baud

the baud rate

E.g. : Enable the serial port working at 115200 bps and send out the data in the buffer array via serial port

1
2
3
serial_begin(115200);
uint8_t buffer[100];
serial_puts(buffer,
sizeof
(buffer));
uint8_t serial_available(void)

Get the unread byte count of the serial port’s receive buffer. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.available();

return value:

the remaining byte count in the receive buffer. If no more data is available, returns 0

int serial_getc(void)

Retrieve a single byte from the serial port’s receive buffer. If the receive buffer is empty, a negative value will be return. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.getc()

return value

the retrieved byte’s value will be returned. If the receive buffer is empty, a negative value will be return.

E.g.: Send back all the data received previously via the serial port

1
2
3
4
5
6
7
8
9
SERIAL_BEGIN();
while
(1)
{
 
int
current_char = serial_getc();
 
if
(current_char >= 0 )
 
{
 
serial_putc( current_char );
 
}
}
void serial_flush(void)

flush all the unread data in the serial port’s receive buffer. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.flush()

void serial_putc(uint8_t c)

Sent out a single byte via the serial port. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.putc()

E.g.: Send back all the data received previously via the serial port

1
2
3
4
5
6
7
8
9
SERIAL_BEGIN();
while
(1)
{
 
int
current_char = serial_getc();
 
if
(current_char >= 0 )
 
{
 
serial_putc( current_char );
 
}
}
void serial_puts(uint8_t *buf, size_t size)

Sent out the data in buf via the serial port. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.puts()

buf:

the pointer to the data buffer

size:

the byte count to be sent out

E.g. make the serial port work at 15200bps and sent out the data in the buffer array

1
2
3
serial_begin(115200);
uint8_t buffer[100];
serial_puts(buffer,
sizeof
(buffer));
void serial_puts(uint8_t *buf)

Sent out all the bytes inside buf until a 0 value has met. If there are many serial ports available inside the AVR chip, this function will operate on the first one.

Similar to the arduino code Serial.puts()

buf:

the pointer to the data buffer ending with 0 value

E.g. make the serial port work at 15200bps and sent out a string

1
2
serial_begin(115200);
serial_puts((uint8_t *)
"Hello World\n"
);
serial_xxxx_at() function series

These are a series of functions, like serial_getc_at(). They can be used when the AVR chip has multiple serial port. Please refer to the file net_serial.h and net_serial.cpp for detail.

Sleep & Delay

sleep(unsigned long ms)

Let the AVR chip sleep for the given period and wake up it. Make the AVR sleep will reduce power consumption.

The effect is similar to delay() function and it will save power.

ms:

the sleep time, in millisecond

E.g.: let the AVR chip sleep for 1.5 second

1
sleep(1500);
delay_alert(unsigned long ms)

Make the AVR chip delay for a while. The delay mode will be suspended to resume the previous execution when the alert() function is invoked by some interrupt handling routine.

Together with the alert(), this function can be used to implement the signal(event) mechanism

ms:

the delay time, in millisecond

E.g. : wait for 5 second, it will return early when the user press a key (using external interrupt #1)

1
2
3
4
5
6
7
8
9
10
11
12
13
void
main()
{
 
ENABLE_INT( 1, LOW );
 
PRINT(
"wait or press key...\n"
);
 
delay_alert(5000);
 
PRINT(
"exit...\n"
);
}
ISR(INT1_vect)
{
 
PRINT(
"key pressed\n"
);
 
alert();
}
alert()

Notify the delay_alert() function to suspend

Debugging support

PRINT( msg, [base] )

Send out the given string, integer or the number with specified base via serial port. The equivalent code is

1
print( msg, [base], serial_puts );

msg:

Data to be sent out, the type can be

  • string constant or variable(char *)
  • single character or byte(char)
  • integer constant or variable(int)

base:

optional parameter, when specified, the msg must be the integer or string type. Set the base of the output number format. E.g.  10- decimal, 16 – hex

E.g.: sent out a string, a decimal number and a hex number via serial port

1
2
3
4
5
6
7
8
SERIAL_BEIGN();
char
* text =
"hellow AVR\n"
;
int
number = 12;
PRINT(
"Direct Text output\n"
);
PRINT( 1234 );
PRINT( text );
PRINT( number );
PRINT( number, 16);
DBG_PRINT(msg, [base])

Send out message only in debug mode. When the _DEBUG macro has been defined, this macro is same with PRINT(). Otherwise, the macro will do nothing.

This macro can be used for debugging code.

E.g. output the debug info

1
2
#define _DEBUG
DBG_PRINT(
"program started.\n"
);

Text Formatting

print(val, [base], put_func)

Convert the number of val to text format, output data will be sent via the putc_func callback

val:

the following types are supported

  • char
  • unsigned char (uint8_t)
  • int
  • unsigned int (uint16_t)
  • long
  • unsigned long (uint32_t)
  • double

base:
optional,when specified, the val type cannot be double. Use this parameter to specify the base, decimal will be used by default.

put_func:

When the val type is char or uint8_t(single byte), the following function type should be used:

1
typedef
void
(*putc_func)( uint8_t);

For each newly generated character, the callback function putc_func will be invoked by the print function during number to text converting phase.
The function serial_putc can be used as parameter and then the print function will sent out the text via serial port.

When the val uses other types,  the following function type should be used:

1
typedef
void
(*puts_func)(uint8_t *);

The print function will invoke puts_func callback to output the val data.
The serial_puts function can be used as parameter and then the print function will output val to the serial port.

E,g,: Convert the number value into Text format and store it into the buffer array and sent it out via serial port at the same time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char
buffer[20];
uint8_t pos = 0;
void
putc_to_buffer(uint8_t c)
{
 
buffer[pos++] = c;
}
void
main()
{
 
int
number = 123456;
 
char
byte = 120;
 
print( byte, putc_to_buffer);
 
SERIAL_BEGIN();
 
print( number, serial_puts );
}
print(const char str[], puts_func)

Output the str data via invoking the puts_func callback.

str:

can be a  string variable or constant

puts_func:

Function pointer using the following prototype

1
typedef
void
(*puts_func)(uint8_t *);

print will invoke puts_func to output the generated text data
serial_puts can be used as the parameter and print will sent out the str data via serial port.

println(puts_func)

invoke puts_func callback function and output a newline (\n)

puts_func:

function pointer using the prototype:

1
typedef
void
(*puts_func)( uint8_t *);

E.g. send out a string contains a newline character via serial port

1
2
3
 
SERIAL_BEGIN();
 
println( serial_puts );
}
println(val, [base], put_func)

similar to print(val, [base], put_func), an extra newline char will be appended in the end of string

println(str, puts_func)
similar to print(str, puts_func), an extra newline char will be appended in the end of string

2 Interfaces/Classed not supported by Arduino-Lite

The following interfaces/classed isn’t provided by Arduino-Lite

TypeItemReplace item
ClassStringuse the print()/prints() function or the functions provided by libc. Simply copy the code in Arduino is OK.
Serialreplaced by serial_xxx functions
Functiontone()Simply copy the code in Arduino is OK
noTone()Simply copy the code in Arduino is OK

2 the Arduino-Lite PIN numbering and mapping

For the chips Atmega8/Atmegax8(m48, m88, m168, m328 and etc),Arduino-Lite uses an extended numbering which is compatible with Arduine. Arduino-Lite’s extended numbering make it possible to use the ADC pins as general IO (DIGITAL_WRITE/READ).

For the Attiny chips which not supported by Arduino, Arduino-Lite defined the following numbering:

Notice,only the Arduino-Lite newly added interfaces can use the Arduino-Lite extended Pin numbering

  • Atmega8/Atmegax8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//                  +-\/-+
//            PC6  1|    |28  PC5 (AI 5/*D19)
//      (D 0) PD0  2|    |27  PC4 (AI 4/*D18)
//      (D 1) PD1  3|    |26  PC3 (AI 3/*D17)
//      (D 2) PD2  4|    |25  PC2 (AI 2/*D16)
// PWM+ (D 3) PD3  5|    |24  PC1 (AI 1/*D15)
//      (D 4) PD4  6|    |23  PC0 (AI 0/*D14)
//            VCC  7|    |22  GND
//            GND  8|    |21  AREF
//     *(D20) PB6  9|    |20  AVCC
//     *(D21) PB7 10|    |19  PB5 (D 13)
// PWM+ (D 5) PD5 11|    |18  PB4 (D 12)
// PWM+ (D 6) PD6 12|    |17  PB3 (D 11) PWM
//      (D 7) PD7 13|    |16  PB2 (D 10) PWM
//      (D 8 )PB0 14|    |15  PB1 (D 9) PWM
//                  +----+
// (PWM+ indicates the additional PWM pins on the ATmega168.)
// Pins with the star(*): arduinoLite extention
  • Attiny2313
1
2
3
4
5
6
7
8
9
10
11
12
13
//Fuse setting for 2313: eFuse: 0xFF lFuse: 0xDF hFuse:0xDD
//                               +-\/-+
//                       RESET  1|    |20  VCC
//  D0               (RXD) PD0  2|    |19  PB7 (UCSK/SCL/PCINT7)    D14
//  D1               (TXD) PD1  3|    |18  PB6 (MISO/DO/PCINT6)     D13
//                 (XTAL2) PA1  4|    |17  PB5 (MOSI/DI/SDA/PCINT5) D12
//                 (XTAL1) PA0  5|    |16  PB4 (OC1B/PCINT4)        D11 PWM
//  D2    (CKOUT/XCK/INT0) PD2  6|    |15  PB3 (OC1A/PCINT3)        D10 PWM
//  D3              (INT1) PD3  7|    |14  PB2 (OC0A/PCINT2)        D9  PWM
//  D4                (T0) PD4  8|    |13  PB1 (AIN1/PCINT1)        D8
//  PWM D5       (OC0B/T1) PD5  9|    |12  PB0 (AIN0/PCINT0)        D7
//                         GND 10|    |11  PD6 (ICP)                D6
//                               +----+
  • Attiny26
1
2
3
4
5
6
7
8
9
10
11
12
//                                    +-\/-+
//  ~PWM  D0 MOSI/DI/SDA/!OC1A) PB0  1|    |20  PA0 (ADC0)               A0/D7
//  PWM D1       (MISO/DO/OC1A) PB1  2|    |19  PA1 (ADC1)               A1/D8
//  ~PWM  D2    (SCK/SCL/!OC1B) PB2  3|    |18  PA2 (ADC2)               A2/D9
//  PWM D3               (OC1B) PB3  4|    |17  PA3 (AREF)
//                              VCC  5|    |16  GND
//                              GND  6|    |15  AVCC
//  D4/A7          (ADC7/XTAL1) PB4  7|    |14  PA4 (ADC3)               A3/D10
//  D5/A8          (ADC8/XTAL2) PB5  8|    |13  PA5 (ADC4)               A4/D11
//  D6/A9        (ADC9/INT0/T0) PB6  9|    |12  PA6 (ADC5/AIN0)          A5/D12
//                (ADC10/RESET) PB7 10|    |11  PA7 (ADC6/AIN1)          A6/D13
//                                    +----+

3 Example circuits(minimal working system)

  • Atmega8 and Atmegax8(Atmega48, Atmega88, Atmega168 and etc)

The minimal working system supports serial port firmware download:

The crystal oscillator in the above figure can be omitted and the AVR’s internal RC oscillator can be used instead. The system can be burned with a STK500v1 compatible bootloader to enable the serial firmware downloading feature(using the make upload command provided by arduino-Lite)

  • Attiny26

ElecFreaks – electronic design, electronics components, development platform

VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

Related Posts

Go To Top