极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 22524|回复: 7

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

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

评分

参与人数 2 +33 收起 理由
Ansifa + 30 漆包线威武!
幻生幻灭 + 3 小线飞的神采奕奕

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-3-3 11:52:15 | 显示全部楼层
附上代码和烧录文件、VB编的上位机测试程序。

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

  7. #include "EtherShield.h"
  8. static uint8_t mymac[6] = { 0x54,0x55,0x58,0x10,0x00,0x25};
  9. static uint8_t myip[4] = { 192,168,2,23};
  10. static uint8_t broadcastip[4] = { 192,168,2,255};
  11. // DestPort 1001, SrcPort 1000
  12. #define DEST_PORT_L  0xE9
  13. #define DEST_PORT_H  0x03
  14. #define SRC_PORT_L  0xE8
  15. #define SRC_PORT_H  0x03
  16. const char iphdr[] PROGMEM ={ 0x45,0,0,0x82,0,0,0x40,0,0x20}; // 0x82 is the total

  17. struct UDPPayload {
  18. uint8_t data[2];
  19. };
  20. UDPPayload udpPayload;

  21. // Packet buffer, must be big enough to packet and payload
  22. #define BUFFER_SIZE 150
  23. static uint8_t buf[BUFFER_SIZE+1];

  24. EtherShield es=EtherShield();

  25. // bits representing segments A through G (and decimal point) for numerals 0-9
  26. const int numeral[10] = {
  27. //ABCDEFG /dp
  28. B11111100, // 0
  29. B01100000, // 1
  30. B11011010, // 2
  31. B11110010, // 3
  32. B01100110, // 4
  33. B10110110, // 5
  34. B10111110, // 6
  35. B11100000, // 7
  36. B11111110, // 8
  37. B11110110, // 9
  38. };
  39. // pins for decimal point and each segment
  40. // dp,G,F,E,D,C,B,A
  41. const int segmentPins[] = { 9,8,7,6,5,4,3,2};
  42. const int nbrDigits= 2; // the number of digits in the LED display
  43. //dig 1 2
  44. const int digitPins[nbrDigits] = { A4,A5};
  45. int dppin=A3; //set the decimal point link to PIN A3

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

  49. char string1[2];//Humd
  50. char string2[2];//Temp



  51. void setup(){

  52. Serial.begin(19200);
  53.   
  54. es.ES_enc28j60Init(mymac);
  55. //init the ethernet/ip layer:
  56. es.ES_init_ip_arp_udp_tcp(mymac,myip,80);

  57. for(int i=0; i < 8; i++)
  58. pinMode(segmentPins[i], OUTPUT); // set segment and DP pins to output
  59. for(int i=0; i < nbrDigits; i++)
  60. pinMode(digitPins[i], OUTPUT);
  61. }


  62. void loop(){

  63. int chk = DHT11.read(DHT11PIN);

  64.   Serial.print("Read sensor: ");
  65.   switch (chk)
  66.   {
  67.    case DHTLIB_OK:
  68.                 Serial.println("OK");
  69.                 break;
  70.     case DHTLIB_ERROR_CHECKSUM:
  71.                 Serial.println("Checksum error");
  72.                 break;
  73.     case DHTLIB_ERROR_TIMEOUT:
  74.                 Serial.println("Time out error");
  75.                 break;
  76.     default:
  77.                 Serial.println("Unknown error");
  78.                 break;
  79.   }

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

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

  84. udpPayload.data[0] =humd;
  85. udpPayload.data[1] =temp;
  86. broadcastData() ;
  87. Serial.println("Data send over UDP.");

  88. //Display on 7segment
  89.   int counter1=200;
  90. while(counter1)
  91. {
  92.     counter1--;
  93. //    showNumber(DHT11.humidity);
  94. showNumber(humd);
  95.    }
  96.     counter1=200;
  97. while(counter1)
  98. {
  99.     counter1--;
  100.     analogWrite(dppin,0);
  101. //    showNumber(DHT11.temperature-2);
  102. showNumber(temp);
  103.     analogWrite(dppin,200);
  104.    }
  105. }

  106. // Broadcast the data in the udpPayload structure
  107. void broadcastData( void ) {
  108.   uint8_t i=0;
  109.   uint16_t ck;
  110.   // Setup the MAC addresses for ethernet header
  111.   while(i<6){
  112.     buf[ETH_DST_MAC +i]= 0xff; // Broadcsat address
  113.     buf[ETH_SRC_MAC +i]=mymac[i];
  114.     i++;
  115.   }
  116.   buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
  117.   buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
  118.   es.ES_fill_buf_p(&buf[IP_P],9,iphdr);

  119.   // IP Header
  120.   buf[IP_TOTLEN_L_P]=28+sizeof(UDPPayload);
  121.   buf[IP_PROTO_P]=IP_PROTO_UDP_V;
  122.   i=0;
  123.   while(i<4){
  124.     buf[IP_DST_P+i]=broadcastip[i];
  125.     buf[IP_SRC_P+i]=myip[i];
  126.     i++;
  127.   }
  128.   es.ES_fill_ip_hdr_checksum(buf);
  129.   buf[UDP_DST_PORT_H_P]=DEST_PORT_H;
  130.   buf[UDP_DST_PORT_L_P]=DEST_PORT_L;
  131.   buf[UDP_SRC_PORT_H_P]=SRC_PORT_H;
  132.   buf[UDP_SRC_PORT_L_P]=SRC_PORT_L; // lower 8 bit of src port
  133.   buf[UDP_LEN_H_P]=0;
  134.   buf[UDP_LEN_L_P]=8+sizeof(UDPPayload); // fixed len
  135.   // zero the checksum
  136.   buf[UDP_CHECKSUM_H_P]=0;
  137.   buf[UDP_CHECKSUM_L_P]=0;
  138.   // copy the data:
  139.   i=0;
  140.   // most fields are zero, here we zero everything and fill later
  141.   uint8_t* b = (uint8_t*)&udpPayload;
  142.   while(i< sizeof( UDPPayload ) ){
  143.     buf[UDP_DATA_P+i]=*b++;
  144.     i++;
  145.   }
  146.   // Create correct checksum
  147.   ck=es.ES_checksum(&buf[IP_SRC_P], 16 + sizeof( UDPPayload ),1);
  148.   buf[UDP_CHECKSUM_H_P]=ck>>8;
  149.   buf[UDP_CHECKSUM_L_P]=ck& 0xff;
  150.   es.ES_enc28j60PacketSend(42 + sizeof( UDPPayload ), buf);
  151. }

  152. void showNumber( int number)
  153. {
  154. if(number == 0)
  155. showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
  156. else
  157. {
  158. // display the value corresponding to each digit
  159. // leftmost digit is 0, rightmost is one less than the number of places
  160. for( int digit = nbrDigits-1; digit >= 0; digit--)
  161. {
  162. if(number > 0)
  163. {
  164. showDigit( number % 10, digit) ;
  165. number = number / 10;
  166. }
  167. }
  168. }
  169. }
  170. // Displays given number on a 7-segment display at the given digit position
  171. void showDigit( int segnumber, int digit)
  172. {
  173. digitalWrite( digitPins[digit], HIGH );
  174. for(int segment = 1; segment <8; segment++)
  175. {
  176. boolean isBitSet = bitRead(numeral[segnumber], segment);
  177. // isBitSet will be true if given bit is 1

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

  179. digitalWrite( segmentPins[segment], isBitSet);
  180. }
  181. delay(5);
  182. digitalWrite( digitPins[digit], LOW );
  183. }

  184. // End

复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2013-3-3 12:51:16 | 显示全部楼层
这就是心灵手巧
回复 支持 反对

使用道具 举报

发表于 2013-3-3 18:20:57 | 显示全部楼层
好密集的飞线。。。。
回复 支持 反对

使用道具 举报

发表于 2013-3-4 08:33:30 | 显示全部楼层
哇,高档高档,又是一神,有没有更细一点的图了
回复 支持 反对

使用道具 举报

发表于 2013-3-5 23:16:28 | 显示全部楼层
哈哈,飞线神马的最有趣了
回复 支持 反对

使用道具 举报

发表于 2013-4-19 15:43:51 | 显示全部楼层
还可以从网口取电,就是poe的了。
回复 支持 反对

使用道具 举报

发表于 2014-7-23 22:26:13 | 显示全部楼层
好恐怖的飞线
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-6 13:30 , Processed in 0.147961 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表