大约今年年初,接到领导安排的任务,要开发一个单片机控制系统,其他的功能对我而言都比较常规,只是用到以太网通讯,第一次接触。本来以为自己要编写一个完整的TCP/IP协议栈,后来查找到PIC18F97J60这款单片机,有Microchip开源免费的协议栈可以使用,终于解决了方向问题。
工作电压3.3V , 采用纳瓦技术, 单片机功耗小;提供5 种不同的振荡器工作模式, 使用户在开发应用硬件时有很大的选择范围, 时钟频率最高可达到41.67 M H z;带有片上调试接口, 可以进行实时在线仿真和调试, 而无需仿真器;集成了128 KB 的片上ROM和3808 字节的数据R A M ,可满足大多数应用场合, 并可进行外部扩展;带有16 路通道的10 位AD 转换器、5 个定时器模块、4 个外部中断引脚、5个捕捉/ 比较/P W M (C PP )模块、2 个主控同步串行口模块(可支持SP I和护C 主/从模式)、2 个增强型USART模块、并行从动端口(1名P )模块、输人复用的双模拟比较器, 有多达70个通用1/ 0 引脚;
12) 支持全双工和半双工模式。P I C 1 8 F 9 7J6 o 单片机内部该以太网模块.
#define THIS_IS_STACK_APPLICATION
#define VERSION "v4.02" // TCP/IP stack version
// These headers must be included for required defs.
#include "TCPIP Stack/TCPIP.h"
#include "BSP_UART.H"
#include "DS1302.H"
#include
DS1302_VAL DS1302REG; //added by beyond
#define ConfigPortModule (WORD)1021
#define ConfigPortPC (WORD)1022
#define FLASH_CONFIG_ADDRESS (DWORD)0xF000
#define MAGICWORD (WORD)0x5050
#define MAXTCPLINKTIMER (DWORD)TICK_SECOND*10
#define MODE_UDP 1
#define MODE_TCP_SERVER 2
#define MODE_TCP_CLIENT 3
#define MAXRDY_COM 512
#define MAXRDY_ETH MAC_TX_BUFFER_SIZE
#define CNT_COM 340 //512*3/4
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define LED_LINK LED1_IO
#define LED_TRANS LED0_IO
#define LED_RUN LED0_IO
typedef enum _SM_APP
{
SM_APP_INIT,
SM_APP_ARP_ASK,
SM_APP_ARP_RESOLVE,
SM_APP_UDP_SOCKET,
SM_APP_UDP,
SM_APP_TCP_SERVER_SOCKET,
SM_APP_TCP_SERVER,
SM_APP_TCP_CLIENT_SOCKET,
SM_APP_TCP_CLIENT,
SM_APP_ERROR
} SM_APP;
// Set configuration fuses
#if defined(__18CXX)
#if defined(__18F97J60)
/*
DEBUG = OFF Background debugger disabled; RB6 and RB7 configured as general purpose I/O pins
XINST = OFF Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
STVR = ON Reset on stack overflow/underflow enabled
WDT = ON WDT enabled
CP0 = OFF Program memory is not code-protected
FCMEN = ON Fail-Safe Clock Monitor enabled
IESO = ON Two-Speed Start-up enabled
FOSC2 = ON Clock selected by FOSC1:FOSC0 as system clock is enabled when OSCCON<1:0> = 00
FOSC = HSPLL HS oscillator, PLL enabled and under software control
WDTPS = 16 1:16 about 64ms
ETHLED = ON RA0/RA1 are multiplexed with LEDA/LEDB when Ethernet module is enabled and function as I/O when Ethernet is disabled
*/
#pragma config DEBUG = ON, XINST=OFF, STVR = ON, WDT=OFF, CP0 = OFF, FCMEN = ON, IESO = ON, FOSC2=ON, FOSC=HSPLL, WDTPS = 32, ETHLED=ON
#else
#error " MCU define error";
#endif
#endif
// This is used by other stack elements.
// Main application must define this and initialize it with proper values.
APP_CONFIG AppConfig;
TICK ExchTimer;
SM_APP smApp;
UDP_SOCKET UDPConfigSocket = INVALID_UDP_SOCKET;
UDP_SOCKET UDPSocket = INVALID_UDP_SOCKET;
TCP_SOCKET TCPSocket = INVALID_SOCKET;
NODE_INFO Remote;
BOOL FlagFirstRun;
MAC_ADDR RemoteMACAddr;
BOOL LED_Blink;
TICK TCPLinkTimer;
#pragma udata SEC_BUF // locate 0x800
BYTE Buffer[512];
#pragma udata // Return to default section
// Private helper functions.
// These may or may not be present in all applications.
static void InitializeBoard(void);
// application config write and read
static void InitAppConfig(APP_CONFIG *pAppConfig);
static BOOL SaveAppConfig(APP_CONFIG *pAppConfig);
static BOOL LoadAppConfig(APP_CONFIG *pAppConfig);
static void FlashWrite(BYTE* pBuffer, BYTE Len, DWORD Addr);
static BYTE FlashRead(DWORD Addr);
static void FlashErase(DWORD Addr);
// applicaiton task
static void AppTask(void);
// configuration task
static void ConfigTask(void);
static void InitTask(void);
static void socketprocess(void);
static void rs232process(void);
unsigned char ascii_rec(unsigned char ascii);
#pragma interruptlow LowISR
void LowISR(void)
{
TickUpdate();
}
#pragma code lowVector=0x18
void LowVector(void){_asm goto LowISR _endasm}
#pragma code
unsigned char socketrecflag=0;
unsigned char socketsendflag=0;
char STXdata[100],STXCount,STXlen;
unsigned char interval;
//*****************************************
// Main application entry point.
void main(void)
{
static TICK t = 0;
// Initialize any application specific hardware.
InitializeBoard();
// Initialize all stack related components.
// Following steps must be performed for all applications using
// the Microchip TCP/IP Stack.
TickInit();
RS485_Init();
// Initiates board setup process if button is depressed
// on startup
if(BUTTON_IO == 0)
{
LED_LINK = 0;
// Initialize AppConfig and save it into flash memory
InitAppConfig(&AppConfig);
SaveAppConfig(&AppConfig);
LED_LINK = 1;
}
else
{
//read configuration from Flash Memory
LoadAppConfig(&AppConfig);
// test the magic flag, if not then initialize, save the config to Flash Memory
if(AppConfig.MagicWord != MAGICWORD)
{
LED_LINK = 0;
// Initialize AppConfig and save it into flash memory
InitAppConfig(&AppConfig);
SaveAppConfig(&AppConfig);
LED_LINK = 1;
}
}
// Initialize core stack layers (MAC, ARP, TCP, UDP)
StackInit();
FlagFirstRun = TRUE;
// Once all items are initialized, go into infinite loop and let
// stack items to 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 be broken
// down into smaller pieces so that other tasks can have CPU time.
while(1)
{
ReadRTC(&DS1302REG);
InitTask();
// This task performs normal stack task including checking
// for incoming packet, type of packet and calling
// appropriate stack entity to process it.
uart485process();
StackTask();
// Configuration Task
ConfigTask();
// Application Task
AppTask();
//BYTE i;
// Blink SYSTEM LED every second.
if( LED_Blink == TRUE)
{
LED_Blink = FALSE;
LED_TRANS = 0;
t = TickGet();
}
if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/50 )
{
LED_TRANS = 1;
}
/*
if ( TickGetDiff(TickGet(), t) >= TICK_SECOND/2 )
{
t = TickGet();
LED_RUN ^= 1;
}
*/
_asm CLRWDT _endasm
}
}
文章评论(0条评论)
登录后参与讨论