📄 mainp.c
字号:
#include <reg52.h>
#include <absacc.h>
#include <stdio.h>
#include <intrins.h>
#include <ownet.h>
#include <define.h>
//#include <drive.h>
// local function prototypes
SMALLINT owAcquire(int,char *);
//void owRelease(int);
void usDelay(unsigned int delay);
int owHasErrors(void);
int owGetErrorNum(void);
SMALLINT owTouchReset(int portnum);
SMALLINT owTouchBit(int portnum, SMALLINT sendbit);
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte);
//SMALLINT owReadByte(int portnum);
//SMALLINT owSpeed(int portnum, SMALLINT new_speed);
//SMALLINT owLevel(int portnum, SMALLINT new_level);
//SMALLINT bitacc(SMALLINT,SMALLINT,SMALLINT,uchar *);
SMALLINT owFirst(int portnum, SMALLINT do_reset, SMALLINT alarm_only);
SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only);
void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read);
uchar docrc8(int portnum, uchar x);
void setcrc8(int portnum, uchar reset);
union{
unsigned char a[2];
unsigned int b;
}deal_now;
unsigned int music[][3]={{500,500,250},{1000,1000,400},{500,500,400},
{200,200,450},{1000,1000,400},{1500,1500,400}};
bit f_deal;
sbit led=P1^4;
sbit clock=P2^4;
sbit speak=P0^7;
main()
{
uchar portnum = 0;
uchar rslt;
unsigned long ll;
unsigned char lee;
unsigned char ddd,ccc,ff;
uchar serial_num[8];
TMOD |= 0x21;
IE = 0x82;
TH0=0x0;
TL0=0x0;
TR0=0;
ccc=0x32;
ff=2;
ddd=ccc^ff;
usDelay(30000);
do{
do{
led=0;
portnum = 0;
usDelay(100);
rslt = owFirst(portnum, TRUE, FALSE);
}while((rslt==0)&&clock);
owSerialNum(portnum,serial_num,TRUE);
rslt = owNext(portnum, TRUE, FALSE);
led=1;
for(lee=0;lee<6;lee++)
{
for(ll=music[lee][2];ll>0;ll--)
{
speak=1;
usDelay(music[lee][0]);
speak=0;
usDelay(music[lee][1]);
};
usDelay(3000);
}
for(ll=50;ll<1000;ll++)
{
for(lee=0;lee<20;lee++)
{
speak=1;
usDelay(ll);
speak=0;
usDelay(ll);
};
};
led=0;
}while(1);
}
void timea() interrupt 1
{
f_deal=0;
}
//---------------------------------------------------------------------------
// Attempt to acquire a 1-Wire net
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'port_zstr' - zero terminated port name.
//
// Returns: TRUE - success, port opened
//
//SMALLINT owAcquire(int portnum, char *port_zstr)
//{
// port_zstr = 0;
// portnum = 0;
//
// OW_PORT = 1; // drive bus high.
// usDelay(44); // give time for line to settle
//
// checks to make sure the line is idling high.
// return (OW_PORT==1?TRUE:FALSE);
//}
//---------------------------------------------------------------------------
// Release the previously acquired a 1-Wire net.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
//
//void owRelease(int portnum)
//{
// portnum = 0;
//}
//--------------------------------------------------------------------------
// Description:
// Delay for at least 'len' microseconds
//
// Adapted from Kevin Vigor's TINI 1Wire code.
//
// Works only @33Mhz. We need 8.25 machine cycles per iteration to get the microsecond
// delay. Can't have .25 of a cycle, so this function uses 8 cycles per iteration. This
// produces an error of 3% ( .03 = (8.25-8)/8 ). Most of the use of this function by
// 1-Wire routines calls for delays under 50 microseconds, in which case it is under by
// less than 1.5 microseconds. It is assumed the overhead of calling the function will
// add enough to account for the difference. For values much greater than 50 (only in
// reset function), a "fudge factor" is added to account for any error.
//
//
void usDelay(unsigned int delay)
{
deal_now.b=0xffff-delay+30;
TH0=deal_now.a[0];
TL0=deal_now.a[1];
TR0=1;
f_deal=1;
while(f_deal);
//do{delay--;}while(delay>0);
// delay = 0;
}
//int owHasErrors(void)
//{
// if(owErrorStack[ owErrorPointer ].owErrorNum)
// return 1; //TRUE
// else
// return 0; //FALSE
//}
//--------------------------------------------------------------------------
// The 'owGetErroNum' returns the error code of the top-most error on the
// error stack. NOTE: This function has the side effect of popping the
// current error off the stack. All successive calls to 'owGetErrorNum'
// will further clear the error stack.
//
// For list of error codes, see 'ownet.h'
//
// Returns: int : The error code of the top-most error on the stack
//
//int owGetErrorNum(void)
//{
// int i = owErrorStack[ owErrorPointer ].owErrorNum;
// owErrorStack[ owErrorPointer ].owErrorNum = 0;
// if(!owErrorPointer)
// owErrorPointer = SIZE_OWERROR_STACK - 1;
// else
// owErrorPointer = (owErrorPointer - 1);
// return i;
//}
static SMALLINT USpeed = 0; // current 1-Wire Net communication speed
static SMALLINT ULevel = 0; // current 1-Wire Net level
//--------------------------------------------------------------------------
// Reset all of the devices on the 1-Wire Net and return the result.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
//
// Returns: TRUE(1): presense pulse(s) detected, device(s) reset
// FALSE(0): no presense pulses detected
//
SMALLINT owTouchReset(int portnum)
{
uchar result;
portnum = 0;
// Code from appnote 126.
OW_PORT = 0; // drive bus low.
//usDelay(44);
usDelay(480); // 500-(3% error) ~= 480 us
OW_PORT = 1; // bus high.
//usDelay(11);
usDelay(120); // 125-(3% error) ~= 120 us
result = !OW_PORT; // get presence detect pulse.
//usDelay(32);
usDelay(360); // 372-(3% error) ~= 360 us
return result;
}
//--------------------------------------------------------------------------
// Send 1 bit of communication to the 1-Wire Net and return the
// result 1 bit read from the 1-Wire Net. The parameter 'sendbit'
// least significant bit is used and the least significant bit
// of the result is the return bit.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'sendbit' - the least significant bit is the bit to send
//
// Returns: 0: 0 bit read from sendbit
// 1: 1 bit read from sendbit
//
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
{
unsigned char result;
portnum = 0;
// start timeslot.
OW_PORT = 0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
// usDelay(5);
// send bit out.
OW_PORT = sendbit;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//usDelay(10);
// sample result @ 15 us.
result = OW_PORT;
usDelay(50);
// timeslot done.
OW_PORT = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//usDelay(5);
return result;
}
//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and return the
// result 8 bits read from the 1-Wire Net. The parameter 'sendbyte'
// least significant 8 bits are used and the least significant 8 bits
// of the result is the return byte.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns: 8 bits read from sendbyte
//
SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
{
uchar i;
uchar result = 0;
portnum = 0;
for (i = 0; i < 8; i++)
{
result |= (owTouchBit(portnum,sendbyte & 1) << i);
sendbyte >>= 1;
}
return result;
}
//--------------------------------------------------------------------------
// Send 8 bits of communication to the 1-Wire Net and verify that the
// 8 bits read from the 1-Wire Net is the same (write operation).
// The parameter 'sendbyte' least significant 8 bits are used.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
// 'sendbyte' - 8 bits to send (least significant byte)
//
// Returns: TRUE: bytes written and echo was the same
// FALSE: echo was not the same
//
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
{
return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
}
//--------------------------------------------------------------------------
// Send 8 bits of read communication to the 1-Wire Net and and return the
// result 8 bits read from the 1-Wire Net.
//
// 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to
// indicate the symbolic port number.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -