/* ******************************************************************************* ** ** This device driver was created by Applilet2 for 78K0S/Kx1+ ** 8-Bit Single-Chip Microcontrollers ** ** Copyright(C) NEC Electronics Corporation 2002 - 2012 ** All rights reserved by NEC Electronics Corporation. ** ** This program should be used on your own responsibility. ** NEC Electronics Corporation assumes no responsibility for any losses ** incurred by customers or third parties arising from the use of this file. ** ** Filename : main.c ** Abstract : This file implements main function. ** APIlib : Applilet2 for 78K0S/Kx1+ V2.41 [9 Sep. 2008] ** ** Device : uPD78F9222 ** ** Compiler : CC78K0S ** ** Creation date: 2012/04/04 ** ******************************************************************************* */ /* ******************************************************************************* ** Include files ******************************************************************************* */ #include "macrodriver.h" #include "watchdogtimer.h" #include "system.h" #include "port.h" #include "ad.h" #include "serial.h" /* Start user code for include definition. Do not edit comment generated here */ /* End user code for include definition. Do not edit comment generated here */ #include "user_define.h" /* ******************************************************************************* ** Global define ******************************************************************************* */ /* Start user code for global definition. Do not edit comment generated here */ #define WAIT_MS 188 //#define INTERVAL_WAIT_2 NOP();NOP();NOP();NOP();NOP(); #define INTERVAL_WAIT_2 #define DATA_MAX 500 #define COUNT_READER_MIN 5 #define COUNT_DATA1_MIN 4 #define STATE_REJECT_PULLUP 1 #define STATE_WAIT_READER_L 2 #define STATE_WAIT_READER_H 3 #define STATE_GET_DATA_L 4 #define STATE_GET_DATA_H 5 #define STATE_FIN 6 #define STATE_ERROR 7 #define AD_ENABLE //#define DEBUG void wait_ms(unsigned int); void print_chr(unsigned char); void print_num(unsigned char); void print_addat(unsigned short); #ifdef AD_ENABLE void get_addata(unsigned short* , unsigned short* , unsigned short* , unsigned short*); void print_LM61CIZ(unsigned short, unsigned short); static const unsigned short AD2TEMP[83] = {0,5,10,15,20,25,30,34,39,44,49,54,59,64,69,73,78,83,88,93,98,103,108,112,117,122,127,132,137,142,147,152,156,161,166,171,176,181,186,191,195,200,205,210,215,220,225,230,234,239,244,249,254,259,264,269,274,278,283,288,293,298,303,308,313,317,322,327,332,337,342,347,352,356,361,366,371,376,381,386,391,396,400}; #endif BOOL ad_finish; BOOL uart_tx_finish; unsigned char dat[DATA_MAX/8 +1]; unsigned short addat0,addat1,addat2,addat3; /* End user code for global definition. Do not edit comment generated here */ /* **----------------------------------------------------------------------------- ** ** Abstract: ** This function implements main function. ** ** Parameters: ** None ** ** Returns: ** None ** **----------------------------------------------------------------------------- */ void main( void ) { /* Start user code. Do not edit comment generated here */ #ifdef DEBUG UCHAR volatile dbg_i=0; #endif UCHAR i=0; UCHAR j=0; UCHAR volatile decdat =0; UCHAR volatile decstate=0; UCHAR volatile decpos =0; USHORT volatile dechumidity =0; USHORT volatile dectemperature=0; UCHAR volatile deccrc =0; UINT cnt=0; WDT_Restart(); /* restart wdt */ UART6_Start(); /* startup uart */ #ifdef AD_ENABLE get_addata( &addat0 , &addat1 , &addat2 , &addat3 ); /* reject 1st ad result */ #endif print_chr('w');print_chr('a');print_chr('k');print_chr('e');print_chr('u');print_chr('p');print_chr('.');print_chr('.');print_chr('.'); wait_ms(500);WDT_Restart();wait_ms(500);WDT_Restart(); wait_ms(500);WDT_Restart();wait_ms(500);WDT_Restart(); /* wait over 1sec for sensor startup */ print_chr('o');print_chr('k');print_chr(0xa);print_chr(0xd); while (1) { for(cnt=0; cnt<=(DATA_MAX/8); cnt++) dat[cnt]=0x00; /* initialize */ PORT_ChangeP123Output(0); /* output P12_3=L */ wait_ms(10); /* wait over 1ms */ WDT_Restart(); /* ************************************************************ ** receive sensor data ** ************************************************************ */ DI(); /* disable interrupt */ PORT_ChangeP123Input(1); /* input P12_3 with pullup */ for(cnt=0; cnt<=(DATA_MAX/8);cnt++){ dat[cnt]|=P12.3<<0; dat[cnt]|=P12.3<<1; dat[cnt]|=P12.3<<2; dat[cnt]|=P12.3<<3; dat[cnt]|=P12.3<<4; dat[cnt]|=P12.3<<5; dat[cnt]|=P12.3<<6; dat[cnt]|=P12.3<<7; } WDT_Restart(); EI(); /* enable interrupt */ /* **************** end receive sensor data ******************* */ #ifdef DEBUG print_chr('r');print_chr('c');print_chr('v');print_chr(':');print_chr(' '); for(cnt=0; cnt<=(DATA_MAX/8); cnt++){ if( dat[cnt]&0x01 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x02 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x04 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x08 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x10 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x20 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x40 ){ print_chr('1'); } else { print_chr('0'); } if( dat[cnt]&0x80 ){ print_chr('1'); } else { print_chr('0'); } } print_chr(' '); print_chr(0xa); print_chr(0xd); #endif /* ********************************************************************* ** 受け取ったデータをデコードする ** ********************************************************************* */ j=0; decpos =0; decstate=STATE_REJECT_PULLUP; dechumidity=0; dectemperature=0; deccrc=0; for(cnt=0; cnt<=(DATA_MAX/8); cnt++){ for(i=0; i<=7; i++){ decdat = dat[cnt]&0x1< j ){ decstate=STATE_ERROR; break; } decstate=STATE_WAIT_READER_H; j=0; } case STATE_WAIT_READER_H: /* watching sensor reader pulse H */ if(decdat){ j++; break; }else{ if( COUNT_READER_MIN > j ){ decstate=STATE_ERROR; break; } decstate=STATE_GET_DATA_L; j=0; } case STATE_GET_DATA_L: /* watching sensor data pulse L */ if(!decdat){ j++; break; }else{ decstate=STATE_GET_DATA_H; j=0; } case STATE_GET_DATA_H: /* watching sensor data pulse H */ if(decdat){ j++; break; }else{ if( j < COUNT_DATA1_MIN ){ if (decpos <= 15){dechumidity =dechumidity<<1 ;} else if(decpos <= 31){dectemperature=dectemperature<<1;} else {deccrc =deccrc<<1 ;} #ifdef DEBUG print_chr('[');print_chr('0');print_chr(']'); #endif } else{ if (decpos <= 15){dechumidity =dechumidity<<1 ;dechumidity++; } else if(decpos <= 31){dectemperature=dectemperature<<1;dectemperature++;} else {deccrc =deccrc<<1 ;deccrc++; } #ifdef DEBUG print_chr('[');print_chr('1');print_chr(']'); #endif } decpos++; if(decpos < 40){ decstate=STATE_GET_DATA_L; j=0; break; }else{ decstate=STATE_FIN; j=0; } } case STATE_FIN: /* check CRC error detection */ if( ( (dechumidity>>8 &0xff) + (dechumidity &0xff) + (dectemperature>>8&0xff) + (dectemperature&0xff) ) == deccrc){ decstate=STATE_FIN; break; }else{ decstate=STATE_ERROR; } default: decstate=STATE_ERROR; break; }/* end switch*/ if(decstate==STATE_FIN || decstate==STATE_ERROR) break; /* break for */ } if(decstate==STATE_FIN || decstate==STATE_ERROR) break; /* break for */ WDT_Restart(); } #ifdef DEBUG print_chr(0xa);print_chr(0xd); print_chr('h');print_chr('u');print_chr('m');print_chr(':'); for(dbg_i=0;dbg_i<=15;dbg_i++){ if(dechumidity&(0x8000>>dbg_i)){print_chr('1');} else {print_chr('0');} } print_chr(',');print_chr('t');print_chr('e');print_chr('m');print_chr(':'); for(dbg_i=0;dbg_i<=15;dbg_i++){ if(dectemperature&(0x8000>>dbg_i)){print_chr('1');} else {print_chr('0');} } print_chr(',');print_chr('C');print_chr('R');print_chr('C');print_chr(':'); for(dbg_i=0;dbg_i<=7;dbg_i++){ if(deccrc&(0x80>>dbg_i)){print_chr('1');} else {print_chr('0');} } print_chr(0xa);print_chr(0xd); #endif /* ************ デコード処理ここまで ********************************************* */ #ifdef AD_ENABLE /* ************ GET A/D CONVERT RESULT ****************************************** */ get_addata( &addat0 , &addat1 , &addat2 , &addat3 ); #endif /* ************************************************************ ** print out hum,temp,ad data ** ************************************************************ */ if(decstate==STATE_FIN){ print_chr('d');print_chr('e');print_chr('c');print_chr('o');print_chr('d');print_chr('e');print_chr(':');print_chr(' '); print_num((dechumidity/1000)%10); print_num((dechumidity/100) %10); print_num((dechumidity/10) %10); print_chr('.'); print_num((dechumidity%10)); print_chr('%');print_chr('R');print_chr('H');print_chr(' ');print_chr(',');print_chr(' '); if(dectemperature&0x8000){ print_chr('-'); } dectemperature=dectemperature&0x7fff; print_num((dectemperature/1000)%10); print_num((dectemperature/100) %10); print_num((dectemperature/10) %10); print_chr('.'); print_num((dectemperature%10)); print_chr('d');print_chr('e');print_chr('g');print_chr('C'); print_chr(0xa); print_chr(0xd); }else{ print_chr('C');print_chr('R');print_chr('C');print_chr('e');print_chr('r');print_chr('r'); print_chr(0xa); print_chr(0xd); } #ifdef AD_ENABLE print_chr('A');print_chr('D');print_chr('C');print_chr('0');print_chr(':'); print_addat(addat0); print_chr(0xa); print_chr(0xd); print_chr('A');print_chr('D');print_chr('C');print_chr('1');print_chr(':'); print_addat(addat1); print_chr(0xa); print_chr(0xd); print_chr('A');print_chr('D');print_chr('C');print_chr('2');print_chr(':'); print_addat(addat2); print_chr(0xa); print_chr(0xd); print_chr('A');print_chr('D');print_chr('C');print_chr('3');print_chr(':'); print_addat(addat3); print_chr(0xa); print_chr(0xd); /* LM61CIZ custom! */ print_LM61CIZ(addat0,addat2); /* end LM61CIZ */ #endif /* ************ print out end ********************************* */ WDT_Restart(); for(cnt=0;cnt<=4;cnt++){ /* wait over 2sec */ wait_ms(500); WDT_Restart(); } } /* End user code. Do not edit comment generated here */ } /* Start adding user code. Do not edit comment generated here */ void print_LM61CIZ(unsigned short addat0, unsigned short addat1){ USHORT volatile temp1 = AD2TEMP[addat0-123]; USHORT volatile temp2 = AD2TEMP[addat1-123]; print_chr('t');print_chr('e');print_chr('m');print_chr('p');print_chr('1');print_chr('('); print_num((char)( temp1/100%10)); print_num((char)((temp1/10)%10)); print_chr('.'); print_num((char)( temp1 %10)); print_chr(')');print_chr(','); print_chr('t');print_chr('e');print_chr('m');print_chr('p');print_chr('2');print_chr('('); print_num((char)( temp2/100%10)); print_num((char)((temp2/10)%10)); print_chr('.'); print_num((char)( temp2 %10)); print_chr(')');print_chr(','); print_chr(0xa);print_chr(0xd); } #ifdef AD_ENABLE void get_addata(unsigned short *result0, unsigned short *result1, unsigned short *result2, unsigned short *result3 ){ AD_SelectADChannel(ADCHANNEL0); ad_finish=0; AD_Start(); while(ad_finish == 0); AD_Read(result0); AD_Stop; AD_SelectADChannel(ADCHANNEL1); ad_finish=0; AD_Start(); while(ad_finish == 0); AD_Read(result1); AD_Stop; AD_SelectADChannel(ADCHANNEL2); ad_finish=0; AD_Start(); while(ad_finish == 0); AD_Read(result2); AD_Stop; AD_SelectADChannel(ADCHANNEL3); ad_finish=0; AD_Start(); while(ad_finish == 0); AD_Read(result3); AD_Stop; } #endif void wait_ms(unsigned int wait){ unsigned char count; for(;wait>0;wait--){ for(count=WAIT_MS;count>0;count--){ ; } } return; } void print_chr(unsigned char c){ uart_tx_finish=0; UART6_SendData(&c,1); while( uart_tx_finish==0 ){ ; } } void print_num(unsigned char c){ if(0<=c && c<=9){ print_chr(c+0x30); }else{ print_chr('_'); } } void print_addat(unsigned short c){ print_num((c/1000)%10); print_num((c/100 )%10); print_num((c/10 )%10); print_num((c )%10); } /* End user code adding. Do not edit comment generated here */