⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ledtest.c

📁 The information in this publication is believed to be accurate as of its publication date. Such info
💻 C
📖 第 1 页 / 共 2 页
字号:
                           docrc16(0x07);
                           docrc16(0x00);
						   docrc16(0x0e);
                             // read and calculate the read crc
                           docrc16(temp1);
                           lastcrc16 = docrc16(temp2);
                           // crc should now be 0xB001
                           if (lastcrc16 != 0xB001)
                               {
							    ow_reset();
				                tx_str("make PIOA high crc error!");
					           }
                         else{
	                          tx_str("make PIOA high crc correct!");
			                 }
                            tx_lfbs(); 
							 
                             /*  ow_reset();                
					       write_byte(0xCC);   //LSB address
	                       write_byte(0x55);   //MSB address
					       write_byte(0x07);   //LSB address
	                       write_byte(0x00);   //MSB address
                           write_byte(0x6d);  //
                           temp1=read_byte();
						   temp2=read_byte();
						  // crcbit=read_bit();
						   setcrc16(0x0000);     //
                           docrc16(0x55);
                           docrc16(0x07);
                           docrc16(0x00);
						   docrc16(0x6d);
                             // read and calculate the read crc
                           docrc16(temp1);
                           lastcrc16 = docrc16(temp2);
                           // crc should now be 0xB001
                           if (lastcrc16 != 0xB001)
                               {
							    ow_reset();
				                tx_str("make PIOA low crc error!");
					           }
                         else{
	                          tx_str("make PIOA low crc correct!");
			                 }
                            tx_lfbs(); 
							 delay(10000);
							 ow_reset();                
					       write_byte(0xCC);   //LSB address
	                       write_byte(0xaa);   //MSB address
					       write_byte(0x07);   //LSB address
	                       write_byte(0x00);   //MSB address
                           test=read_byte();  //LSB address
                           temp1=read_byte();
						   temp2=read_byte();
						  // crcbit=read_bit();
						   setcrc16(0x0000);     //
                           docrc16(0xaa);
                           docrc16(0x07);
                           docrc16(0x00);
						   docrc16(test);
                             // read and calculate the read crc
                           docrc16(temp1);
                           lastcrc16 = docrc16(temp2);
                           // crc should now be 0xB001
                           if (lastcrc16 != 0xB001)
                               {
							    ow_reset();
				                tx_str("read status crc error!");
					           }
                         else{
	                          tx_str("read status crc correct!");
			                 }
                            tx_lfbs(); 
               			     if (test==0xff)
							       {
								     tx_str("read_byte=0xff!");
									 tx_lfbs();
									}
							   else if(test<=48) {	
	         				         tx_str("read_byte=");
									 tx_char((test+48));
									  tx_lfbs();
									}
									else {  tx_str("read_byte=");
									 tx_char((test));
									  tx_lfbs();
									}*/
						/* break;
		   case '8':
                           ow_reset();                
					       write_byte(0xCC);   //skip command
	                       write_byte(0x55);   //write status command
					       write_byte(0x07);   //LSB address
	                       write_byte(0x00);   //MSB address
                           write_byte(0x6d);  //data
                           temp1=read_byte();
						   temp2=read_byte();
						  // crcbit=read_bit();
						   setcrc16(0x0000);     //
                           docrc16(0x55);
                           docrc16(0x07);
                           docrc16(0x00);
						   docrc16(0x6d);
                             // read and calculate the read crc
                           docrc16(temp1);
                           lastcrc16 = docrc16(temp2);
                           // crc should now be 0xB001
                           if (lastcrc16 != 0xB001)
                               {
							    ow_reset();
				                tx_str("make PIOA low(Channel FF = 1 transistor off) crc error!");
					           }
                         else{
	                          tx_str("make PIOA low(Channel FF = 1 transistor off) crc correct!");
			                 }
                            tx_lfbs(); 
						//	 delay(10000);
						 break;
          default:   
		            tx_str("default break!");
					break;
		 }
		 }*/
   while(1);
  }

 /*unsigned char rx_char(void)
    {
	   while(!RI)
	      {;}
	   RI=0;
	   return SBUF;
	 }*/

void tx_str(unsigned char *str)
    {int i;
      for (i=0;i<strlen(str);i++)
	        {
			  tx_char(str[i]);
	        }
	}
void tx_char(unsigned char c)
   {
    while(!TI)
	  {
	   ;
	   }
	   TI=0;
	   SBUF=c;
	 }

void tx_lfbs(void)
    {int y;
		 tx_char(0x0A);
	 	for (y=0;y<70;y++)
			 {
              tx_char(0x08);
			}
	 }
void Led_blink(void)
  {
  int i;
      for(i=0;i<10;i++)
          {
           P15=1;
	       delay(200);
	       P15=0;
	       delay(200);
	      }
	}

void Init_Rs232(void)
   {IE=0X00;
    SCON=0x50;
    TMOD=0x20;
	TH1=0xfd;
	TL1=0xfd;
	TR1=1;
	TI=1;
   }

 void delay(int delay)
  {int i,j;
   for (i=0;i<delay;i++)
      for (j=0;j<255;j++);
   }


// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
void write_bit(char bitval)
{
  if (bitval==0)
      {
	   writedatalow();
	   }
  else{
       writedatahigh();
	   }
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us
// WRITE_BYTE - writes a byte to the one-wire bus.
//
/*void writedatalow(void)
  {
  }

void writedatahigh(void)
  {int count=6000;

   owData=0;
   owRst=0;
   wdelay(1);
   owData=1;
   owRst=1;
   owEn=1;
   owBsy=1;
   wdelay(1);
   owEn=0;
   while(count!=0)
       {
	     if(
   }*/

void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
wdelay(5);
}

