One-Wire communication with a DS2401 via ATMEGA32

Go To Last Post
23 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've an ATMEGA32 and a DS2401 (64 bit ROM- Silicon serial number). I want to read it's data.

Can anyone help me? giving me a clue, share a tutorial link for One-Wire communication or a sample code or something else ...

* I use Codevision.

Appreciate your time and consideration.
Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did it not occur to you to look on the Manufacturer's website :?:

Maxim provide full documentation, tutorials, and sample code.

http://www.8052.com/forum/read/1...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Did it not occur to you to look on the Manufacturer's website :?:

Maxim provide full documentation, tutorials, and sample code.

http://www.8052.com/forum/read/1...

Thank you dear friend :) I am checking them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Codevision comes with <1wire.h>

This will give you all the functions that you need to read your DS2401 chip. e.g. Read ROM and Search ROM

In fact, you can use the 1-Wire Search Example program that identifies all the One-Wire devices that are on the bus.

Yes, of course you can use third-party libraries. OTOH, CV has got everything already available!

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Codevision comes with <1wire.h>

This will give you all the functions that you need to read your DS2401 chip. e.g. Read ROM and Search ROM

In fact, you can use the 1-Wire Search Example program that identifies all the One-Wire devices that are on the bus.

Yes, of course you can use third-party libraries. OTOH, CV has got everything already available!

David.

Thank you dear Mr David :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Codevision comes with <1wire.h>

This will give you all the functions that you need to read your DS2401 chip. e.g. Read ROM and Search ROM

In fact, you can use the 1-Wire Search Example program that identifies all the One-Wire devices that are on the bus.

Yes, of course you can use third-party libraries. OTOH, CV has got everything already available!

David.

I wrote this code but I receive just 255 on the LCD:
http://electronics.stackexchange...

Would you please take a look at it? I couldn't upload it here. (I don't know why!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/* the ATmega32A port and bit used for the 1 Wire bus must be
   specified in the Project|Configure|C Compiler|Libraries 1 Wire
   menu */
#include 
#include <1wire.h>
#include 
#include 
#include 
#include 
#define MAX_DEVICES 8

/* allocate RAM space for the ROM codes & status bit */
unsigned char rom_codes[MAX_DEVICES][9];

void showlcd(char *buf)
{
    lcd_clear();
    lcd_puts(buf);
    delay_ms(2000);
}

void main(void)
{
    unsigned char i, j, devices;
    char buf[33], hexbuf[3];
    lcd_init(16);
    showlcd("Hello LCD");
    while (1) {
        /* detect how many DS1820/DS18S20 devices
           are connected to the bus and
           store their ROM codes in the rom_codes array */
        devices = w1_search(0xf0, rom_codes);

        /* display the ROM codes for each detected device */
        sprintf(buf, "%-u DEVICE(S)", devices);
        showlcd(buf);
        if (devices) {
            for (i = 0; i < devices; i++) {
                sprintf(buf, "DEVICE #%-u\n", i + 1);
                for (j = 0; j < 8; j++) {
                    sprintf(hexbuf, "%02X", rom_codes[i][j]);
                    strcat(buf, hexbuf);
                }
                showlcd(buf);
            };
        };
    }
}

I tried this program with two DS18B20 chips on PORTB.2 and a 16x2 LCD.

Obviously you have to select PORTB.2 in the Project 1-Wire Library Configuration
Likewise, your LCD must use different pins.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This thread seems to be a cross post: https://www.avrfreaks.net/index.p...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

/* the ATmega32A port and bit used for the 1 Wire bus must be
   specified in the Project|Configure|C Compiler|Libraries 1 Wire
   menu */
#include 
#include <1wire.h>
#include 
#include 
#include 
#include 
#define MAX_DEVICES 8

/* allocate RAM space for the ROM codes & status bit */
unsigned char rom_codes[MAX_DEVICES][9];

void showlcd(char *buf)
{
    lcd_clear();
    lcd_puts(buf);
    delay_ms(2000);
}

void main(void)
{
    unsigned char i, j, devices;
    char buf[33], hexbuf[3];
    lcd_init(16);
    showlcd("Hello LCD");
    while (1) {
        /* detect how many DS1820/DS18S20 devices
           are connected to the bus and
           store their ROM codes in the rom_codes array */
        devices = w1_search(0xf0, rom_codes);

        /* display the ROM codes for each detected device */
        sprintf(buf, "%-u DEVICE(S)", devices);
        showlcd(buf);
        if (devices) {
            for (i = 0; i < devices; i++) {
                sprintf(buf, "DEVICE #%-u\n", i + 1);
                for (j = 0; j < 8; j++) {
                    sprintf(hexbuf, "%02X", rom_codes[i][j]);
                    strcat(buf, hexbuf);
                }
                showlcd(buf);
            };
        };
    }
}

I tried this program with two DS18B20 chips on PORTB.2 and a 16x2 LCD.

Obviously you have to select PORTB.2 in the Project 1-Wire Library Configuration
Likewise, your LCD must use different pins.

David.

Thank you very much dear Mr. Johan.
I add the LCD configuration to the code and upload it on my card. but I receive only "HELLO" LCD and "0 DEVICES(S)"! It don't recognize my DS2401. Why?

/* the ATmega32A port and bit used for the 1 Wire bus must be
   specified in the Project|Configure|C Compiler|Libraries 1 Wire
   menu */
#include 
#include <1wire.h>
#include 
#include 
#include 
#include 
#include 
#define MAX_DEVICES 8

/* allocate RAM space for the ROM codes & status bit */
unsigned char rom_codes[MAX_DEVICES][9];

void showlcd(char *buf)
{
    lcd_clear();
    lcd_puts(buf);
    delay_ms(2000);
}

void main(void)
{

    unsigned char i, j, devices;
    char buf[33], hexbuf[3];
	// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// 1 Wire Bus initialization
// 1 Wire Data port: PORTB
// 1 Wire Data bit: 2
// Note: 1 Wire port settings must be specified in the
// Project|Configure|C Compiler|Libraries|1 Wire IDE menu.
w1_init();

// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTA Bit 0
// RD - PORTA Bit 1
// EN - PORTA Bit 2
// D4 - PORTA Bit 4
// D5 - PORTA Bit 5
// D6 - PORTA Bit 6
// D7 - PORTA Bit 7
// Characters/line: 8

    lcd_init(16);
    showlcd("Hello LCD");
    while (1) {
        /* detect how many DS1820/DS18S20 devices
           are connected to the bus and
           store their ROM codes in the rom_codes array */
        devices = w1_search(0xf0, rom_codes);

        /* display the ROM codes for each detected device */
        sprintf(buf, "%-u DEVICE(S)", devices);
        showlcd(buf);
        if (devices) {
            for (i = 0; i < devices; i++) {
                sprintf(buf, "DEVICE #%-u\n", i + 1);
                for (j = 0; j < 8; j++) {
                    sprintf(hexbuf, "%02X", rom_codes[i][j]);
                    strcat(buf, hexbuf);
                }
                showlcd(buf);
            };
        };
    }
} 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I add the LCD configuration to the code and upload it on my card. but I receive only "HELLO" LCD and "0 DEVICES(S)"! It don't recognize my DS2401. Why?

The answer is pretty obvious.
You have not connected your DS2401 to the AVR.

It needs GND pin to be connected to 0V on your AVR
It needs DQ pin to be connected to PB2 on your AVR

It also needs a real 4k7 resistor between DQ and 5V on your AVR.

I have no idea what your clock speed is. A mega32A from the factory will run at 1MHz. Nothing on the 1-wire bus will work properly if you have told the Project Configuration the wrong speed.

You can see from the showlcd() function that it pauses for 2 seconds after each message. So you might see the "0 DEVICE(s)" message being rewritten every 2 secs.

Incidentally, all those masses of initialisation statements do nothing.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
Quote:
I add the LCD configuration to the code and upload it on my card. but I receive only "HELLO" LCD and "0 DEVICES(S)"! It don't recognize my DS2401. Why?

The answer is pretty obvious.
You have not connected your DS2401 to the AVR.

It needs GND pin to be connected to 0V on your AVR
It needs DQ pin to be connected to PB2 on your AVR

It also needs a real 4k7 resistor between DQ and 5V on your AVR.

I have no idea what your clock speed is. A mega32A from the factory will run at 1MHz. Nothing on the 1-wire bus will work properly if you have told the Project Configuration the wrong speed.

You can see from the showlcd() function that it pauses for 2 seconds after each message. So you might see the "0 DEVICE(s)" message being rewritten every 2 secs.

Incidentally, all those masses of initialization statements do nothing.

David.

Oh! No! Are you kidding me dear Mr. David? :wink:
I connected the DO of the DS2401 to PORTB.2 And connected it with a 5k resistor to +5. And connected GND to ground! but I only receive "0 DEVICE(S)".

I set my ATmega32 clock at 1MHZ.

Are you sure that there is no difference between DS18B20 and DS2401 to coding for?

If it is necessary, I can upload its pictures and put the links here.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Read the fuses on your ATmega32.

If you have connected the DS2401 correctly, then it must be the speed.

No, all one-wire devices will be read with w1_search().

Look at "ROM FUNCTIONS FLOW CHART Figure 4" in your data sheet.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What do you mean by speed? Its clock frequency? It is 1-MHz, Is it OK?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

replace this line

    showlcd("Hello LCD");

with this

    strcpyf(buf, "takes 22 secs\nto print");
    for (i = 0; i < 22; i++) {
        lcd_putchar(buf[i]);
        delay_ms(1000);
    }

If it prints very quickly or very fast, then you are not running at 1MHz.

Since you don't seem able to read the fuses, perhaps you could report how long the message takes to print.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I said, I config my ATmegsa32 clock at 1Mhz!

BTW, it takes 22 seconds to print the whole message.("takes 22 ...."). and after that, I receive "0 Device(s)" again.

:(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm really sorry Mr.David. Really really sorry :(
It works.
thank you very much for your time and consideration.

Origin of the error was a deterministic wire :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ebrahim.rcii wrote:
a deterministic wire

A what :?: :?:

:?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh!Sorry! :D google translate fault! :D

A wire disconnection!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that we guessed that you meant broken wire.

Please add your country to your profile. You will get better answers if we know you come from Iran, Pakistan, Indonesia, ... or wherever.

I am pleased that you have got it working.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, Broken wire.

I'm Iranian. but how this can help me receive better answer!? :D

Thank you :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If we know that you come from Iran, we know that some components may be difficult to buy. Some English words may be difficult to understand. The weather is a lot hotter than England!

We may try to use good English when we reply. After all, it can't be easy to understand some English expressions. Especially slang words or jokes.

There are several members from Iran. You may meet them in person. Neighbouring countries will know how good the phones or mail service to Iran is.

So I think there is every reason to give your country. You may not want to reveal your town or email but the country is safe information.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:)
You are right
Thank you very much. :)