LEDs on the Web51 board

no prev chap.  projekty.html  Server setup
Subdirectories led1, led2 a led3 of the projekt directory contain examples that show the basic usage of Web51 development system, using only the two onboard LEDs. Here, these examples are described in more detail and the progress from led1 to led3 demonstrates a possible way of controlling peripherals. Led3 even contains a simple state machine.

Contents

LED 1 - Basic example of a single http server with server-side CGI.
LED 2 - 2 server-side CGI scripts (setting the state with a form, reading back).
LED 3 - led2 with added control of "flashing" state in a loop and javascript.

LED 1

Basic example of a simple http server with a server-side CGI script. CGI controls LEDs on the Web51 board. It reads the state back and displays an image of either a shining or an idle LED.

According to its parameter, the simple testP3 script tests either P3.4 or P3.5 where the LEDs and buttons are connected. Zero value (LED is ON) sets Zflag to 1. According to Zflag, the http server prints out the respective text, as described in the http.asm description.

;; Directory entry
        .section cpu_dir, #alloc
        .word   't'+'e'+'s'+'t'+'P'+'3'+'.'+'c'+'g'+'i'
        .byte   0x10            ;cgi
        .byte   0               ;reserved
        .word   cgibegin
        .word   cgiend

;; CGI call
        .section cpu_files, #alloc
cgibegin:                       ;testP3.cgi
        .pcode  testP3
        .pcode  pret
cgiend:

;; CGI procedure
        .text
testP3: mov     a,P3
        clr     zflag	
        anl     a,state80
        jnz     retP3
        setb    zflag
retP3:  ret

LED 2

More complicated http server with server-side CGI. The first CGI script finds out LED states and inserts appropriate symbols into the page (***). The second CGI script processes data returned from the html form and sets LED states accordingly.

The LEDsetup.cgi script that controls LEDs is a little more complicated.

;LEDsetup.cgi
        .section cpu_dir, #alloc

        .word   'L'+'E'+'D'+'s'+'e'+'t'+'u'+'p'+'.'+'c'+'g'+'i'
        .byte   0x10            ;cgi
        .byte   0               ;reserved
        .word   cgibegin
        .word   cgiend

        .section cpu_files, #alloc

cgibegin:                       ;LEDsetup.cgi
        .pcode  LEDsetup
        .pcode  pret
cgiend:

        .text
.global	LEDsetup

The html request is searched for "0=." or "1=.". If the sequence is found, the respective LED is turned on or off according to the parameter.

LEDsetup:
        mov     state80,#'0'     ; find '0' item
        lcall   scanpar
        jnb     zflag, no0cmd
        ;; cmd0
        mov     a,@r0
        mov     C,acc.0
        mov     LED0,C
no0cmd: mov     state80,#'1'     ; find '1' item
        lcall   scanpar
        jnb     zflag, no1cmd
        ;; cmd1
        mov     a,@r0
        mov     C,acc.0
        mov     LED1,C
no1cmd: ret

LED 3

JavaScript More complicated example of a simple http server with server-side CGI and a background process. The first CGI script finds out LED states and inserts appropriate symbols into the page (** *). Second CGI script checks the states of the control variables of the background process ("off", "flashing", "on"). The third CGI processes data returned from the html form and sets the variables of the background process (LED off, flashing, on) accordingly. The backround process, synchronized by a timer, turns LEDs on or off according to its control variables. It also changes LED modes if any of the buttons are pressed. To simplify the control, client side uses JavaScript.

Scripts in the LED3 example that control the LEDs are much more complicated. testP3.cgi is the same as in LED1 or LED2. LEDsetup.cgi is similar to the script in LED2, but it does not control the LED directly; instead it sets bits 0 and 1, or 2 and 3, of the LEDmode variable. This variable can be also changed by pressing the buttons on the Web51 board.

LEDsetup:mov    state80,#'0'   ; find '0' item
        lcall   scanpar
        jnb     zflag, no0cmd
        ;; cmd0
        mov     a,@r0
        cjne    a,#'F',noFlash0
        mov     a,LEDmode
        clr     Acc.0   ;Off LED0
        setb    Acc.1   ;Flash LED0
        sjmp    SetL0
noFlash0:cjne   a,#'0',noOn0
        mov     a,LEDmode
        setb    Acc.0   ;On LED0
        clr     Acc.1   ;NoFlash LED0
        sjmp    SetL0
noOn0:  mov     a,LEDmode
        anl     A,#0b11111100   ;Of LED0
SetL0:  mov     LEDmode,a	

