极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 17267|回复: 3

ENC28J60用EtherCard库文件初始化失败的问题

[复制链接]
发表于 2013-2-14 11:51:34 | 显示全部楼层 |阅读模式
本帖最后由 superid888 于 2013-2-19 12:34 编辑

    想用Arduino+ENC28J60+DHT11+两位数码管做一个带显示的温湿度监控下位机,各硬件和子模块测试正常,ENC28J60的CS接在D10,实测供电电压为3.25V,IDE用的1.03版,EtherCard用的是最新版,现在的问题是温湿度显示正常,但ENC28J60初始化失败,提示“Failed to access Ethernet controller
Failed to set IP address”
单独用EtherCard的ping范例测试正常,说明硬件和EtherCard库无问题,供电电压也正常,百思不得其解,请帮忙找出原因,谢谢!附上代码:
#include <EtherCard.h>
    static byte mymac[] = {0x74,0x69,0x69,0x3D,0x30,0x31};
    static byte myip[] = {192,168,1,200};
    byte Ethernet::buffer[100];
    static byte destIp[] = {192,168,1,115};
    int myPORT=1000, destPORT=1001;

// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral[10] = {
//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[nbrDigits] = { A4,A5};

//code for DHT11
#define DHT11_PIN 0 // ADC0 接到模拟口0
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
while(!(PINC & _BV(DHT11_PIN))); // wait for 50us
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN))
result |=(1<<(7-i));
while((PINC & _BV(DHT11_PIN))); // wait '1' finish
}
return result;
}

void setup()
{
for(int j=0; j < 8; j++)
pinMode(segmentPins[j], OUTPUT); // set segment and DP pins to output
for(int j=0; j < nbrDigits; j++)
pinMode(digitPins[j], OUTPUT);

DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
Serial.begin(57600);
Serial.println("Ready");
  

if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0);
        Serial.println( "Failed to access Ethernet controller");
if (!ether.staticSetup(myip));
        Serial.println("Failed to set IP address");

}


void loop()
{
byte dht11_dat[5],*UDPdata=dht11_dat;    //define send_data_pointer as char*;
byte dht11_in;
byte i;
// start condition
// 1. pull-down i/o pin from 18ms
PORTC &= ~_BV(DHT11_PIN);
delay(18);
PORTC |= _BV(DHT11_PIN);
delayMicroseconds(40);
DDRC &= ~_BV(DHT11_PIN);
delayMicroseconds(40);
dht11_in= PINC & _BV(DHT11_PIN);
if(dht11_in){
//Serial.println("dht11 start condition 1 not met");
return;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if(!dht11_in){
//Serial.println("dht11 start condition 2 not met");
return;
}
delayMicroseconds(80);
// now ready for data reception
for (i=0; i<5; i++)
dht11_dat = read_dht11_dat();
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
byte dht11_check_sum =
dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
{
//Serial.println("DHT11 checksum error");
}

ether.sendUdp ((char*) UDPdata, sizeof UDPdata, myPORT, destIp, destPORT);
//Serial.println("DATA send by UDP ");

Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println("C ");

//delay(1000);

int counter1=200;
while(counter1)
{
    counter1--;
    showNumber(dht11_dat[0]);
   }
    counter1=200;
while(counter1)
{
    counter1--;
    digitalWrite(9,LOW);
    showNumber(dht11_dat[2]);
    digitalWrite(9,HIGH);
   }
}


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 number, int digit)
{
digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment <8; segment++)
{
boolean isBitSet = bitRead(numeral[number], segment);
// isBitSet will be true if given bit is 1

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

digitalWrite( segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite( digitPins[digit], LOW );
}
回复

使用道具 举报

 楼主| 发表于 2013-2-14 18:17:17 | 显示全部楼层
找到原因了,是DHT11代码与ethercard库冲突,用了DHT11标准库就好了,但代码从5.8K增加到8.1K。
回复 支持 反对

使用道具 举报

发表于 2013-2-15 12:46:28 | 显示全部楼层
Arduino+ENC28J60+DHT11哪里买的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-2-15 19:43:36 | 显示全部楼层
thinke365 发表于 2013-2-15 12:46
Arduino+ENC28J60+DHT11哪里买的?

淘宝上很多,过初十就可以下单。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 09:10 , Processed in 0.046294 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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