Professional Documents
Culture Documents
"TCPIP Stack/TCPIP.h"
"webserver.h"
"servo.h"
"srf08.h"
<p32xxxx.h>
<peripheral/timer.h> //neccesary for interrupt timers
"timer.h"
{
// LEDs
LED0_TRIS = 0;
LED1_TRIS = 0;
LED2_TRIS = 0;
LED3_TRIS = 0;
LED4_TRIS = 0;
LED5_TRIS = 0;
LED6_TRIS = 0;
LED7_TRIS = 0;
LED_PUT(0x00);
#if defined(__PIC32MX__)
{
// Enable multi-vectored interrupts
INTEnableSystemMultiVectoredInt();
// Enable optimal performance
SYSTEMConfigPerformance(GetSystemClock());
mOSCSetPBDIV(OSC_PB_DIV_1); // Use 1:1 CPU Core:Peripheral clocks
// Disable JTAG port so we get our I/O pins back, but first
// wait 50ms so if you want to reprogram the part with
// JTAG, you'll still have a tiny window before JTAG goes away.
// The PIC32 Starter Kit debuggers use JTAG and therefore must not
// disable JTAG.
DelayMs(50);
#if !defined(__MPLAB_DEBUGGER_PIC32MXSK) && !defined(__MPLAB_DEBUGGER_FS2)
DDPCONbits.JTAGEN = 0;
#endif
LED_PUT(0x00); // Turn the LEDs off
CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5
, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards
}
#endif
AD1CHS = 0; // Input to AN0 (potentiometer)
AD1PCFGbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor)
AD1PCFGbits.PCFG2 = 0; // Disable digital input on AN2 (potentiometer)
// ADC
AD1CON1 =
on parts with
AD1CON2 =
AD1CON3 =
AD1CSSL =
0x84E4;
a 12bit
0x0404;
0x1003;
1 << 2;
// Deassert all chip select lines so there isn't any problem with
// initialization order. Ex: When ENC28J60 is on SPI2 with Explorer 16,
// MAX3232 ROUT2 pin will drive RF12/U2CTS ENC28J60 CS line asserted,
// preventing proper 25LC256 EEPROM operation.
#if defined(ENC_CS_TRIS)
ENC_CS_IO = 1;
ENC_CS_TRIS = 0;
#endif
}
/*********************************************************************
* Function:
void InitAppConfig(void)
*
* PreCondition:
MPFSInit() is already called.
*
* Input:
None
*
* Output:
Write/Read non-volatile config variables.
*
* Side Effects:
None
*
* Overview:
None
*
* Note:
None
********************************************************************/
// MAC Address Serialization using a MPLAB PM3 Programmer and
// Serialized Quick Turn Programming (SQTP).
// The advantage of using SQTP for programming the MAC Address is it
// allows you to auto-increment the MAC address without recompiling
// the code for each unit. To use SQTP, the MAC address must be fixed
// at a specific location in program memory. Uncomment these two pragmas
// that locate the MAC address at 0x1FFF0. Syntax below is for MPLAB C
// Compiler for PIC18 MCUs. Syntax will vary for other compilers.
//#pragma romdata MACROM=0x1FFF0
static ROM BYTE SerializedMACAddress[6] = {MY_DEFAULT_MAC_BYTE1, MY_DEFAULT_MAC_
BYTE2, MY_DEFAULT_MAC_BYTE3, MY_DEFAULT_MAC_BYTE4, MY_DEFAULT_MAC_BYTE5, MY_DEFA
ULT_MAC_BYTE6};
//#pragma romdata
void InitAppConfig(void)
{
#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
unsigned char vNeedToSaveDefaults = 0;
#endif
while (1)
{
// Start out zeroing all AppConfig bytes to ensure all fields are
// deterministic for checksum generation
memset((void*) &AppConfig, 0x00, sizeof (AppConfig));
AppConfig.Flags.bIsDHCPEnabled = TRUE;
AppConfig.Flags.bInConfigMode = TRUE;
memcpypgm2ram((void*) &AppConfig.MyMACAddr, (ROM void*) SerializedMACAdd
ress, sizeof (AppConfig.MyMACAddr));
//
{
//
_prog_addressT MACAddressAddress;
//
MACAddressAddress.next = 0x157F8;
//
_memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAd
dressAddress, sizeof(AppConfig.MyMACAddr));
//
}
AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_B
YTE2 << 8ul | MY_DEFAULT_IP_ADDR_BYTE3 << 16ul | MY_DEFAULT_IP_ADDR_BYTE4 << 24u
l;
AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2 <<
8ul | MY_DEFAULT_MASK_BYTE3 << 16ul | MY_DEFAULT_MASK_BYTE4 << 24ul;
AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2
<< 8ul | MY_DEFAULT_GATE_BYTE3 << 16ul | MY_DEFAULT_GATE_BYTE4 << 24ul;
AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAU
LT_PRIMARY_DNS_BYTE2 << 8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3 << 16ul | MY_DEFAULT_
||
(NVMValidationStruct.wOriginalChecksum != wOriginalAppConfig
Checksum) ||
(NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYT
E*) & AppConfig, sizeof (AppConfig))))
{
// Check to ensure that the vNeedToSaveDefaults flag is zero,
// indicating that this is the first iteration through the do
// loop. If we have already saved the defaults once and the
// EEPROM/Flash still doesn't pass the validity check, then it
// means we aren't successfully reading or writing to the
// EEPROM/Flash. This means you have a hardware error and/or
// SPI configuration error.
if (vNeedToSaveDefaults)
{
while (1);
}
// Set flag and restart loop to load ROM defaults and save them
vNeedToSaveDefaults = 1;
continue;
}
// If we get down here, it means the EEPROM/Flash has valid contents
// and either matches the ROM defaults or previously matched and
// was run-time reconfigured by the user. In this case, we shall
// use the contents loaded from EEPROM/Flash.
break;
}
#endif
break;
}
}
void BlinkLed1(void)
{
// Blink LD1 every second.
if (TickGet() - t >= TICK_SECOND / 2ul)
{
t = TickGet();
LED0_IO ^= 1;
}
}