硬件:野火stm32f407开发板(带网络功能)
lan芯片:lan8720A
系统:rt-thread
http服务的功能:
1.能控制野火STM32F407板载的 RGB 灯
2..实时更新ADC电压值和系统时间,更新频率为1S,这里的ADC电压值和系统时间均为虚拟值,只做测试用。
注意:LED控制部分网页和http服务器之间采用的是CGI的交互方式,不会刷新整个页面。ADC电压值和系统时间采用的是SHTML(即包含命令的HTML),每秒钟刷新一次页面。
网页截图如下:
前端页面代码如下:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- saved from url=(0021)http://192.168.1.122/ -->
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="File-List" href="http://192.168.1.122/STM32F4x7_files/filelist.xml">
<link rel="Edit-Time-Data" href="http://192.168.1.122/STM32F4x7_files/editdata.mso"><title>STM32F4x7</title></head>
<body style="" lang="EN-US" link="blue" vlink="blue">
<div class="Section1">
<div style="margin-left: 30pt;">
<p class="MsoNormal" style="text-align: center;" align="center"><img alt="" id="_x0000_i1025" src="./index_files/ST.gif" style="border: 0px solid ; width: 766px; height: 58px;"><b><i><span style="font-size: 24pt;"><br>
<strong>STM32F4x7 Webserver Demo</strong></span></i></b><span style="font-size: 13.5pt;"> </span><b><span style="font-size: 18pt; font-family: Verdana; color: rgb(51, 51, 255);"><br>
Based on the lwIP TCP/IP stack </span></b></p>
<div class="MsoNormal" style="text-align: center;" align="center">
<hr align="center" size="2" width="100%"></div>
<div align="center">
<table class="MsoNormalTable" style="width: 720.75pt;" border="1" cellpadding="0" width="961">
<tbody>
<tr style="">
<td style="padding: 1.5pt; background: rgb(51, 51, 255) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; text-align: center;">
<p class="MsoNormal"><b><i><span style="font-size: 10pt; font-family: Verdana;"><a href="http://192.168.1.122/index.html"><span style="color: white;">Home
page</span></a></span></i></b><b><i><span style="font-family: Verdana;"><o:p></o:p></span></i></b></p>
</td>
<td style="padding: 1.5pt; background: rgb(51, 51, 255) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; text-align: center;">
<p class="MsoNormal"><b><i><span style="font-size: 10pt; font-family: Verdana;"><a href="http://192.168.1.122/STM32F4x7LED.html"><span style="color: white;"> Led
control</span></a></span></i></b><b><i><span style="font-family: Verdana;"><o:p></o:p></span></i></b></p>
</td>
<td style="padding: 1.5pt; background: rgb(51, 51, 255) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; text-align: center;">
<p class="MsoNormal"><b><i><span style="font-size: 10pt; font-family: Verdana;"><a href="http://192.168.1.122/STM32F4x7ADC.shtml"><span style="color: white;">ADC
status bar</span></a></span></i></b><b><i><span style="font-family: Verdana;"><o:p></o:p></span></i></b></p>
</td>
</tr>
</tbody>
</table>
</div>
<div align="center">
<table class="MsoNormalTable" style="background: white none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 574.5pt;" border="0" cellpadding="0" cellspacing="0" width="766">
<tbody>
<tr style="">
<td style="padding: 0cm;" valign="top">
<table class="MsoNormalTable" style="width: 100%;" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr style="">
<td style="padding: 2.25pt;">
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<tr>
<td valign="top" width="72%">
<h1><span style="color: rgb(51, 51, 255);">STM32
F-4 Series</span></h1>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">A new
generation on STM32 with significant improvement in</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"> features /
performance:<o:p></o:p></span></p>
<ul type="disc">
<li class="MsoNormal" style=""><span style="font-size: 10pt; font-family: Verdana;">More Memory<o:p></o:p></span></li>
<li class="MsoNormal" style=""><span style="font-size: 10pt; font-family: Verdana;">Advanced
features<o:p></o:p></span></li>
<li class="MsoNormal" style=""><span style="font-size: 10pt; font-family: Verdana;">Maintain
close pin-out compatibility<o:p></o:p></span></li>
<li class="MsoNormal" style=""><span style="font-size: 10pt; font-family: Verdana;">Maintain
close software compatibility<o:p></o:p></span></li>
</ul>
<span style="font-size: 10pt; font-family: Verdana;">Complement
the existing family with more performance, memory and features<br>
<br>
<a style="font-weight: bold;" href="http://www.st.com/internet/mcu/class/1734.jsp" target="_blank">The STM32F4x7 home page</a></span></td>
<td width="2%"></td>
<td align="right" valign="top" width="26%"><br><br>
<img style="width: 322px; height: 213px;" alt="" src="./index_files/stm32.jpg"></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr style="">
<td style="padding: 2.25pt;">
<p class="MsoNormal"><o:p> </o:p></p>
</td>
</tr>
<tr style="">
<td style="padding: 2.25pt;" valign="top">
<table class="MsoNormalTable" style="width: 507.4pt;" border="0" cellpadding="0" cellspacing="0" width="677">
<tbody>
<tr style="">
<td style="padding: 2.25pt; background: rgb(51, 51, 255) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 504.2pt;" width="672">
<p class="MsoNormal"><span style="color: white;"> </span><strong><span style="font-family: Verdana; color: white;">About this
demonstration</span></strong><span style="font-family: Verdana; color: white;"><o:p></o:p></span></p>
</td>
<td style="border: medium none ; padding: 0cm;" width="4">
<p class="MsoNormal"> </p>
</td>
</tr>
<tr style="height: 11.25pt;">
<td style="padding: 2.25pt; width: 504.2pt; height: 11.25pt;" valign="top" width="672">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">This
webserver is a part of a demonstration package developed on the top
level of the lwIP TCP/IP stack. <br>
<br>
The package contains nine applications:</span></p>
<ol>
<li><span style="font-size: 10pt; font-family: Verdana;">Applications running in standalone (without an RTOS):</span></li>
</ol>
<ul style="margin-left: 40px;">
<li><span style="font-size: 10pt; font-family: Verdana;">A
Webserver. <o:p></o:p></span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A TFTP server.</span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A TCP echo
client application</span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A TCP echo
server application</span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A UDP echo
client application</span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A UDP echo
server application </span></li>
</ul>
<ol start="2">
<li><span style="font-size: 10pt; font-family: Verdana;">Applications running with FreeRTOS operating system:</span></li>
</ol>
<ul style="margin-left: 40px;">
<li><span style="font-size: 10pt; font-family: Verdana;">A Webserver
based on netconn API.</span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A Webserver
based on socket API.</span></li>
<li><span style="font-size: 10pt; font-family: Verdana;">A TCP/UDP
echo server application based on netconn API. </span></li>
</ul>
<ul type="disc">
</ul>
<p class="MsoNormal" style=""><o:p> </o:p></p>
</td>
<td style="border: medium none ; padding: 0cm;" width="4">
<p class="MsoNormal"> </p>
</td>
</tr>
<tr style="">
<td style="padding: 2.25pt; background: rgb(51, 51, 255) none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 504.2pt;" width="672">
<p class="MsoNormal"><span style="color: white;"> </span><strong><span style="font-family: Verdana; color: white;">About LwIP</span></strong><span style="font-family: Verdana; color: white;"><o:p></o:p></span></p>
</td>
<td style="border: medium none ; padding: 0cm;" width="4">
<p class="MsoNormal"> </p>
</td>
</tr>
<tr style="">
<td colspan="2" style="padding: 2.25pt; width: 507.4pt;" width="677">
<p class="MsoNormal" style="margin-bottom: 12pt;"><span style="font-size: 10pt; font-family: Verdana;">lwIP,
pronounced lightweight IP, is an open source TCP/IP stack developed by
Adam Dunkels at the Swedish Institute of Computer Science and is
maintained now by a world wide community of developers.<br>
<br>
lwIP features:<br>
<br>
- IP (Internet Protocol) including packet forwarding over multiple
network interfaces<br>
- ICMP (Internet Control Message Protocol) for network maintenance and
debugging<br>
- UDP (User Datagram Protocol) including experimental UDP-lite
extensions<br>
- TCP (Transmission Control Protocol) with congestion control, RTT
estimation and fast recovery/fast retransmit<br>
- Specialized raw API for enhanced performance<br>
- Optional Berkeley-alike socket API<br>
- DHCP (Dynamic Host Configuration Protocol)<br>
- PPP (Point-to-Point Protocol)<br>
- ARP (Address Resolution Protocol) for Ethernet<br>
<br>
For more informations you can refer to the website: <a href="http://savannah.nongnu.org/projects/lwip/">http://savannah.nongnu.org/projects/lwip/
</a><o:p></o:p></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><o:p></o:p></p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<span style="display: none;"><o:p></o:p></span></div>
<div align="center">
<table class="MsoNormalTable" style="background: black none repeat scroll 0% 50%; -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial; width: 574.5pt;" border="0" cellpadding="0" cellspacing="0" width="766">
<tbody>
<tr style="">
<td style="padding: 0cm; width: 100%;" width="100%">
<p class="MsoNormal"><img id="_x0000_i1028" src="./index_files/pixel.gif" border="0" height="1" width="1"></p>
</td>
</tr>
</tbody>
</table>
</div>
<p class="MsoNormal" style="text-align: center;" align="center"><span style="display: none;"><o:p> </o:p></span></p>
<div align="center">
<table class="MsoNormalTable" style="width: 574.5pt;" border="0" cellpadding="0" cellspacing="0" width="766">
<tbody>
<tr style="">
<td style="padding: 0cm; width: 100%;" width="100%">
<div id="footer">
<p class="legal" style="text-align: center;" align="center">All rights reserved 2011
STMicroelectronics </p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body></html>
STM32F4x7LED.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- saved from url=(0038)http://192.168.1.122/STM32F4x7LED.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>STM32F4x7LED</title>
<meta content="MSHTML 6.00.2800.1561" name="GENERATOR">
<style ="font-weight:="" normal;="" font-family:="" verdana;"=""></style></head>
<body>
<h4><small style="font-family: Verdana;"><small><big><big><big style="font-weight: bold;"><big><strong><span style="font-style: italic;">STM32F4x7 Leds control</span></strong>
</big></big></big></big></small></small></h4>
<hr style="width: 100%; height: 2px;">
<table style="width: 961px; height: 30px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="font-family: Verdana; font-weight: bold; font-style: italic; background-color: rgb(51, 51, 255); text-align: center;"><small><a href="http://192.168.1.122/index.html"><span style="color: white;">Home
page</span></a></small></td>
<td style="font-family: Verdana; font-weight: bold; font-style: italic; background-color: rgb(51, 51, 255); text-align: center;"><small><a href="http://192.168.1.122/STM32F4x7LED.html"><span style="color: white;">Led
control</span></a></small></td>
<td style="font-family: Verdana; font-weight: bold; font-style: italic; background-color: rgb(51, 51, 255); text-align: center;"><small><a href="http://192.168.1.122/STM32F4x7ADC.shtml"><span style="color: white;">ADC
status bar</span></a></small></td>
</tr>
</tbody>
</table>
<br>
<big><br>
</big><small><span style="font-family: Verdana;">This
page
allows you to control the four LEDs: LED1, LED2, LED3 and LED4 located in
the STM324xG-EVAL board. To put on/off</span><br style="font-family: Verdana;">
<span style="font-family: Verdana;">a LED you have to
check/uncheck its corresponding checkbox. Then you have to click on
"Send" button to submit the new LEDs</span><br style="font-family: Verdana;">
<span style="font-family: Verdana;">configuration. Finally
check in the STM324xG-EVAL board that you get the desired LEDs
are putted on/off. </span><br style="font-family: Verdana;">
</small>
<br>
<br>
<table style="width: 787px; color: white; height: 31px;" white="" color="" ;="" 100="" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<td class="tabtitle" style="height: 15px; background-color: rgb(51, 51, 255);"><big><big><font size="-1"><big><big><strong>STM32
Webserver LEDs Control</strong></big></big></font></big></big></td>
</tr>
</tbody>
</table>
<br>
<form method="get" action="http://192.168.1.122/leds.cgi"><input value="1" name="led" type="checkbox">LED1<br>
<input value="2" name="led" type="checkbox">LED2<br>
<input value="3" name="led" type="checkbox">LED3<br>
<input value="4" name="led" type="checkbox">LED4
<br>
<br>
<input value="Send" type="submit"> </form>
<h3><font size="-1"><span style="color: black;"></span>
</font></h3><h3 style="font-weight: normal; font-family: Verdana;"><font size="-1"><br>
</font></h3>
<span style="font-family: Verdana;"></span><font size="-1"><a style="font-family: Verdana;" href="http://192.168.1.122/STM32F4x7.html"><big><span style="font-weight: bold;"></span></big></a></font>
<font size="-1"><span style="color: black;">_______________________________________________________________________________________________________________<br>
</span></font><font class="footmsg">
<comment title="#config timefmt="%Y" " xmlns="http://disruptive-innovations.com/zoo/nvu"><!--#config timefmt="%Y" --></comment><comment title="#echo var="DATE_GMT" " xmlns="http://disruptive-innovations.com/zoo/nvu"><!--#echo var="DATE_GMT" --></comment>
<span style="color: silver;">
All rights
reserved 2011 STMicroelectronics</span> </font>
</body></html>
STM32F4x7ADC.shtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html><head><title>STM32F4x7ADC</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="refresh" content="1">
<meta content="MSHTML 6.00.2900.3698" name="GENERATOR">
<style></style><!-- saved from url=(0038)http://192.168.0.10/STM32F107ADC.shtml --></head>
<body>
<h4><small style="font-family: Verdana;"><small><big><big><big style="font-weight: bold;"><big><strong><em><span style="font-style: italic;">STM32F4x7 ADC Conversion </span></em></strong></big></big></big></big></small></small></h4>
<hr style="width: 100%; height: 2px;">
<span style="font-weight: bold;"></span><span style="font-weight: bold;">
<table style="width: 961px; height: 30px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="font-weight: bold; font-style: italic; font-family: Verdana; background-color: rgb(51, 51, 255); text-align: center;"><small><a href="/index.html"><span style="color: white;">Home
page</span></a></small></td>
<td style="font-weight: bold; font-style: italic; font-family: Verdana; background-color: rgb(51, 51, 255); text-align: center;"><small><a href="/STM32F4x7LED.html"><span style="color: white;">Led
control</span></a></small></td>
<td style="font-weight: bold; font-style: italic; font-family: Verdana; background-color: rgb(51, 51, 255); text-align: center;"><small><a href="/STM32F4x7ADC.shtml"><span style="color: white;">ADC
status bar</span></a></small></td>
</tr>
</tbody>
</table>
</span><small><span style="font-family: Verdana;"></span><span style="font-family: Verdana;"><br></span></small><dl><dt><small style="font-family: Verdana;">This page allows you
to get continuously the ADC 3 Channel 7 analog input converted value.
This ADC Channel is connected to the</small></dt><dt><small style="font-family: Verdana;">STM324xG-EVAL board's
potentiometer. The ADC value is updated, automatically, each 1s with
the last
converted ADC Channel 7 value. </small></dt><dt><small style="font-family: Verdana;">You could check this by
changing the potentiometer position and check that the ADC value is updated, by an
automatic refresh of this </small></dt><dt><small style="font-family: Verdana;">page,
with the new converted value.</small></dt></dl>
<br>
<span style="font-family: Verdana;"></span><span style="font-family: Verdana;"></span>
<table border="1" cellpadding="5" cellspacing="1">
<tbody>
<tr>
<td><small><span style="font-family: Verdana;">ADC
Converted Value</span> </small></td>
<td><!--#t--> <small><span style="font-family: Verdana;">mv</span></small> </td>
</tr>
</tbody>
</table>
<hr>
<div style="background-color:#0066CC;color:#fff;margin-top:30px; ">
<h3 >
<span >RTC   Date ¦ time </span>
</h3>
</div>
<div align="center">
<table border="1" cellpadding="10">
<tr>
<td width="200">Current   Time</td>
<td width="200"><!--#h--></td>
</tr>
</table>
<br><br>
<table border="1" cellpadding="10">
<tr>
<td width="200">Current   Date</td>
<td width="200"><!--#y--></td>
</tr>
</table>
<br><br>
</div>
<h3 style="font-weight: normal;"><br>
</h3>
<font size="-1"><span style="color: black;"></span></font><br>
<font size="-1"><span style="color: black;"></span></font><span style="font-family: Verdana;"></span><a style="font-family: Verdana;" href="/STM32F107.html"><big><span style="font-weight: bold;"></span></big></a><font size="-1"><span style="color: black;">_______________________________________________________________________________________________________________<br>
</span></font><font class="footmsg">
<comment title="#config timefmt="%Y" " xmlns="http://disruptive-innovations.com/zoo/nvu"><!--#config timefmt="%Y" --></comment><comment title="#echo var="DATE_GMT" " xmlns="http://disruptive-innovations.com/zoo/nvu"><!--#echo var="DATE_GMT" --></comment>
<span style="color: silver;">
All rights
reserved 2011 STMicroelectronics</span> </font>
</body></html>
1.将index.html,STM32F4x7LED.html,STM32F4x7ADC.shtml及index_files目录拷贝到具有fsdata.c文件的fs文件夹内。然后点击makefsdata.exe,生成网页数据文件fsdata.c
httpd_cgi_ssi.c
/**
******************************************************************************
* @file httpd_cg_ssi.c
* @author MCD Application Team
* @version V1.1.0
* @date 31-July-2013
* @brief Webserver SSI and CGI handlers
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "lwip/debug.h"
#include "httpd.h"
#include "lwip/tcp.h"
#include "fs.h"
#include "netconf.h"
#include "LAN8742A.h"
#include <string.h>
#include <stdlib.h>
#include "./Bsp/led/bsp_led.h"
#define NUM_CONFIG_CGI_URIS 2 //CGI的URI数量
#define NUM_CONFIG_SSI_TAGS 4 //SSI的TAG数量
extern void RTC_Get_Time(u8 *hour,u8 *min,u8 *sec,u8 *ampm); //声明RTC_Get_Timer()函数
extern void RTC_Get_Date(u8 *year,u8 *month,u8 *date,u8 *week); //声明RTC_Get_Date()函数
tSSIHandler ADC_Page_SSI_Handler;
uint32_t ADC_not_configured=1;
/* we will use character "t" as tag for CGI */
//char const* TAGCHAR="t";
//char const** TAGS=&TAGCHAR;
static const char *ppcTAGs[]= //SSIµÄTag
{
"t", //ADC值
"h", //时间
"y" //日期
};
/* CGI handler for LED control */
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]);
/* Html request for "/leds.cgi" will start LEDS_CGI_Handler */
const tCGI LEDS_CGI={"/leds.cgi", LEDS_CGI_Handler};
/* Cgi call table, only one CGI used */
tCGI CGI_TAB[1];
/**
* @brief Configures the ADC.
* @param None
* @retval None
*/
static void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Configure ADC Channel 12 as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC Common Init */
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Configuration ------------------------------------------------------*/
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 Regular Channel Config */
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_56Cycles);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* ADC1 regular Software Start Conv */
ADC_SoftwareStartConv(ADC1);
}
/**
* @brief ADC_Handler : SSI handler for ADC page
*/
u16_t ADC_Handler(int iIndex, char *pcInsert, int iInsertLen)
{
/* We have only one SSI handler iIndex = 0 */
if (iIndex ==0)
{
char Digit1=0, Digit2=0, Digit3=0, Digit4=0;
uint32_t ADCVal = 0;
/* configure ADC if not yet configured */
if (ADC_not_configured ==1)
{
ADC_Configuration();
ADC_not_configured=0;
}
/* get ADC conversion value */
ADCVal = ADC_GetConversionValue(ADC1);
/* convert to Voltage, step = 0.8 mV */
ADCVal = (uint32_t)(ADCVal*3300/4096);
/* get digits to display */
Digit1= ADCVal/1000;
Digit2= (ADCVal-(Digit1*1000))/100 ;
Digit3= (ADCVal-((Digit1*1000)+(Digit2*100)))/10;
Digit4= ADCVal -((Digit1*1000)+(Digit2*100)+ (Digit3*10));
/* prepare data to be inserted in html */
*pcInsert = (char)(Digit1+0x30);
*(pcInsert + 1) = (char)(Digit2+0x30);
*(pcInsert + 2) = (char)(Digit3+0x30);
*(pcInsert + 3) = (char)(Digit4+0x30);
/* 4 characters need to be inserted in html*/
return 4;
}
return 0;
}
void RTCTime_Handler(char *pcInsert)
{
u8 hour,min,sec,ampm;
RTC_Get_Time(&hour,&min,&sec,&m);
*pcInsert = (char)((hour/10) + 0x30);
*(pcInsert+1) = (char)((hour%10) + 0x30);
*(pcInsert+2) = ':';
*(pcInsert+3) = (char)((min/10) + 0x30);
*(pcInsert+4) = (char)((min%10) + 0x30);
*(pcInsert+5) = ':';
*(pcInsert+6) = (char)((sec/10) + 0x30);
*(pcInsert+7) = (char)((sec%10) + 0x30);
}
//SSIHandler中需要用到处理RTC日期的函数
void RTCdate_Handler(char *pcInsert)
{
u8 year,month,date,week;
RTC_Get_Date(&year,&month,&date,&week);
*pcInsert = '2';
*(pcInsert+1) = '0';
*(pcInsert+2) = (char)((year/10) + 0x30);
*(pcInsert+3) = (char)((year%10) + 0x30);
*(pcInsert+4) = '-';
*(pcInsert+5) = (char)((month/10) + 0x30);
*(pcInsert+6) = (char)((month%10) + 0x30);
*(pcInsert+7) = '-';
*(pcInsert+8) = (char)((date/10) + 0x30);
*(pcInsert+9) = (char)((date%10) + 0x30);
*(pcInsert+10) = ' ';
*(pcInsert+11) = 'w';
*(pcInsert+12) = 'e';
*(pcInsert+13) = 'e';
*(pcInsert+14) = 'k';
*(pcInsert+15) = ':';
*(pcInsert+16) = (char)(week + 0x30);
}
/**
* @brief CGI handler for LEDs control
*/
const char * LEDS_CGI_Handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
uint32_t i=0;
/* We have only one SSI handler iIndex = 0 */
if (iIndex==0)
{
/* Check cgi parameter : example GET /leds.cgi?led=2&led=4 */
for (i=0; i<iNumParams; i++)
{
/* check parameter "led" */
if (strcmp(pcParam[i] , "led")==0)
{
/* switch led1 ON if 1 */
if(strcmp(pcValue[i], "1") ==0)
{
LED1_ON;
}
/* switch led2 ON if 2 */
else if(strcmp(pcValue[i], "2") ==0)
{
LED2_ON;
}
/* switch led3 ON if 3 */
else if(strcmp(pcValue[i], "3") ==0)
{
LED3_ON;
}
/* switch led4 ON if 4 */
else if(strcmp(pcValue[i], "4") ==0)
{
LED1_OFF;
LED2_OFF;
LED3_OFF;
}
}
}
}
/* uri to send after cgi call*/
return "/STM32F4x7LED.html";
}
//SSI的Handler句柄
static u16_t SSIHandler(int iIndex,char *pcInsert,int iInsertLen)
{
switch(iIndex)
{
case 0:
ADC_Handler(iIndex,pcInsert,iInsertLen);
break;
case 1:
RTCTime_Handler(pcInsert);
break;
case 2:
RTCdate_Handler(pcInsert);
break;
}
return strlen(pcInsert);
}
/**
* Initialize SSI handlers
*/
void httpd_ssi_init(void)
{
/* configure SSI handlers (ADC page SSI) */
http_set_ssi_handler(SSIHandler,ppcTAGs,NUM_CONFIG_SSI_TAGS);
}
/**
* Initialize CGI handlers
*/
void httpd_cgi_init(void)
{
/* configure CGI handlers (LEDs control CGI) */
CGI_TAB[0] = LEDS_CGI;
http_set_cgi_handlers(CGI_TAB, 1);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
bsp_rtc.c
/**
******************************************************************************
* @file main.c
* @author fire
* @version V1.0
* @date 2015-xx-xx
* @brief RTC驱动
******************************************************************************
* @attention
*
* 实验平台:野火 STM32 F407 开发板
* 论坛 :http://www.firebbs.cn
* 淘宝 :https://fire-stm32.taobao.com
*
******************************************************************************
*/
#include "stm32f4xx.h"
#include "./Bsp/RTC/bsp_rtc.h"
#include "./Bsp/led/bsp_led.h"
#include "./Bsp/usart/bsp_debug_usart.h"
#include "./Bsp/lcd/bsp_ili9806g_lcd.h"
u8 RTC_Wait_Synchro(void)
{
u8 value;
if(RTC_WaitForSynchro() == SUCCESS) value = 0;
else value = 1;
return value;
}
//RTC进入初始化模式
//返回值:0,成功 1,失败
u8 RTC_Init_Mode(void)
{
u8 value;
if(RTC_EnterInitMode() == SUCCESS) value = 0;
else value = 1;
return value;
}
//RTC写入后备区域SRAM
//BKPx:后备区寄存器编号,编号:0~19
//data:要写入的数据,32位长度
void RTC_Write_BKP(u32 BKPx,u32 data)
{
RTC_WriteBackupRegister(BKPx,data);
}
//RTC读取后备区域
//BKPx:后备区寄存器编号,编号0~19
//返回值:读取到的数据
u32 RTC_Read_BKP(u32 BKPx)
{
u32 temp;
temp = RTC_ReadBackupRegister(BKPx);
return temp;
}
//十进制转换为BCD码
//val:要转换的十进制数
//返回值:BCD码
u8 RTC_DEC2BCD(u8 val)
{
u8 bcdhigh = 0;
while(val >= 10)
{
bcdhigh++;
val -= 10;
}
return ((u8)(bcdhigh<<4)|val);
}
//BCD码转换为十进制数据
//val:要转换的BCD码
//返回值:十进制数据
u8 RTC_BCD2DEC(u8 val)
{
u8 temp = 0;
temp = (val>>4)*10;
return (temp+(val&0x0f));
}
/**
* @brief 设置时间和日期
* @param 无
* @retval 无
*/
void RTC_TimeAndDate_Set(void)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
// 初始化时间
RTC_TimeStructure.RTC_H12 = RTC_H12_AMorPM;
RTC_TimeStructure.RTC_Hours = HOURS;
RTC_TimeStructure.RTC_Minutes = MINUTES;
RTC_TimeStructure.RTC_Seconds = SECONDS;
RTC_SetTime(RTC_Format_BINorBCD, &RTC_TimeStructure);
RTC_WriteBackupRegister(RTC_BKP_DRX, RTC_BKP_DATA);
// 初始化日期
RTC_DateStructure.RTC_WeekDay = WEEKDAY;
RTC_DateStructure.RTC_Date = DATE;
RTC_DateStructure.RTC_Month = MONTH;
RTC_DateStructure.RTC_Year = YEAR;
RTC_SetDate(RTC_Format_BINorBCD, &RTC_DateStructure);
RTC_WriteBackupRegister(RTC_BKP_DRX, RTC_BKP_DATA);
}
void RTC_Get_Time(u8 *hour,u8 *min,u8 *sec,u8 *ampm)
{
RTC_TimeTypeDef RTC_TimeStructure;
RTC_GetTime(RTC_Format_BCD,&RTC_TimeStructure); //读取时间
*hour = RTC_BCD2DEC(RTC_TimeStructure.RTC_Hours); //获取小时
*min = RTC_BCD2DEC(RTC_TimeStructure.RTC_Minutes); //获取分钟
*sec = RTC_BCD2DEC(RTC_TimeStructure.RTC_Seconds); //获取秒钟
*ampm = RTC_TimeStructure.RTC_H12; //获取ampm
}
void RTC_Get_Date(u8 *year,u8 *month,u8 *date,u8 *week)
{
RTC_DateTypeDef RTC_DateStructure;
RTC_GetDate(RTC_Format_BCD,&RTC_DateStructure);
*year = RTC_BCD2DEC(RTC_DateStructure.RTC_Year);
*month = RTC_BCD2DEC(RTC_DateStructure.RTC_Month);
*date = RTC_BCD2DEC(RTC_DateStructure.RTC_Date);
*week = RTC_DateStructure.RTC_WeekDay;
}
/**
* @brief 显示时间和日期
* @param 无
* @retval 无
*/
void RTC_TimeAndDate_Show(void)
{
int a=1;
uint8_t Rtctmp=0;
char LCDTemp[100];
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
while(a<30)
{
// 获取日历
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
// 每秒打印一次
if(Rtctmp != RTC_TimeStructure.RTC_Seconds)
{
// 打印日期
printf("The Date : Y:20%0.2d - M:%0.2d - D:%0.2d - W:%0.2d\r\n",
RTC_DateStructure.RTC_Year,
RTC_DateStructure.RTC_Month,
RTC_DateStructure.RTC_Date,
RTC_DateStructure.RTC_WeekDay);
//液晶显示日期
//先把要显示的数据用sprintf函数转换为字符串,然后才能用液晶显示函数显示
sprintf(LCDTemp,"The Date:Y:20%0.2d-M:%0.2d-D:%0.2d-W:%0.2d",
RTC_DateStructure.RTC_Year,
RTC_DateStructure.RTC_Month,
RTC_DateStructure.RTC_Date,
RTC_DateStructure.RTC_WeekDay);
#ifdef USE_LCD_DISPLAY
ILI9806G_DispStringLine_EN(LINE(8),LCDTemp);
#endif
// 打印时间
printf("The Time : %0.2d:%0.2d:%0.2d \r\n\r\n",
RTC_TimeStructure.RTC_Hours,
RTC_TimeStructure.RTC_Minutes,
RTC_TimeStructure.RTC_Seconds);
//液晶显示时间
sprintf(LCDTemp,"The Time : %0.2d:%0.2d:%0.2d",
RTC_TimeStructure.RTC_Hours,
RTC_TimeStructure.RTC_Minutes,
RTC_TimeStructure.RTC_Seconds);
#ifdef USE_LCD_DISPLAY
ILI9806G_DispStringLine_EN(LINE(10),LCDTemp);
#endif
(void)RTC->DR;
}
Rtctmp = RTC_TimeStructure.RTC_Seconds;
a++;
}
}
/**
* @brief RTC配置:选择RTC时钟源,设置RTC_CLK的分频系数
* @param 无
* @retval 无
*/
void RTC_CLK_Config(void)
{
RTC_InitTypeDef RTC_InitStructure;
/*使能 PWR 时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* PWR_CR:DBF置1,使能RTC、RTC备份寄存器和备份SRAM的访问 */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI)
/* 使用LSI作为RTC时钟源会有误差
* 默认选择LSE作为RTC的时钟源
*/
/* 使能LSI */
RCC_LSICmd(ENABLE);
/* 等待LSI稳定 */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
/* 选择LSI做为RTC的时钟源 */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#elif defined (RTC_CLOCK_SOURCE_LSE)
/* 使能LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* 等待LSE稳定 */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* 选择LSE做为RTC的时钟源 */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#endif /* RTC_CLOCK_SOURCE_LSI */
/* 使能RTC时钟 */
RCC_RTCCLKCmd(ENABLE);
/* 等待 RTC APB 寄存器同步 */
RTC_WaitForSynchro();
/*=====================初始化同步/异步预分频器的值======================*/
/* 驱动日历的时钟ck_spare = LSE/[(255+1)*(127+1)] = 1HZ */
/* 设置异步预分频器的值 */
RTC_InitStructure.RTC_AsynchPrediv = ASYNCHPREDIV;
/* 设置同步预分频器的值 */
RTC_InitStructure.RTC_SynchPrediv = SYNCHPREDIV;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
/* 用RTC_InitStructure的内容初始化RTC寄存器 */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
printf("\n\r RTC 时钟初始化失败 \r\n");
}
}
/**
* @brief RTC配置:选择RTC时钟源,设置RTC_CLK的分频系数
* @param 无
* @retval 无
*/
#define LSE_STARTUP_TIMEOUT ((uint16_t)0x05000)
void RTC_CLK_Config_Backup(void)
{
__IO uint16_t StartUpCounter = 0;
FlagStatus LSEStatus = RESET;
RTC_InitTypeDef RTC_InitStructure;
/* 使能 PWR 时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* PWR_CR:DBF置1,使能RTC、RTC备份寄存器和备份SRAM的访问 */
PWR_BackupAccessCmd(ENABLE);
/*=========================选择RTC时钟源==============================*/
/* 默认使用LSE,如果LSE出故障则使用LSI */
/* 使能LSE */
RCC_LSEConfig(RCC_LSE_ON);
/* 等待LSE启动稳定,如果超时则退出 */
do
{
LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
StartUpCounter++;
}while((LSEStatus == RESET) && (StartUpCounter != LSE_STARTUP_TIMEOUT));
if(LSEStatus == SET )
{
printf("\n\r LSE 启动成功 \r\n");
/* 选择LSE作为RTC的时钟源 */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
}
else
{
printf("\n\r LSE 故障,转为使用LSI \r\n");
/* 使能LSI */
RCC_LSICmd(ENABLE);
/* 等待LSI稳定 */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
printf("\n\r LSI 启动成功 \r\n");
/* 选择LSI作为RTC的时钟源 */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
}
/* 使能 RTC 时钟 */
RCC_RTCCLKCmd(ENABLE);
/* 等待 RTC APB 寄存器同步 */
RTC_WaitForSynchro();
/*=====================初始化同步/异步预分频器的值======================*/
/* 驱动日历的时钟ck_spare = LSE/[(255+1)*(127+1)] = 1HZ */
/* 设置异步预分频器的值为127 */
RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
/* 设置同步预分频器的值为255 */
RTC_InitStructure.RTC_SynchPrediv = 0xFF;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
/* 用RTC_InitStructure的内容初始化RTC寄存器 */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
printf("\n\r RTC 时钟初始化失败 \r\n");
}
}
/**********************************END OF FILE*************************************/
main.c文件内容如下:
/**
******************************************************************************
* @file main.c
* @author fire
* @version V1.0
* @date 2015-xx-xx
* @brief HTTP 网页服务器
******************************************************************************
* @attention
*
* 实验平台:野火 STM32 F407 开发板
* 论坛 :http://www.firebbs.cn
* 淘宝 :https://fire-stm32.taobao.com
*
******************************************************************************
*/
#include "stm32f4xx.h"
#include "./Bsp/led/bsp_led.h"
#include "./Bsp/lcd/bsp_ili9806g_lcd.h"
#include "./Bsp/usart/bsp_debug_usart.h"
#include "./Bsp/systick/bsp_SysTick.h"
#include "lwip/tcp.h"
#include "netconf.h"
#include "LAN8742A.h"
#include "httpd.h"
#include "./Bsp/RTC/bsp_rtc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern __IO uint8_t EthLinkStatus;
__IO uint32_t LocalTime = 0; /* this variable is used to create a time reference incremented by 10ms */
/* Private function prototypes -----------------------------------------------*/
static void TIM3_Config(uint16_t period,uint16_t prescaler);
/* Private functions ---------------------------------------------------------*/
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
/* 初始化LED */
LED_GPIO_Config();
ILI9806G_Init ();
/* 初始化调试串口,一般为串口1 */
Debug_USART_Config();
printf("\n\r这是一个RTC日历实验 \r\n");
#ifdef USE_LCD_DISPLAY
ILI9806G_GramScan (5);
#endif
/* 初始化系统滴答定时器 */
SysTick_Init();
TIM3_Config(999,899);//10ms定时器
printf("LAN8720A Ethernet Demo\n");
printf("在电脑端浏览器直接输入地址:%d.%d.%d.%d,既可访问\n",IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
/* Configure ethernet (GPIOs, clocks, MAC, DMA) */
ETH_BSP_Config();
printf("LAN8720A BSP INIT AND COMFIGURE SUCCESS\n");
/* Initilaize the LwIP stack */
LwIP_Init();
/* Http webserver Init */
httpd_init();
RTC_CLK_Config();
/* 显示时间和日期 */
while(1)
{
/* check if any packet received */
if (ETH_CheckFrameReceived())
{
/* process received ethernet packet */
LwIP_Pkt_Handle();
}
/* handle periodic timers for LwIP */
LwIP_Periodic_Handle(LocalTime);
RTC_TimeAndDate_Show();
}
}
/**
* @brief 通用定时器3中断初始化
* @param period : 自动重装值。
* @param prescaler : 时钟预分频数
* @retval 无
* @note 定时器溢出时间计算方法:Tout=((period+1)*(prescaler+1))/Ft us.
* Ft=定时器工作频率,为SystemCoreClock/2=90,单位:Mhz
*/
static void TIM3_Config(uint16_t period,uint16_t prescaler)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); ///使能TIM3时钟
TIM_TimeBaseInitStructure.TIM_Prescaler=prescaler; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_Period=period; //自动重装载值
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许定时器3更新中断
TIM_Cmd(TIM3,ENABLE); //使能定时器3
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief 定时器3中断服务函数
* @param 无
* @retval 无
*/
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
{
LocalTime+=10;//10ms增量
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
烧录程序测试:
在浏览器中输入192.168.1.122就可以访问页面。
CSDN:基于STM32F407的嵌入式WebServer例程