直接贴代码:
iic.h:
#ifndef _IIC_H#define _IIC_H//oˉêyéù?÷voidIIC_Start(void); voidIIC_Stop(void); voidIIC_Ack(bitackbit); voidIIC_SendByte(unsignedcharbyt); bitIIC_WaitAck(void); unsignedcharIIC_RecByte(void); unsignedcharad_read(unsignedcharadd); #endif
iic.c:
/*程序说明: IIC总线驱动程序软件环境: KeiluVision4.10硬件环境: CT107单片机综合实训平台8051,12MHz日期: 2011-8-9*/#include "stc15f2k60s2.h"#include "intrins.h"#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();} #define SlaveAddrW 0xA0#define SlaveAddrR 0xA1//总线引脚定义sbitSDA=P2^1; /*数据线*/sbitSCL=P2^0; /*时钟线*///总线启动条件voidIIC_Start(void) { SDA=1; SCL=1; somenop; SDA=0; somenop; SCL=0; } //总线停止条件voidIIC_Stop(void) { SDA=0; SCL=1; somenop; SDA=1; } //应答位控制voidIIC_Ack(bitackbit) { if(ackbit) { SDA=0; } else { SDA=1; } somenop; SCL=1; somenop; SCL=0; SDA=1; somenop; } //等待应答bitIIC_WaitAck(void) { SDA=1; somenop; SCL=1; somenop; if(SDA) { SCL=0; IIC_Stop(); return0; } else { SCL=0; return1; } } //通过I2C总线发送数据voidIIC_SendByte(unsignedcharbyt) { unsignedchari; for(i=0;i<8;i++) { if(byt&0x80) { SDA=1; } else { SDA=0; } somenop; SCL=1; byt<<=1; somenop; SCL=0; } } //从I2C总线上接收数据unsignedcharIIC_RecByte(void) { unsignedcharda; unsignedchari; for(i=0;i<8;i++) { SCL=1; somenop; da<<=1; if(SDA) da|=0x01; SCL=0; somenop; } returnda; } unsignedcharad_read(unsignedcharadd) { unsignedchartemp; IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(add); IIC_WaitAck(); IIC_Stop(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); temp=IIC_RecByte(); IIC_Stop(); returntemp; }
main.c:
#include "stc15f2k60s2.h"#include "intrins.h"#include <iic.h>//----------------------------------------------------#define uchar unsigned char #define uint unsigned int //----------------------------------------------------ucharseg[]={ 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0X82,0XF8, 0X80,0X90,0XFF};//数码管显示值uchardianya; doublezhiliang=0; doublejiaqian=0; ucharflag=0; uintdis; uinttim; ucharer=0,san=5,si=0;wu=0,liu=0,qi=0,ba=0; //----------------------------------------------------voidallinit(); voidcheck(); voidTimer0Init(void); voidkey(); voiddisplay(ucharer,san,si,wu,liu,qi,ba); voidDelayms(uintms); //----------------------------------------------------voidmain() { allinit(); Timer0Init(); while(1) { key(); check(); display(er,san,si,wu,liu,qi,ba); } } voidallinit() { P2=0xa0;P0=0x00; P2=0x80;P0=0xff; P2=0xe0;P0=0xff;//段选P2=0xc0;P0=0xff;//位选} voidcheck() { dianya=ad_read(0x01); if(dianya<(255/2)) { P2=0x80;P0=0xfe; } else { P2=0x80;P0=0xff; } if(flag==1) { P2=0xa0;P0=0x10; dis=(uint)(zhiliang*1000); wu=dis/1000; liu=dis/100%10; qi=dis/10%10; ba=dis%10; } elseif(flag==2) { P2=0xa0;P0=0x00; dis=(uint)(jiaqian*100); wu=dis/1000; liu=dis/100%10; qi=dis/10%10; ba=dis%10; } } voidkey() { if(P30==0) { Delayms(5); if(P30==0) { EA=1; ET0=1; flag=1; } while(!P30) { display(er,san,si,wu,liu,qi,ba); }; } if(P31==0) { Delayms(5); if(P31==0) { EA=0; ET0=0; flag=2; } while(!P31) { display(er,san,si,wu,liu,qi,ba); }; } } voiddisplay(ucharer,san,si,wu,liu,qi,ba) { P2=0xe0;P0=0xff; P2=0xe0;P0=seg[er]-0x80;//段选P2=0xc0;P0=0x02;//位选Delayms(1); P2=0xe0;P0=0xff; P2=0xe0;P0=seg[san];//段选P2=0xc0;P0=0x04;//位选Delayms(1); P2=0xe0;P0=0xff; P2=0xe0;P0=seg[si];//段选P2=0xc0;P0=0x08;//位选Delayms(1); P2=0xe0;P0=seg[wu];//段选P2=0xc0;P0=0x10;//位选Delayms(1); P2=0xe0;P0=seg[liu]-0x80;//段选P2=0xc0;P0=0x20;//位选Delayms(1); P2=0xe0;P0=seg[qi];//段选P2=0xc0;P0=0x40;//位选Delayms(1); P2=0xe0;P0=seg[ba];//段选P2=0xc0;P0=0x80;//位选Delayms(1); } voidDelayms(uintms) //@11.0592MHz{ unsignedchari, j,k; for(k=ms;k>0;k--) { _nop_(); _nop_(); _nop_(); i=11; j=190; do { while (--j); } while (--i); } } voidTimer0Init(void) //1毫秒@11.0592MHz{ AUXR|=0x80; //定时器时钟1T模式TMOD&=0xF0; //设置定时器模式TL0=0xCD; //设置定时初值TH0=0xD4; //设置定时初值TF0=0; //清除TF0标志TR0=1; //定时器0开始计时} voidtimer0() interrupt1{ tim++; if(tim%100==0) { zhiliang=zhiliang+0.001; jiaqian=jiaqian+0.005; if(tim==1000) { tim=0; if(zhiliang==99.99) { flag=2; } } } }