📄 1-wire1.c
字号:
/*By George Elefteriadis.24/8/2000.geoelec@hotmail.comThis program reads the code of all 1-wire devices.The microcontroller is an AT90S2313.Connect a resistor between PD2 and 5V an connect the ibutton in PD2 and ground.The programis tested in MCU00100 board and uses the Uart to display the code and PORTB to display theCRC as it calculated by the routine of crc.In case of wrong reading the transaction goes fromthe beginning and theres no chance of displaying a wrong byte.*/#include<stdio.h>#include<90s2313.h>#include<delay.h>#include<mem.h> char table[]="0123456789ABCDEF";char code[8];//send read rom-0x33 on 1-wire void command(unsigned char read){ int j;unsigned char i;for (j=0;j<=7;j++){i=read&1;if (i==1){PORTD.2=0;delay_us(9);PORTD.2=1;delay_us(90);}else{PORTD.2=0;delay_us(90);PORTD.2=1;delay_us(9);}read=read>>1;}}//end of read rom command //store the ascii characters in sram location 0xa0-0xafvoid sram(unsigned char store){unsigned char l,q,i,z,index;index=peekb(0x61);l=store;q=l&0x0f;i=l&0xf0;i=i>>4;i=i+0x80;z=peekb(i);pokeb(index,z);q=q+0x80;z=peekb(q);index++;pokeb(index,z);index++;pokeb(0x61,index);}//end of store//read the 8 bytes of codeunsigned char read(void){int j,f;unsigned char byte;byte=0x00;for(f=0;f<=7;f++){for (j=0;j<=7;j++){PORTD.2=0;delay_us(6);PORTD.2=1;delay_us(13);if (PIND.2==1){byte=byte>>1;byte=byte|0x80;}else {byte=byte>>1;}delay_us(765);}code[f]=byte;}return byte;}//end of read function//the cyclic redundancy checkvoid crc(unsigned char byte){unsigned char crc,bit0,cbit,r;int i;crc=peekb(0x60);for(i=0;i<=7;i++){cbit=crc&0x01;bit0=byte&0x01;crc=crc>>1;r=cbit^bit0;if(r==1){crc=crc^0x8c;}byte=byte>>1;}pokeb(0x60,crc);} //end of crc main(){unsigned char testcrc,q,i; int m;UBRR=25;UCR=0x18; DDRD=0xff;PORTD=0xff;DDRB=0xff;PORTB=0xff;start: pokeb(0x60,0x00);pokeb(0x61,0xa0);reset_pulse: PORTD.2=0;//cbi portd,2delay_us(500);PORTD.2=1;//sbi portd,2delay_us(45);if (PIND.2==1){goto reset_pulse;}while (PIND.2==0){}delay_us(220);command(0x33);read();for(m=0;m<=7;m++){sram(code[m]);}for(m=0;m<=6;m++){crc(code[m]);}testcrc=peekb(0x60); pokeb(0xb0,0x0d);if(testcrc==code[7]){if(code[7]!=0){PORTB=testcrc;for(q=0xa0;q<=0xb1;q++){i=peekb(q);putchar(i);delay_ms(250); }}}goto start;}/*for (q=0x60;q<=0x67;q++)pokeb(q,read());for (q=0x60;q<=0x67;q++){byte0=peekb(q);byte0=0xff-byte0;*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -