Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. Ver:V1.02
  3. */
  4. #include "Unibus.h"
  5. /*****************************************************************
  6. * Function Name: UnibusInitt()
  7. * Return Value: void
  8. * Parameters: void
  9. * Description:Uibus initialization
  10. *****************************************************************/
  11. void _Unibus::UnibusInit()
  12. {
  13.     pinMode(UnibusWrie, INPUT_PULLUP);  
  14. }
  15. /*****************************************************************
  16. * Function Name: Write_Unibus(unsigned char cmd,unsigned char data,unsigned char checksum)
  17. * Return Value: unsigned char,Send correctly returns 0, error returns 1
  18. * Parameters: unsigned char cmd,unsigned char data,unsigned char checksum
  19. * Description:send data
  20. *****************************************************************/
  21. unsigned char _Unibus::Write_Unibus(unsigned char cmd,unsigned char data,unsigned char checksum)
  22. {
  23.     unsigned char free_count1=0;
  24.     unsigned char  i,j=0;
  25.     while(!digitalRead(UnibusWrie))
  26.     {
  27.       free_count1++;
  28.       if(free_count1==100)      //If the bus is Low and continues to low after waiting, no data is sent
  29.       {
  30.         free_count1=0;              
  31.         return 1;
  32.       }      
  33.     }
  34.     free_count1=0;  
  35.     if(digitalRead(UnibusWrie))  
  36.     {
  37.       pinMode(UnibusWrie, OUTPUT);
  38.       digitalWrite(UnibusWrie, LOW);    //Send 4ms low, can be 3 ~ 18ms
  39.       delay(4);
  40.       pinMode(UnibusWrie, INPUT);
  41.       free_count1=0;
  42.  
  43.       while(digitalRead(UnibusWrie))          //Waiting for an ACK signal
  44.       {
  45.         free_count1++;
  46.         if(free_count1==100)    //500us after receiving no response signal, communication fail, return 1
  47.         {          
  48.           free_count1=0;            
  49.           return 1;
  50.         }
  51.         delayMicroseconds(5);       //delay 5us
  52.       }
  53.       free_count1=0;
  54.       if(!digitalRead(UnibusWrie))        
  55.       {
  56.         while(!digitalRead(UnibusWrie))     //Wait for the slave to release the bus
  57.         {
  58.           free_count1++;
  59.           if(free_count1==100) //500us after the slave does not release the bus, the communication fail, returns 1
  60.           {                          
  61.             return 1;
  62.           }
  63.           delayMicroseconds(5);     //delay 5us
  64.         }
  65.     pinMode(UnibusWrie, OUTPUT);  
  66.     for(i=1;i<=8;i++)
  67.     {
  68.       j=0x80&(cmd);
  69.       if(j)
  70.       {
  71.         digitalWrite(UnibusWrie, LOW);
  72.         delayMicroseconds(50);
  73.         digitalWrite(UnibusWrie, HIGH);
  74.         delayMicroseconds(70);  
  75.       }
  76.       if(!j)
  77.       {
  78.         digitalWrite(UnibusWrie, LOW);
  79.         delayMicroseconds(50);
  80.         digitalWrite(UnibusWrie, HIGH);
  81.         delayMicroseconds(28);  
  82.       }
  83.       cmd=cmd<<1;
  84.     }
  85.    
  86.     for(i=1;i<=8;i++)
  87.     {
  88.       j=0x80&(data);
  89.       if(j)
  90.       {
  91.         digitalWrite(UnibusWrie, LOW);
  92.         delayMicroseconds(50);
  93.         digitalWrite(UnibusWrie, HIGH);
  94.         delayMicroseconds(70);  
  95.       }
  96.       if(!j)
  97.       {
  98.         digitalWrite(UnibusWrie, LOW);
  99.         delayMicroseconds(50);
  100.         digitalWrite(UnibusWrie, HIGH);
  101.         delayMicroseconds(28);  
  102.       }
  103.       data=data<<1;
  104.     }
  105.    
  106.       for(i=1;i<=8;i++)
  107.     {
  108.       j=0x80&(checksum);
  109.       if(j)
  110.       {
  111.         digitalWrite(UnibusWrie, LOW);
  112.         delayMicroseconds(50);
  113.         digitalWrite(UnibusWrie, HIGH);
  114.         delayMicroseconds(70);  
  115.       }
  116.       if(!j)
  117.       {
  118.         digitalWrite(UnibusWrie, LOW);
  119.         delayMicroseconds(50);
  120.         digitalWrite(UnibusWrie, HIGH);
  121.         delayMicroseconds(28);  
  122.       }
  123.       checksum=checksum<<1;
  124.     }
  125.     digitalWrite(UnibusWrie, LOW);
  126.     delayMicroseconds(50);
  127.     pinMode(UnibusWrie, INPUT); //Release the bus
  128.     return 0;
  129.       }
  130.     }
  131.     return 1;
  132. }
  133. /*****************************************************************
  134. * Function Name: Read_Unibus(unsigned char *Temp_data)
  135. * Return Value: unsigned char,Receive successfully returns 0, error returns 1
  136. * Parameters: unsigned char *Temp_data,Used to save CMD \ Data \ checksum
  137. * Description:Receive data,Receive 24bit data,CMD(8bit)+data(8bit)+Checksum(8bit)
  138. *****************************************************************/
  139. unsigned char _Unibus::Read_Unibus(unsigned char *Temp_data)
  140. {
  141.     unsigned char free_count1=0,free_count2=0;
  142.     unsigned char i=0,j=0,to_count=0;
  143.     unsigned char temp_cmd=0,temp_data=0,temp_checksum=0;
  144.     unsigned char tempA=0;
  145.     pinMode(UnibusWrie, INPUT_PULLUP);
  146.     while(digitalRead(UnibusWrie)==1)
  147.     {  
  148.       delayMicroseconds(50);       //Delay=500us
  149.       free_count1++;
  150.       if(free_count1==100)
  151.       return 1;
  152.     }
  153.      
  154.     free_count1=0;
  155.     if(digitalRead(UnibusWrie)==0)
  156.     {
  157.         while(!digitalRead(UnibusWrie))
  158.         {
  159.           delayMicroseconds(100);       //Delay=100us
  160.           free_count1++;
  161.           if(free_count1==35)           //Received a low level of 3.5 ~ 7ms effective
  162.           {
  163.             break;
  164.           }
  165.           if(digitalRead(UnibusWrie))
  166.           {
  167.             return 1;
  168.           }
  169.         }
  170.         free_count1=0;
  171.         while(!digitalRead(UnibusWrie))
  172.         {
  173.           delayMicroseconds(20);       //Delay=20us  
  174.           free_count1++;
  175.           if(digitalRead(UnibusWrie))
  176.           {
  177.             break;
  178.           }
  179.           if(free_count1==175)           //Slave 250us does not receive a response signal, error, a single delay recommended no more than 20us
  180.           {
  181.             return 1;                    
  182.           }          
  183.         }
  184.         pinMode(UnibusWrie, OUTPUT);
  185.         digitalWrite(UnibusWrie, LOW);
  186.         delayMicroseconds(80);       //Delay=80us  
  187.         pinMode(UnibusWrie, INPUT);
  188.         to_count=0;
  189.         for(i=1;i<=8;i++)
  190.         {
  191.           while(digitalRead(UnibusWrie))   //Waiting to pull down
  192.           {
  193.            to_count++;
  194.            if(to_count>250)
  195.            return 1;
  196.           }
  197.          to_count=0;
  198.          while(!digitalRead(UnibusWrie)) //Waiting to be pulled high
  199.          {
  200.           to_count++;
  201.           if(to_count>250)
  202.           return 1;
  203.          }
  204.         delayMicroseconds(40);     //40us judgment BIT
  205.         if(digitalRead(UnibusWrie))
  206.         j=1;
  207.         if(!digitalRead(UnibusWrie))
  208.         j=0;
  209.         temp_cmd=j|(temp_cmd<<1);    //Receive D7 ~ D0  
  210.       }
  211.          to_count=0;
  212.         for(i=1;i<=8;i++)
  213.         {
  214.           while(digitalRead(UnibusWrie))   //Waiting to pull down
  215.           {
  216.            to_count++;
  217.            if(to_count>250)
  218.            return 1;
  219.           }
  220.          to_count=0;
  221.          while(!digitalRead(UnibusWrie)) //Waiting to be pulled high
  222.          {
  223.           to_count++;
  224.           if(to_count>250)
  225.           return 1;
  226.          }
  227.         delayMicroseconds(40);     //40us judgment BIT
  228.         if(digitalRead(UnibusWrie))
  229.         j=1;
  230.         if(!digitalRead(UnibusWrie))
  231.         j=0;
  232.         temp_data=j|(temp_data<<1);    //Receive D7 ~ D0    
  233.       }
  234.          to_count=0;
  235.         for(i=1;i<=8;i++)
  236.         {
  237.           while(digitalRead(UnibusWrie))   //Waiting to pull down
  238.           {
  239.            to_count++;
  240.            if(to_count>250)
  241.            return 1;
  242.           }
  243.          to_count=0;
  244.          while(!digitalRead(UnibusWrie)) //Waiting to be pulled high
  245.          {
  246.           to_count++;
  247.           if(to_count>250)
  248.           return 1;
  249.          }
  250.         delayMicroseconds(40);     //40us judgment BIT
  251.         if(digitalRead(UnibusWrie))
  252.         j=1;
  253.         if(!digitalRead(UnibusWrie))
  254.         j=0;
  255.         temp_checksum=j|(temp_checksum<<1);    //Receive D7 ~ D0    
  256.       }
  257.       tempA=temp_cmd+temp_data;
  258.       if(tempA==temp_checksum)
  259.       {
  260.         Temp_data[0]=temp_cmd;
  261.         Temp_data[1]=temp_data;
  262.         Temp_data[2]=temp_checksum;          
  263.         return 0;
  264.       }                
  265.       return 1;
  266.            
  267.     }
  268.           return 1;
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. <script type="text/javascript">window.location.href = 'https://www.bestmodulescorp.com/pub/errors/report.php?id=223615054944&skin=default';</script>