|
自己寫的代碼 打算分享出來 雖然不是最精簡算法,但是能基本實現(xiàn)賽題要求的功能,僅供參考。和大家一起交流
- #include <stc15f2k60s2.h>
- #include <absacc.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit S4=P3^3;
- sbit S5=P3^2;
- sbit S6=P3^1;
- sbit S7=P3^0;
- sbit SDA = P2^1; /* 數(shù)據(jù)線 */
- sbit SCL = P2^0; /* 時鐘線 */
- uchar SMG_duanma[12]={0XC0,0XF9,0XA4,0XB0,
- 0X99,0X92,0X82,0XF8,
- 0X80,0X90,0XFF,0X7f};
- uchar SMG_pianxuan[10]={0x01,0x02,0X02,0x04,0x08,
- 0x10,0x20,0X20,0x40,0x80};
- uchar SMGSL_huanchong[10]={10,0,11,5,0,0,0,11,0,0};
- uchar SMGJG_huanchong[10]={10,0,11,5,0,0,0,11,0,0};
- int flag,shuiliang,qiehuan,flag1;
- void Delay1ms(int time) //@12.000MHz
- {
- uchar i, j;
- while(time--)
- {
- i = 12;
- j = 169;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- void Delay6us() //@12.000MHz
- {
- unsigned char i;
- _nop_();
- _nop_();
- i = 15;
- while (--i);
- }
- //總線啟動條件
- void IIC_Start()
- {
- SDA = 1;
- SCL = 1;
- Delay6us();
- SDA = 0;
- Delay6us();
- SCL = 0;
- }
- //總線停止條件
- void IIC_Stop()
- {
- SDA = 0;
- SCL = 1;
- Delay6us();
- SDA = 1;
- Delay6us();
- }
- //等待應(yīng)答
- bit IIC_WaitAck()
- {
- bit ackbit;
-
- SCL = 1;
- Delay6us();
- ackbit = SDA;
- SCL = 0;
- Delay6us();
- return ackbit;
- }
- //通過I2C總線發(fā)送數(shù)據(jù)
- void IIC_SendByte(uchar byt)
- {
- uchar i;
- for(i=0; i<8; i++)
- {
- SCL = 0;
- Delay6us();
- if(byt & 0x80) SDA = 1;
- else SDA = 0;
- Delay6us();
- SCL = 1;
- byt <<= 1;
- Delay6us();
- }
- SCL = 0;
- }
- //從I2C總線上接收數(shù)據(jù)
- uchar IIC_RecByte()
- {
- uchar i, da;
- for(i=0; i<8; i++)
- {
- SCL = 1;
- Delay6us();
- da <<= 1;
- if(SDA) da |= 1;
- SCL = 0;
- Delay6us();
- }
- return da;
- }
- long AD_read()
- {
- long temp;
- IIC_Start();
- IIC_SendByte(0x90);
- IIC_WaitAck();
- IIC_SendByte(0x01);
- IIC_WaitAck();
- IIC_Stop();
-
- IIC_Start();
- IIC_SendByte(0x91);
- IIC_WaitAck();
- temp=IIC_RecByte();
- IIC_Stop();
- temp=temp*1.9;
- return temp;
- }
復(fù)制代碼
代碼只展示部分
|
評分
-
查看全部評分
|