void ADC_Update(void) { if (IR_Emitter_Powered) { // ------------ Read reflected light levels ------------ SAR10_SetADCChannel(ADC_CHAN_IR_FRONT); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); // Wait while data is not ready adcIRFront = SAR10_iGetData(); // Read result adcIRFront -= adcIRFrontDark; // Correct reading for ambient lighting SAR10_SetADCChannel(ADC_CHAN_IR_LEFT); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); // Wait while data is not ready adcIRLeft = SAR10_iGetData(); // Read result adcIRLeft -= adcIRLeftDark; // Correct reading for ambient lighting SAR10_SetADCChannel(ADC_CHAN_IR_RIGHT); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); // Wait while data is not ready adcIRRight = SAR10_iGetData(); // Read result adcIRRight -= adcIRRightDark; // Correct reading for ambient lighting IR_Emitter_Off(); } else { //------------ Read ambient light levels ------------ SAR10_SetADCChannel(ADC_CHAN_USER); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); //Wait while data is not ready adcUser = SAR10_iGetData(); // Read result SAR10_SetADCChannel(ADC_CHAN_IR_FRONT); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); //Wait while data is not ready adcIRFrontDark = SAR10_iGetData(); // Read result adcIRFrontDark += ADC_OFFSET_IR_FRONT;// Apply offset SAR10_SetADCChannel(ADC_CHAN_IR_LEFT); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); //Wait while data is not ready adcIRLeftDark = SAR10_iGetData(); // Read result adcIRLeftDark += ADC_OFFSET_IR_LEFT; // Apply offset SAR10_SetADCChannel(ADC_CHAN_IR_RIGHT); SAR10_Trigger(); // Trigger new sample while(SAR10_fIsDataAvailable()==0); //Wait while data is not ready adcIRRightDark = SAR10_iGetData(); // Read result adcIRRightDark += ADC_OFFSET_IR_RIGHT;// Apply offset IR_Emitter_On(); } }
void main(void) { unsigned char k; unsigned char ret; unsigned int *p; M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts // Insert your main routine code here. RES_WDT = 0; Init_IIC(); delay_us(200); //wait for the LCD driver power on HT1621B_Init(); //back light CurrentBackLight = 3; PWM8_BL_WritePeriod(255); // Set period to eight clocks PWM8_BL_WritePulseWidth(3); // Set pulse width to generate a 50% duty PWM8_BL_Start(); //WriteAll_1621(0,a,16); //在起始地址为0 处连续写入16个字节数据 UART_Board_Start(UART_PARITY_NONE); // UART_Board_EnableInt(); UART_Board_IntCntl(UART_Board_ENABLE_RX_INT | UART_Board_DISABLE_TX_INT); UART_Sensor_Start(UART_PARITY_NONE); // UART_Sensor_EnableInt(); UART_Sensor_IntCntl(UART_Sensor_ENABLE_RX_INT | UART_Sensor_DISABLE_TX_INT); Timer16_WritePeriod(3200); Timer16_WriteCompareValue(3200); Timer16_EnableInt(); Timer16_Start(); // UART_Board_CPutString("Woody is a genius!"); PMSFrameFlag = 0; HeadFlag = 0; DataPtr = 0; SAR10_SetClk(SAR10_SYSCLK_16); // Set clock source - system clock/64 SAR10_SetRunMode(SAR10_ONESHOT); // Set running method - one-shot SAR10_SetADCChannel(SAR10_CHS_AMUX0); // Set Port_0_5 as input SAR10_DisableInt(); // Enable SAR10 interrupt SAR10_Start(); // Start conversion #ifdef USE_SI7020 Si7020Init(); #else HumiChipInit(); #endif LCD_Init(); RES_WDT = 0; while(1) { if (!RecTimeoutTimer) { PMSFrameFlag = 0; HeadFlag = 0; DataPtr = 0; } if (PMSFrameFlag) { PMSFrameFlag = 0; if (FrameCheck()) { data_pm2_5 = MyPMSUnion.MyPMFrame.PM2_5_US; data_pm1_0 = MyPMSUnion.MyPMFrame.PM1_0_US; // UART_Board_CPutString("PM2.5:"); // UART_Board_PutSHexInt(data_pm2_5); // UART_Board_PutCRLF(); } else { // UART_Board_CPutString("Checksum fail"); } } if (!one_sec_timer) { one_sec_timer = ONE_SECOND_TIMER_RELOAD; /* k++; if (k>9) k = 0; a[0] = Digit[k]; a[1] = Digit[k]; a[2] = Digit[k]; WriteAll_1621(0,a,3); */ MUX_CR2 |= 0X02; //connect P2.1 Analog bus SAR10_Trigger(); //Trigger new sample while(SAR10_fIsDataAvailable()==0);//Wait while data is not ready LightADCValue = SAR10_iGetData(); // Read result MUX_CR2 &= ~0X02; //disconnect P2.1 Analog bus // UART_Board_CPutString("Light: "); // UART_Board_PutSHexInt(LightADCValue); // UART_Board_PutCRLF(); // PWM8_BL_WritePulseWidth(LightRank(LightADCValue)); DstBackLight = LightRank(LightADCValue); } if(!rh_sample_timer) { rh_sample_timer = ONE_SECOND_TIMER_RELOAD; #ifdef USE_SI7020 if (!RHSampleStep) { ret = Si7020SendCommand(MRH_NHMM); // send the command(Measure RH, No Hold Master Mode) if(ret) { RHSampleStep = 1; } } else { Si7020Read_RH_NHM(RecBuf); Si7020Data = *(unsigned int *)RecBuf; if (CRC8Check()) { // UART_Board_CPutString("RH: "); // UART_Board_PutSHexInt(Si7020Data); // UART_Board_PutCRLF(); Humidity = Si7020CalcRH(Si7020Data); // UART_Board_PutSHexByte(Humidity); // UART_Board_PutCRLF(); } Si7020Read_Temp_after_RHM(RecBuf); Si7020Data = *(unsigned int *)RecBuf; // UART_Board_CPutString("Temperature: "); // UART_Board_PutSHexInt(Si7020Data); // UART_Board_PutCRLF(); Temperature = Si7020CalcTemp(Si7020Data); // UART_Board_PutSHexByte(Temperature); // UART_Board_PutCRLF(); RHSampleStep = 0; } #else HumiChipRdHnT(); /*print the data to the PC*/ /* UART_Board_CPutString("HumiChip: "); UART_Board_PutSHexByte(HumiChipData[0]); UART_Board_PutChar(' '); UART_Board_PutSHexByte(HumiChipData[1]); UART_Board_PutChar(' '); UART_Board_PutSHexByte(HumiChipData[2]); UART_Board_PutChar(' '); UART_Board_PutSHexByte(HumiChipData[3]); UART_Board_PutChar(' '); UART_Board_PutSHexByte(HumiChipData[4]); UART_Board_PutChar(' '); UART_Board_PutSHexByte(HumiChipData[5]); UART_Board_PutCRLF();*/ if (HumiChipCheckSum()) { Humidity = HumiChipData[4]; Temperature = HumiChipData[1]; } #endif } if (!LcdUpdateTimer) { LcdUpdateTimer = HALF_SECOND_TIMER_RELOAD; LCDOuputAll(); } if (!DataUploadTimer) { DataUploadTimer = TWO_SECOND_TIMER_RELOAD; B2BSendData(); } if (B2BFrameFlag) { B2BFrameFlag = 0; if (B2BFrameCheck()) { // UART_Board_CPutString("B2B OK"); // UART_Board_PutCRLF(); HeapLife = myRxUnion.myRxFrame.HepaLife; CarbonLife = myRxUnion.myRxFrame.CarbonLife; SpeedLvl = myRxUnion.myRxFrame.Speed; Odor = myRxUnion.myRxFrame.Odor; Plasma = myRxUnion.myRxFrame.Plasma; LockStatus = myRxUnion.myRxFrame.Lock; mode = myRxUnion.myRxFrame.Mode; Timer = myRxUnion.myRxFrame.Timer; } else { // UART_Board_CPutString("B2B fail"); // UART_Board_PutCRLF(); } } RES_WDT = 0; } }