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

📄 i2cread.cpp

📁 通过I2C总线
💻 CPP
字号:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define SMBus_Port   0x4000

#define Max  120
#define Nax  120

char string[Max][Nax];
int num1,num2;

extern void NEWIODELAY(void);
extern void Delay5ms(void);
extern void check_smbus_ready(void);

unsigned  I2CReadByte(unsigned dev_id, unsigned index)
{
    unsigned i, reg_val;
    outp(SMBus_Port + 0x04,dev_id + 1);

    NEWIODELAY();
    NEWIODELAY();
    check_smbus_ready();

    outp(SMBus_Port + 0x03 , index);
    NEWIODELAY();
    NEWIODELAY();
    outp(SMBus_Port + 0x02 , 0x48);
    NEWIODELAY();
    NEWIODELAY();

    for(i=0;i<0x9ff;i++)
       NEWIODELAY();

    check_smbus_ready();

    reg_val = inp(SMBus_Port + 0x05);
    NEWIODELAY();
    NEWIODELAY();

    return(reg_val);
}


void NEWIODELAY(void)
{
    _asm  out  0xeb,al
}

void Delay5ms(void)
{
     for(int i=0;i<4000;i++)
     {
	NEWIODELAY();
     }
}

void check_smbus_ready(void)
{
     unsigned i;
     int tmp,status;
     for(i=0;i<0x800;i++)
     {
	 status = inp(SMBus_Port + 0);
	 NEWIODELAY();
	 outp(SMBus_Port + 0,status);
	 NEWIODELAY();

	 tmp = status & 0x02;
	 if(tmp != 0)    break;

	 status &= 0xbf;
	 if(status == 0) break;

	 tmp=status & 0x04;
	 if(tmp !=0) break;
     }
}



void main(void)
{
    char *p1,*q1;
    unsigned  V_DIMM;
    unsigned nibble,high;
    unsigned i;
    float nibble1,cycle;
    int freq;
    unsigned int hex;
    char sign1[10] = "Mhz";
    int length=0;

    int j,b;

    if ((p1 = (char *) malloc(50)) == NULL)
    {
       printf("Not enough memory to allocate buffer\n");
       exit(1);  // terminate program if out of memory
    }

    if ((q1 = (char *) malloc(50)) == NULL)
    {
       printf("Not enough memory to allocate buffer\n");
       exit(1);  // terminate program if out of memory
    }


    V_DIMM = I2CReadByte(0xa0,0x00);
    if(V_DIMM==0x80)
    {
	   V_DIMM = I2CReadByte(0xa0,0x09);
	   nibble = V_DIMM & 0x0f;
	   high = V_DIMM>>4;

	   if( (nibble>=0x01)||(nibble<=0x09) )
	   {
	     nibble1 = nibble/10;
	   }

	   if(nibble==0x0a)
	   {
	     nibble1 = 0x25;
	   }
	   if(nibble==0x0b)
	   {
	     nibble1 = 0.33;
	   }
	   if(nibble==0x0c)
	   {
	     nibble1 = 0.66;
	   }
	   if(nibble==0x0d)
	   {
	     nibble1 = 0.75;
	   }
	   cycle = nibble1 + high;
	   freq = 2000/cycle;

	   itoa(freq,p1,10);
	   strcat(p1,sign1);
	   for(num2=0;*p1!='\0';num2++)
	   {
	      string[101][num2]=*p1++;
//	      length++;
	   }
	   printf("%s\n",p1);
	   length = strlen(string[101]);
	   string[101][length] = '\0';
    }
    else
    {
	   freq = 0;
	   itoa(freq,p1,10);
	   strcat(p1,sign1);
	   for(num2=0;*p1!='\0';num2++)
	   {
	      string[101][num2]=*p1++;
//	      length++;
	   }
	   printf("%s\n",p1);
	   length = strlen(string[101]);
	   string[101][length] = '\0';
     }

     V_DIMM = I2CReadByte(0xa4,0x00);
     if(V_DIMM==0x80)
     {
	   V_DIMM = I2CReadByte(0xa4,0x09);
	   nibble = V_DIMM & 0x0f;
	   high = V_DIMM>>4;

	   if( (nibble>=0x01)||(nibble<=0x09) )
	   {
	      nibble1 = nibble/10;
	   }

	   if(nibble==0x0a)
	   {
	      nibble1 = 0x25;
	   }
	   if(nibble==0x0b)
	   {
	      nibble1 = 0.33;
	   }
	   if(nibble==0x0c)
	   {
	      nibble1 = 0.66;
	   }
	   if(nibble==0x0d)
	   {
	      nibble1 = 0.75;
	   }
	   cycle = nibble1 + high;

	   freq = 2000/cycle;
	   itoa(freq,q1,10);
	   strcat(q1,sign1);
	   printf("%s\n",q1);
	   for(num2=0;*q1!='\0';num2++)
	   {
	      string[102][num2]=*q1++;
//	      length++;
	   }
	   printf("%s\n",q1);
	   length = strlen(string[102]);
	   string[102][length] = '\0';
     }
     else
     {
	   freq = 0;
	   itoa(freq,q1,10);
	   strcat(q1,sign1);
	   printf("%s\n",q1);
	   for(num2=0;*q1!='\0';num2++)
	   {
	      string[102][num2]=*q1++;
//	      length++;
	   }
	   length = strlen(string[102]);
	   string[102][length] = '\0';
     }

     printf("%s  %s\n",string[101],string[102]);
//     free(p1);
//     free(q1);
}

⌨️ 快捷键说明

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