3003 views |5 replies
Last login 2023-10-7
Online Time 253 hours
Prestige 1125 points
Points 830 points
I want a program related to the msp430f5529 light or rain sensor.
[Copy link]
/*********************************************************** File name: iic.c Author: czhang Description: Subroutine of iic bus. Version: 1.0, October 11, 2003 **********************************************************************************************/ #include
#include "BH1750.h" #include "iic.h" #include "general.h" /**************************************************************************** Initialize IIC ***************************************************************************/ void InitIIC() { IIC_DIR |= IIC_SCL; //SCL pin is output IIC_DIR &= ~IIC_SDA; //SDA pin is input C_SCL; Stop(); } /******************************************************************************* There is a short delay because the speed of MSP430 is relatively fast. The user can adjust the delay length according to the clock frequency********************************************************************/ void delay() { unsigned char q0; for(q0=0;q0<20;q0++) { _NOP(); } } /******************************************************************************* Start IIC bus***************************************************************************/ void Start() { SDA_OUT; S_SDA; delay(); S_SCL; delay(); C_SDA; delay(); C_SCL; delay(); } /******************************************************************************* Stop operation, release IIC bus***********************************************************************/ void Stop() { SDA_OUT; C_SCL; delay(); C_SDA; delay(); S_SCL; delay(); S_SDA; delay(); } /*************************************************************************** IIC bus response***********************************************************************/ void Ack() { SDA_OUT; C_SDA; delay(); S_SCL; delay(); C_SCL; delay(); S_SDA; } /******************************************************************************* IIC bus no response***************************************************************************/ void NoAck() { SDA_OUT; S_SDA; delay(); S_SCL; delay(); C_SCL; delay(); } /******************************************************************************* IIC bus test response return value: IIC response bit value 0: response 1: no response ******************************************************************************/ unsigned char TestAck() { unsigned char ack; S_SCL; delay(); SDA_IN; delay(); ack=READ_SDA; delay(); C_SCL; delay(); return(ack); } /*********************************************************************** IIC bus write 8-bit data input: 8-bit data to be written***********************************************************************/ void Write8Bit(unsigned int input) { unsigned char temp,q0; SDA_OUT; for(temp=8;temp!=0;temp--) { q0=input&0x80; if(q0==0x80) S_SDA; else C_SDA; delay(); S_SCL; delay(); C_SCL; delay(); input=input<<1; } } /******************************************************************************* IIC bus reads 8-bit data Return value: read 8-bit data **********************************************************************/ unsigned char Read8Bit() { unsigned char temp,q0,rbyte=0; SDA_IN; for(temp=8;temp!=0;temp--) { S_SCL; delay(); rbyte=rbyte<<1; SDA_IN; q0=READ_SDA; if(q0==IIC_SDA) rbyte=rbyte|0x1; delay(); C_SCL; delay(); } return(rbyte); } Have you used it before, feel it~?