0% found this document useful (0 votes)
85 views5 pages

Duplex Config Flags

This document contains code definitions and functions for configuring and using an ENC28J60 Ethernet controller with a PIC18 microcontroller. It defines constants for duplex mode, timer prescaler values, and LED statuses. Functions are included for initializing the Ethernet controller, getting the IP address, sending and receiving TCP/UDP packets, reading sensor data, and interrupting to blink an LED. The main function initializes peripherals, configures the Ethernet controller with DHCP or static IP, and enters a loop to process packets and send sensor data on a timed interval.

Uploaded by

Eddy Gutty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views5 pages

Duplex Config Flags

This document contains code definitions and functions for configuring and using an ENC28J60 Ethernet controller with a PIC18 microcontroller. It defines constants for duplex mode, timer prescaler values, and LED statuses. Functions are included for initializing the Ethernet controller, getting the IP address, sending and receiving TCP/UDP packets, reading sensor data, and interrupting to blink an LED. The main function initializes peripherals, configures the Ethernet controller with DHCP or static IP, and enters a loop to process packets and send sensor data on a timed interval.

Uploaded by

Eddy Gutty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// duplex config flags

#include "__NetEthEnc28j60.h"

#define Spi_Ethernet_HALFDUPLEX 0x00 // half duplex


#define Spi_Ethernet_FULLDUPLEX 0x01 // full duplex
#define TMR0_PRE 34286
#define OFF 0
#define ON 1
#define BLINK 2
#define periodeEnvoie 1
#define taillePacket 512

// Prototypes
float getTempKimo(char channel);
float getHygroKimo(char channel);

// mE ehternet NIC pinout


sfr sbit Net_Ethernet_28j60_RST at LATD3_bit; // for writing to output pin
always use latch (PIC18 family)
sfr sbit Net_Ethernet_28j60_CS at LATD2_bit; // for writing to output pin
always use latch (PIC18 family)
sfr sbit Net_Ethernet_28j60_RST_Direction at TRISD3_bit;
sfr sbit Net_Ethernet_28j60_CS_Direction at TRISD2_bit;
// end ethernet NIC definitions

int led0Status = 0;
int led1Status = 0;
char send = 0;
char secondes = 0;
char nb200ms = 0;

SOCKET_28j60_Dsc *socket_global;

// Data to send
char dat[taillePacket];
unsigned int dat_pos = 0;

/***********************************
* RAM variables
*/
unsigned char myMacAddr[6] = "ENC28" ; // my
MAC address
unsigned char myIpAddr[4] = {0,0,0,0} ; // my
IP address
unsigned char gwIpAddr[4] = {192,168,1,254};
// gateway (router) IP address
unsigned char ipMask[4] = {255,255,255,0};
// network mask (for example : 255.255.255.0)
unsigned char dnsIpAddr[4] = {192,168,1,254};
// DNS server IP address

unsigned char getRequest[15]; // HTTP


request buffer
unsigned char dyna[31] ; //
buffer for dynamic response
unsigned char dyna1[31] ;
unsigned char dyna2[31] ;

char server[] = "api.cosm.com";


char broadcast[4] = {192,168,1,255};
char remoteHostIpAddr[4] = {192,168,1,17};
char model[] = "{\
\"version\":\"1.0.0\",\
\"datastreams\":[\
{\"id\":\"temperature\", \"current_value\":\"21.8\"},\
{\"id\":\"humidity\", \"current_value\":\"50.1\"},\
]\
}";

// this method makes a HTTP connection to the server:


void sendData() {
int i;

for (i=0;i<taillePacket;i++) *(dat+i) = 0;

LATD.f1 = 1;
//memcpy(remoteHostIpAddr, Net_Ethernet_28j60_dnsResolve(server, 5), 4);

// send the HTTP PUT request:


sprintf(dat, "PUT /v2/feeds/**my_feed**HTTP/1.1 \n\
Host: api.cosm.com \n\
Accept: */* \n\
X-ApiKey: **my_key**\n\
User-Agent: ENC28J60 + PIC18F4550 \n\
Content-Length: %d \n\
Content-Type: application/x-www-form-urlencoded \n\
Connection: close\n\
\n\
{ \n\
\"version\":\"1.0.0\", \n\
\"datastreams\":[ \n\
{\"id\":\"temperature\", \"current_value\":\"%2.1f\"}, \n\
{\"id\":\"humidity\", \"current_value\":\"%2.1f\"} \n\
] \n\
}",strlen(model),getTempKimo(1),getHygroKimo(0));

while(Net_Ethernet_28j60_connectTCP(remoteHostIpAddr, 80, 10000,


&socket_global)!=1);
while(socket_global->state != 3); //wait for connection
Net_Ethernet_28j60_startSendTCP(socket_global);
//Net_Ethernet_28j60_sendUDP(broadcast, 58180, 80, dat, strlen(trame));
}

