📄 maindemo.c
字号:
/*********************************************************************
*
* Example Web Server Application using Microchip TCP/IP Stack
*
*********************************************************************
* FileName: MainDemo.c
* Dependencies: string.H
* StackTsk.h
* Tick.h
* http.h
* MPFS.h
* mac.h
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
* Complier: Microchip C18 v3.02 or higher
* Microchip C30 v2.01 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* This software is owned by Microchip Technology Inc. ("Microchip")
* and is supplied to you for use exclusively as described in the
* associated software agreement. This software is protected by
* software and other intellectual property laws. Any use in
* violation of the software license may subject the user to criminal
* sanctions as well as civil liability. Copyright 2006 Microchip
* Technology Inc. All rights reserved.
*
* This software is provided "AS IS." MICROCHIP DISCLAIMS ALL
* WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
* TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* INFRINGEMENT. Microchip shall in no event be liable for special,
* incidental, or consequential damages.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Nilesh Rajbharti 4/19/01 Original (Rev. 1.0)
* Nilesh Rajbharti 2/09/02 Cleanup
* Nilesh Rajbharti 5/22/02 Rev 2.0 (See version.log for detail)
* Nilesh Rajbharti 7/9/02 Rev 2.1 (See version.log for detail)
* Nilesh Rajbharti 4/7/03 Rev 2.11.01 (See version log for detail)
* Howard Schlunder 10/1/04 Beta Rev 0.9 (See version log for detail)
* Howard Schlunder 10/8/04 Beta Rev 0.9.1 Announce support added
* Howard Schlunder 11/29/04 Beta Rev 0.9.2 (See version log for detail)
* Howard Schlunder 2/10/05 Rev 2.5.0
* Howard Schlunder 1/5/06 Rev 3.00
* Howard Schlunder 1/18/06 Rev 3.01 ENC28J60 fixes to TCP,
* UDP and ENC28J60 files
* Howard Schlunder 3/01/06 Rev. 3.16 including 16-bit micro support
* Howard Schlunder 4/12/06 Rev. 3.50 added LCD for Explorer 16
* Howard Schlunder 6/19/06 Rev. 3.60 finished dsPIC30F support, added PICDEM.net 2 support
********************************************************************/
/*
* Following define uniquely deines this file as main
* entry/application In whole project, there should only be one such
* definition and application file must define AppConfig variable as
* described below.
*/
#define THIS_IS_STACK_APPLICATION
#define VERSION "v3.60b" // TCP/IP stack version
#define BAUD_RATE (19200) // bps
// These headers must be included for required defs.
#include <string.h>
#include "..\Include\Compiler.h"
#include "..\Include\StackTsk.h"
#include "..\Include\Tick.h"
#include "..\Include\MAC.h"
#include "..\Include\Helpers.h"
#include "..\Include\Delay.h"
#include "..\Include\UART.h"
#include "..\Include\MPFS.h"
#include "..\Include\LCDBlocking.h"
#if defined(STACK_USE_DHCP)
#include "..\Include\DHCP.h"
#endif
#if defined(STACK_USE_HTTP_SERVER)
#include "..\Include\HTTP.h"
#endif
#if defined(STACK_USE_FTP_SERVER) && defined(MPFS_USE_EEPROM)
#include "..\Include\FTP.h"
#endif
#if defined(STACK_USE_ANNOUNCE)
#include "..\Include\Announce.h"
#endif
#if defined(MPFS_USE_EEPROM)
#include "..\Include\XEEPROM.h"
// MASTERS labs
extern void Lab4(void);
extern void Lab5(void);
#else
#define Lab4()
#define Lab5()
#endif
// The SPBRG_VAL is used for PIC18s only. See InitializeBoard() for
// PIC24, dsPIC initialization of the baud rate generator.
#define USART_USE_BRGH_LOW
#if defined(USART_USE_BRGH_LOW)
#define SPBRG_VAL ( ((INSTR_FREQ/BAUD_RATE)/16) - 1)
#else
#define SPBRG_VAL ( ((INSTR_FREQ/BAUD_RATE)/4) - 1)
#endif
#if (SPBRG_VAL > 255) && !defined(__C30__)
#error "Calculated SPBRG value is out of range for currnet CLOCK_FREQ."
#endif
// This is used by other stack elements.
// Main application must define this and initialize it with proper values.
APP_CONFIG AppConfig =
{
{MY_DEFAULT_IP_ADDR_BYTE1, MY_DEFAULT_IP_ADDR_BYTE2, MY_DEFAULT_IP_ADDR_BYTE3, MY_DEFAULT_IP_ADDR_BYTE4},
{MY_DEFAULT_MAC_BYTE1, MY_DEFAULT_MAC_BYTE2, MY_DEFAULT_MAC_BYTE3, MY_DEFAULT_MAC_BYTE4, MY_DEFAULT_MAC_BYTE5, MY_DEFAULT_MAC_BYTE6},
{MY_DEFAULT_MASK_BYTE1, MY_DEFAULT_MASK_BYTE2, MY_DEFAULT_MASK_BYTE3, MY_DEFAULT_MASK_BYTE4},
{MY_DEFAULT_GATE_BYTE1, MY_DEFAULT_GATE_BYTE2, MY_DEFAULT_GATE_BYTE3, MY_DEFAULT_GATE_BYTE4}
};
BYTE myDHCPBindCount = 0;
#if defined(STACK_USE_DHCP)
extern BYTE DHCPBindCount;
#else
#define DHCPBindCount (0xFF)
#endif
/*
* Set configuration fuses
*/
#if defined(__18CXX)
#if defined(__18F8722)
#pragma config OSC=HSPLL, FCMEN=OFF, IESO=OFF, PWRT=OFF, WDT=OFF, LVP=OFF
#elif defined(__18F87J10) || defined(__18F86J15) || defined(__18F86J10) || defined(__18F85J15) || defined(__18F85J10) || defined(__18F67J10) || defined(__18F66J15) || defined(__18F66J10) || defined(__18F65J15) || defined(__18F65J10)
#pragma config XINST=OFF, WDTEN=OFF, FOSC2=ON, FOSC=HSPLL
#elif defined(__18F97J60) || defined(__18F96J65) || defined(__18F96J60) || defined(__18F87J60) || defined(__18F86J65) || defined(__18F86J60) || defined(__18F67J60) || defined(__18F66J65) || defined(__18F66J60)
#pragma config XINST=OFF, WDT=OFF, FOSC2=ON, FOSC=HSPLL, ETHLED=ON
#elif defined(HI_TECH_C)
__CONFIG(1, HSPLL);
__CONFIG(2, WDTDIS);
__CONFIG(3, MCLREN);
__CONFIG(4, XINSTDIS & LVPDIS);
#endif
#elif defined(__PIC24F__)
_CONFIG2(FNOSC_PRIPLL & POSCMOD_XT) // Primary XT OSC with 4x PLL
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF) // JTAG off, watchdog timer off
#elif defined(__dsPIC33F__) || defined(__PIC24H__)
_FOSCSEL(FNOSC_PRIPLL) // PLL enabled
_FOSC(OSCIOFNC_OFF & POSCMD_XT) // XT Osc
_FWDT(FWDTEN_OFF) // Disable Watchdog timer
// JTAG should be disabled as well
#elif defined(__dsPIC30F__)
_FOSC(XT_PLL16) // XT Osc + 16X PLL
_FWDT(WDT_OFF) // Disable Watchdog timer
_FBORPOR(MCLR_EN & PBOR_OFF & PWRT_OFF)
#endif
// Private helper functions.
// These may or may not be present in all applications.
static void InitAppConfig(void);
static void InitializeBoard(void);
static void ProcessIO(void);
BOOL StringToIPAddress(char *str, IP_ADDR *buffer);
static void DisplayIPValue(IP_ADDR *IPVal);
static void SetConfig(void);
#if defined(MPFS_USE_EEPROM)
static BOOL DownloadMPFS(void);
static void SaveAppConfig(void);
#else
#define SaveAppConfig()
#endif
// NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE FAST/MOVFF bug
// The interruptlow keyword is used to work around the bug when using C18
#if defined(HI_TECH_C)
void interrupt HighISR(void)
#else
#pragma interruptlow HighISR
void HighISR(void)
#endif
{
#ifdef __18CXX
TickUpdate();
#endif
#if defined(STACK_USE_SLIP)
MACISR();
#endif
}
#if defined(__18CXX) && !defined(HI_TECH_C)
#pragma code highVector=0x08
void HighVector (void)
{
_asm goto HighISR _endasm
}
#pragma code // Return to default code section
#endif
ROM char NewIP[] = "New IP Address: ";
ROM char CRLF[] = "\r\n";
// Main application entry point.
#ifdef __C30__
int main(void)
#else
void main(void)
#endif
{
static TICK t = 0;
// Initialize any application specific hardware.
InitializeBoard();
#ifdef USE_LCD
// Initialize and display the stack version on the LCD
LCDInit();
DelayMs(100);
strcpypgm2ram(LCDText, "TCPStack " VERSION " "
" ");
LCDUpdate();
#endif
// Initialize all stack related components.
// Following steps must be performed for all applications using
// PICmicro TCP/IP Stack.
TickInit();
// Following steps must be performed for all applications using
// PICmicro TCP/IP Stack.
MPFSInit();
// Initialize Stack and application related NV variables.
InitAppConfig();
// For MASTERs demos
// Self destruct the data in the EEPROM and our FLASH for next
// class session
if(BUTTON1_IO == 0 && BUTTON2_IO == 0)
{
#if defined(MPFS_USE_EEPROM)
WORD i;
// Delete AppConfig data and FAT
XEESetAddr(0, 0x0000);
for(i=0; i < 255; i++)
XEEWrite(0xFF);
XEEEndWrite();
#endif
#if defined(USE_LCD)
LCDErase();
#endif
#if !defined(HPC_EXPLORER)
// Erase PIC code
SelfDestruct();
#endif
while(1);
}
// Initiates board setup process if button is depressed
// on startup
if(BUTTON0_IO == 0)
{
SetConfig();
}
StackInit();
#if defined(STACK_USE_HTTP_SERVER)
HTTPInit();
#endif
#if defined(STACK_USE_FTP_SERVER) && defined(MPFS_USE_EEPROM)
FTPInit();
#endif
#if defined(STACK_USE_DHCP) || defined(STACK_USE_IP_GLEANING)
if(!AppConfig.Flags.bIsDHCPEnabled)
{
// Force IP address display update.
myDHCPBindCount = 1;
#if defined(STACK_USE_DHCP)
DHCPDisable();
#endif
}
#endif
// Once all items are initialized, go into infinite loop and let
// stack items execute their tasks.
// If application needs to perform its own task, it should be
// done at the end of while loop.
// Note that this is a "co-operative mult-tasking" mechanism
// where every task performs its tasks (whether all in one shot
// or part of it) and returns so that other tasks can do their
// job.
// If a task needs very long time to do its job, it must broken
// down into smaller pieces so that other tasks can have CPU time.
while(1)
{
// Blink SYSTEM LED every second.
if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
{
t = TickGet();
LED0_IO ^= 1;
}
// This task performs normal stack task including checking
// for incoming packet, type of packet and calling
// appropriate stack entity to process it.
StackTask();
#if defined(STACK_USE_HTTP_SERVER)
// This is a TCP application. It listens to TCP port 80
// with one or more sockets and responds to remote requests.
HTTPServer();
#endif
#if defined(STACK_USE_FTP_SERVER) && defined(MPFS_USE_EEPROM)
FTPServer();
#endif
// In future, as new TCP/IP applications are written, it
// will be added here as new tasks.
// Add your application specific tasks here.
ProcessIO();
Lab4();
Lab5();
// For DHCP information, display how many times we have renewed the IP
// configuration since last reset.
if ( DHCPBindCount != myDHCPBindCount )
{
myDHCPBindCount = DHCPBindCount;
putrsUART(NewIP);
DisplayIPValue(&AppConfig.MyIPAddr); // Print to UART
putrsUART(CRLF);
#if defined(STACK_USE_ANNOUNCE)
AnnounceIP();
#endif
}
}
}
static void DisplayIPValue(IP_ADDR *IPVal)
{
// printf("%u.%u.%u.%u", IPVal->v[0], IPVal->v[1], IPVal->v[2], IPVal->v[3]);
BYTE IPDigit[5];
#ifdef USE_LCD
BYTE i;
BYTE LCDPos=16;
#endif
itoa(IPVal->v[0], IPDigit);
putsUART(IPDigit);
#ifdef USE_LCD
for(i=0; i < strlen(IPDigit); i++)
{
LCDText[LCDPos++] = IPDigit[i];
}
LCDText[LCDPos++] = '.';
#endif
while(BusyUART());
WriteUART('.');
itoa(IPVal->v[1], IPDigit);
putsUART(IPDigit);
#ifdef USE_LCD
for(i=0; i < strlen(IPDigit); i++)
{
LCDText[LCDPos++] = IPDigit[i];
}
LCDText[LCDPos++] = '.';
#endif
while(BusyUART());
WriteUART('.');
itoa(IPVal->v[2], IPDigit);
putsUART(IPDigit);
#ifdef USE_LCD
for(i=0; i < strlen(IPDigit); i++)
{
LCDText[LCDPos++] = IPDigit[i];
}
LCDText[LCDPos++] = '.';
#endif
while(BusyUART());
WriteUART('.');
itoa(IPVal->v[3], IPDigit);
putsUART(IPDigit);
#ifdef USE_LCD
for(i=0; i < strlen(IPDigit); i++)
{
LCDText[LCDPos++] = IPDigit[i];
}
while(LCDPos < 32)
{
LCDText[LCDPos++] = ' ';
}
LCDUpdate();
#endif
while(BusyUART());
}
static char AN0String[8];
//static char AN1String[8] = "";
static void ProcessIO(void)
{
#ifdef __C30__
// Note: floats and sprintf uses a lot of program memory/CPU cycles, so it's commented out
// float Temperature;
//
// // Convert temperature result into ASCII string
// Temperature = ((float)(ADC1BUF0)*(3.3/1024.)-0.500)*100.;
// sprintf(AN1String, "%3.1f0C", Temperature);
// Convert potentiometer result into ASCII string
itoa((unsigned)ADC1BUF0, AN0String);
#else
// AN0 should already be set up as an analog input
// ADCON0bits.GO = 1;
// Wait until A/D conversion is done
// while(ADCON0bits.GO);
// AD converter errata work around (ex: PIC18F87J10 A2)
PRODL = ADCON2;
ADCON2bits.ADCS0 = 1;
ADCON2bits.ADCS1 = 1;
ADCON2 = PRODL;
// Convert 10-bit value into ASCII string
itoa(*((WORD*)(&ADRESL)), AN0String);
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -