Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "LIB.h"
- #include "tsani.h"
- #include <utility.h>
- #include <userint.h>
- #include <ansi_c.h>
- #include <cvirte.h>
- #include "4.h"
- #include "toolbox.h"
- //extern int panelHandle;
- #define DELAY 0
- void initPorts(void)
- {
- portMask(0,0x7F);
- portMask(1,0x00);
- portMask(2,0x07);
- portOut(0,0);
- portOut(1,0);
- portOut(2,0);
- }
- int writeBus (int sda, int scl)
- {
- unsigned char data;
- // data = (sda<<3)|(scl<<4)|0x07;
- data=(sda<<3) | (scl<<4) | (1<<5) | (1<<6) | (0x07);
- return portOut(0, data);
- }
- //data=(sda<<3) | (scl<<4) | (1<<5) | (1<<6) | (0x07);
- int readBus (int* sda, int* scl)
- {
- unsigned char val0, val1;
- if ((!portIn (0,&val0)) && (!portIn(1, &val1)))
- {
- //*sda = (val0>>1)&0x01;
- //*scl = (val1>>2)&0x01; or
- *sda = (val0 >> 7) &0x01;
- *scl = (val1 >> 0) &0x01;
- return 0;
- }
- else
- return -1;
- }
- /*int val0 = 0
- int val1 = 0;
- portIn(1,&val1);
- portIn(0,&val0);
- *sda = val0 >> 7;
- *scl = val1 & 1;*/
- int currentSDA(void)
- {
- /* unsigned char val;
- if (!portIn(0,&val)){
- return (val>>7) & 1; // ??? ???????? ?? 1 ???? I2C-2
- }
- else return -1; */
- int sda = 0;
- int scl = 0;
- readBus(&sda, &scl);
- return sda;
- }
- int sendStart(void)
- {
- writeBus(1,1);
- Delay(DELAY);
- writeBus(0,1);
- Delay(DELAY);
- writeBus(0,0);
- Delay(DELAY);
- return 0;
- }
- int sendStop(void)
- {
- //writeBus(currentSDA(), 0);
- writeBus(0, 0);
- Delay(DELAY);
- writeBus(0, 1);
- Delay(DELAY);
- writeBus(1, 1);
- Delay(DELAY);
- return 0;
- }
- int sendBit(int bit)
- {
- writeBus(currentSDA(),0);
- Delay(DELAY);
- writeBus(bit,0);
- Delay(DELAY);
- writeBus (currentSDA(),1);
- Delay(DELAY);
- writeBus(currentSDA(), 0);
- return 0;
- }
- int sendByte(int byte)
- {
- for(int i=0; i<8; i++)
- sendBit((byte>>(7-i))&1);
- return 0;
- }
- int getACK (void)
- {
- writeBus(currentSDA(),0);
- Delay(DELAY);
- writeBus(1,0);
- Delay(DELAY);
- writeBus(1,1);
- for(int i=0; i<100; i++){
- Delay(DELAY);
- if(currentSDA()==0){
- return 0;
- }
- }
- return -1;
- }
- int readBit(int* bit)
- {
- writeBus(currentSDA(),0);
- Delay(DELAY);
- writeBus(1,0);
- Delay(DELAY);
- writeBus(1,1);
- if (currentSDA()!=-1){
- *bit = currentSDA(); //-
- return 0;
- }
- return -1;
- }
- int readByte(int* byte)
- {
- int bit =0;
- *byte = 0;
- for(int i=0; i<8; i++){
- if( readBit(&bit) ==-1){
- return -1;
- }
- //readBit(&bit);
- *byte |= (bit<<(7-i));
- //*byte = (*byte << 1) + bit;
- }
- return 0;
- }
- int write_I2C (int adr, int subadr, int byte){
- sendStart(); //1)
- //adr = (adr<<1);
- sendByte(adr<<1); //2)
- getACK();
- /*if(getACK()==-1){ //3)
- sendStop();
- return -1;
- } */
- sendByte(subadr); //4)
- getACK();
- /*if(getACK()==-1){ //5)
- sendStop();
- return -1;
- } */
- sendByte(byte); //6)
- getACK();
- /*if(getACK()==-1){ //7)
- sendStop();
- return -1;
- } */
- sendStop(); //8) .
- return 0;
- }
- int read_I2C(int adr, int subadr, int* byte)
- {
- int adr1, adr2;
- sendStart();//1)
- adr1=adr<<1;
- sendByte(adr1); //2)
- if (getACK()==-1) //3)
- {
- sendStop();
- return -1;
- }
- sendByte(subadr); //4)
- if (getACK()==-1) //5)
- {
- sendStop();
- return -1;
- }
- sendStop(); //6)
- sendStart(); // 7)
- adr2=((adr<<1)|1);
- sendByte(adr2); //8)
- if (getACK()==-1) //9)
- {
- sendStop();
- return -1;
- }
- readByte(byte); //10)
- sendBit(1); //11) ??????? NACK; // or 1
- sendStop();//12) ?????? ?????? STOP.
- return 0;
- }
- void task1(void){
- sendStart();
- //sendStop();
- // sendBit(0x01);
- //sendByte(0x01);
- write_I2C(0x00, 0x01, 0x03); // ? ??????? 8 ????????? ?????? (????????? 0?00-0?07), ?????? ????????? ???????????? // 0x01 ?
- }
- void task2(void){// ?????????? ??????? ??????? (?????????? ??????? ?????? ?????????? ?? ??????? ? ?????????? ????? 0,25 ?).
- int arr[8] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
- while (1) {
- for (int i=0; i< 8; i++){
- write_I2C(0x00, 0x00, arr[i]); // 0x01
- // Delay(0.25);
- }
- }
- }
- void task3(void){ // ???????? ? ???????? ?????? ??????? ???? ??? ? ??????? ?????????
- //TesterI2C, ????????? ??? ?? ???? I2C ? ???????? ?? ?????.
- char sym;
- int byte =0;;
- int sub = 8; // ????? ? ??????? ???????????? 8 ????????? ?????? (????????? 0x08ч0x0F),
- char arr[9]={0} ;
- for(int i = 0; i< 8; i++){
- read_I2C(0x00, sub++, &byte); //0x01
- sym = byte;
- arr[i] = sym;
- }
- printf("%s", arr);
- }
- int readTemp(int * data1, int * data2)
- {
- sendStart();
- sendByte((0x28<<1)|1); //??????? ???????? ???????
- getACK();
- readByte(data1); //?????? 1 ????
- writeBus(0,0);
- Delay(DELAY);
- writeBus(0,1);
- Delay(DELAY);
- writeBus(0,0);
- Delay(DELAY);
- writeBus(1,0);
- Delay(DELAY);
- readByte(data2); //?????? 2 ????
- writeBus(1,0);
- Delay(DELAY);
- writeBus(1,1);
- Delay(DELAY);
- writeBus(1,0);
- Delay(DELAY);
- sendStop();
- return 0;
- }
- void read_word(int *data){
- int data1;
- int data2;
- int sign=0;
- int temp;
- readTemp(&data1, &data2);
- // readTemp(0x28, &data2);
- temp=(data1<<8)| (data2);
- sign=temp&0x8000;
- temp=temp&0x7FFF;
- temp=temp>>6;
- temp=temp|sign;
- printf("%d", temp);
- *data=temp;
- }
- /* int x,y;
- x = 0;
- y = 0;
- readTemp(0x28,&x, &y);
- int t = (x<<2)+(y>>6);
- double temp;
- if ((x&128) == 128)
- {
- temp = -(double)(256-(t>>2)-(t&3)*0.25);
- }
- else
- {
- temp = (double)((t>>2)+(t&3)*0.25);
- }*/
- /* int data1;
- int data2;
- short int sign=0;
- short int temp;
- readTemp(0x28, &data1, &data2);
- // readTemp(0x28, &data2);
- temp=((((short int)data1)<<8)| ((short int)data2));
- sign=temp&0x8000;
- temp=temp&0x7FFF;
- temp=temp>>6;
- temp=temp|sign;
- printf("%d", temp);
- *data=temp;*/
- /*void readWord2(char* byte,char* byte2,char address,char subaddress)
- readWord2(&tmp,&tmp1,0x28,0);
- int readTemp(int * byte1, int * byte2){
- sendStart();
- sendByte(0x28<<1);
- getACK();
- sendByte(0x00);
- getACK();
- sendStop();
- sendStart();
- sendByte((address<<1)+1);
- getACK();
- readByte(byte);
- sendBit(0);
- readByte(byte2);
- sendBit(1);
- sendStop();
- }*/
- /*int readTemp(int * data1, int * data2)
- {
- sendStart();
- sendByte((0x28<<1)|1); //??????? ???????? ???????
- getACK();
- readByte(data1); //?????? 1 ????
- //writeBus(0,0);
- // Delay(DELAY);
- //writeBus(0,1);
- //Delay(DELAY);
- //writeBus(0,0);
- //Delay(DELAY);
- //writeBus(1,0);
- //Delay(DELAY);
- readByte(data2); //?????? 2 ????
- //writeBus(1,0);
- //Delay(DELAY);
- //writeBus(1,1);
- //Delay(DELAY);
- //writeBus(1,0);
- //Delay(DELAY);
- sendSTOP();
- return 0;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement