//***************************************************************************** //** Sjekk alarmer fra radio //** Sendes bare hvis aktiv, og skiller mellom innbrudd og andre alarmer. //***************************************************************************** void alarm_Radio(RADIO_ALARM_MSG *myalarm, char nodeadr) { WDR(); if (CheckFlag(FEdisall)) //All messages disabled? return; if (CheckFlag(FEAlarm) == 0) //Innbrudd deaktivert? { //Send alle andre alarmer. if ((myalarm->type == RALARMTYPE_PIR) || (myalarm->type == RALARMTYPE_MAGNET) || (myalarm->type == RALARMTYPE_GENPIR)) return; else if (myalarm->type == RALARMTYPE_FIRE) Siren(1); AlarmRadioSMS((RADIO_ALARM_MSG *) myalarm, nodeadr); //Enter alarmmsg into queue } else { //Send alle alarmer. if ((myalarm->type == RALARMTYPE_PIR) || (myalarm->type == RALARMTYPE_MAGNET) || (myalarm->type == RALARMTYPE_GENPIR)) { Siren(1); AlarmRadioSMS((RADIO_ALARM_MSG *) myalarm, nodeadr); //Enter alarmmsg into queue } else if (myalarm->type == RALARMTYPE_FIRE) { Siren(1); AlarmRadioSMS((RADIO_ALARM_MSG *) myalarm, nodeadr); //Enter alarmmsg into queue } else AlarmRadioSMS((RADIO_ALARM_MSG *) myalarm, nodeadr); //Enter alarmmsg into queue } }
/*! \brief Restarts the watchdog and checks for data on UART #2 * * */ void readEM250(void){ WDR(); if (SCI2S1_RDRF){ ucSci2RcvBuf[head] = SCI2D; head++; } }
//***************************************************************************** // DeAktiver innbrudd Radio //***************************************************************************** void alarm_DeAktAlarm(void) { WDR(); ClearFlag(FEAlarm); //Turn off main alarm ClearFlag(FEdisall); //Alarmer aktive igjen Siren(0); }
//***************************************************************************** // Aktiver innbrudd Radio //***************************************************************************** void alarm_AktAlarm(void) { WDR(); SetFlag(FEAlarm); //Activate main alarm ClearFlag(FEdisall); //Alarmer aktive igjen Siren(0); }
/************************************************************************************************** ** Wait for start of second */ void RTC_wait_for_second_tick(void) { while (RTC_second_tick_SIG == 0) { sleep_cpu(); WDR(); GPS_task(); } }
void wait_avr(unsigned short msec) { TCCR0 = 3; while( msec-- ) { TCNT0 = (unsigned char)(256 - (XTAL_FRQ / 64) * 0.001); SET_BIT(TIFR, TOV0); WDR(); while( !GET_BIT(TIFR, TOV0) ); } TCCR0 = 0; }
/*! \brief Calculate checksum for a string of bytes (API packet uses this) * 255 bytes max. */ unsigned char ucCalcChecksum(unsigned char *bytes,unsigned char length){ unsigned char checksum; checksum=0; do{// for(;length;length--){ checksum+=bytes[0]; bytes++; WDR(); }while(--length); return (~checksum);//same as 0xFF - checksum }
/* ** This function tells if a touch is detected. */ bool TouchIdle(LcdTouch_t* structure) { #ifdef UseWatchdog WDR(); #endif if(!structure) return false; //TouchScreen_Data_t* TouchReport = &structure->TouchResponse; unsigned char State1 = Cursor_NoAction; bool Return = false; signed int TouchX = -1, TouchY = -1; if(IsTSPress) { if(ignore_cnt > 1) { IsTSPress = 0; State1 = Cursor_Move; TouchCoOrdGet(structure, &TouchX, &TouchY); structure->TouchResponse.x1 = TouchX; structure->TouchResponse.y1 = TouchY; Return = true; }else { ignore_cnt++; IsTSPress = 0; } }else ignore_cnt = 0; if(structure->LastState1 == Cursor_NoAction && State1 == Cursor_Move) { structure->LastState1 = Cursor_Down; structure->TouchResponse.touch_event1 = Cursor_Down; } else if((structure->LastState1 == Cursor_Down || structure->LastState1 == Cursor_Move) && State1 == Cursor_Move) { structure->LastState1 = Cursor_Move; structure->TouchResponse.touch_event1 = Cursor_Move; } else if((structure->LastState1 == Cursor_Down || structure->LastState1 == Cursor_Move) && State1 == Cursor_NoAction) { structure->LastState1 = Cursor_Up; structure->TouchResponse.touch_event1 = Cursor_Up; ignored_touch = analog_touch_filter_level; dbidx = 0; } else if(structure->LastState1 == Cursor_Up && State1 == Cursor_NoAction) { structure->LastState1 = Cursor_NoAction; structure->TouchResponse.touch_event1 = Cursor_NoAction; ignored_touch = analog_touch_filter_level; dbidx = 0; structure->TouchResponse.x1 = -1; structure->TouchResponse.y1 = -1; } return Return; }
void UART_getstr (char *s) { uint8_t i=0; uint8_t ch=0; do { WDR(); // restart licznika Watchdoga ch=UART_getchar(); // pobieraj znak s[i]=ch; // wstaw go do ³añcucha i inkrementuj i++; // inkrementuj iloœæ znaków odebranych } while ((ch != 10) && (ch != 13) && (i <= UART_MAX_GETSTR)); // dopóki nie napotka znaków koñca linii lub nie przekroczy d³ugoœci MAX_GETSTR s[i-1]=0; // wstaw 0 na koniec ³añcucha }
/*! \brief Send data stored in the TX_DATA_PTR Buffer * */ void vWriteTxDataSci2Len(unsigned char length) { unsigned char i; length = length + TX_DATA_OFFSET - 3; //Data+ Frame apibuf[LENGTH_OFFSET] = length; apibuf[length+3] = ucCalcChecksum(apibuf+3,length); length +=3+1;//add in Delim, length[2], checksum WDR(); for (i=0;i<= length;i++) { SEND_CHARACTER_TO_EM250(apibuf[i]); readEM250();//make sure we don't miss a read } }
//***************************************************************************** //** Sample aktuell inngang 50 ganger og returner gjennomsnitt. //***************************************************************************** char Sample(char input) { char a=0, result; int res=0; while (a < 10) { WDR(); ADMUX = 0x60; ADMUX |= input; //Sett innport (Andre bit uforandret) ADCSRA |= BIT(char,6); //ADSC = bit 6 while (ADCSRA & BIT(char,6)); //Vent til ferdig konvertert result = ADCH; res = res + result; a++; } result = res / 10; return (result); }
//***************************************************************************** //** Sample aktuell inngang 50 ganger og returner gjennomsnitt. //***************************************************************************** int SampleRandom(void) { char a=0, result; int res=0; while (a < 15) { WDR(); ADMUX = 0x60; ADMUX |= 0; //Sett innport (Andre bit uforandret) NTC ADCSRA |= BIT(char,6); //ADSC = bit 6 while (ADCSRA & BIT(char,6)); //Vent til ferdig konvertert result = ADCL; res = res << 1; if (result & 0b01000000) res = res + 1; a++; } return (res); }
/*! \brief Checks RX buffer for an API packet. returns number of characters * * Requirements * Must not be a broadcast * Must be from a specific address OR contain only 1 char of data * This is determined by whether or not the RX address is fixed. * The transmission source is copied to source64 pointer supplied unless it is null. * */ unsigned char ucReadApiSci2(unsigned char *dataDest, unsigned char *source64) /* Returns number of characters read. Copies the source 64 bit address from the API data to source64 variable provided Copies the API Data to the dataDest variable provided. */ { unsigned char len,i,j,stop,sum; // when using a char, the compiler upconverts to a short for comparisons and pointers unless forced.(unsigned char)( Equation) TRY_AGAIN: readEM250(); // switch (state){ // case STATE_DELIM: if (state == STATE_DELIM){ while(API_DELIMITER != ucSci2RcvBuf[tail] && tail != head){//find API delimiter tail++; //discard character WDR(); } if (tail != head) { state = STATE_LENGTH; goto TRY_AGAIN; } return 0; // case STATE_LENGTH: }else if (state == STATE_LENGTH){ if ((unsigned char)(head - tail) < 3) { //do we have the length? return 0; //no abort } lengthApi = ucSci2RcvBuf[(unsigned char)(tail+LENGTH_OFFSET)]; i = tail + 1; if (0 != ucSci2RcvBuf[i] || lengthApi > MAX_RX_PACKET_LENGTH) { //do we have an invalid length > ?? characters? tail++; //invalid, Try Again; state = STATE_DELIM; goto TRY_AGAIN; } state = STATE_PACKET; return 0; // case STATE_PACKET: }else if (state == STATE_PACKET){ if ((unsigned char)(head - tail) <= lengthApi + LENGTH_OFFSET +1) { //has the whole packet arrived return 0; // nope } state = STATE_VALID; goto TRY_AGAIN;//read serial port // case STATE_VALID: }else if (state == STATE_VALID){ i = tail+RX_OPTIONS_OFFSET; j = tail+RX_FRAME_OFFSET; if (RX_BROADCAST == ucSci2RcvBuf[i] || RX_FRAME_TYPE!=ucSci2RcvBuf[j]) {//if it is a broadcast or wrong frame type tail+=lengthApi+4;//delete unwanted packet state = STATE_DELIM;//reset state machine goto TRY_AGAIN; } if (isFixed){ for(i=0; i < 8 ; i++){ if (fixedSourceAddress[i] != ucSci2RcvBuf[(tail+RX_SOURCE64_OFFSET+i)&0xFF]){//Is the address correct tail+=lengthApi+4;//delete unwanted packet state = STATE_DELIM;//reset state machine goto TRY_AGAIN; } } }else if (lengthApi != RX_ONE_CHAR_LENGTH){//must be 1 char long. tail+=lengthApi+4;//delete unwanted packet state = STATE_DELIM;//reset state machine goto TRY_AGAIN; } state = STATE_CHECKSUM; goto TRY_AGAIN; // case STATE_CHECKSUM: }else if (state == STATE_CHECKSUM){ stop = tail + lengthApi + 3;//this may wrap so for loop must use != stop sum = 0; for (i=tail + LENGTH_OFFSET+1;i != stop; i++){ sum += ucSci2RcvBuf[i]; readEM250(); } if (ucSci2RcvBuf[stop] != (0xFF-sum)){ state = STATE_DELIM;//invalid checksum goto TRY_AGAIN; } state = STATE_DATA; goto TRY_AGAIN; // case STATE_DATA: }else if (state == STATE_DATA){ stop = tail + lengthApi + 3;//this may wrap len = 0; for(i = tail+ RX_DATA_OFFSET; i != stop; i++){//copy data to buffer dataDest[len] = ucSci2RcvBuf[i]; len++; readEM250(); }//for if (source64) { //if pointer isn't null for(i=0; i <8; i++){//copy source address to buffer source64[i] = ucSci2RcvBuf[(unsigned char)(tail+RX_SOURCE64_OFFSET+i)]; } } tail += ucSci2RcvBuf[(unsigned char)(tail+LENGTH_OFFSET)] +4; state = STATE_DELIM; return len; }//switch or if (state) return 0; }
VOID SetWdtInterval(CWDTINTERVAL wdtInterval) { wdt_enable(wdtInterval); WDR(); }
void main(void) { //u16 ad_val; //u8 ad_cnt = 0, cnt = 0; //u32 ad_sum = 0; //u8 second = 0; peripheral_init(); //³õʼ»¯ variables_init(); while(1) { if(timer_100ms_flag) { Button_Scan(); timer_100ms_flag = FALSE; WDR(); //kick watchdog } Con_Monitor(); switch(module_state) { case MODULE_STATE_INIT: Con_Module_Init_Proc(); break; case MODULE_STATE_WORK_NORM: Con_Module_Work_Norm_Proc(); break; case MODULE_STATE_WORK_WARN: Con_Module_Work_Warn_Proc(); break; case MODULE_STATE_COMMUNICATE: Con_Module_Communicate_Proc(); break; case MODULE_STATE_SERVICE: Con_Module_Service_Proc(); break; case MODULE_STATE_ERROR: Con_Module_Error_Proc(); break; case MODULE_STATE_QUIT: Con_Module_Quit_Proc(); break; default: break; } //Button_Scan(); //if(timer_100ms_flag) //{ //cnt++; //} //ad_val = EEPROM_Read(0x10); //Seg_Number_Show(ad_val, 100); //LedBlink(LED_D1, BLINK_1HZ); //LedBlink(LED_D2, BLINK_2HZ); //Seg_Number_Show(969, 100); //Seg_Alpha_Show(g, o, d, 100); //Delay(100); //ÑÓʱ } }
//******************************************************************* //Watchdog initialize // prescale: 16K void watchdog_init(void) { WDR(); //this prevents a timout on enabling WDTCR= 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs }
void delayus(u16 us) { _delay_us(us); WDR(); }
void DoPassThroughMode(void) { ICSSC_DRST_DRS0 = 0x1; //Set for Midrange value ICSSC_DRST_DRS1 = 0x0; //Set for Midrange value ICSSC_DMX32 = 1;//fine tuned for max freq w/ 32.768 input freq DisableInterrupts; // Disable UARTS - we're going to manually control the lines from now on. _SCI1C2.Byte = 0; _SCI2C2.Byte = 0; // Freescale // --------- // Tx_U1 - PTB-D1 - Output // Rx_U1 - PTB-D0 - Input // Tx_U2 - PTC-D7 - Output = DIN_XBEE // Rx_U2 - PTC-D6 - Input = DOUT_XBEE // // CTS_U1 - PTD6 - Input // RTS_U1 - PTC1 - Output // CTS_U2 - PTD7 - Input // RTS_U2 - PTC0 - Output // Configure port pins for output mode PTDDD =0; // set all Port D to an input FSCALE_CFG_HOST_TX = 1; PTCDD =(1<<7) | 3; // bit 7, 1, 0 are outputs, others are treated as inputs.(Reset is treated as an input when not in reset) FSCALE_HOST_TX = 1;//initialize state to 11 FSCALE_EM250_TX = 1;// //================================================== // First Set //================================================== // Goal: Sample Tx and Rx more frequently than RTS and CTS since the // flow control signals change less frequently than the data pins. // Read data from Host and write it to XBee (U1:Rx -> U2:Tx) // Note: Do not rewrite - this sequence produces the fastest code (so far)! //#define STATE_ON //#define FORCE_STATE #ifdef STATE_ON PTEDD = 3; #endif for(;;){ #ifdef FORCE_STATE FSCALE_EM250_TX = 1; FSCALE_HOST_TX = 1; #endif #ifdef STATE_ON PTED = 3; #endif if (RESET){ RESET_DIR_EM250 = 0; } L11: if (!FSCALE_HOST_RX) { //(5 clocks) //1 instruction (3 instructions during transition) FSCALE_EM250_TX = 0; goto L01E;//change state to 01;sample EM250 next } L11E: if (!FSCALE_EM250_RX) { //1 instruction FSCALE_HOST_TX = 0; goto L10;//change state to 10 } if (!RESET){ RESET_DIR_EM250 = 1;//reset EM250 } //PTCD_PTCD1= PTDD_PTDD7 ; //3 instructions asm { //equal to 1.5 instructions LDA PTDD; // get RTS/CTS PTDD_PTDD6/7 (4 clocks) ROLA; //bit shift rotate through carry ROLA; //(1 clock * 3) ROLA; // } //note that if HOST_RX or EM250_RX changes,in the next 2 instructions the RTS/CTS sample will be dropped. sampling will be done next loop (saves code space) if (!FSCALE_HOST_RX) { //1 instruction FSCALE_EM250_TX = 0; goto L01E; } if (!FSCALE_EM250_RX) { //1 instruction FSCALE_HOST_TX = 0; goto L10; } //PTCD_PTCD0 = PTDD_PTDD6; //3 instructions asm { AND #0x3; // clear; allows reset to pass through dependant on direction ORA #0x80; // set EM250_TX (2 clocks) STA PTCD;//write RTS/CTS (4 clocks) //1 instruction } if (!FSCALE_HOST_RX) { //1 instruction FSCALE_EM250_TX = 0; goto L01E; } if (!FSCALE_EM250_RX) { //1 instruction FSCALE_HOST_TX = 0; goto L10; } WDR(); // 2 instructions overhead }//for for(;;){ #ifdef FORCE_STATE FSCALE_EM250_TX = 0; FSCALE_HOST_TX = 1; #endif #ifdef STATE_ON PTED = 1; #endif if (RESET){ RESET_DIR_EM250 = 0; } L01: if (FSCALE_HOST_RX) { FSCALE_EM250_TX = 1; goto L11E; } L01E: if (!FSCALE_EM250_RX) { FSCALE_HOST_TX = 0; goto L00; } if (!RESET){ RESET_DIR_EM250 = 1;//reset EM250 } // PTCD_PTCD1= PTDD_PTDD7 ; asm { LDA PTDD; ROLA; ROLA; ROLA; } if (FSCALE_HOST_RX) { FSCALE_EM250_TX = 1; goto L11E; } if (!FSCALE_EM250_RX) { FSCALE_HOST_TX = 0; goto L00; } //PTCD_PTCD0 = PTDD_PTDD6; asm { AND #0x3; // clear EM250_TX (Based on state of Host_RX) STA PTCD; } WDR(); }//for for(;;){ #ifdef FORCE_STATE FSCALE_EM250_TX = 1; FSCALE_HOST_TX = 0; #endif #ifdef STATE_ON PTED = 2; #endif if (RESET){ RESET_DIR_EM250 = 0; } L10: if (!FSCALE_HOST_RX) { FSCALE_EM250_TX = 0; goto L00E; } L10E: if (FSCALE_EM250_RX) { FSCALE_HOST_TX = 1; goto L11; } if (!RESET){ RESET_DIR_EM250 = 1;//reset EM250 } // PTCD_PTCD1= PTDD_PTDD7 ; asm { LDA PTDD; ROLA; ROLA; ROLA; } if (!FSCALE_HOST_RX) { FSCALE_EM250_TX = 0; goto L00E; } if (FSCALE_EM250_RX) { FSCALE_HOST_TX = 1; goto L11; } //PTCD_PTCD0 = PTDD_PTDD6; asm { AND #0x3; // clear; allows reset to pass through dependant on direction ORA #0x80; // set EM250_TX (Based on state of Host_RX) STA PTCD; } WDR(); }//for for(;;){ #ifdef FORCE_STATE FSCALE_EM250_TX = 0; FSCALE_HOST_TX = 0; #endif #ifdef STATE_ON PTED = 0; #endif if (RESET){ RESET_DIR_EM250 = 0; } L00: if (FSCALE_HOST_RX) { FSCALE_EM250_TX = 1; goto L10E; } L00E: if (FSCALE_EM250_RX) { FSCALE_HOST_TX = 1; goto L01; } if (!RESET){ RESET_DIR_EM250 = 1;//reset EM250 } // PTCD_PTCD1= PTDD_PTDD7 ; asm { LDA PTDD; ROLA; ROLA; ROLA; } if (FSCALE_HOST_RX) { FSCALE_EM250_TX = 1; goto L10E; } if (FSCALE_EM250_RX) { FSCALE_HOST_TX = 1; goto L01; } //PTCD_PTCD0 = PTDD_PTDD6; asm { AND #0x3; // clear EM250_TX (Based on state of Host_RX) STA PTCD; } WDR(); }//for }
/* ** This function tells if a touch is detected. */ bool microe_touch_idle(LcdTouch_t* Touch) { if(!Touch) return false; MicroeTouch_t *MikroeTouch = Touch->UsrData; #ifdef UseWatchdog WDR(); #endif bool Return = false; if(MikroeTouch->touch_conv_finish) { MikroeTouch->touch_conv_finish = false; unsigned char State1 = Cursor_NoAction; signed int TouchX = -1, TouchY = -1; if(MikroeTouch->IsTSPress) { if(MikroeTouch->ignore_cnt > 1) { MikroeTouch->IsTSPress = 0; State1 = Cursor_Move; TouchCoOrdGet(Touch, &TouchX, &TouchY); Touch->TouchResponse.x1 = TouchX; Touch->TouchResponse.y1 = TouchY; if(Touch->pDisplay->LcdTimings->orientation == LCD_ORIENTATION_LANDSCAPE || Touch->pDisplay->LcdTimings->orientation == LCD_ORIENTATION_LANDSCAPE_FLIP) { Touch->TouchResponse.y1 = Touch->pDisplay->LcdTimings->X - Touch->TouchResponse.y1; signed int x1_Back = Touch->TouchResponse.x1; Touch->TouchResponse.x1 = Touch->TouchResponse.y1; Touch->TouchResponse.y1 = x1_Back; Touch->TouchResponse.x1 = Touch->pDisplay->LcdTimings->X - Touch->TouchResponse.x1; Touch->TouchResponse.y1 = Touch->pDisplay->LcdTimings->Y - Touch->TouchResponse.y1; } if(Touch->pDisplay->LcdTimings->orientation == LCD_ORIENTATION_PORTRAIT_FLIP || Touch->pDisplay->LcdTimings->orientation == LCD_ORIENTATION_LANDSCAPE_FLIP) { Touch->TouchResponse.x1 = Touch->pDisplay->LcdTimings->X - Touch->TouchResponse.x1; Touch->TouchResponse.y1 = Touch->pDisplay->LcdTimings->Y - Touch->TouchResponse.y1; } if(Touch->pDisplay->LcdTimings->orientation == LCD_ORIENTATION_LANDSCAPE || Touch->pDisplay->LcdTimings->orientation == LCD_ORIENTATION_LANDSCAPE_FLIP) { Touch->TouchResponse.x1 = Touch->pDisplay->LcdTimings->X - Touch->TouchResponse.x1; Touch->TouchResponse.y1 = Touch->pDisplay->LcdTimings->Y - Touch->TouchResponse.y1; } if(Touch->flip_x) Touch->TouchResponse.x1 = Touch->pDisplay->LcdTimings->X - Touch->TouchResponse.x1; if(Touch->flip_y) Touch->TouchResponse.y1 = Touch->pDisplay->LcdTimings->Y - Touch->TouchResponse.y1; Return = true; }else { MikroeTouch->ignore_cnt++; MikroeTouch->IsTSPress = 0; } }else MikroeTouch->ignore_cnt = 0; if(Touch->LastState1 == Cursor_NoAction && State1 == Cursor_Move) { Touch->LastState1 = Cursor_Down; Touch->TouchResponse.touch_event1 = Cursor_Down; } else if((Touch->LastState1 == Cursor_Down || Touch->LastState1 == Cursor_Move) && State1 == Cursor_Move) { Touch->LastState1 = Cursor_Move; Touch->TouchResponse.touch_event1 = Cursor_Move; } else if((Touch->LastState1 == Cursor_Down || Touch->LastState1 == Cursor_Move) && State1 == Cursor_NoAction) { Touch->LastState1 = Cursor_Up; Touch->TouchResponse.touch_event1 = Cursor_Up; MikroeTouch->ignored_touch = ANALOG_TOUCH_FILTER_LEVEL; MikroeTouch->dbidx = 0; } else if(Touch->LastState1 == Cursor_Up && State1 == Cursor_NoAction) { Touch->LastState1 = Cursor_NoAction; Touch->TouchResponse.touch_event1 = Cursor_NoAction; MikroeTouch->ignored_touch = ANALOG_TOUCH_FILTER_LEVEL; MikroeTouch->dbidx = 0; Touch->TouchResponse.x1 = -1; Touch->TouchResponse.y1 = -1; } } return Return; }