superid888 发表于 2013-3-3 11:43:51

学习笔记:ATMEGA8L最小系统+ENC28J60 UDP传输+两位数码管显示的温湿度监控模块

    经过几天摸索,调通了ENC28J60的UDP传输代码,用ATMEGA8L最小系统搭建了个基于UDP传输和两位数码管显示的温湿度监控下位机,可以用PC作为上位机组建多机房的远程温湿度和设备状态监控系统。ATMEGA8L熔丝设定为8MHz,用内部RC振荡器,USB端口取电,经2个二极管串接降压到3.5V左右为ATMEGA8L和ENC28J60供电,电路组装在洞洞板上,预留了ISP编程接口(兼连接ENC28J60模块的接口),用了一个ADSL线路分离器的外壳做模块的外壳,成本不到30元。

superid888 发表于 2013-3-3 11:52:15

附上代码和烧录文件、VB编的上位机测试程序。

/*
This sketch receives humidity and temperature from sensor DHT11,
display on two digits seven-segment LED and send UDP message to remote server, prints them to the serial port.
The code can only compily by IDE 002X
Created 28 Feb 2013, by Chen JW. [email protected]
*/

#include "EtherShield.h"
static uint8_t mymac = { 0x54,0x55,0x58,0x10,0x00,0x25};
static uint8_t myip = { 192,168,2,23};
static uint8_t broadcastip = { 192,168,2,255};
// DestPort 1001, SrcPort 1000
#define DEST_PORT_L0xE9
#define DEST_PORT_H0x03
#define SRC_PORT_L0xE8
#define SRC_PORT_H0x03
const char iphdr[] PROGMEM ={ 0x45,0,0,0x82,0,0,0x40,0,0x20}; // 0x82 is the total

struct UDPPayload {
uint8_t data;
};
UDPPayload udpPayload;

// Packet buffer, must be big enough to packet and payload
#define BUFFER_SIZE 150
static uint8_t buf;

EtherShield es=EtherShield();

// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// pins for decimal point and each segment
// dp,G,F,E,D,C,B,A
const int segmentPins[] = { 9,8,7,6,5,4,3,2};
const int nbrDigits= 2; // the number of digits in the LED display
//dig 1 2
const int digitPins = { A4,A5};
int dppin=A3; //set the decimal point link to PIN A3

#include <dht11.h>
dht11 DHT11;
#define DHT11PIN A0 //DHT11 PIN 3 == UNO A0

char string1;//Humd
char string2;//Temp



void setup(){

Serial.begin(19200);

es.ES_enc28j60Init(mymac);
//init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip,80);

for(int i=0; i < 8; i++)
pinMode(segmentPins, OUTPUT); // set segment and DP pins to output
for(int i=0; i < nbrDigits; i++)
pinMode(digitPins, OUTPUT);
}


void loop(){

int chk = DHT11.read(DHT11PIN);

Serial.print("Read sensor: ");
switch (chk)
{
   case DHTLIB_OK:
                Serial.println("OK");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.println("Checksum error");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.println("Time out error");
                break;
    default:
                Serial.println("Unknown error");
                break;
}

int humd=int(DHT11.humidity);
int temp=int(DHT11.temperature-2);

Serial.println(humd);
Serial.println(temp);

udpPayload.data =humd;
udpPayload.data =temp;
broadcastData() ;
Serial.println("Data send over UDP.");

//Display on 7segment
int counter1=200;
while(counter1)
{
    counter1--;
//    showNumber(DHT11.humidity);
showNumber(humd);
   }
    counter1=200;
while(counter1)
{
    counter1--;
    analogWrite(dppin,0);
//    showNumber(DHT11.temperature-2);
showNumber(temp);
    analogWrite(dppin,200);
   }
}

// Broadcast the data in the udpPayload structure
void broadcastData( void ) {
uint8_t i=0;
uint16_t ck;
// Setup the MAC addresses for ethernet header
while(i<6){
    buf= 0xff; // Broadcsat address
    buf=mymac;
    i++;
}
buf = ETHTYPE_IP_H_V;
buf = ETHTYPE_IP_L_V;
es.ES_fill_buf_p(&buf,9,iphdr);

// IP Header
buf=28+sizeof(UDPPayload);
buf=IP_PROTO_UDP_V;
i=0;
while(i<4){
    buf=broadcastip;
    buf=myip;
    i++;
}
es.ES_fill_ip_hdr_checksum(buf);
buf=DEST_PORT_H;
buf=DEST_PORT_L;
buf=SRC_PORT_H;
buf=SRC_PORT_L; // lower 8 bit of src port
buf=0;
buf=8+sizeof(UDPPayload); // fixed len
// zero the checksum
buf=0;
buf=0;
// copy the data:
i=0;
// most fields are zero, here we zero everything and fill later
uint8_t* b = (uint8_t*)&udpPayload;
while(i< sizeof( UDPPayload ) ){
    buf=*b++;
    i++;
}
// Create correct checksum
ck=es.ES_checksum(&buf, 16 + sizeof( UDPPayload ),1);
buf=ck>>8;
buf=ck& 0xff;
es.ES_enc28j60PacketSend(42 + sizeof( UDPPayload ), buf);
}

void showNumber( int number)
{
if(number == 0)
showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
else
{
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = nbrDigits-1; digit >= 0; digit--)
{
if(number > 0)
{
showDigit( number % 10, digit) ;
number = number / 10;
}
}
}
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int segnumber, int digit)
{
digitalWrite( digitPins, HIGH );
for(int segment = 1; segment <8; segment++)
{
boolean isBitSet = bitRead(numeral, segment);
// isBitSet will be true if given bit is 1

//isBitSet = ! isBitSet; // remove this line if common cathode display

digitalWrite( segmentPins, isBitSet);
}
delay(5);
digitalWrite( digitPins, LOW );
}

// End

davidce 发表于 2013-3-3 12:51:16

这就是心灵手巧

迷你强 发表于 2013-3-3 18:20:57

:funk:好密集的飞线。。。。

Cupid 发表于 2013-3-4 08:33:30

哇,高档高档,又是一神,有没有更细一点的图了

anzedick 发表于 2013-3-5 23:16:28

哈哈,飞线神马的最有趣了

数字 发表于 2013-4-19 15:43:51

还可以从网口取电,就是poe的了。

daniu101130 发表于 2014-7-23 22:26:13

好恐怖的飞线
页: [1]
查看完整版本: 学习笔记:ATMEGA8L最小系统+ENC28J60 UDP传输+两位数码管显示的温湿度监控模块