#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#ifdef __cplusplus
extern "C"
#endif
////////////////////////////////////////////////////////////////////////////////////
word ITIME = 400; //100 ITIME = ATIME
word itime = 0x6C; //DB
dword Sf = 1;
dword Tint;
dword CH0_NF;
dword CH1_NF;
dword CH_NF;
dword Ratio;
dword LUX;
dword Gain = 1;
word gain = 0x00; // for 2581
//byte PDRIVE_GAIN = 0x23; // for 2771 LED Drive Strength.& Proximity Diode Select.& (0:0) & ALS Gain Control.
dword ch0;
dword ch1;
dword Proximity;
int z = 0 , y = 0;
byte add ;
dword TYPE;
byte Prox_byte; // for 2771
//unsigned int iGain=2;
//unsigned int tIntCycles=148;
unsigned long lux_data;
unsigned long chScale0;
unsigned long chScale1;
unsigned long channel1;
unsigned long channel0;
unsigned long ratio1;
unsigned long ratio;
unsigned int b, m;
unsigned long temp;
word Prox_word;
word ch0_word;
word ch1_word;
byte ch0_byte;
byte ch1_byte;
unsigned long lux;
int d;
word read_data[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //2581 use 5 , 2771 use 7
byte disp_D1,disp_D2,disp_D3,disp_D4;
word Count;
const byte disp_data[16]={0x3f,0x12,0x5d,0x57,0x72,0x67,0x6F,0x13,0x7F,0x77,0x7B,0x6E,0x4C,0x5E,0x6D,0x69}; // a
//hi = on //0x3f,0x06,0x5b,0x4f,0x66,0x6D,0x7D,0x07,0x7F,0x6f // |---|
// 0 1 2 3 4 5 6 7 8 9 A B C D E F // f| g |e
int i=0,s=1,u=0,v=0,l=0,o=0,p=0; // |---|
int k=0; // d| |b
int T=0x1e;
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////
void Init_I2C (void){
IICC2 = 0x00;
IICA = 0x00;
IICS = 0x92;
IICC1 |= (unsigned char)0x80;
}
////////////////////////////////////////////////////////////////
void IIC_write_byte( byte Command, byte Count){
IICC_TXAK = 0;
IICC_TX = 1;
IICC_MST = 1; // And generate START condition;
IICD =add; //Slave Address
while (!IICS_IICIF){}// wait until IBIF;
IICS_IICIF=1; // clear the interrupt event flag;
while(IICS_RXAK){}// check for RXAK;
IICD = Command;
while (!IICS_IICIF){}// wait until IBIF;
IICS_IICIF=1; // clear the interrupt event flag;
while(IICS_RXAK){}// check for RXAK;
IICD = Count;
while (!IICS_IICIF){}// wait until IBIF;
IICS_IICIF=1; // clear the interrupt event flag;
while(IICS_RXAK){}// check for RXAK;
IICS_IICIF=1; // clear the interrupt event flag;
IICC_MST = 0; // generate STOP condition;
}
//////////////////////////////////////////////////////////////
void Read_word(unsigned reg){
int i=0;
byte temp;
IICC_TXAK = 0; // RX/TX = 1; MS/SL = 1; TXAK = 0;
IICC_TX = 1;
IICC_MST = 1; // And generate START condition;
IICD = add;
while (!IICS_IICIF){}// wait until IBIF;
IICS_IICIF=1; // clear the interrupt event flag;
while(IICS_RXAK){}// check for RXAK;
IICD = reg;
while (!IICS_IICIF){}// wait until IBIF;
IICS_IICIF=1; // clear the interrupt event flag;
while(IICS_RXAK){}// check for RXAK;
for(;i<5;i++){ }
IICC_RSTA = 1;
IICD = ++add;
while (!IICS_IICIF){}
IICS_IICIF=1;
while (IICS_RXAK){}
IICC_TX = 0;
temp = IICD;
while (!IICS_IICIF){}
IICS_IICIF=1;
d+=1;
read_data[d] = IICD;
IICC_TXAK = 1;
while (!IICS_IICIF){}
IICS_IICIF=1;
IICC_MST = 0;
d+= 1;
read_data[d] = IICD;
add -=1 ;
}
///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
void MCU_init(void); /* Device initialization function declaration */
////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// |___|
//////////////////////////////////////////////////////////////////////// // c ,h
void delay(byte n){ //250US //
byte i=0;
byte j;
for(j=0;j<n;j++) {
for (i; i<=200; i++){ }
}
if(PTFD_PTFD4==0){ y=1; }
if(PTFD_PTFD4==1 && y==1){
y=0;
l++;
if(l>=4){ l=0; }
}
// if(PTFD_PTFD4==0){
// o++;
// if(o==300){
// l++;
// gain += 1;
// if(l >= 3) { l = 0; }
// o=0;
// }
// }
}
//////////////////////////////////////////////////////////////////////////////
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void main(void) {
MCU_init(); /* call Device Initialization */
asm_main(); /* call the assembly function */
add = 0x72; TYPE = 0x2771; disp_D1=2; disp_D2=7; disp_D3=7; disp_D4=1;
PTDDS = 0x04;
PTDPE = 0x00;
PTDDD = 0xff;
PTDD = 0x04;
PTEDS = 0xff;
PTEPE = 0xff;
PTEDD = 0xff;
PTED = 0x00;
PTGDS = 0x1f;
PTGPE = 0x1f;
PTGDD = 0x1f;
PTGD = 0x1f;
PTFPE = 0XFF;
PTFDS = 0X00;
PTFD = 0X10;
//############################### ALL SHOW DATA #################################
for (k=0;k<800;k++){
for (s=0;s<=4;s++){
if(s==0){
PTED = disp_data[disp_D4];
T=0X1E;
PTGD = T;
delay(2000);
}
if(s==1){ //第3位元 拾位數
PTED = disp_data[disp_D3];
T=0X1D;
PTGD = T;
delay(2000);
}
//第2位元 百位數
if(s==2){
PTED = disp_data[disp_D2];
T=0X1B;
PTGD = T;
delay(2000);
}
if(s==3){ //第1位元 千位數
PTED = disp_data[disp_D1];
T=0X17;
PTGD = T;
delay(2000);
}
if(s==4){
PTDD = 0XFF;
PTGD = 0x0F;
PTED = 0x00;
if(l==0) { PTED = 0x01; }//1
PTDD = 0X00;
delay(2000);
PTDD = 0XFF;
}
}
l=0;
}
//############################### ALL SHOW DATA ########## END ####################
Init_I2C ();
IIC_write_byte (0x80,0x0F); //POWER ON , ADC_E , P_E
delay(100);
delay(200);
IIC_write_byte (0x81,0x6C); // SET ATIME=400ms
delay(100);
delay(200);
IIC_write_byte (0x82,0XFF); // SET P_CONTROL MXA=1023
delay(100);
delay(200);
IIC_write_byte (0x8E,0X03); // SET P_PULSE
delay(100);
delay(200);
IIC_write_byte (0x8F,0X60); // SET P_LED Courrent , P_use IR diode , GAIN=1
delay(100);
delay