同学们好,最近做cc430f5137和mlx90614非接触式测温,软件模拟IIc和硬件IIc模块都尝试了,都收不到ack信号,做了近两个月了,附上模拟IIc的代码,跪求大神解答,希望可以有点实质性的建议,先谢谢了
#include "msp430_kyg.h"
void Init_CLK(unsigned char nDco) //DCO_CLK 1,8,12,16MHZ------------------
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
switch(nDco)
{
case 1:
UCSCTL3 |= SELREF_2+FLLREFDIV_4; // Set DCO FLL reference = REFO,
UCSCTL2 = FLLD_0 + 374; // Set DCO 1MHz
break;
case 8:
UCSCTL3 |= SELREF_2+FLLREFDIV_4; // Set DCO FLL reference = REFO,
UCSCTL2 = FLLD_2 + 374; // Set DCO 8MHz
break;
case 12:
UCSCTL3 |= SELREF_2+FLLREFDIV_0; // Set DCO FLL reference = REFO,
UCSCTL2 = FLLD_0 + 374; // Set DCO Multiplier for 12MHz
case 16:
UCSCTL3 |= SELREF_2+FLLREFDIV_4; // Set DCO FLL reference = REFO,
UCSCTL2 = FLLD_3 + 374; // Set DCO 16MHz
break;
default:
break;
}
}
#include "mlx90615.h"
//--------------------------------------------------------------------------
//Description: mlx90615操作函数
//write by Meredith
//2010.4.21
//email:cyberkyg@163.com
//此代码仅供学习交流使用,未经作者允许,不可用于其他用途!
//--------------------------------------------------------------------------
void MLX90615_init(void)
{
mSDA_OUT; // Set SDA as Output
mSDA_LOW();
mSCL_OUT; // Set SCL as Output
mSCL_HIGH();
mSDA_HIGH();
SendRequest();
}
void START_bit(void)
{
mSDA_OUT;
mSDA_HIGH(); // Set SDA line
delay_Tbuf; // Wait a few microseconds
mSCL_HIGH(); // Set SCL line
delay_Tbuf; // Generate bus free time between Stop
// and Start condition (Tbuf=4.7us min)
mSDA_LOW(); // Clear SDA line
delay_Tbuf; // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
mSCL_LOW(); // Clear SCL line
delay_Tbuf; // Wait a few microseconds
}
void STOP_bit(void)
{
mSDA_OUT;
mSCL_LOW(); // Clear SCL line
delay_Tbuf; // Wait a few microseconds
mSDA_LOW(); // Clear SDA line
delay_Tbuf; // Wait a few microseconds
mSCL_HIGH(); // Set SCL line
delay_Tbuf; // Stop condition setup time(Tsu:sto=4.0us min)
mSDA_HIGH(); // Set SDA line
}
unsigned char TX_byte(unsigned char Tx_buffer)
{
unsigned char Bit_counter;
unsigned char Ack_bit;
unsigned char bit_out;
for(Bit_counter=8; Bit_counter>0; Bit_counter--)
{
if(Tx_buffer&0x80)
bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
else
bit_out=0; // else clear bit_out
send_bit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
mSCL_HIGH(); // Set SCL line
delay_Tbuf; // High Level of Clock Pulse------------------
mSCL_LOW(); // Clear SCL line
delay_Tbuf; // Low Level of Clock Pulse----------------------
// mSDA_HIGH(); // Master release SDA line ,
mSDA_IN; // SDA-input
_NOP();_NOP();_NOP();
mSCL_HIGH(); // Set SCL line
delay_Tbuf; // High Level of Clock Pulse
if(P1Input(BIT3))
Ack_bit=1; //Read acknowledgment bit, save it in Ack_bit
else
Ack_bit=0;
mSCL_LOW(); // Clear SCL line
delay_Tbuf; // Low Level of Clock Pulse
return Ack_bit;
}//End of Receive_bit
unsigned char RX_byte(unsigned char ack_nack)
{
unsigned char RX_buffer;
unsigned char Bit_Counter;
for(Bit_Counter=8; Bit_Counter>0; Bit_Counter--)
{
if(Receive_bit()) // Get a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
unsigned int MemRead(unsigned char SlaveAddress,unsigned char command)
{
unsigned int data; // Data storage (DataH:DataL)
unsigned char Pec; // PEC byte storage
unsigned char DataL; // Low data byte storage
unsigned char DataH; // High data byte storage
unsigned char arr[6]; // Buffer for the sent bytes
unsigned char PecReg; // Calculated PEC byte storage
unsigned char ErrorCounter; // Defines the number of the attempts for communication with MLX90614
ErrorCounter=0x00; // Initialising of ErrorCounter
do{
crc[5]=0; /* Load CRC value 0x000000000107 */
crc[4]=0;
crc[3]=0;
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
BitPosition=47; /* Set maximum bit position at 47 */
shift=0;
//Find first 1 in the transmited message
i=5; /* Set highest index */
j=0;
while((pec[i]&(0x80>>j))==0 && i>0){
BitPosition--;
if(j<7){
j++;
}
else{
j=0x00;
i--;
}
}/*End of while */
shift=BitPosition-8; /*Get shift value for crc value*/
//Shift crc value
while(shift){
for(i=5; i<0xFF; i--){
if((crc[i-1]&0x80) && (i>0)){
temp=1;
}
else{
temp=0;
}
crc[i]<<=1;
crc[i]+=temp;
}/*End of for*/
shift--;
}/*End of while*/
//Exclusive OR between pec and crc
for(i=0; i<=5; i++){
pec[i] ^=crc[i];
}/*End of for*/
}while(BitPosition>8);/*End of do-while*/