no0cmd: mov     state80,#'1'    ; find '1' item
        lcall   scanpar
        jnb     zflag, no1cmd
	;; cmd1
        mov     a,@r0
        cjne    a,#'F',noFlash1
        mov     a,LEDmode
        clr     Acc.2   ;Off LED1
        setb    Acc.3   ;Flash LED1
        sjmp    SetL1
noFlash1:cjne   a,#'0',noOn1
        mov     a,LEDmode
        setb    Acc.2   ;On LED1
        clr     Acc.3   ;NoFlash LED1
        sjmp    SetL1
noOn1:  mov     a,LEDmode
        anl     A,#0b11110011   ;Of LED1
SetL1:  mov     LEDmode,a	

no1cmd: ret

testLED.cgi, unlike testP3.cgi, checks the value in the internal LEDmode variable.

According to LEDmode, LED control is performed in the fast section of the main loop (specified in www8051.asm).

        .section fast, #alloc
;*************************************************************
        mov     a,LEDmode
        jb      Acc.1,nosetL0   ;Flash LED0
        mov     C,Acc.0
        cpl     C
        mov     LED0,C
nosetL0:
        jb      Acc.3,nosetL1   ;Flash LED1
        mov     C,Acc.2
        cpl     C
        mov     LED1,C
nosetL1:

The main loop also scans buttons. Buttons are scanned approximately every 50 ms in the slow section of the main loop.

        .section slow, #alloc
;*************************************************************
;~50 ms loop
        dec     flashtimer
        setb    LED0            ;enable readout keyboard
        setb    LED1
;unpack packed bit array
        mov     a,LEDmode
        anl     a,#0b00000011   ;LED0
        mov     R4,a
        mov     a,LEDmode
        anl     a,#0b00001100   ;LED1
        mov     R5,a
        mov     a,LEDmode
        anl     a,#0b00110000   ;KBD0
        mov     R6,a
        mov     a,LEDmode
        anl     a,#0b11000000   ;KBD1
        mov     R7,a

The button value is read and compared to two other samples from previous passes.

        mov     C,LED1
        mov     Acc.5,C
        cjne    a,#0b10000000,NCH1     ;test KBD1 change 1->0

If the button press is valid, that is, next to last sample is 1, last sample is 0 and current sample is 0, LED mode changes.
Note: Currently, R7 stores the last two-bit "keypress" sample. Normally we should rotate the new value into this two-bit field and mask it; however, in this case, a particular value is checked, so the rotation and masking operations result in a constant that can be specified directly.

        mov     R7,#0b00000000

The following sequence cyclically changes values of bits 2 and 3 in R5 in this order: ....00.. -> ....01.. -> ....10.. -> ....00..

        mov     a,R5
        add     a,#0b00000100
        jb      P,no11L1                ;jmp if 00000100 or 00001000
        clr     a                       ;00001100 -> 00000000
no11L1: mov     R5,a
        sjmp    tstL0

If no keypress detected, store new button sample.

NCH1:   rl      a
        anl     a,#0b11000000
        mov     R7,a

Perform the same processing with the other button.

tstL0:  mov     a,R6
        mov     C,LED0
        mov     Acc.3,C
        cjne    a,#0b00100000,NCH0      ;test KBD1 change 1->0
        mov     R6,#0b00000000
        mov     a,R4
        add     a,#0b00000001
        jb      P,no11L0                ;jmp if 00000001 or 00000010
        clr     a                       ;00000011 -> 00000000
no11L0: mov     R4,a
        sjmp    packarr
NCH0:   rl      a
        anl     a,#0b00110000
        mov     R6,a
packarr:mov     a,R4
        orl     a,R5
        orl     a,R6
        orl     a,R7
        mov     LEDmode,a
        jnb     Acc.1,noFl0    ;Flash LED0

If flashing is requested, copy the "flash state" from the counter.

        mov     a,flashtimer
        mov     C,Acc.4
        mov     LED0,C
        mov     a,LEDmode
noFl0:	
        jnb     Acc.3,noFl1    ;Flash LED1
        mov     a,flashtimer
        mov     C,Acc.4
        mov     LED1,C
noFl1:

JavaScript  Why does this example use JavaScript? First, it automatically submits the form. If LED mode is changed, the form is automatically sent to the "index.html" page. After that, second part of the script reloads the page after 50 ms, this time specified as "/". The reason is, that LEDs are set asynchronously, so an incorrect state is shown after mode change. The form reloads to reflect the change and display correct LED states.





Sponzored by LPhard Ltd. Graphics by GIMP Created by EasyPad

(c)Copyright 2000 - 2002, HW server & Radek Benedikt
Web51@HW.cz, Web51.HW.cz
no prev chap.  Obsah  Server setup