An embedded system with interrupts is presented here:
S21 with interrupt and IoT board specification
The main module contains S2.1 processor with memory. The board has four interrupts (int0, int1, int2, int3) and four input ports (10, 11, 12, 13). I made a set of tools for practicing with this kind of programming. The tool consists of the compiler, the assembler and the simulator of the processor (S2.1 processor). The language I used can be found here (Rz language). The interrupt program looks like this:
// simple interrupt // a simple loop run for 10000 inst.
// when interrupt occurs, it prints out cnt
cnt // global counter
int0()
print(cnt," ")
cnt = cnt + 1
main()
cnt = 0
while( 1 ) // wait for int
doze() // sleep and wait for interrupt
This program has an empty main loop. It waits for interrupt. The interrupt service routine increments a counter and print it out. The simulation runs for 1 second, or 1,000,000 time unit at the speed of the processor 1MHz. The sequence of commands to run the experiment is, compile the program (simple-int.txt), assemble the assembly file, run the simulator. The output of compiler will be an assembly language file (simple-int-s.txt). This file is assembled into an executable object file (simple-int-s.obj) suitable for the processor simulator.
C:\iot-rz\test>rz simple-int.txt > simple-int-s.txt
C:\iot-rz\test>asm simple-int-s.txt
C:\iot-rz\test>sim simple-int-s.obj
load program, last address 37
int 201
<0> int 402
<1> int 603
<2> int 804
. . .
<94> int 19296
<95> int 19497
<96> int 19698
<97> int 19899
<98> stop, clock 20000, execute 1897 instructions
C:\iot-rz\test>
ei(n) // n
0,1,2,3 equivalent to mov r1 #n, trap
r1 #16
di(n) // n 0,1,2,3
equivalent to mov r1 #n, trap r1 #15settimer0(k) // equivalent
to mov r1 #k, trap r1 #13
settimer1(k) // equivalent
to mov r1 #k, trap r1 #14x = readport(m) // m 10,11,12,13int0()
x = readport(10)
print(x)
main()
while(1)
doze() // sleep and wait
for interruptint0()
x = readport(12)
y = x - ((x / 256) * 256) // modulo 256
print(y)
main()
settimer0(200)
// set timer0 to 200
while(1)
doze()
+-50. The default
digital wave is a square wave of amplitude (0,5).turn-on-pumpt = 0while( t < 10 ) delay(1 sec) t = t + 1turn-off-pump t // global int() t = t + 1 if( t == 10 ) turn-off-pump disableint()main() settimer(1 sec) turn-on-pump t = 0 while(1) doze()
i = 0while( i < 2 ) x = readwave() if x > 0 then while(1) x = readwave() if x < 0
then // zero cross from + to - t0 =
readclock() break else if x < 0 then while(1) x = readwave() if x > 0 then t1 =
readclock() break i = i + 1
// we now have two zero crossif t1 > t0 then dt = t1 - 10else dt = t0 - t1f = 1 / (2*dt) // compute the
frequency state input next-state action
1 +
2
2 +
2 it is + + ... stay
on 2
2 -
3 zero cross + to -
3 -
3 it is - - ... stay
on 3
3 +
4 zero cross - to +
1 -
5 start -
5 -
5 it is - - ... stay
on 5
5 +
2 goto start of +state // globalt0, t1 // globalint() x = readwave() if state == 1 if( x > 0 ) then state = 2 else state =
5 else if state == 2 if( x > 0 ) then state = 2 else state = 3 t0 =
readclock() // record time of zero cross + to - else if state == 3 if( x < 0 ) then state = 3 else state = 4 t1 =
readclock() // record time of zero cross - to + else if state == 5 if( x < 0 ) then state = 5 else state = 2main() state = 1 while(1) doze() if state == 4 then dt = t1 - t0 f = 1 /
(2*dt) // compute the
frequency state =
1
// reset for next cycle settimer0(d). It
determines how often the interrupt occurs. The number d
is the number to unit time of interrupt interval. Rz 3.6 has the following reserve words to use IoT board
asm(s)
// insert assembly string into asm source print() //
print int and constant string "..." printc(c) // print char prints(s) // print string settimer0(t) settimer1(t) di(n)
// diable int n ei(n)
// enable int n doze()
// sleep wait int x = readport(k) // read iot-board ports x = input() // input returns
stringx = malloc(16) // allocate 16 words from heappushm r1 ; push
multiple registers R[0]..R[15] to stack, r1 is stack pointerpopm r1 ; pop
multiple registers R[0]..R[15] from stack, r1 is stack pointerxch r1
; exchange return address of interrupt (RetAds register) with r1