//---------------------------------------------------------------------------- // C main line //----------------------------------------------------------------------------
#include <m8c.h> // part specific constants and macros #include "PSoCAPI.h" // PSoC API definitions for all User Modules
#define uchar unsigned char #define clearbit( MSKN_REG, MASK ) (MSKN_REG &= ~MASK) // Clear Bit Mask(s) #define setbit( MSKN_REG, MASK ) (MSKN_REG |= MASK) // Set Bit Mask(s) #define AddRd 0x81 //Read Address #define sda 0x20 #define scl 0x80
unsigned char Reg_dat[2]={0,0}; unsigned char E2P_dat[2]={0,0}; unsigned char txbuf[2]={0,0}; BYTE F_ACK=0;
/**/ void uDelay() {;;;} void mDelay(unsigned int j)//A normal delay { unsigned int i; for(;j>0;j--) { for(i=0;i<63;i++) {;} } }
void Start(void) { setbit(PRT1DR,sda); //置Sda为高 setbit(PRT1DR,scl); //置scl为高 uDelay();uDelay(); clearbit(PRT1DR,sda); //sda为低 uDelay(); clearbit(PRT1DR,scl); } //stop IIC void Stop(void) { clearbit(PRT1DR,sda); //sda为低 uDelay(); setbit(PRT1DR,scl); //置scl为高 uDelay();uDelay(); setbit(PRT1DR,sda); //sda为高 uDelay();uDelay(); clearbit(PRT1DR,scl); uDelay(); setbit(PRT1DR,scl);
} /* void reposion() { unsigned int i; clearbit(PRT1DR,scl); uDelay(); setbit(PRT1DR,sda); //准备接收应答 uDelay(); setbit(PRT1DR,scl); uDelay();uDelay(); if(((PRT1DR&sda))&&i<2500){i++; F_ACK=0;} //判断I2C设备有应答则置ACK为1; else F_ACK=1; clearbit(PRT1DR,scl); uDelay(); }*/ //ack IIC void Ack(void) { clearbit(PRT1DR,sda); //sda为低 uDelay(); setbit(PRT1DR,scl); //置scl为高 uDelay();uDelay(); clearbit(PRT1DR,scl); //scl为低 uDelay(); } //unack IIC void NoAck(void) { setbit(PRT1DR,sda); //sda为高 uDelay(); setbit(PRT1DR,scl); //置scl为高 uDelay();uDelay(); clearbit(PRT1DR,scl); //scl为低 uDelay(); } void i2c_init() { setbit(PRT1DR,sda); uDelay();uDelay(); setbit(PRT1DR,scl); uDelay();uDelay(); } /**/ void Send(unsigned char Data,unsigned char n) { unsigned int i; unsigned char BitCounter; unsigned char temp; BitCounter=n; do { temp=Data; clearbit(PRT1DR,scl); uDelay();uDelay(); if((temp&0x80)==0x80) setbit(PRT1DR,sda); else clearbit(PRT1DR,sda); setbit(PRT1DR,scl); //保持数据 temp=Data<<1; Data=temp; BitCounter--; } while(BitCounter); uDelay(); clearbit(PRT1DR,scl); uDelay(); setbit(PRT1DR,sda); //准备接收应答 uDelay(); setbit(PRT1DR,scl); uDelay();uDelay(); if((PRT1DR&sda)&&(i<250)){i++; F_ACK=0;} //判断I2C设备有应答则置ACK为1; else F_ACK=1; clearbit(PRT1DR,scl); uDelay(); setbit(PRT1DR,sda); uDelay(); } unsigned char Read(void) { unsigned char temp=0; unsigned char temp1=0; unsigned char BitCounter=8; setbit(PRT1DR,sda); do { clearbit(PRT1DR,scl); uDelay();uDelay(); setbit(PRT1DR,scl); //准备接收数据 uDelay();uDelay(); if(PRT1DR&sda) temp=temp|0x01; //接收1 else temp=temp&0xfe; //接收0 if(BitCounter-1) { temp1=temp<<1; temp=temp1; } BitCounter--; } while(BitCounter); return(temp); } /**/ void Sensor_reset() //器件重置 { Start(); Send(0x40,8); Send(0xfe,8); uDelay(); clearbit(PRT1DR,scl); } /**/ BYTE WrToSensor(unsigned char AddWr,unsigned char com) { unsigned char i; // unsigned char *PData; Start(); Send(AddWr,8); //Write Address Send(com,8); //Write Data Stop(); mDelay(20); if(F_ACK) return 1; else return 0; } /**/ int RdFromSensor() { int temp; int temp1,temp2;//,*PData; Start(); Send(0x40,8); //写器件地址 uDelay(); Send(0xf1,8); //写入命令 uDelay(); Start(); Send(0x41,8); //发送命读写命令 mDelay(80); temp1=Read(); // Read two Data Ack(); // uDelay(); temp2=Read(); mDelay(5); clearbit(PRT1DR,scl); //钳住总线 NoAck(); Stop(); temp=(temp1<<8)|(temp2&0x00ff); //temp=temp1+temp2; return temp; // } } /**/ /*BYTE ReadRegistor(unsigned char Reg_dat[]) { unsigned char i; unsigned char *PData; PData=Reg_dat; for(i=0;i<2;i++) { Start(); Send(0x80,8);//Ack(); Send(0xE3,8);//Ack(); mDelay(5); Start(); Send(0x81,8); *(PData+i)=Read(); clearbit(PRT1DR,scl); NoAck(); Stop(); if(F_ACK) return 1; else return 0; } }*/ /**/ /* int ReadE2prom(unsigned char addr,unsigned char add) { int temp; int temp1,temp2; unsigned char i; //unsigned char *PData; //PData=E2P_dat; // for(i=0;i<2;i++) // { Start(); Send(0x40,8); uDelay(); Send(0xfa,8); uDelay(); Send(addr,8); Send(add,8); mDelay(10); Start(); Send(0x41,8); temp1=Read(); Ack(); temp1=Read(); mDelay(5); clearbit(PRT1DR,scl); NoAck(); Stop(); temp=temp1+temp1; return temp; // } }*/ void main() { BYTE f_ack,r_ack,E_ack; uchar temp,E_dat,E_temp,rx_dat;//,tx_temp; unsigned int Result=0,tx_temp; DAC8_1_Start(DAC8_1_FULLPOWER); DAC8_1_WriteStall(254); i2c_init(); Sensor_reset(); mDelay(500); DAC8_1_WriteStall(0); mDelay(100); while(1) { F_ACK=0; DAC8_1_WriteStall(0); //WrToSensor(0x40,0xf1); //Stop(); mDelay(50); Result=RdFromSensor(); //if(F_ACK) ;DAC8_1_WriteStall(254); if((Result-tx_temp)!=0) DAC8_1_WriteStall(Result); //else DAC8_1_WriteStall(254); tx_temp=Result; mDelay(5); } }
|
|