LEDs on the Web51 board |
|
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.
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.
- Source html code of the LED1 page
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <head> <body> <center><H1>AT89C8252 WWW server</H1></center> <table border=0> <tr><td width=100><b>LED0</b></td> <td>'?testP3.cgi?0x10'<img src="black.gif" alt="" border=0 width=16 height=16> {<img src="red.gif" alt="" border=0 width=16 height=16>}</td></tr> <tr><td width=100><b>LED1</b></td> <td>'?testP3.cgi?0x20'<img src="black.gif" alt="" border=0 width=16 height=16> {<img src="yellow.gif" alt="" border=0 width=16 height=16>}</td></tr> </table> </body></html>For clarity, individual elements of the CGI meta-language were highlighted:
CGI script call output if the CGI sets Zflag to 0 output if the CGI sets Zflag to 1- Source html code as converted by html2db.pl and stored in Web51
(Note: " · " represents non-printable binary parameters of respective CGI procedures)
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <head> <body> <center><H1>AT89C8252 WWW server</H1></center> <table border=0> <tr><td width=100><b>LED0</b></td> <td>'?testP3.cgi?·<img src="black.gif" alt="" border=0 width=16 height=16> {<img src="red.gif" alt="" border=0 width=16 height=16>}</td></tr> <tr><td width=100><b>LED1</b></td> <td>'?testP3.cgi?·<img src="black.gif" alt="" border=0 width=16 height=16> {<img src="yellow.gif" alt="" border=0 width=16 height=16>}</td></tr> </table> </body></html>- Expanded html page, as sent by Web 51 server
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <head> <body> <center><H1>AT89C8252 WWW server</H1></center> <table border=0> <tr><td width=100><b>LED0</b></td> <td><img src="black.gif" alt="" border=0 width=16 height=16></td></tr> <tr><td width=100><b>LED1</b></td> <td><img src="yellow.gif" alt="" border=0 width=16 height=16></td></tr> </table> </body></html>- This is what the user will see
AT89C8252 WWW server
LED0 LED1 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
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.
- Source html code of LED2
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <head> <body> <center><H1>AT89C8252 WWW server</H1></center> 'LEDsetup.cgi'<form action="/" method="GET"><table border=0> <tr> <td width=100><b>LED0</b></td><td>'?testP3.cgi?0x10'<font color="Black" size=9>*</font> {<font color="Red" size=9>*</font>}</td> <td>On<input type="radio" name=0 value=0 '?testP3.cgi?0x10'{checked} alt=""> Off<input type="radio" name=0 value=1 '?testP3.cgi?0x10'checked{} alt=""></td> </tr> <tr> <td width=100><b>LED1</b></td><td>'?testP3.cgi?0x20'<font color="Black" size=9>*</font> {<font color="Yellow" size=9>*</font>}</td> <td>On<input type="radio" name=1 value=0 '?testP3.cgi?0x20'{checked} alt=""> Off<input type="radio" name=1 value=1 '?testP3.cgi?0x20'checked{} alt=""></td> </tr> <tr><td> </td><td><input type="submit" value="Set LED" alt=""></td></tr> </table></form> </body></html>- Expanded html page, as sent by Web51
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <head> <body> <center><H1>AT89C8252 WWW server</H1></center> <form action="/" method="GET"><table border=0> <tr> <td width=100><b>LED0</b></td><td><font color="Red" size=9>*</font></td> <td>On<input type="radio" name=0 value=0 checked alt="">Off<input type="radio" name=0 value=1 alt=""></td> </tr> <tr> <td width=100><b>LED1</b></td><td><font color="Black" size=9>*</font></td> <td>On<input type="radio" name=1 value=0 alt="">Off<input type="radio" name=1 value=1 checked alt=""></td> </tr> <tr><td> </td><td><input type="submit" value="Set LED" alt=""></td></tr> </table></form> </body></html>- This is what the user will see
AT89C8252 WWW server
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 LEDsetupThe 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
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.
- Source html page of LED3
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <script language="JavaScript"> <!-- b="/"; function w() { if (document.URL.indexOf("index.html")!=-1) { window.setTimeout("location.replace(b);",50); } } // --> </script></head> <body onLoad="w();"> <center><H1>AT89C8252 WWW server</H1></center> 'LEDsetup.cgi'<form action="index.html" method="GET"><table border=0> <tr> <td width=100><b><font color="Red">LED0</font></b></td><td>'?testP3.cgi?0x10'<font color="Black" size=9>*</font> {<font color="Red" size=9>*</font>}</td> <td> On<input type="radio" name=0 value=0 '?testLED.cgi?0x01'checked{} alt="" onClick="submit();"> Flash<input type="radio" name=0 value=F '?testLED.cgi?0x02'checked{} alt="" onClick="submit();"> Off<input type="radio" name=0 value=1 '?testLED.cgi?0x03'{checked} alt="" onClick="submit();"> </td> </tr> <tr> <td width=100><b><font color="Yellow">LED1</font></b></td><td>'?testP3.cgi?0x20'<font color="Black" size=9>*</font> {<font color="Yellow" size=9>*</font>}</td> <td> On<input type="radio" name=1 value=0 '?testLED.cgi?0x04'checked{} alt="" onClick="submit();"> Flash<input type="radio" name=1 value=F '?testLED.cgi?0x08'checked{} alt="" onClick="submit();"> Off<input type="radio" name=1 value=1 '?testLED.cgi?0x0C'{checked} alt="" onClick="submit();"> </td> </tr> </table></form> </body></html>- Expanded html page as sent by Web51
<html><head> <meta http-equiv="Cache-Control" content="no-cache"> <title>89C8252 WWW server</title> <script language="JavaScript"> <!-- b="/"; function w() { if (document.URL.indexOf("index.html")!=-1) { window.setTimeout("location.replace(b);",50); } } // --> </script></head> <body onLoad="w();"> <center><H1>AT89C8252 WWW server</H1></center> <form action="index.html" method="GET"><table border=0> <tr> <td width=100><b><font color="Red">LED0</font></b></td><td><font color="Red" size=9>*</font></td> <td> On<input type="radio" name=0 value=0 alt="" onClick="submit();"> Flash<input type="radio" name=0 value=F checked alt="" onClick="submit();"> Off<input type="radio" name=0 value=1 alt="" onClick="submit();"> </td> </tr> <tr> <td width=100><b><font color="Yellow">LED1</font></b></td><td><font color="Black" size=9>*</font></td> <td> On<input type="radio" name=1 value=0 alt="" onClick="submit();"> Flash<input type="radio" name=1 value=F alt="" onClick="submit();"> Off<input type="radio" name=1 value=1 checked alt="" onClick="submit();"> </td> </tr> </table></form> </body></html>- This is what the user will see
AT89C8252 WWW server
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: rettestLED.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,aThe 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->0If 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,#0b00000000The 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 tstL0If no keypress detected, store new button sample.
NCH1: rl a anl a,#0b11000000 mov R7,aPerform 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 LED0If 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:
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.
| Web51 description | News | FAQ | ORDER FORM | DOWNLOAD | Links |
| (c)Copyright 2000 - 2002, HW server & Radek Benedikt
Web51@HW.cz, Web51.HW.cz |
|