unsigned char read_bit(void)
   {uchar owireda;
    owireda=readowdatabit();
/*	if (owireda ==0xff)
	       {
			   tx_str("read one bit program error!");
			 }
	if (owireda ==0x00)
	     {
		    tx_str("read one bit==0x00!");
			 }
    if (owireda ==0x01)
	     {
		    tx_str("read one bit==0x01!");
			 }
if ((owireda !=0x01)&&(owireda !=0x00)&&(owireda !=0xff))
         {
		    tx_str("read one bit program error !=0x01,0x00,0xff!");
			}*/
	return(owireda);
	}


void wdelay(int us)
   {
     int s;
	 for (s=0;s<us;s++);
	 }
//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-24)/16 = 28.5 - we use 29. Presence checked
// another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
//
/*unsigned char ow_reset(void)
{
unsigned char presence;
owData = 0; //pull DQ line low
wdelay(29); // leave it low for 480us
owData = 1; // allow line to return high
wdelay(3); // wait for presence
presence = owData; // get presence signal
wdelay(25); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part*/

//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
// shifts it left
wdelay(6); // wait for rest of timeslot
}
return(value);
}

unsigned char First(uchar conditional)
{int flag; 
 unsigned char g; // Output bit
 unsigned char x = 0; 
 unsigned char m = 1; // ROM Bit index
 unsigned char n = 0; // ROM Byte index
 unsigned char k = 1; // bit mask
 unsigned char discrepMarker = 0; // discrepancy marker
 unsigned char nxt; // return value

   LastDeviceFlag = FALSE;
   LastDiscrepancy = 0;
   
   nxt = FALSE; // set the next flag to false
   crc8=0;

   flag = ow_reset();

   if(flag||LastDeviceFlag) // no parts -> return false
      {
        LastDiscrepancy = 0; // reset the search
         return FALSE;
      }
  if (conditional)
          { write_byte(0xec);
		  }
  else{
      write_byte(0xF0);
      }
	  do
      // for all eight bytes
        {
         x = 0;
		/* if(read_bit==0xff)
		     {
			   tx_str("read one bit program error!");
			 }*/
         if(read_bit()==1)
		    {
		     x = 2;
            // tx_str(" first bit=1!");
            //  tx_lfbs();
	        }
         wdelay(6);
         if(read_bit()==1) 
		      {x |= 1; // and its complement
               // tx_str(" next bit=1!");
                 //tx_lfbs();
			   }
           if(x ==3) // there are no devices on the 1-wire
		        {
				  tx_str(" not find slave device!");
				   tx_lfbs();
                   break;
				}
           else
             {
               if(x>0) // all devices coupled have 0 or 1
                     g = x>>1; // bit write value for search
               else
                 {
// if this discrepancy is before the last
// discrepancy on a previous Next then pick
// the same as last time
                   if(m<LastDiscrepancy)
				           {
                            g = ((ROM_NO[n]&k)>0);
						   }
                   else // if equal to last pick 1
				       {
                            g = (m==LastDiscrepancy); // if not then pick 0
					   }
// if 0 was picked then record
// position with mask k
                   if (g==0) discrepMarker = m;
                 }
                 if(g==1) // isolate bit in ROM[n] with mask k
                      ROM_NO[n] |= k;
                else
                      ROM_NO[n] &= ~k;
                 write_bit(g); // ROM search write
                 m++; // increment bit counter m
                 k = k<<1; // and shift the bit mask k
                 if(k==0) // if the mask is 0 then go to new ROM
                      { // byte n and reset mask
                        crc8=docrc8(ROM_NO[n],crc8); // accumulate the CRC
                        n++; k++;
                      }
                }
         }while(n<8); //loop until through all ROM bytes 0-7
if(m<65||crc8) // if search was unsuccessful then
LastDiscrepancy=0; // reset the last discrepancy to 0
else
    {
// search was successful, so set lastDiscrep,
// lastOne, nxt
     LastDiscrepancy = discrepMarker;
     LastDeviceFlag = (LastDiscrepancy==0);
     nxt = TRUE; // indicates search is not complete yet, more
     tx_str("Find Device!");
	  tx_lfbs();
      // parts remain
}
return nxt;
}
// Perform Match ROM
//
unsigned char Send_MatchRom(void)
{
unsigned char i;
if(ow_reset()) 
  {
   tx_str("owire reset failure!");
   return FALSE;
   }
write_byte(0x55); // match ROM command
for(i=0;i<8;i++)
{
 write_byte(ROM_NO[i]); //send ROM code
}
return TRUE;
}

//--------------------------------------------------------------------------
// Reset crc16 to the value passed in
//
// 'reset' - data to set crc16 to.
//
void setcrc16(ushort reset)
{
   utilcrc16 = reset;
   return;
}
//--------------------------------------------------------------------------
// Calculate a new CRC16 from the input data short.  Return the current
// CRC16 and also update the global variable CRC16.
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number is provided to
//              indicate the symbolic port number.
// 'data'     - data to perform a CRC16 on
//
// Returns: the current CRC16
//
ushort docrc16(ushort cdata)
{
   cdata = (cdata ^ (utilcrc16 & 0xff)) & 0xff;
   utilcrc16 >>= 8;

   if (oddparity[cdata & 0xf] ^ oddparity[cdata >> 4])
     utilcrc16 ^= 0xc001;

   cdata <<= 6;
   utilcrc16  ^= cdata;
   cdata <<= 1;
   utilcrc16   ^= cdata;

   return utilcrc16;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -