EmbDev.net

Forum: µC & Digital Electronics read DS2401 with ATmega 32 and a 1-Wire protocol


von ebrahim (Guest)


Rate this post
0 useful
not useful
DS2401 is a 64-bit ROM memory that used as a hardware-serial-number. It 
has only two pin. One of them is ground and one of them is Data/Power 
pin. So for communicate with this chip there is a protocol that called 
1-Wire.

I want to read my DS2401 chip with AVR ATmega32 , and I use 1wire.h 
library in codevision. As mentioned in Codevision AVR User Manual I must 
use w1_read() function to read a byte of DS2401. but it returns me only 
255 [On the LCD]!

This is my codevision code:
1
/*****************************************************
2
This program was produced by the
3
CodeWizardAVR V2.05.0 Professional
4
Automatic Program Generator
5
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
6
http://www.hpinfotech.com
7
Project : 
8
Version : 
9
Date    : 7/19/2014
10
Author  : 
11
Company : 
12
Comments: 
13
14
15
Chip type               : ATmega32A
16
Program type            : Application
17
AVR Core Clock frequency: 1.000000 MHz
18
Memory model            : Small
19
External RAM size       : 0
20
Data Stack size         : 512
21
*****************************************************/
22
23
#include <mega32a.h>
24
#include <delay.h>
25
#include <stdio.h>
26
27
// 1 Wire Bus interface functions
28
#include <1wire.h>
29
30
// Alphanumeric LCD Module functions
31
#include <alcd.h>
32
33
// Declare your global variables here
34
35
void main(void)
36
{
37
int init_res,read_byte,i;
38
char read_byte_char[64];
39
// Declare your local variables here
40
41
// Input/Output Ports initialization
42
// Port A initialization
43
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
44
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
45
PORTA=0x00;
46
DDRA=0x00;
47
48
// Port B initialization
49
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
50
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
51
PORTB=0x00;
52
DDRB=0x00;
53
54
// Port C initialization
55
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
56
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
57
PORTC=0x00;
58
DDRC=0x00;
59
60
// Port D initialization
61
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
62
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
63
PORTD=0x00;
64
DDRD=0x00;
65
66
// Timer/Counter 0 initialization
67
// Clock source: System Clock
68
// Clock value: Timer 0 Stopped
69
// Mode: Normal top=0xFF
70
// OC0 output: Disconnected
71
TCCR0=0x00;
72
TCNT0=0x00;
73
OCR0=0x00;
74
75
// Timer/Counter 1 initialization
76
// Clock source: System Clock
77
// Clock value: Timer1 Stopped
78
// Mode: Normal top=0xFFFF
79
// OC1A output: Discon.
80
// OC1B output: Discon.
81
// Noise Canceler: Off
82
// Input Capture on Falling Edge
83
// Timer1 Overflow Interrupt: Off
84
// Input Capture Interrupt: Off
85
// Compare A Match Interrupt: Off
86
// Compare B Match Interrupt: Off
87
TCCR1A=0x00;
88
TCCR1B=0x00;
89
TCNT1H=0x00;
90
TCNT1L=0x00;
91
ICR1H=0x00;
92
ICR1L=0x00;
93
OCR1AH=0x00;
94
OCR1AL=0x00;
95
OCR1BH=0x00;
96
OCR1BL=0x00;
97
98
// Timer/Counter 2 initialization
99
// Clock source: System Clock
100
// Clock value: Timer2 Stopped
101
// Mode: Normal top=0xFF
102
// OC2 output: Disconnected
103
ASSR=0x00;
104
TCCR2=0x00;
105
TCNT2=0x00;
106
OCR2=0x00;
107
108
// External Interrupt(s) initialization
109
// INT0: Off
110
// INT1: Off
111
// INT2: Off
112
MCUCR=0x00;
113
MCUCSR=0x00;
114
115
// Timer(s)/Counter(s) Interrupt(s) initialization
116
TIMSK=0x00;
117
118
// USART initialization
119
// USART disabled
120
UCSRB=0x00;
121
122
// Analog Comparator initialization
123
// Analog Comparator: Off
124
// Analog Comparator Input Capture by Timer/Counter 1: Off
125
ACSR=0x80;
126
SFIOR=0x00;
127
128
// ADC initialization
129
// ADC disabled
130
ADCSRA=0x00;
131
132
// SPI initialization
133
// SPI disabled
134
SPCR=0x00;
135
136
// TWI initialization
137
// TWI disabled
138
TWCR=0x00;
139
140
// 1 Wire Bus initialization
141
// 1 Wire Data port: PORTB
142
// 1 Wire Data bit: 2
143
// Note: 1 Wire port settings must be specified in the
144
// Project|Configure|C Compiler|Libraries|1 Wire IDE menu.
145
// w1_init(); <-------------------------
146
147
// Alphanumeric LCD initialization
148
// Connections specified in the
149
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
150
// RS - PORTA Bit 0
151
// RD - PORTA Bit 1
152
// EN - PORTA Bit 2
153
// D4 - PORTA Bit 4
154
// D5 - PORTA Bit 5
155
// D6 - PORTA Bit 6
156
// D7 - PORTA Bit 7
157
// Characters/line: 16
158
lcd_init(16);
159
160
while (1)
161
      {
162
      init_res = w1_init();
163
164
      if (init_res ==1)
165
      {
166
         lcd_clear(); 
167
         lcd_gotoxy(1,0);
168
         lcd_puts("Chip Connected");   
169
         delay_ms(1000);
170
171
         lcd_clear();
172
         lcd_gotoxy(1,0);
173
         lcd_puts("Serial Number:"); 
174
175
         for (i=1;i<9;i++)
176
            {
177
               read_byte=w1_read();
178
               lcd_gotoxy(i,1);
179
               sprintf(read_byte_char,"%i",read_byte); 
180
               lcd_puts(read_byte_char);   
181
               delay_ms(1000);
182
            };
183
184
185
      }
186
      else
187
      {
188
189
        lcd_clear();
190
        lcd_gotoxy(1,1);
191
        lcd_puts("Chip Not Found");
192
      } 
193
      delay_ms(2000);
194
      }
195
}

Q1: Why?

Q2: Each time that I call w1_read function, DS2401 send me next 1 byte? 
or the same first 1 byte? or total of the ROM?

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.