void Net_Ethernet_28j60_UserTCP(SOCKET_28j60_Dsc *socket) {


if( Net_Ethernet_28j60_bufferEmptyTCP(socket)) {
Net_Ethernet_28j60_disconnectTCP(socket);
send = 0;
LATD.f1 = 0;
}
}

unsigned int Net_Ethernet_28j60_UserUDP(UDP_28j60_Dsc *udpDsc) {

unsigned int len; // my reply length

// reply is made of the remote host IP address in human readable format


ByteToStr(udpDsc->remoteIP[0], dyna); // first IP address
byte
dyna[3] = '.';
ByteToStr(udpDsc->remoteIP[1], dyna + 4); // second
dyna[7] = '.';
ByteToStr(udpDsc->remoteIP[2], dyna + 8); // third
dyna[11] = '.';
ByteToStr(udpDsc->remoteIP[3], dyna + 12); // fourth

dyna[15] = ':'; // add separator

// then remote host port number


WordToStr(udpDsc->remotePort, dyna + 16);
dyna[21] = '[';
WordToStr(udpDsc->destPort, dyna + 22);
dyna[27] = ']';
dyna[28] = 0;

// the total length of the request is the length of the dynamic string plus
the text of the request
len = 28 + udpDsc->dataLength;

// puts the dynamic string into the transmit buffer


Net_Ethernet_28j60_putBytes(dyna, 28);

// then puts the request string converted into upper char into the transmit
buffer

while(udpDsc->dataLength--)
{
Net_Ethernet_28j60_putByte(toupper(Net_Ethernet_28j60_getByte()));
}
return(len);
}

float getTempKimo(char channel)


{
return ((ADC_Read(channel)/40.96)-50);
}

float getHygroKimo (char channel)


{
return (ADC_Read(channel)/40.96);
}

void interrupt (void)


{
if (INTCON.T0IF == 1)
{
TMR0H = TMR0_PRE / 256;
TMR0L = TMR0_PRE % 256;

nb200ms++;

if (nb200ms%5 == 0)
{
nb200ms = 0;
Net_Ethernet_28j60_UserTimerSec ++;
secondes++;
}

if (secondes%10 == 0)
{
secondes = 0;
send = 1;
}

if(led0Status == 0)
{
LATD.f0 = 0;
}
else if(led0Status == 1)
{
LATD.f0 = 1;
}
else if(led0Status == 2)
{
LATD.f0 = !LATD.f0;
}

INTCON.T0IF = 0;
}

}
void main()
{
char useDHCP = 0;
int i = 0;

ADCON1 = 0;
ANCON0 = 0x0f;

PORTA = 0 ;
TRISA = 0xff ; // set PORTA as input for ADC

PORTB = 0 ;
TRISB = 0xff ; // set PORTB as input for buttons

LATD = 0 ;
TRISD = 0 ; // set PORTD as output

INTCON.GIE = 1;
T0CON = 0b10000110;
TMR0H = TMR0_PRE / 256;
TMR0L = TMR0_PRE % 256;
INTCON.T0IE = 1;

LATD.f1 = 1;

Net_Ethernet_28j60_stackInitTCP();

SPI1_Init();

SPI_Rd_Ptr = SPI1_Read;

Net_Ethernet_28j60_Init(myMacAddr, myIpAddr,
Spi_Ethernet_FULLDUPLEX) ;

if(Net_Ethernet_28j60_getIpAddress()[0] == 0) useDHCP = 1;
LATD.f1 = 0;
led0Status = 2;

if(useDHCP == 1)
{
while(Net_Ethernet_28j60_initDHCP(10) == 0) // try to get one from
DHCP until it works
{
Net_Ethernet_28j60_Init(myMacAddr, myIpAddr,
Spi_Ethernet_FULLDUPLEX);
//Net_Ethernet_28j60_Init(myMacAddr, myIpAddr,
Spi_Ethernet_HALFDUPLEX) ;
}

memcpy(myIpAddr, Net_Ethernet_28j60_getIpAddress(), 4) ; //
get assigned IP address
memcpy(ipMask, Net_Ethernet_28j60_getIpMask(), 4) ; //
get assigned IP mask
memcpy(gwIpAddr, Net_Ethernet_28j60_getGwIpAddress(), 4) ; //
get assigned gateway IP address
memcpy(dnsIpAddr, Net_Ethernet_28j60_getDnsIpAddress(), 4) ; //
get assigned dns IP address

}
else
{
Net_Ethernet_28j60_confNetwork(ipMask, gwIpAddr, dnsIpAddr) ; //
use configured IP address
}

led0Status = 1;
while(1)
{
/*
* if necessary, test the return value to get error code
*/

Net_Ethernet_28j60_doPacket() ; // process incoming Ethernet


packets

if(Net_Ethernet_28j60_doDHCPLeaseTime() && (useDHCP == 1))


{ // is the lease time expired
led0Status = 2;
while (!Net_Ethernet_28j60_renewDHCP(5)) ; // try to renew
until it works
led0Status = 0;
}

if (send == 1)
{
sendData();
}
}
}

You might also like