Advertisement
reeps

Tsani4

Apr 10th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.71 KB | None | 0 0
  1. #include "LIB.h"  
  2. #include "tsani.h"
  3. #include <utility.h>
  4. #include <userint.h>
  5. #include <ansi_c.h>
  6. #include <cvirte.h>    
  7. #include "4.h"
  8. #include "toolbox.h"
  9.  
  10. //extern int panelHandle;
  11.  
  12.  
  13. #define DELAY 0
  14.  
  15.  
  16. void initPorts(void)
  17. {
  18.     portMask(0,0x7F);  
  19.     portMask(1,0x00);
  20.     portMask(2,0x07);
  21.    
  22.     portOut(0,0);
  23.     portOut(1,0);
  24.     portOut(2,0);
  25.    
  26. }
  27.  
  28. int writeBus (int sda, int scl)
  29. {
  30.     unsigned char data;
  31. //  data = (sda<<3)|(scl<<4)|0x07;
  32. data=(sda<<3) | (scl<<4) | (1<<5) | (1<<6) | (0x07);                               
  33.     return portOut(0, data);
  34. }
  35. //data=(sda<<3) | (scl<<4) | (1<<5) | (1<<6) | (0x07);
  36.  
  37. int readBus (int* sda, int* scl)
  38. {                              
  39.     unsigned char val0, val1;  
  40.     if ((!portIn (0,&val0)) && (!portIn(1, &val1)))
  41.     {
  42.         //*sda = (val0>>1)&0x01;
  43.         //*scl = (val1>>2)&0x01; or
  44.         *sda =  (val0 >> 7) &0x01;
  45.         *scl =  (val1 >> 0) &0x01;
  46.         return 0;
  47.     }
  48.     else
  49.         return -1;
  50. }
  51.     /*int val0 = 0
  52.       int val1 = 0;
  53.     portIn(1,&val1);
  54.     portIn(0,&val0);
  55.     *sda = val0 >> 7;
  56.     *scl = val1 & 1;*/
  57.    
  58. int currentSDA(void)
  59. {
  60. /*  unsigned char val;
  61.     if (!portIn(0,&val)){
  62.         return (val>>7) & 1; // ??? ???????? ?? 1 ???? I2C-2
  63.     }
  64.     else return -1; */
  65.    
  66.     int sda = 0;
  67.     int scl = 0;
  68.     readBus(&sda, &scl);   
  69.     return sda;
  70. }
  71.  
  72.    
  73.  
  74.  
  75.  
  76. int sendStart(void)
  77. {      
  78.     writeBus(1,1);
  79.     Delay(DELAY);
  80.     writeBus(0,1);
  81.     Delay(DELAY);
  82.     writeBus(0,0);
  83.     Delay(DELAY);
  84.     return 0;
  85. }
  86.  
  87. int sendStop(void)
  88. {
  89.     //writeBus(currentSDA(), 0);
  90.     writeBus(0, 0);
  91.     Delay(DELAY);
  92.     writeBus(0, 1);
  93.     Delay(DELAY);
  94.     writeBus(1, 1);
  95.     Delay(DELAY);
  96.      
  97.     return 0;
  98. }
  99.  
  100.  
  101.    
  102. int sendBit(int bit)
  103. {
  104.     writeBus(currentSDA(),0);
  105.     Delay(DELAY);
  106.     writeBus(bit,0);
  107.     Delay(DELAY);
  108.     writeBus (currentSDA(),1);
  109.     Delay(DELAY);
  110.     writeBus(currentSDA(), 0);
  111.     return 0;
  112. }
  113.  
  114. int sendByte(int byte)
  115. {
  116.     for(int i=0; i<8; i++)
  117.         sendBit((byte>>(7-i))&1);
  118.     return 0;
  119. }
  120.  
  121.  
  122. int getACK (void)  
  123. {
  124.     writeBus(currentSDA(),0);
  125.     Delay(DELAY);
  126.     writeBus(1,0);
  127.     Delay(DELAY);
  128.     writeBus(1,1);
  129.     for(int i=0; i<100; i++){
  130.         Delay(DELAY);
  131.         if(currentSDA()==0){
  132.             return 0;          
  133.         }
  134.     }
  135.     return -1;
  136. }
  137.  
  138.  
  139. int readBit(int* bit)
  140. {          
  141.     writeBus(currentSDA(),0);
  142.  
  143.     Delay(DELAY);
  144.     writeBus(1,0);
  145.     Delay(DELAY);
  146.     writeBus(1,1);
  147.     if (currentSDA()!=-1){
  148.         *bit = currentSDA(); //-
  149.         return 0;
  150.     }
  151.     return -1;
  152.  
  153. }  
  154.  
  155. int readByte(int* byte)
  156. {
  157.     int bit =0;
  158.     *byte = 0;
  159.     for(int i=0; i<8; i++){
  160.         if( readBit(&bit) ==-1){
  161.             return -1;
  162.         }
  163.         //readBit(&bit);
  164.         *byte |= (bit<<(7-i));
  165.         //*byte = (*byte << 1) + bit;
  166.     }  
  167.     return 0;  
  168. }
  169.  
  170.  
  171.  
  172. int write_I2C (int adr, int subadr, int byte){
  173.    
  174.     sendStart();  //1)
  175.    
  176.     //adr = (adr<<1);
  177.     sendByte(adr<<1); //2)
  178.     getACK();
  179.     /*if(getACK()==-1){ //3)
  180.         sendStop();
  181.         return -1;
  182.     } */
  183.    
  184.     sendByte(subadr); //4)
  185.      getACK();
  186.     /*if(getACK()==-1){ //5)
  187.         sendStop();
  188.         return -1;
  189.     }         */
  190.    
  191.     sendByte(byte); //6)
  192.     getACK();
  193.     /*if(getACK()==-1){ //7)
  194.         sendStop();
  195.         return -1;
  196.     }   */
  197.     sendStop(); //8) .
  198.    
  199.     return 0;
  200. }
  201.  
  202. int read_I2C(int adr, int subadr, int* byte)
  203. {
  204.     int adr1, adr2;
  205.    
  206.     sendStart();//1)
  207.    
  208.     adr1=adr<<1;
  209.    
  210.     sendByte(adr1); //2)
  211.    
  212.     if (getACK()==-1) //3)
  213.     {
  214.         sendStop();
  215.         return -1;
  216.     }
  217.    
  218.     sendByte(subadr); //4)
  219.    
  220.     if (getACK()==-1) //5)
  221.     {
  222.         sendStop();
  223.         return -1;
  224.     }
  225.    
  226.     sendStop(); //6)
  227.    
  228.     sendStart(); //  7)
  229.    
  230.     adr2=((adr<<1)|1);
  231.    
  232.     sendByte(adr2); //8)
  233.    
  234.     if (getACK()==-1)  //9)
  235.     {
  236.         sendStop();
  237.         return -1;
  238.     }
  239.    
  240.     readByte(byte); //10)
  241.    
  242.     sendBit(1);     //11) ??????? NACK; // or 1
  243.  
  244.     sendStop();//12) ?????? ?????? STOP.
  245.    
  246.     return 0;
  247. }
  248.  
  249. void task1(void){
  250.     sendStart();
  251.     //sendStop();
  252. //  sendBit(0x01);
  253.     //sendByte(0x01);
  254.     write_I2C(0x00, 0x01, 0x03);    // ? ???????  8 ????????? ??????  (????????? 0?00-0?07), ?????? ????????? ???????????? // 0x01 ?
  255.    
  256. }
  257.  
  258. void task2(void){// ?????????? ??????? ??????? (?????????? ??????? ?????? ?????????? ?? ??????? ? ?????????? ????? 0,25 ?).
  259.     int arr[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
  260.     while (1)   {
  261.    
  262.     for (int i=0; i< 8; i++){
  263.    
  264.     write_I2C(0x00, 0x00, arr[i]); // 0x01
  265.    
  266. //  Delay(0.25);
  267.        
  268.         }
  269.     }
  270. }
  271. void task3(void){  // ???????? ? ???????? ?????? ??????? ???? ??? ? ??????? ?????????
  272. //TesterI2C, ????????? ??? ?? ???? I2C ? ???????? ?? ?????.
  273.    
  274.     char sym;  
  275.     int byte =0;;            
  276.     int sub = 8;  // ????? ? ??????? ???????????? 8 ????????? ?????? (????????? 0x08ч0x0F),
  277.     char arr[9]={0} ;
  278.     for(int i = 0; i< 8; i++){
  279.        
  280.         read_I2C(0x00, sub++, &byte); //0x01
  281.         sym = byte;
  282.         arr[i] = sym;      
  283.    
  284.     }
  285.     printf("%s", arr);
  286. }
  287.  
  288.        
  289. int readTemp(int * data1, int * data2)
  290. {
  291.  
  292.  
  293.     sendStart();
  294.     sendByte((0x28<<1)|1); //??????? ???????? ???????
  295.     getACK();
  296.     readByte(data1);  //?????? 1 ????
  297.        
  298.     writeBus(0,0);
  299.     Delay(DELAY);
  300.     writeBus(0,1);
  301.     Delay(DELAY);
  302.     writeBus(0,0);
  303.     Delay(DELAY);
  304.     writeBus(1,0);
  305.     Delay(DELAY);
  306.    
  307.     readByte(data2); //?????? 2 ????
  308.    
  309.     writeBus(1,0);
  310.     Delay(DELAY);
  311.     writeBus(1,1);
  312.     Delay(DELAY);
  313.     writeBus(1,0);
  314.     Delay(DELAY);
  315.    
  316.     sendStop();
  317.    
  318.     return 0;
  319. }
  320.  
  321.  
  322. void read_word(int *data){
  323.     int data1;
  324.     int data2;
  325.     int sign=0;
  326.     int temp;
  327.     readTemp(&data1, &data2);
  328. //  readTemp(0x28, &data2);
  329.     temp=(data1<<8)| (data2);
  330.    
  331.     sign=temp&0x8000;
  332.    
  333.     temp=temp&0x7FFF;
  334.    
  335.     temp=temp>>6;
  336.     temp=temp|sign;
  337.    
  338.     printf("%d", temp);  
  339.    
  340.     *data=temp;
  341. }      
  342.  
  343. /*      int x,y;
  344.                 x = 0;
  345.                 y = 0;
  346.                 readTemp(0x28,&x, &y);
  347.                 int t = (x<<2)+(y>>6);
  348.                 double temp;
  349.                 if ((x&128) == 128)
  350.                 {
  351.                     temp = -(double)(256-(t>>2)-(t&3)*0.25);
  352.                 }
  353.                 else
  354.                 {
  355.                     temp = (double)((t>>2)+(t&3)*0.25);
  356.                 }*/
  357. /*  int data1;
  358.     int data2;
  359.     short int sign=0;
  360.     short int temp;
  361.     readTemp(0x28, &data1, &data2);
  362. //  readTemp(0x28, &data2);
  363.     temp=((((short int)data1)<<8)| ((short int)data2));
  364.     sign=temp&0x8000;
  365.     temp=temp&0x7FFF;
  366.     temp=temp>>6;
  367.     temp=temp|sign;
  368.     printf("%d", temp);  
  369.     *data=temp;*/
  370. /*void readWord2(char* byte,char* byte2,char address,char subaddress)
  371. readWord2(&tmp,&tmp1,0x28,0);
  372. int readTemp(int * byte1, int * byte2){
  373.     sendStart();
  374.     sendByte(0x28<<1);
  375.     getACK();
  376.     sendByte(0x00);
  377.     getACK();
  378.     sendStop();
  379.     sendStart();
  380.     sendByte((address<<1)+1);
  381.     getACK();
  382.     readByte(byte);
  383.     sendBit(0);
  384.    
  385.     readByte(byte2);
  386.     sendBit(1);
  387.     sendStop();
  388.    
  389. }*/
  390.  
  391. /*int readTemp(int * data1, int * data2)
  392. {
  393.  
  394.  
  395.     sendStart();
  396.     sendByte((0x28<<1)|1); //??????? ???????? ???????
  397.     getACK();
  398.  
  399.     readByte(data1);  //?????? 1 ????
  400.        
  401.     //writeBus(0,0);
  402. //  Delay(DELAY);
  403.     //writeBus(0,1);
  404.     //Delay(DELAY);
  405.     //writeBus(0,0);
  406.     //Delay(DELAY);
  407.     //writeBus(1,0);
  408.     //Delay(DELAY);
  409.    
  410.     readByte(data2); //?????? 2 ????
  411.    
  412.     //writeBus(1,0);
  413.     //Delay(DELAY);
  414.     //writeBus(1,1);
  415.     //Delay(DELAY);
  416.     //writeBus(1,0);
  417.     //Delay(DELAY);
  418.    
  419.     sendSTOP();
  420.    
  421.     return 0;
  422. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement