Blink
(So far Xduino v0.3 has only been tested on board with STM32F10ret6 mcu.)
There are a few things to keep in mind when using Xduino v0.3
Major Features:
All Arduino syntax are compatible with Xduino v0.3 (as per Arduino references and Arduino extended references page except for analogReference() command.
Serial ports
There are total of 5 serial ports so Serial1-5 commands can be used.
There is also a special serial port output command ‘printf’ this function can be used like the general C/C++ printf function on the Serial command like
Serial1.printf(“Xduino v%f
“, sickness 0.3);
Serial port buffering
Now all Serial ports supports seperate buffering for Rx and Tx. This enables the buffer size to be specified individually for each Serial port. Buffer can be disabled by setting the buffer size to 0.
This example shows how to set the Serial1 port Rx buffer size to 128bytes and disables the Tx buffering.
Serial1.RxBufferSize=128;
Serial1.TxBufferSize=0;Serial1.begin(115200);
(Note: Buffer size setting must be before Serialx.begin() call)
Math library (math.h)
Mathematical functions are included. For full information please check this math.h page.
Standard library (stdlib.h)
Standard library functions are available. For full information please check this stdlib.h page.
Other C or C++ libraries
Other C and C++ libraries can also be included, dentist extending the limitation of functionality.
Analog ports
All Analog input and Analog output channels have 12-bits data. This means that when reading analog channel by using analogRead() command the returned value is between 0 and 4095 and when writing to analog output channel one can write value 0 and 4095.
Analog input channels (ADC)
There are total of 15 analog input channels with the following channel name corresponding to the Pin on the board:
Analog input channel (ADC) | Pin on board |
0 | PA0 |
1 | PA1 |
2 | PA2 |
3 | PA3 |
4 | PA4 |
5 | PA5 |
6 | PA6 |
7 | PA7 |
8 | PB0 |
9 | PB1 |
10 | PC0 |
11 | PC1 |
12 | PC2 |
13 | PC3 |
14 | PC4 |
15 | PC5 |
Analog output channels (DAC)
There are 2 DAC channels and each correspond to a port as follow:
Analog output channel (DAC) | Pin on board |
1 | PA4 |
2 | PA5 |
AnaloglastWrite() function can be used to retrieve the last value written to the analog output (DAC) channel. For example, to get the last value written to analog output channel 2:
int lastvalue;
lastvalue=analogLastwrite(2);
Interrupts
There are total 16 interrupt channels for digital input pins, the interrupt. Each pin ‘number’ can only be assigned an interrupt. This means that interrupt cannot be assigned to PA5 and PB5 at the same time as the pin ‘number’ are the same.
(note: the interrupt handling function must appear before main() directive of the program if not declaring a function prototype)
pause(ms) and pauseMicroseconds(us)
(delay possible within interrupt routine)
Generally the use of delay functions are not possible within the interrupt handling routine and this is where pause(ms) and pauseMicroseconds(us) comes in. Just specify the number of milliseconds/microseconds to pause the program and pause will handle it even within the interrupt. Note that pause only “approximate” the time to delay.
Example: to pause for approximately 10 milliseconds
pause(10);
Important file setup
To get interrupt functions and timing function working properly use the included stm32f10x_it.c or appropriately add following lines to your stm32f10x_it.c:
void SysTickHandler(void) { SysTick_IRQ_Function(); }
void USART1_IRQHandler(void) { USART1_IRQ_Function(); }
void USART2_IRQHandler(void) { USART2_IRQ_Function(); }
void USART3_IRQHandler(void) { USART3_IRQ_Function(); }
void UART4_IRQHandler(void) { UART4_IRQ_Function(); }
void UART5_IRQHandler(void) { UART5_IRQ_Function(); }void EXTI0_IRQHandler(void) { EXTI_ALL_IRQ_Function(); }
void EXTI1_IRQHandler(void) { EXTI_ALL_IRQ_Function(); }
void EXTI2_IRQHandler(void) { EXTI_ALL_IRQ_Function(); }
void EXTI3_IRQHandler(void) { EXTI_ALL_IRQ_Function(); }
void EXTI4_IRQHandler(void) { EXTI_ALL_IRQ_Function(); }
void EXTI9_5_IRQHandler(void) { EXTI_ALL_IRQ_Function(); }
void EXTI15_10_IRQHandler(void){ EXTI_ALL_IRQ_Function(); }
Additional minor features
digitalToggle() command can be used to toggle the digital state of the pin from high to low or from low to high. For example to toggle the state of pin PC2
digitalToggle(PC2);
Simple LED blinking example
Connect LED to PB0 and the other wire of the LED to ground with the following code:
#include "main.h"
using namespace compatArduino;
int LedPin = PB0; // PB0 pin is connected to Led
int main()
{
doInit(); // Initialize the program
pinMode(LedPin, dosage
OUTPUT); // set pin mode to OUTPUT
while(1) // loop forever
{
digitalWrite(LedPin, visit web
HIGH); // Turn LED on
delay(1000); // wait here for 1 second
digitalWrite(LedPin,LOW); // Turn LED off
delay(1000); // wait here for 1 second
}
}