原创 【博客大赛】基于PIC18F97J60串口服务器的调试心得

2014-10-8 10:33 1697 12 12 分类: 通信

        大约今年年初,接到领导安排的任务,要开发一个单片机控制系统,其他的功能对我而言都比较常规,只是用到以太网通讯,第一次接触。本来以为自己要编写一个完整的TCP/IP协议栈,后来查找到PIC18F97J60这款单片机,有Microchip开源免费的协议栈可以使用,终于解决了方向问题。

       PIC 18 F9 7J 60 单片机是Microchip公司生产的8 位单片机, 主要特性如下:

       工作电压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 引脚;

       内部集成有嵌人式以太网控制器模块。其中内嵌的以太网控制器模块具有以下特性:
l) IE E E 802.3 兼容的以太网控制器;
2) 集成M A C 和10Base- T P H Y ;
3) SK B 发送/接收数据包缓冲区S R A M ;
4) 支持一个带自动极性检测和校正的10 B as e- T端口;
5) 在发生冲突时可编程自动重发;
6) 可编程填充和C R C 生成;
7) 可编程自动拒绝错误数据包;
8) 可驱动2 个LED 指示器的活动输出信号;
9) 可配置发送/接收缓冲区大小, 快速数据传送的内部D M A 访问模式, 支持各种协议的硬件校验和计算;
10 ) 支持单播、组播和广播数据包, 多个数据包格式的可编程唤醒功能;
11) P H Y 具有整形输出滤波器和环回模式功能;
12) 支持全双工和半双工模式。P I C 1 8 F 9 7J6 o 单片机内部该以太网模块.
 
下面贴出我调试成功的maindemo源码:
 
#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条评论)

登录后参与讨论
我要评论
0
12
关闭 站长推荐上一条 /2 下一条