TMP112-与CC2530的程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/////////////////////////////////////////////////////////////////////////////////////////////////////////ÒÔÏÂΪTMP112.hµÄÄÚÈÝ///////////////////////////////////////////////////////////////////////////////////////////////////////#ifndef__TMP112_H#define__TMP112_H/**SDA(data)andSCL(clock)bits**Specialnote!!!**Iftheclockanddatalinesareinthesameport,youwillneed*tobewareoftheRead/Modify/WriteissueintheCC2530-since*abitsetorclearonanyonebitinaportwillreadandwrite*backallotherbits.*/#ifdef__cplusplusexternC{#endif#includeZComDef.h#includeioCC2530.h#includehal_types.h/*I2CmoduleusesPORT0*//*Changeportasrequired-defaultstoport0*/#defineSCL_BVBV(7)#defineSCLP0_7/*clockonport0bit7*/#defineSCL_DIRP0DIR#defineSDA_BVBV(6)#defineSDAP0_6/*dataonport0bit6*/#defineSDA_DIRP0DIR//#defineI2CTRISTRISB#defineTRUE1#defineFALSE0#defineI2C_ERROR(0xff)#defineI2C_LASTFALSE/*SendAck:nomorebytestosend*/#defineI2C_MORETRUE/*SendAck:morebytestosend*/#defineSCL_HIGH()SCL_DIR|=SCL_BV;SCL=1#defineSCL_LOW()SCL_DIR|=SCL_BV;SCL=0#defineSDA_HIGH()SDA_DIR|=SDA_BV;SDA=1#defineSDA_LOW()SDA_DIR|=SDA_BV;SDA=0/**Timingsforthei2cbus.Timesareroundeduptothenearest*microsecond.*///typedefunsignedcharuchar;voidDelayUs(uint8data);voidI2C_Stop(void);voidI2C_Start(void);uint8I2C_SendByte(uint8data);uint8I2C_ReadByte(void);voidI2C_SendAck(uint8status);externuint16ReadTemp(void);externuint8WriteTmp112(void);#endif/////////////////////////////////////////////////////////////////////////////////////////////////////////ÒÔÏÂΪTMP112.CµÄÄÚÈÝ///////////////////////////////////////////////////////////////////////////////////////////////////////#includeTMP112.h#includehal_types.hvoidDelayUs(uint8data){uint16n;n=data*200;do{asm(NOP);asm(NOP);asm(NOP);}while(--n);}voidI2C_Stop(void){SCL_LOW();DelayUs(2);SDA_LOW();DelayUs(5);SCL_HIGH();DelayUs(10);SDA_HIGH();//floatdatahighDelayUs(5);return;}voidI2C_Start(void){//ensureclockislowSDA_HIGH();//ensuredataishighDelayUs(10);SCL_HIGH();DelayUs(10);SDA_LOW();//High---lowtransitionDelayUs(10);SCL_LOW();}uint8I2C_SendByte(uint8data){uint8i;uint8ret;ret=0;for(i=0;i=7;i++){if((datai)&0x80){SDA_HIGH();//bittosend}else{SDA_LOW();}DelayUs(5);SCL_HIGH();DelayUs(10);SCL_LOW();DelayUs(5);}SDA_DIR&=~SDA_BV;DelayUs(5);SCL_HIGH();DelayUs(10);if(SDA){ret=0;}else{ret=1;}DelayUs(4);SCL_LOW();DelayUs(5);SDA_HIGH();DelayUs(5);returnret;}uint8I2C_ReadByte(void){uint8i;uint8data;for(i=0;i8;i++){SDA_DIR&=~SDA_BV;DelayUs(4);SCL_HIGH();DelayUs(10);data=data1;data|=SDA;DelayUs(10);SCL_LOW();}returndata;}voidI2C_SendAck(uint8status){if(status==0){SDA_LOW();}else{SDA_HIGH();}DelayUs(10);SCL_HIGH();DelayUs(10);SCL_LOW();DelayUs(10);}uint16ReadTemp(void){uint8valh;uint8vall;uint16data;//floattemp;data=0;I2C_Start();//Æô¶¯I2C×ÜÏßI2C_SendByte(0x90);//·¢ËÍÆ÷¼þ´ÓµØÖ·£¨Ð´£©I2C_SendByte(0x00);//·¢ËÍÆ÷¼þζȼĴæÆ÷×ÓµØÖ·I2C_Start();I2C_SendByte(0x91);//·¢ËÍÆ÷¼þ´ÓµØÖ·£¨¶Á£©valh=I2C_ReadByte();//¶ÁζȸßλI2C_SendAck(0);vall=I2C_ReadByte();//¶ÁζȵÍλI2C_SendAck(1);I2C_Stop();data|=valh;data=8;data|=vall;data=4;returndata;}uint8WriteTmp112(void){I2C_Start();I2C_SendByte(0x90);//·¢ËÍÆ÷¼þ´ÓµØÖ·£¨Ð´£©I2C_SendByte(0x01);//·¢ËÍÆ÷¼þ¼Ä´æÆ÷£¨ÅäÖ㩵ØÖ·I2C_SendByte(0x61);//ÅäÖüĴæÆ÷µÄÖµOS(0)R1(1)R0(1)F1(0)F0(0)POL(0)TM(0)SD(1)//±È½Ïģʽ£¬µÍ±£³ÖI2C_SendByte(0xa0);//CR1(1)CR0(0)AL(1)EM(0)0000//4HzI2C_Stop();return0;}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功