Interrupt blink

(So far Xduino v0.31 has only been tested on board with STM32F10ret6 mcu.)

Command reference:

Pin mode

pinMode(PIN,MODE);
Set the specified PIN to MODE
MODE can be

  • INPUT for receiving digital input
  • OUTPUT for sending digital output
  • ANALOGIN for receiving analog input (ADC)
  • ANALOGOUT for sending analog output (DAC)

(not that not every PIN has analog functionality, please refer to PIN definition table below)
example:

pinMode(PB1,INPUT);

Digital input/output

digitalWrite(PIN,DATA);
Send digtal DATA output to PIN. DATA can be either HIGH or LOW
example:

pinMode(PB2,OUTPUT);
digitalWrite(PB2,HIGH);

digitalRead(PIN);
Read digital input from PIN. The result can be either HIGH or LOW.
example:

bool PB1state;

pinMode(PB1,INPUT);
PB1state=digitalRead(PB1);
if(PB1state==HIGH)
{
Serial1.println("PB1 state is high");
} else {
Serial1.println("PB1 state is low");
}

digitalToggle(PIN);
Toggle the digital state of PIN. If PIN is high then change PIN state to LOW.
If PIN is low then change state to HIGH.
example:

pinMode(PB2,OUTPUT);
digitalWrite(PB2,HIGH);
delay_mS(1000); // pause for 1000 milliseconds
digitalToggle(PB2);

Analog input (ADC)

analogRead(ADC_CHANNEL);

Read analog input, the output value is between 0-4095 representing 0-3.3V.
To find out which pin cirresponds to ADC_CHANNEL number please check the PIN definition at the Analog input channels table below

int AnalogData;
pinMode(PB0, ANALOGIN);
AnalogData=analogRead(1);
Serial1.printf("Analog channel 1 (PB0) input is %d",AnalogData);

Analog output (DAC)

analogWrite(DAC_CHANNEL,DATA);
Output DATA to the specified Analog DAC_CHANNEL. The output value must
be between 0-4095 representing 0-3.3V. To find out which pin corresponds to DAC_CHANNEL number
check the PIN definition of the Analog output channels table below.

analogWrite(1,0); // no voltage to DAC channel 1
delay(1000); // wait 1000 milliseconds
analogWrite(1,2047); // half of the full voltage
delay(1000); // wait 1000 milliseconds
analogWrite(1,4095); //full voltage output

analogLastWrite(DAC_CHANNEL);
Get the data value used by previous analogWrite command on the DAC_CHANNEL number.

int lastDACvalue;
lastDACvalue=analogLastWrite(1); // get the value from DAC channel 1

Interrupt setting

attachInterrupt(PIN,FUNCTION_NAME,INTERRUPT_MODE);
Activate the specified FUNCTION_NAME when PIN gets into INTERRUPT_MODE.
FUNCTION_NAME is the function which has to be declared in the main program.
INTERRUPT_MODE can be

  • RISING for activating when the PIN changes state from LOW to HIGH only
  • FALLING for activating when the PIN changes state from HIGH to LOW only
  • CHANGE for activating when PIN changes state

(Important: read the Interrupt note to understand interrupt channel selection.)

An example of using interrupt to blink an LED is at the Interrupt LED blink

detachInterrupt(PIN);
Deactivate Interrupt assigned on PIN

Example:

detachInterrupt(PB8);

interrupts();
Enable all interrupts that has been assigned by attachInterrupt command
This is generally used after noInterrupts command to reactive the interrupts.

Example:

interrupts();

noInterrupts(void);
Deactivate all interrupts assigned by attacheInterrupt command.

Example:

noInterrupts();

// simple SPI functionality emulation output only, without latch
shiftOut(u8 PxnDataOut,u8 PxnClock,bool BitOrder,u8 Data);
(explanation coming soon)

// simple function to retrieve time when Pxn changes state
pulseIn(u8 Pxn,bool startState,u32 TimeOut=0);
(explanation coming soon)

Timing

millis(void);
Return the number of milliseconds since the program execution started.

Example:

Serial1.printf("This has been running for %lu milliseconds.",millis());

micros();
Return the number of microseconds since the program execution started.

Example:

Serial1.printf("This has been running for %lu microseconds.",micros());

delay(MILLISECONDS);
Stop program execution for MILLISECONDS.
Note: this cannot be used within interrupt functions

Example:

delay(1000); // stop for 1000 milliseconds which is equal to 1 second

delayMicroseconds(MICROSECONDS);
Stop program execution for MICROSECONDS.
Note: this cannot be used within interrupt functions

Example:

delayMicroseconds(10000); // stop for 10000 microseconds which is equal to 10 milliseconds

pause(MILLISECONDS);
Stop program execution for ‘approximately’ MILLISECONDS.
This can be used in interrupt functions.
Note: the delay is only an approximation

Example:

pause(1000);

pauseMicroseconds(MICROSECONDS);Stop program execution for ‘approximately’ MICROSECONDS.
This can be used in interrupt functions.
Note: the delay is only an approximation

Example:

pauseMicroseconds(10000);

Random number

randomSeed(SEED);
Choose SEED to be seed of the random number in the program.
SEED must be a number.
To allow the program to ‘attempt’ to randomize the seed put nothing as the SEED.

Example:
To allow program to ‘attempt’ to randomize the seed autmatically

randomSeed();

To specify a random seed

randomSeed(1234);

random(MAX);
Generate a random number between 0 and MAX-1

Example:

int myNumber;
myNumber=random(1000); // get a number between 0 and 999

random(MIN,MAX);
Generate a random number between MIN and MAX-1

Example:

int myNumber;
myNumber=random(1000,2000); // get a number between 1000 and 1999

Extended reference

There are a few things to keep in mind when using Xduino v0.3
Major Features:
All Arduino syntax are compatible with Xduino v0.31 (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 with up to 5 parameters on the Serial command like

Serial1.printf("Xduino v%f 
",0.3);
Serial1.printf("Parameter: %d %d %d %d %d
",1,2,3,4,5); // maximum 5 parameters

Serial port buffer
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++ library
Other C and C++ libraries can also be included, 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);

Interrupt note
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);

Blink an led connected to PA15 when PB8 changes state from LOW to HIGH using interrupt

int LEDpin=PA15

void BlinkLed(void)
{
Serial1.println("Interrupt activated");
digitalWrite(LEDpin,HIGH);
pause(1000); // must use pause instead of delay in interrupt function
digitalWrite(LEDpin,LOW);
}

int main()
{
doInit();

Serial1.begin(115200);
attachInterrupt(PB8,BlinkLed,RISING);
}