I don't know what the "best" way to aproach this actually is. I don't think me posting hundreds or thousands of lines of code is going to actually help others that much and I don't think I'll get much out of it either.
Instead I think I'll post snippets and routines, explaining what they do (or at least intend to do) and why. This way I think it's easier to tag along, one step at the time and, which is important, we can discuss suggestions on improvements and alternative ways of doing the same thing. In the end we will hopefully have a set of routines which can be used to build a working application.
Before we go on I just have to point out that this is new territory for me too. I don't know much about ethernet, UDP, TCP, sockets, HTTP, HTML etc so this is very much a learning experience for me as well. Please feel free to correct any statements I make in error and please do bring suggestions on how to do things differently and, hopefully, better.
Right, the Wiznet W5100 ethernet chip (which is used on the Ethernet shield) is controlled by reading and writing to its internal registers. There are quite a few registers and instead of using a bunch of 'magic numbers' (ie the adress of the register in the W5100's memory) I've put together a file (W5100_defs.pbp) containing the name and memory adress for the registers as well as a couple of other constants we might need. Attached to this post is v1.0 of that file (remove .txt extenstion), there might be newer versions in the posts to come.
I've set up my AMICUS18 board to use the hardware USART at 115200 baud for debugging purposes. Then I set up the TRISB and TRISC registers to match the pinout of the modified Ethernet shield:
' By default the AMICUS18 board uses the PIC18F25K20, make sure to ' set up MCSP or whatever IDE is being used to compile for the ' correct device. DEFINE OSC 64 DEFINE LOADER_USED 1 ' We're using a bootloader. DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1 DEFINE HSER_CLROERR 1 ' Clear overflow automatically DEFINE HSER_SPBRG 138 ' 115200 Baud @ 64MHz, -0,08% SPBRGH = 0 BAUDCON.3 = 1 ' Enable 16 bit baudrate generator TRISC = %10010111 ' RC7: USART RX ' RC6: USART TX ' RC5: ETHERNET SPI Data out (SDO/MOSI) ' RC4: ETHERNET SPI Data in (SDI/MISO) ' RC3: ETHERNET SPI clock (SCK) set to INPUT according to datasheet.... ' RC2: N/U - currently ' RC2: N/U - currently ' RC1: N/U - currently TRISB = %11011011 ' RB7: N/U - currently ' RB6: N/U - currently ' RB5: ETHERNET Chip select line to W5100 ' RB4: N/U - currently ' RB3: N/U - currently ' RB2: ETHERNET Chip select line to SD-Card ' RB1: N/U and can not be used with the Ethernet shield ' RB0: ETHERNET Interrupt pin from W5100
' Aliases for the MSSP module status and control bits. SSPEN VAR SSPCON1.5 ' SSP Enable bit CKP VAR SSPCON1.4 ' Clock Polarity Select SMP VAR SSPSTAT.7 ' Data input sample phase CKE VAR SSPSTAT.6 ' Clock Edge Select bit SSPIF VAR PIR1.3 ' SPI interrupt flag 'Set up the MSSP module. CKP = 0 ' Clock idles low CKE = 1 ' Transmit on active to idle transition. SSPIF = 0 ' Clear buffer full status SMP = 0 ' Sample in middle of data SSPEN = 1 ' Enable SPI pins SSPCON1.0 = 1 'Slow down SPI clock to Fosc/16 for now.
Finally I included the file with all the register definitions for the W5100:
INCLUDE "W5100_defs.pbp" ' Register definitions and constants for the W5100.
/Henrik.
Re: Serial comunication problem
Well that would seem to rule out any OSC issues. The Pic is running and running at 4Mhz as you have programmed for. At this point, I think I would just have it run and start touching stuff. Maybe...
cncmachineguy - 6th October 2011, 15:58