初中生
最后登录1970-1-1
在线时间 小时
注册时间2016-9-24
|
楼主 |
发表于 2016-10-9 09:34:47
|
显示全部楼层
下面是MLX90614的.c文件了,时序调试了好久都不行,
#include<iocc2530.h>
#include"hwai.h"
//#include"intrins.h"
#define Nack_counter 10
char bit_out,bit_in;
unsigned char DataH,DataL,Pecreg;
#define SDA P0_7
#define SCL P0_4
//时钟频率为32M-us
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
for(j=0;j<2;j++);
}
void Delay_ms(unsigned int k)
{
T1CC0L = 0xe8;
T1CC0H = 0x03;
T1CTL = 0x0a; //模模式 32分频
while(k)
{
while(!((T1CNTL >= 0xe8)&&(T1CNTH >= 0x03)));
k--;
}
T1CTL = 0x00; //关闭定时器
}
void hwaioutscl(void)//设置端口为输出
{
// P1DIR |= 0x20;//0010 0000 P1_5
P0DIR |=0x40;//P04
}
void hwaiinsda(void)//设置端口为上拉输入
{
// P0DIR &= 0x7f;//0111 1111 P0_6
//P0DIR &=0xbf;//1011 1111
// P0INP &=0xbf;//1011 1111
// P2INP &=0xcf;//1101 1111
P0DIR &= 0x7f;//P07
}
void hwaioutsda(void)//设置端口为输出
{
//P0DIR |= 0x40;//0100 0000
P0DIR |= 0x80;//P07
}
//***
void init()
{
//hwaioutscl();
//hwaioutsda();
SCL=1;//SDA=1;
delay(8);
SCL=0;
Delay_ms(2);
SCL=1;
}
void start_bit(void)
{
// hwaioutscl();
hwaioutsda();
SDA=1;
//delay(1);
SCL=1;
delay(5);
SDA=0;
delay(5);
SCL=0;
//delay(5);
}
void stop_bit(void)
{
//hwaioutscl();
hwaioutsda();
SCL=0;
//delay(5);
SDA=0;
delay(5);
SCL=1;
delay(5);
SDA=1;
}
void tx_byte(unsigned char dat_byte)
{
char i,n,dat;
n=Nack_counter;
for(n=Nack_counter;n>0;n--)
{
dat=dat_byte;
for(i=0;i<8;i++)
{
if(dat&0x80)
bit_out=1;
else
bit_out=0;
send_bit();
dat=dat<<1;
}
receive_bit();
if(bit_in==1)
{
stop_bit();
delay(1);
start_bit();
}
else break;
}
}
void send_bit(void)
{
//hwaioutscl();
hwaioutsda();
if(bit_out==0)
SDA=0;
else
SDA=1;
delay(1);
SCL=1;
delay(1);
SCL=0;
//delay(8);
}
unsigned char rx_byte(void)
{
unsigned char i,dat;
dat=0;
for(i=0;i<8;i++)
{
dat=dat<<1;
receive_bit();
if(bit_in==1)
dat=dat+1;
}
send_bit();
return dat;
}
void receive_bit(void)
{
//hwaioutscl();
//hwaioutsda();
//SDA=1;
//bit_in=1;
hwaiinsda();
SCL=1;
//delay(8);
bit_in=SDA;
/*if(SDA==1)
bit_in=1;
else
bit_in=0;*/
delay(1);
SCL=0;
//delay(8);
}
unsigned int memread(void)
{
hwaioutscl();
init();
//SCL=0;
start_bit();
tx_byte(0x00); //Send SlaveAddress
delay(1);
tx_byte(0x07); //Send Command
//------------*/
stop_bit();
start_bit();
tx_byte(0x01);
bit_out=0;
DataL=rx_byte();
bit_out=0;
DataH=rx_byte();
bit_out=1;
Pecreg=rx_byte();
stop_bit();
return(DataH*256+DataL);
} |
|