📄 p16web.c
字号:
#include "p16_cap.c"
#endif // VID
#endif // UDP
// Default TRIS values for I/O ports
// May be overriden by included files
#ifndef TRISA_VAL
#define TRISA_VAL 0x03 // Port A, bit 0 and 1 analog I/Ps
#endif
#ifndef TRISB_VAL
#define TRISB_VAL 0x20 // Port B, bit 5 pushbutton I/P
#endif
#ifndef TRISC_VAL
#define TRISC_VAL (ALL_IN) // Port C, RS232, i2c, etc
#endif
#ifndef TRISD_VAL
#define TRISD_VAL (ALL_IN) // Port D, data bus
#endif
#ifndef TRISE_VAL
#define TRISE_VAL (ALL_OUT) // Port E, NIC control lines
#endif
void main(void)
{
disable_interrupts(GLOBAL); // Ensure interrupts off
LCD_E = 0; // Disable LCD
SETUP_ADC_PORTS(RA0_ANALOG); // RA0 analog I/P
set_tris_a(TRISA_VAL);
SETUP_ADC(ADC_CLOCK_DIV_32);
SET_ADC_CHANNEL(0);
PORTC = 0;
set_tris_c(TRISC_VAL);
set_tris_d(TRISD_VAL);
NIC_IOW_ = NIC_IOR_ = 1; // Disable NIC
NIC_RESET = 1;
set_tris_e(TRISE_VAL);
init_serial(); // Set up serial
#if SERIAL_INTERRUPTS
enable_serints();
#endif
init_i2c(); // ..and i2c bus
port_b_pullups(TRUE); // Use pullups on port B
TIMER_1_HIGH = 0;
setup_timer_1(TIMER1_SET); // Init timer
T1CON &= 0x7f;
timeout(&ledticks, 0); // Force timeout of LED timer
button_iostate = 1; // Init button debouce variables
button_down = button_in = 0;
USERLED1 = 1; // User LEDs off
USERLED2 = 1;
#if INCLUDE_LCD
init_lcd(); // Init LCD
#endif
#if INCLUDE_TIME
init_time();
#endif
#if INCLUDE_ETH
LCD_SERIAL_OUTPUT; // Set display flags
#else
LCD_OUTPUT; // Suppress serial signon if PPP
#endif
putstr(SIGNON); // Sign on
putstr(COMPILERID);
putch('\n');
#ifdef FIXED_CONFIG
read_nonvol();
if (!USER_BUTTON)
putstr("Fixed config");
while (!USER_BUTTON) ;
#else
while (!read_nonvol() || !USER_BUTTON) // If csum error, or button
{
putstr("Config ");
user_config(); // ..call user config
}
#endif
init_net(); // Init Ethernet or PPP
#if INCLUDE_ETH
#if INCLUDE_DHCP
myip.l = 0; // Null IP addr for DHCP..
disp_myip(); // ..display it
putstr(" DHCP");
#else
net_ok = 1; // If static IP addr, network is OK
init_ip(&router_ip, ROUTER_ADDR); // Initialise router addr
init_ip(&netmask, NETMASK_ADDR); // ..and subnet mask
disp_myip(); // Display my IP addr
#endif
#endif
#if INCLUDE_PPP
putstr("PPP 38400 baud");
#endif
#if INCLUDE_POP3
put_ser("\r\nPress any key to run POP3 client..");
#endif
put_ser("\r\n");
while (1) // Main loop..
{
scan_io(); // Scan I/O, check timer
#if INCLUDE_DHCP
check_dhcp(); // Check DHCP lease expiry
#endif
#if INCLUDE_TIME
check_time(); // Check network real-time clock
#endif
#if INCLUDE_VID
check_cap();
#endif
#if INCLUDE_SMTP
check_client();
if (button_down != button_in)
{
button_in = button_down;
if (button_down)
start_client(SMTP_PORT);
}
#endif
#if INCLUDE_POP3
check_client();
if (kbhit())
{
getch();
start_client(POP3_PORT);
}
#endif
if (get_net()) // If frame has arrived..
{
init_txbuff(0); // ..and Tx buffer
#if INCLUDE_ETH
if (host.eth.pcol == PCOL_ARP)
arp_recv(); // ..is it ARP?
if (host.eth.pcol == PCOL_IP && ip_recv())
#else
if (ip_recv())
#endif
{ // ..or is it IP?
// Launch protocol handler here (and not in ip_recv) to keep stack usage low
if (ipcol == PICMP) // ICMP?
icmp_recv(); // ..call ping handler
#if INCLUDE_UDP
else if (ipcol == PUDP) // UDP datagram?
udp_recv(); // ..call UDP handler
#endif
#if INCLUDE_TCP
else if (ipcol == PTCP) // TCP segment?
{
tcp_recv(); // ..call TCP handler
}
#endif
}
net_rxin = 0;
}
}
}
/* Update the current tick count, return non-zero if changed
** Avoid using boolean local variables, to keep Hitech happy */
BOOL geticks(void)
{
static BYTE tc, lastc=0;
tc = TIMER_1_HIGH - lastc;
if (tc >= TIMER1_DIV)
{
tickcount++;
lastc += TIMER1_DIV;
return 1;
}
return 0;
}
/* Check for timeout using the given tick counter and timeout value
** Force timeout if timeout value is zero */
BOOL timeout(WORD *varp, WORD tout)
{
WORD diff;
diff = tickcount - *varp;
if (!tout || diff >= tout)
{
*varp = tickcount;
return 1;
}
return 0;
}
/* Read ADC values
** Briefly enable RA1 as analog I/P, as this disables RA3 as digital O/P */
void get_adcvals(void)
{
adc1 = read_adc(); // Get first value
#if !INCLUDE_VID
SETUP_ADC_PORTS(RA0_RA1_RA3_ANALOG); // Enable RA1 analog
SET_ADC_CHANNEL(1); // Set multiplexer
delay_us(10); // Allow to setle
adc2 = read_adc(); // Get 2nd value
SETUP_ADC_PORTS(RA0_ANALOG); // Restore RA1 and multiplexer
SET_ADC_CHANNEL(0);
#endif
}
/* Check timer, scan ADCs, toggle LED if timeout */
void scan_io(void)
{
restart_wdt(); // Kick watchdog
if (geticks()) // Get tick count
{ // Debounce user pushutton
if (USER_BUTTON == button_iostate)
button_down = !button_iostate;
else
button_iostate = !button_iostate;
if (tickcount & 1)
{
if (flashled1) // Fast flash user LEDs
USERLED1 = !USERLED1;
if (flashled2)
USERLED2 = !USERLED2;
}
}
get_adcvals(); // Read ADC values
if (timeout(&ledticks, LEDTIME))
SYSLED = !SYSLED; // Toggle system LED
}
/* Display handler; redirects O/P char to LCD, serial, network */
void putch(BYTE b)
{
#if INCLUDE_LCD
if (print_lcd) // O/P to LCD
{
if (b == '\r')
lcd_cmd(LCD_SETPOS);
else if (b == '\n')
lcd_cmd(LCD_SETPOS + LCD_LINE2);
else
lcd_char(b);
}
#endif
if (print_serial) // O/P to serial
{
if (b == '\n')
serial_putch('\r');
serial_putch(b);
}
if (print_net)
{
put_byte(b); // O/P to network Tx buffer, with checksum
}
}
/* Flush the serial buffer */
void flush_serial(void)
{
while (serial_kbhit())
serial_getch();
}
/* Initialise an IP address using the given constants */
void init_ip(LWORD *lwp, BYTE a, BYTE b, BYTE c, BYTE d)
{
lwp->b[0] = d;
lwp->b[1] = c;
lwp->b[2] = b;
lwp->b[3] = a;
}
/* Display my IP address */
void disp_myip(void)
{
print_lcd = print_serial = TRUE; // Set display flags
print_net = FALSE;
putch('\n'); // Display IP address
#if INCLUDE_LCD
lcd_clearline(2);
#endif
disp_decword(myip.b[3]);
putch('.');
disp_decword(myip.b[2]);
putch('.');
disp_decword(myip.b[1]);
putch('.');
disp_decword(myip.b[0]);
}
/* Display a word in unsigned decimal format
** Return the digit count */
BYTE disp_decword(WORD w)
{
BYTE count, n=0, d[5];
do
{
d[n++] = (BYTE)(w % 10) + '0';
w /= 10;
} while(w);
count = n;
while (n)
putch(d[--n]);
return(count);
}
#if DEBUG
/* Display a byte in hexadecimal format */
void disp_hexbyte(BYTE b)
{
disp_hexdig(b >> 4);
disp_hexdig(b);
}
/* Display a hexadecimal digit */
void disp_hexdig(BYTE b)
{
b &= 0x0f;
if (b <= 9)
putch(b + '0');
else
putch(b + '7');
}
#endif
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -