void BoardInit( void ) { uint8_t i; /* Setup SysTick Timer for 1 us interrupts ( not too often to save power ) */ if( SysTick_Config( SystemCoreClock / 1000 ) ) { /* Capture error */ while (1); } // Initialize unused GPIO to optimize power consumption InitUnusedGPIO( ); // Initialize Selector SelectorInit( ); // Initialize SPI SpiInit( ); // Initialize LED for( i = 0; i < LED_NB; i++ ) { LedInit( ( tLed )i ); } LedOn( LED1 ); LedOn( LED2 ); LedOn( LED3 ); LongDelay( 1 ); LedOff( LED1 ); LedOff( LED2 ); LedOff( LED3 ); }
/* Wait for channel to close */ static void UserAppSM_WaitChannelClose(void) { /* Monitor the channel status to check if channel is closed */ if(AntRadioStatus() == ANT_CLOSED) { #ifdef MPG1 LedOff(GREEN); LedOn(YELLOW); #endif /* MPG1 */ #ifdef MPG2 LedOn(GREEN0); LedOn(RED0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Idle; } /* Check for timeout */ if( IsTimeUp(&UserApp_u32Timeout, TIMEOUT_VALUE) ) { #ifdef MPG1 LedOff(GREEN); LedOff(YELLOW); LedBlink(RED, LED_4HZ); #endif /* MPG1 */ #ifdef MPG2 LedBlink(RED0, LED_4HZ); LedOff(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Error; } } /* end UserAppSM_WaitChannelClose() */
/* Wait for channel to open */ static void UserAppSM_WaitChannelOpen(void) { /* Monitor the channel status to check if channel is opened */ if(AntRadioStatus() == ANT_OPEN) { #ifdef MPG1 LedOn(GREEN); #endif /* MPG1 */ #ifdef MPG2 LedOn(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_ChannelOpen; } /* Check for timeout */ if( IsTimeUp(&UserApp_u32Timeout, TIMEOUT_VALUE) ) { AntCloseChannel(); #ifdef MPG1 LedOff(GREEN); LedOn(YELLOW); #endif /* MPG1 */ #ifdef MPG2 LedOn(RED0); LedOn(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Idle; } } /* end UserAppSM_WaitChannelOpen() */
/*---------------------------------------------------------------------------------------------------------------------- Function: LedUpdate Description: Update all LEDs for the current cycle. Requires: - G_u32SystemTime1ms is counting Promises: - All LEDs updated based on their counters */ void LedUpdate(void) { /* Loop through each LED */ for(u8 i = 0; i < TOTAL_LEDS; i++) { /* Check if LED is PWMing */ if(Leds_asLedArray[(LedNumberType)i].eMode == LED_PWM_MODE) { /* Handle special case of 0% duty cycle */ if( Leds_asLedArray[i].eRate == LED_PWM_0 ) { LedOff( (LedNumberType)i ); } /* Handle special case of 100% duty cycle */ else if( Leds_asLedArray[i].eRate == LED_PWM_100 ) { LedOn( (LedNumberType)i ); } /* Otherwise, regular PWM: decrement counter; toggle and reload if counter reaches 0 */ else { if(--Leds_asLedArray[(LedNumberType)i].u16Count == 0) { if(Leds_asLedArray[(LedNumberType)i].eCurrentDuty == LED_PWM_DUTY_HIGH) { /* Turn the LED off and update the counters for the next cycle */ LedOff( (LedNumberType)i ); Leds_asLedArray[(LedNumberType)i].u16Count = LED_PWM_PERIOD - Leds_asLedArray[(LedNumberType)i].eRate; Leds_asLedArray[(LedNumberType)i].eCurrentDuty = LED_PWM_DUTY_LOW; } else { /* Turn the LED on and update the counters for the next cycle */ LedOn( (LedNumberType)i ); Leds_asLedArray[i].u16Count = Leds_asLedArray[i].eRate; Leds_asLedArray[i].eCurrentDuty = LED_PWM_DUTY_HIGH; } } } /* Set the LED back to PWM mode since LedOff and LedOn set it to normal mode */ Leds_asLedArray[(LedNumberType)i].eMode = LED_PWM_MODE; } /* end if PWM mode */ /* LED is in LED_BLINK_MODE mode */ else if(Leds_asLedArray[(LedNumberType)i].eMode == LED_BLINK_MODE) { /* Decrement counter; toggle and reload if counter reaches 0 */ if( --Leds_asLedArray[(LedNumberType)i].u16Count == 0) { LedToggle( (LedNumberType)i ); Leds_asLedArray[(LedNumberType)i].u16Count = Leds_asLedArray[(LedNumberType)i].eRate; } } } /* end for */ } /* end LedUpdate() */
/* * Esta función se debe llamar a la máxima velocidad * para poder tener la base de tiempo */ void RgbLedUpdate(void){ // Supongo que paso una unidad de tiempo de la base de tiempo. ++timeBaseCounter; if( timeBaseCounter >= cyclesPerPeriod ){ // Prender todos los leds if( redIntensityCycles > 0 ){ LedOn(RGBLED_RED); } if( greenIntensityCycles > 0 ){ LedOn(RGBLED_GREEN); } if( blueIntensityCycles > 0 ){ LedOn(RGBLED_BLUE); } // Reiniciar el contador timeBaseCounter = 0; ellapsedRedCycles = 0; ellapsedGreenCycles = 0; ellapsedBlueCycles = 0; } else{ // Controlar el estado del led Verde if( ellapsedRedCycles < redIntensityCycles ){ ++ellapsedRedCycles; } else{ LedOff(RGBLED_RED); } // Controlar el estado del led verde if( ellapsedGreenCycles < greenIntensityCycles ){ ++ellapsedGreenCycles; } else{ LedOff(RGBLED_GREEN); } // Controlar el estado del led Azul if( ellapsedBlueCycles < blueIntensityCycles ){ ++ellapsedBlueCycles; } else{ LedOff(RGBLED_BLUE); } } }
/*----------------------------------------------------------------------------- * Led Statemachine */ void LedCheck(void) { uint16_t currTimeMs; uint8_t i; TState *pState; GET_TIME_MS16(currTimeMs); pState = &sLedState[0]; for (i = 0; i < NUM_LED; i++) { if ((pState->offTime != FOREVER) && (pState->onTime != FOREVER)) { if (pState->state == LED_ON) { if (((uint16_t)(currTimeMs - pState->switchTime)) >= pState->onTime) { pState->switchTime = currTimeMs; LedOff(i); if (pState->cyclic == false) { pState->offTime = FOREVER; } } } else { if (((uint16_t)(currTimeMs - pState->switchTime)) >= pState->offTime) { pState->switchTime = currTimeMs; LedOn(i); } } } pState++; } }
int main(void) { /* perform the needed initialization here */ InitLed(); currentLed = REDLED; LedOn(currentLed); for(;;){ // Esperar para cambiar el estado del led for( i = counter ; i > 0 ; --i ){ } ToggleLed(currentLed); switch(currentLed){ case REDLED: currentLed = YELOWLED; break; case YELOWLED: currentLed = GREENLED; break; case GREENLED: currentLed = RGBLED_RED; break; case RGBLED_RED: currentLed = RGBLED_BLUE; break; case RGBLED_BLUE: currentLed = RGBLED_GREEN; break; case RGBLED_GREEN: currentLed = RGBLED_VIOLT; break; case RGBLED_VIOLT: currentLed = REDLED; break; default: break; } ToggleLed(currentLed); if( elapsedCycles < TOGGLE_CYCLES ){ ++elapsedCycles; } else{ if( counter < accelCounter ){ counter = 10000000; } counter = counter - accelCounter; elapsedCycles = 0; } } return 0; }
void Led::CyclTask(void) { unsigned long time = millis(); _currentTime += (time - _prevTime); _prevTime = time; if(_currentTime > _wakeup) { _currentTime = 0; if(_led_is_on == true) { LedOff(); _wakeup = (_winkCurrent > 0) ? _delay_between_two_wink : _delay_between_two_cycle; } else { _wakeup = _delay_wink; if(_winkCurrent > 0) { LedOn(); _winkCurrent--; } else { _winkCurrent = _winkNumber; } } } }
void vibrate(int offMsec, int id) { if (!InitLed()) return; LedOn(id); Sleep(offMsec); LedOff(id); }
int main(void) { /* Watchdog timer disabled */ WDTCTL = WDTPW + WDTHOLD; BspInit(); DioInit(); DioSet(BUTTON0); UartInit(9600, UartCallback); LedInit(); ButtonInit(); ButtonSetup(0, Button0Cbf, 20); LedOn(0); SchedulerInit(100, 1, 2); // 1ms tick, 1 timed task, 2 loop task SetSchedulerTimedTask(LedRedFlash, 0, 500, 0); // schedule timed_task[0] as led_red_flash, set off every 500ms SetSchedulerLoopTask(ButtonLoop, 0); // schedule loop_task[0] as button_loop SetSchedulerLoopTask(UartManage, 1); // schedule loop_task[1] as uart_loop __eint(); SchedulerRun(); while(1) { } }
/*------------------------------------------------------------------------------ Function: LcdInitialize Description: Initializes the LCD task and manually sends a message to the LCD Requires: - Promises: - LCD task Setup and LCD functions can now be called */ void LcdInitialize(void) { u8 au8Commands[] = { LCD_FUNCTION_CMD, LCD_FUNCTION2_CMD, LCD_BIAS_CMD, LCD_CONTRAST_CMD, LCD_DISPLAY_SET_CMD, LCD_FOLLOWER_CMD }; /* "012345567890123456789" */ u8 au8Welcome[] = "RAZOR SAM3U2 ASCII "; /* State to Idle */ Lcd_StateMachine = LcdSM_Idle; /* Turn on LCD wait 40 ms for it to setup */ AT91C_BASE_PIOB->PIO_SODR = PB_09_LCD_RST; Lcd_u32Timer = G_u32SystemTime1ms; while( !IsTimeUp(&Lcd_u32Timer, LCD_STARTUP_DELAY) ); /* Send Control Command */ TWI0WriteByte(LCD_ADDRESS, LCD_CONTROL_COMMAND, NO_STOP); /* Send Control Commands */ TWI0WriteData(LCD_ADDRESS, NUM_CONTROL_CMD, &au8Commands[0], NO_STOP); /* Wait for 200 ms */ Lcd_u32Timer = G_u32SystemTime1ms; while( !IsTimeUp(&Lcd_u32Timer, LCD_CONTROL_COMMAND_DELAY) ); /* Send Final Command to turn it on */ TWI0WriteByte(LCD_ADDRESS, LCD_DISPLAY_CMD | LCD_DISPLAY_ON, STOP); /* Blacklight - White */ LedOn(LCD_RED); LedOn(LCD_GREEN); LedOn(LCD_BLUE); TWI0WriteByte(LCD_ADDRESS, LCD_CONTROL_DATA, NO_STOP); TWI0WriteData(LCD_ADDRESS, 20, &au8Welcome[0], STOP); Lcd_u32Timer = G_u32SystemTime1ms; G_u32ApplicationFlags |= _APPLICATION_FLAGS_LCD; } /* end LcdInitialize */
/* ** Action for menu GPIO icon click */ static void ActionMenuGPIO(void) { pmFlag = FALSE; sdCardAccessFlag = FALSE; Timer2Stop(); LedOn(); tmrClick = FALSE; rtcSetFlag = FALSE; pageIndex = MENU_IDX_LED; UpdateUartConsoleHelp(); }
/*---------------------------------------------------------------------------------------------------------------------- ISR: HardFault_Handler Description: A non-maskable (always available) core interrupt that occurs when something extraordinary event. In many cases, this is referencing an invalid address, but can be other events of various levels of mystery. Requires: - Promises: - Red LED is on, all others off - Code held */ void HardFault_Handler(void) { #ifdef MPGL1 LedOff(WHITE); LedOff(CYAN); LedOff(PURPLE); LedOff(ORANGE); LedOff(BLUE); LedOff(GREEN); LedOff(YELLOW); LedOn(RED); #endif /* MPGL1 */ #ifdef MPGL2 #ifdef MPGL2_R01 LedOff(BLUE); LedOff(GREEN); LedOff(YELLOW); LedOn(RED); #else LedOff(BLUE0); LedOff(BLUE1); LedOff(BLUE2); LedOff(BLUE3); LedOff(GREEN0); LedOff(GREEN1); LedOff(GREEN2); LedOff(GREEN3); LedOff(RED1); LedOff(RED2); LedOff(RED3); LedOn(RED0); #endif /* MPGL2_R01 */ #endif /* MPGL2 */ while(1); /* !!!!! update to log and/or report error and/or restart */ } /* end HardFault_Handler() */
void LedAllOn() { LedOn(LED_TRIPOD); LedOn(LED_RBOG); LedOn(LED_BBOG); LedOn(LED_LBOG); LedOn(LED_DNT); LedOn(LED_STATUS); }
/*int main(void)*/ void CG_ModeSwitch(void) { uint8_t TSW_info = 0U; /* Initialize system */ CG_SetSystem(); GPIO_SetINT(); LEDInit(); LedOn(LED0); #ifdef DEBUG printf("TMPM341 CG_ModeSwitch DEMO\r\n"); #endif while (1) { TSW_info = GPIO_ReadData(TSW_PORT) & TSWBITS; if (TSW_info == TSW1) { fSysSleep = SLEEP; LedOff(LED0); #ifdef DEBUG printf("Now, Going to Sleep\r\n"); #endif CG_NormalToStop1(); /* connect PG3(INT0) to VCC to recover from STOP mode */ LedOn(LED0); fSysSleep = WAKE; NVIC_DisableIRQ(INT0_IRQn); #ifdef DEBUG printf("Wakeup from Sleep\r\n"); #endif } else { /* Do nothing */ } } }
/** * @brief TMRB0 interrupt service routine (1ms) * @param None * @retval None */ void INTTB0_IRQHandler(void) { static uint16_t tbcount = 0U; static uint8_t ledon = 1U; tbcount++; if (tbcount >= 500U) { tbcount = 0U; /* reverse LED output */ ledon = (ledon == 0U) ? 1U : 0U; if (0U == ledon) { LedOff(LED1); } else { LedOn(LED1); } } else { /* do nothing */ } }
/** * @brief 开机振动声音灯 */ void StateLedBzVbrCheck(void) { static uint8_t sLedBzVbrCount = 0; if (sDevice.pTemp->Disp.Fresh != TURN_ON) { return; } if (sLedBzVbrCount < 2) { S_Led lLed; lLed.Src = LED_POWER_ON; lLed.Time = LED_POWER_ON_TIME; lLed.Pin = LED_PIN_1, LedOn(&lLed); S_Buzzer lBz; lBz.Src = BUZZER_POWER_ON; lBz.Time = BUZZER_POWER_ON_TIME; BuzzerOn(&lBz); S_Vibration lVbr; lVbr.Src = VIBRATION_POWER_ON; lVbr.Time = VIBRATION_POWER_ON_TIME; VibrationOn(&lVbr); } else { // 取固定的值,防止溢出 sLedBzVbrCount = 3; } sLedBzVbrCount++; }
int main (void) { WDTCTL = WDTPW + WDTHOLD; (void)TimerMasterInit(10); //tick every 0.1ms //void PwmCtor( /*@out@*/Pwm_t * pwm, DioPort_t my_port, DioPin_t my_pin); PwmCtor(&pwm_left_fwd, kPort4, kPin3); PwmCtor(&pwm_left_rev, kPort4, kPin4); PwmCtor(&pwm_right_fwd, kPort4, kPin5); PwmCtor(&pwm_right_rev, kPort4, kPin6); MotorSet( 50, 50); BSP_Init(); LedCtor(&led_red, kPort1, kPin0); //P1.0 (red) LedCtor(&led_green, kPort1, kPin1); //P1.1 (green) LedOn(&led_red); #ifdef I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE { addr_t lAddr; createRandomAddress(&lAddr); SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr); } #endif /* I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE */ SMPL_Init(sRxCallback); /* never coming back... */ Connect(); /* but in case we do... */ while (1); }
void RedLedBlink() { LedOn(LED_RED); Delay(300); LedOff(LED_RED); }
void GreenLedBlink() { LedOn(LED_GREEN); Delay(300); LedOff(LED_GREEN); }
/*---------------------------------------------------------------------------------------------------------------------- Function: DebugCommandLedTestToggle Description: Toggles the active state of the LED test which allows typed characters corresponding to LED colors to toggle those LEDs on or off. LEDs are started all ON. They are left in their current state when the function exits. */ static void DebugCommandLedTestToggle(void) { u8 au8LedTestMessage[] = "\n\rLed Test "; /* Print message and toggle the flag */ DebugPrintf(au8LedTestMessage); if(G_u32DebugFlags & _DEBUG_LED_TEST_ENABLE) { G_u32DebugFlags &= ~_DEBUG_LED_TEST_ENABLE; DebugPrintf(G_au8MessageOFF); } else { G_u32DebugFlags |= _DEBUG_LED_TEST_ENABLE; DebugPrintf(G_au8MessageON); #ifdef MPG1 LedOn(WHITE); LedOn(PURPLE); LedOn(BLUE); LedOn(CYAN); LedOn(GREEN); LedOn(YELLOW); LedOn(ORANGE); LedOn(RED); #endif /* MPG 1 */ #ifdef MPG1 LedOn(WHITE); LedOn(PURPLE); LedOn(BLUE); LedOn(CYAN); LedOn(GREEN); LedOn(YELLOW); LedOn(ORANGE); LedOn(RED); #endif /* MPG 1 */ #ifdef MPGL2 #ifdef MPGL2_R01 LedOn(BLUE); LedOn(GREEN); LedOn(YELLOW); LedOn(RED); #else LedOn(BLUE0); LedOn(BLUE1); LedOn(BLUE2); LedOn(BLUE3); LedOn(RED0); LedOn(RED1); LedOn(RED2); LedOn(RED3); LedOn(GREEN0); LedOn(GREEN1); LedOn(GREEN2); LedOn(GREEN3); #endif /* MPGL2_R01 */ #endif /* MPGL2 */ } } /* end DebugCommandLedTestToggle() */
//----------------------------------------------------------------------------- // Main Routine //----------------------------------------------------------------------------- void main(void) { char cmd; PCA0MD &= ~0x40; // Disable Watchdog timer Sysclk_Init(); // Initialize oscillator Port_Init(); // Initialize crossbar and GPIO P2=0xFF; // tie high to turn off pulldowns for open drain devices connected Usb0_Init(); // Initialize USB0 Timer_Init(); // Initialize timer2 LedOn(); //RSTSRC =0x80; ; // enable USB and // missing clock detector reset sources // if we enabled missing clock detector then the device doesn't work, don't know why // watchdog // load watchdog offset PCA0CPL4=0xff; // maximum offset so that watchdog takes a good long time to time out // enable watchdog PCA0MD |= 0x44; // WDT enabled, PCA clock source is timer0 overflow PCA0CPH4 = 0x00; // write value to WDT PCA to start watchdog /* Servo0=0; Servo1=0; Servo2=0; Servo3=0; */ while (1) { // It is possible that the contents of the following packets can change // while being updated. This doesn't cause a problem in the sample // application because the bytes are all independent. If data is NOT // independent, packet update routines should be moved to an interrupt // service routine, or interrupts should be disabled during data updates. PCA0CPH4 = 355; // write value to WDT PCA to reset watchdog EA=0; // disable ints // LedToggle(); cmd=Out_Packet[0]; switch(cmd){ case CMD_SET_SERVO: Out_Packet[0]=0; // command is processed LedToggle(); pwmNumber=Out_Packet[1]; switch(pwmNumber) { // big endian 16 bit value to load into PWM controller case 0: { // servo0 pwmh1=Out_Packet[2]; // store the PCA compare value for later interrupt to load pwml1=Out_Packet[3]; PCA0CPM0 |= 0x49; // enable compare function and match and interrupt for match for pca } break; case 1: { // servo1 pwmh2=Out_Packet[2]; pwml2=Out_Packet[3]; PCA0CPM1 |= 0x49; // enable compare function and enable match and interrupt for match for pca } break; case 2: { // servo1 pwmh3=Out_Packet[2]; pwml3=Out_Packet[3]; PCA0CPM2 |= 0x49; // enable compare function and enable match and interrupt for match for pca } break; case 3: { // servo1 pwmh4=Out_Packet[2]; pwml4=Out_Packet[3]; PCA0CPM3 |= 0x49; // enable compare function and enable match and interrupt for match for pca } } EIE1 |= 0x10; // enable PCA interrupt break; case CMD_DISABLE_SERVO: //cmd: CMD, SERVO { Out_Packet[0]=0; // command is processed LedToggle(); pwmNumber=Out_Packet[1]; switch(pwmNumber) { // big endian 16 bit value to load into PWM controller case 0: { // servo0 PCA0CPM0 &= ~0x40; // disable compare function } break; case 1: { // servo1 PCA0CPM1 &= ~0x40; // disable compare function } break; case 2: { // servo2 PCA0CPM2 &= ~0x40; // disable compare function } break; case 3: { // servo3 PCA0CPM3 &= ~0x40; // disable compare function } } } break; case CMD_SET_ALL_SERVOS: // cmd: CMD, PWMValue0 (2 bytes bigendian), PWMValue1 (2 bytes bigendian) { Out_Packet[0]=0; // command is processed LedToggle(); // TODO the set all servos probably doesn't work because it doesn't save the values for the ISR to later load PCA0CPL0=Out_Packet[2]; PCA0CPH0=Out_Packet[1]; // store the PCA compare value for later interrupt to load, write low value FIRST PCA0CPM0 |= 0x49; // enable compare function and match and interrupt for match for pca PCA0CPL1=Out_Packet[4]; PCA0CPH1=Out_Packet[3]; PCA0CPM1 |= 0x49; // enable compare function and enable match and interrupt for match for pca PCA0CPL2=Out_Packet[6]; PCA0CPH2=Out_Packet[5]; PCA0CPM2 |= 0x49; // enable compare function and enable match and interrupt for match for pca PCA0CPL3=Out_Packet[8]; PCA0CPH3=Out_Packet[7]; PCA0CPM3 |= 0x49; // enable compare function and enable match and interrupt for match for pca } EIE1 |= 0x10; // enable PCA interrupt break; case CMD_DISABLE_ALL_SERVOS: //cmd: CMD { Out_Packet[0]=0; // command is processed LedToggle(); PCA0CPM0 &= ~0x40; // disable compare function PCA0CPM1 &= ~0x40; // disable compare function PCA0CPM2 &= ~0x40; // disable compare function PCA0CPM3 &= ~0x40; // disable compare function } break; case CMD_SET_TIMER0_RELOAD_VALUE: { Out_Packet[0]=0; // command is processed LedToggle(); TH0=255-Out_Packet[1]; // timer0 reload value, 2 for 60Hz, 1 for 91Hz servo pulse rate, 0 for 180 Hz } break; case CMD_SET_PCA0MD_CPS: // bit are ORed with 0x7, left shifted by one, and set to the PCA0MD bits 3:1 { Out_Packet[0]=0; // command is processed LedToggle(); // must disable watchdog before changing these bits PCA0MD&=~0x40; PCA0MD = (PCA0MD & 0xf1) | ( (0x7&Out_Packet[1]) <<1); // this is the PCA control register, bits 3:1 are the CPS bits that control the PCA clock source // CPS = 0 sysclk/12 // CPS = 1 sysclk/4 // CPS = 2 timer0 overflow PCA0MD|=0x40; // re-enable watchdog PCA0CPH4 = 0xff; // write value to WDT PCA to start watchdog } break; case CMD_SET_PORT2: { Out_Packet[0]=0; //ack LedToggle(); P2=Out_Packet[1]; break; } case CMD_SEND_WOWWEE_RS_CMD: { // P2.0 is high Out_Packet[0]=0; // cmd has been processed LedToggle(); rsv2_cmd.cmd = (Out_Packet[1])|(Out_Packet[2]<<8); //rsv2_cmd.msb = (Out_Packet[2]); rsv2_precmd = 1; // we're sending the start 'bit' rsv2_cyclesleft = 517; // 8/1200 rsv2_cmdidx = 12; // 12 bits rsv2_sendcmd = 1; // we're sending a cmd state EIE1|=0x80; // enable timer 3 interrupts, disabled at end of cmd break; } case CMD_SET_PORT_DOUT: { Out_Packet[0]=0; // cmd has been processed LedToggle(); P1MDOUT= (Out_Packet[1]); // setting bit to 1 makes port pin push-pull, 0 makes it open drain P2MDOUT= (Out_Packet[2]); break; } } // switch EA=1; // enable interrupts //LedOn(); } // while(1) }
/*-------------------------------------------------------------------------------------------------- Name : SendMessage Description : Sends the message in global registers on the AVC LAN bus. Argument(s) : None. Return value : (bool) -> TRUE if successful else FALSE. --------------------------------------------------------------------------------------------------*/ bool SendMessage ( void ) { while ( ! IsAvcBusFree() ); // At this point we know the bus is available. LedOn(); // Send start bit. SendStartBit(); // Broadcast bit. Send1BitWord( Broadcast ); // Master address = me. Send12BitWord( MasterAddress ); Send1BitWord( ParityBit ); // Slave address = head unit (HU). Send12BitWord( SlaveAddress ); Send1BitWord( ParityBit ); if ( ! HandleAcknowledge() ) { DumpRawMessage( TRUE ); UsartPutStr( (char*)"SendMessage: No Ack @ Slave address\r\n" ); return FALSE; } // Control flag + parity. Send4BitWord( Control ); Send1BitWord( ParityBit ); if ( ! HandleAcknowledge() ) { DumpRawMessage( TRUE ); UsartPutStr( (char*)"SendMessage: No Ack @ Control\r\n" ); return FALSE; } // Data length + parity. Send8BitWord( DataSize ); Send1BitWord( ParityBit ); if ( ! HandleAcknowledge() ) { DumpRawMessage( TRUE ); UsartPutStr( (char*)"SendMessage: No Ack @ DataSize\r\n" ); return FALSE; } for ( byte i = 0; i < DataSize; i++ ) { Send8BitWord( Data[i] ); Send1BitWord( ParityBit ); if ( ! HandleAcknowledge() ) { DumpRawMessage( TRUE ); sprintf( UsartMsgBuffer, "SendMessage: No Ack @ Data[%d]\r\n", i ); UsartPutStr( UsartMsgBuffer ); return FALSE; } } DumpRawMessage( TRUE ); LedOff(); return TRUE; }
/*-------------------------------------------------------------------------------------------------------------------- Function: UserAppInitialize Description: Initializes the State Machine and its variables. Requires: - Promises: - */ void UserAppInitialize(void) { u8 au8WelcomeMessage[] = "REAL TIME HR GRAPH"; u8 au8Instructions[] = "B0 toggles radio"; /* Clear screen and place start messages */ #ifdef MPG1 LCDCommand(LCD_CLEAR_CMD); LCDMessage(LINE1_START_ADDR, au8WelcomeMessage); LCDMessage(LINE2_START_ADDR, au8Instructions); /* Start with LED0 in RED state = channel is not configured */ LedOn(RED); #endif /* MPG1 */ #ifdef MPG2 PixelAddressType sStringLocation = {LCD_SMALL_FONT_LINE0, LCD_LEFT_MOST_COLUMN}; LcdClearScreen(); LcdLoadString(au8WelcomeMessage, LCD_FONT_SMALL, &sStringLocation); sStringLocation.u16PixelRowAddress = LCD_SMALL_FONT_LINE1; LcdLoadString(au8Instructions, LCD_FONT_SMALL, &sStringLocation); /* Start with LED0 in RED state = channel is not configured */ LedOn(RED0); #endif /* MPG2 */ /* Configure ANT for this application */ G_stAntSetupData.AntChannel = ANT_CHANNEL_USERAPP; G_stAntSetupData.AntSerialLo = ANT_SERIAL_LO_USERAPP; G_stAntSetupData.AntSerialHi = ANT_SERIAL_HI_USERAPP; G_stAntSetupData.AntDeviceType = ANT_DEVICE_TYPE_USERAPP; G_stAntSetupData.AntTransmissionType = ANT_TRANSMISSION_TYPE_USERAPP; G_stAntSetupData.AntChannelPeriodLo = ANT_CHANNEL_PERIOD_LO_USERAPP; G_stAntSetupData.AntChannelPeriodHi = ANT_CHANNEL_PERIOD_HI_USERAPP; G_stAntSetupData.AntFrequency = ANT_FREQUENCY_USERAPP; G_stAntSetupData.AntTxPower = ANT_TX_POWER_USERAPP; /* If good initialization, set state to Idle */ if( AntChannelConfig(ANT_SLAVE) ) { /* Channel is configured, so change LED to yellow */ #ifdef MPG1 LedOff(RED); LedOn(YELLOW); #endif /* MPG1 */ #ifdef MPG2 LedOn(GREEN0); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Idle; } else { /* The task isn't properly initialized, so shut it down and don't run */ #ifdef MPG1 LedBlink(RED, LED_4HZ); #endif /* MPG1 */ #ifdef MPG2 LedBlink(RED0, LED_4HZ); #endif /* MPG2 */ UserApp_StateMachine = UserAppSM_Error; } } /* end UserAppInitialize() */
/* Channel is open, so monitor data */ static void UserAppSM_ChannelOpen(void) { static u8 u8LastState = 0xff; static u8 au8TickMessage[] = "EVENT x\n\r"; /* "x" at index [6] will be replaced by the current code */ static u8 au8DataContent[] = "xxxxxxxxxxxxxxxx"; static u8 au8LastAntData[ANT_APPLICATION_MESSAGE_BYTES] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; static u8 au8TestMessage[] = {0, 0, 0, 0, 0xA5, 0, 0, 0}; bool bGotNewData; /* Check for BUTTON0 to close channel */ if(WasButtonPressed(BUTTON0)) { /* Got the button, so complete one-time actions before next state */ ButtonAcknowledge(BUTTON0); /* Queue close channel and change LED to blinking green to indicate channel is closing */ AntCloseChannel(); u8LastState = 0xff; #ifdef MPG1 LedOff(YELLOW); LedOff(BLUE); LedBlink(GREEN, LED_2HZ); #endif /* MPG1 */ #ifdef MPG2 LedOff(RED0); LedOff(BLUE0); LedBlink(GREEN0, LED_2HZ); #endif /* MPG2 */ /* Set timer and advance states */ UserApp_u32Timeout = G_u32SystemTime1ms; UserApp_StateMachine = UserAppSM_WaitChannelClose; } /* end if(WasButtonPressed(BUTTON0)) */ /* Always check for ANT messages */ if( AntReadData() ) { /* New data message: check what it is */ if(G_eAntApiCurrentMessageClass == ANT_DATA) { UserApp_u32DataMsgCount++; /* Check if the new data is the same as the old data and update as we go */ bGotNewData = TRUE;//used to be false for(u8 i = 0; i < ANT_APPLICATION_MESSAGE_BYTES; i++) { if(G_au8AntApiCurrentData[i] != au8LastAntData[i]) { bGotNewData = TRUE; au8LastAntData[i] = G_au8AntApiCurrentData[i]; au8DataContent[2 * i] = HexToASCIICharUpper(G_au8AntApiCurrentData[i] / 16); au8DataContent[2 * i + 1] = HexToASCIICharUpper(G_au8AntApiCurrentData[i] % 16); } } if(bGotNewData) { /* We got new data: show on LCD */ #ifdef MPG1 LCDClearChars(LINE2_START_ADDR, 20); LCDMessage(LINE2_START_ADDR, au8DataContent); #endif /* MPG1 */ #ifdef MPG2 PixelAddressType sStringLocation1 = {0, 122}; PixelAddressType sStringLocation2 = {8, 122}; PixelAddressType sStringLocation3 = {16, 122}; const unsigned char * HRTop = ReturnTopDigit(G_au8AntApiCurrentData[7]); const unsigned char * HRMiddle = ReturnMiddleDigit(G_au8AntApiCurrentData[7]); const unsigned char * HRBottom = ReturnLastDigit(G_au8AntApiCurrentData[7]); LcdLoadString(HRTop, LCD_FONT_SMALL, &sStringLocation1); LcdLoadString(HRMiddle, LCD_FONT_SMALL, &sStringLocation2); LcdLoadString(HRBottom, LCD_FONT_SMALL, &sStringLocation3); //LcdLoadString(" ",LCD_FONT_SMALL, &sStringLocation); printToScreen(G_au8AntApiCurrentData[7]); #endif /* MPG2 */ /* Update our local message counter and send the message back */ au8TestMessage[7]++; if(au8TestMessage[7] == 0) { au8TestMessage[6]++; if(au8TestMessage[6] == 0) { au8TestMessage[5]++; } } AntQueueBroadcastMessage(au8TestMessage); /* Check for a special packet and respond */ #ifdef MPG1 if(G_au8AntApiCurrentData[0] == 0xA5) { LedOff(LCD_RED); LedOff(LCD_GREEN); LedOff(LCD_BLUE); if(G_au8AntApiCurrentData[1] == 1) { LedOn(LCD_RED); } if(G_au8AntApiCurrentData[2] == 1) { LedOn(LCD_GREEN); } if(G_au8AntApiCurrentData[3] == 1) { LedOn(LCD_BLUE); } } #endif /* MPG1 */ #ifdef MPG2 /*if(G_au8AntApiCurrentData[0] == 0xFF) { LedOff(RED3); LedOff(GREEN3); LedOff(BLUE3); if(G_au8AntApiCurrentData[1] == 0xFF) { LedOn(RED3); } if(G_au8AntApiCurrentData[2] == 0xFF) { LedOn(GREEN3); } if(G_au8AntApiCurrentData[3] == 0xFF) { LedOn(BLUE3); } }*/ #endif /* MPG2 */ } /* end if(bGotNewData) */ } /* end if(G_eAntApiCurrentMessageClass == ANT_DATA) */ else if(G_eAntApiCurrentMessageClass == ANT_TICK) { UserApp_u32TickMsgCount++; /* Look at the TICK contents to check the event code and respond only if it's different */ if(u8LastState != G_au8AntApiCurrentData[ANT_TICK_MSG_EVENT_CODE_INDEX]) { /* The state changed so update u8LastState and queue a debug message */ u8LastState = G_au8AntApiCurrentData[ANT_TICK_MSG_EVENT_CODE_INDEX]; au8TickMessage[6] = HexToASCIICharUpper(u8LastState); DebugPrintf(au8TickMessage); /* Parse u8LastState to update LED status */ switch (u8LastState) { #ifdef MPG1 /* If we are synced with a device, blue is solid */ case RESPONSE_NO_ERROR: { LedOff(GREEN); LedOn(BLUE); break; } /* If we are paired but missing messages, blue blinks */ case EVENT_RX_FAIL: { LedOff(GREEN); LedBlink(BLUE, LED_2HZ); break; } /* If we drop to search, LED is green */ case EVENT_RX_FAIL_GO_TO_SEARCH: { LedOff(BLUE); LedOn(GREEN); break; } #endif /* MPG 1 */ #ifdef MPG2 /* If we are synced with a device, blue is solid */ case RESPONSE_NO_ERROR: { LedOff(GREEN0); LedOn(BLUE0); break; } /* If we are paired but missing messages, blue blinks */ case EVENT_RX_FAIL: { LedOff(GREEN0); LedBlink(BLUE0, LED_2HZ); break; } /* If we drop to search, LED is green */ case EVENT_RX_FAIL_GO_TO_SEARCH: { LedOff(BLUE0); LedOn(GREEN0); break; } #endif /* MPG 2 */ /* If the search times out, the channel should automatically close */ case EVENT_RX_SEARCH_TIMEOUT: { DebugPrintf("Search timeout\r\n"); break; } default: { DebugPrintf("Unexpected Event\r\n"); break; } } /* end switch (G_au8AntApiCurrentData) */ } /* end if (u8LastState != G_au8AntApiCurrentData[ANT_TICK_MSG_EVENT_CODE_INDEX]) */ } /* end else if(G_eAntApiCurrentMessageClass == ANT_TICK) */ } /* end AntReadData() */ /* A slave channel can close on its own, so explicitly check channel status */ if(AntRadioStatus() != ANT_OPEN) { #ifdef MPG1 LedBlink(GREEN, LED_2HZ); LedOff(BLUE); #endif /* MPG1 */ #ifdef MPG2 LedBlink(GREEN0, LED_2HZ); LedOff(BLUE0); #endif /* MPG2 */ u8LastState = 0xff; UserApp_u32Timeout = G_u32SystemTime1ms; UserApp_StateMachine = UserAppSM_WaitChannelClose; } /* if(AntRadioStatus() != ANT_OPEN) */ } /* end UserAppSM_ChannelOpen() */
int Control() { unsigned long int FreqCountNext,FreqCountPrev,FreqC; unsigned long int ADCCounts; long int ADCCountTank1Pr, ADCCountTank1Lev, ADCCountTank2Pr, ADCCountTank2Lev; float f2v,integral,i1,i2,p1,p2,p1ini, p2ini, i1ini, i2ini, thp,thn,offsetP,offsetN; int LockWindow,LockCount,calmode,error, AvDelCnt; float OP, TrapezF, IntegErr, FreqAv, Th_Unlock,ERLast,der; int unlockDelay,derc; int count,i, pi_set,g,PhaseSignValue,integconst,start,iRet; //calmode=calibrate(); //unlockDelay = 200; // if(calmode==1) { Print("control before LED!\r\n"); X304_Init(); Print("control after x304 init!\r\n"); //SetDoLow(); Print("control before com install!\r\n"); // DelayMs(1000); InstallCom(cport,9600L,8,0,1); // InstallCom(commandport,115200L,8,0,1); Print("control after comm install!\r\n"); X304_Write_One_DO(0, 0); p1 = (ReadNVRAM(0))/100; i1=(ReadNVRAM(1))/100; Th_Unlock = ReadNVRAM(2)/10; derc= ReadNVRAM(3)/1000; offsetN=(ReadNVRAM(4))/100; offsetP=(ReadNVRAM(5))/100; integconst=(ReadNVRAM(6)); LockWindow=(ReadNVRAM(7)); while(1) { TankPer=0; Print("control before LED!\r\n"); LedOn(); DelayMs(200); LedOff(); Print("control after LED!\r\n"); ADCCountTank1Lev = Modbus(); Print("control after modbus!\r\n"); //ADCCountTank1Lev = Modbus2(); //ADCCountTank2Pr = Modbus3(); // ADCCountTank2Lev = Modbus4(); TankPer =(ADCCountTank1Lev-ZERO)*SLOPE; if ((ADCCountTank1Lev-ZERO)<0) TankPer=0; else TankPer=TankPer; Meterdisplay_out= TankPer*0.01; // printCom(1,"ADC Value is=%u %u %u %u\n\r",ADCCountTank1Pr, ADCCountTank1Lev, ADCCountTank2Pr, ADCCountTank2Lev); // OP = 10 * ADCCountTank1Lev / 65536; X304_AnalogOut(0, Meterdisplay_out); Print("ADCCountTank1Lev= %d\n",ADCCountTank1Lev); Print("TankPer= %f\n",TankPer); } } }
void IdlePattern() { int i, d; memset(buffer, 0x00, BUFFERSIZE); for(i = 0; i < SIZE; i++) { LedOn(0, 0, i); LedOn(7, 0, i); LedOn(0, 7, i); LedOn(7, 7, i); LedOn(0, i, 0); LedOn(7, i, 0); LedOn(0, i, 7); LedOn(7, i, 7); LedOn(i, 0, 0); LedOn(i, 7, 0); LedOn(i, 0, 7); LedOn(i, 7, 7); for(d = 0; d < 2; d++) { Refresh(); } } }
/*-------------------------------------------------------------------------------------------------- Name : AvcReadMessage Description : Read incoming messages on the AVC LAN bus. Argument(s) : None. Return value : (AvcActionID) -> Action ID associated with this message. --------------------------------------------------------------------------------------------------*/ AvcActionID AvcReadMessage ( void ) { ReadBits( 1 ); // Start bit. LedOn(); Broadcast = ReadBits( 1 ); MasterAddress = ReadBits( 12 ); bool p = ParityBit; if ( p != ReadBits( 1 ) ) { UsartPutCStr( PSTR("AvcReadMessage: Parity error @ MasterAddress!\r\n") ); return (AvcActionID)FALSE; } SlaveAddress = ReadBits( 12 ); p = ParityBit; if ( p != ReadBits( 1 ) ) { UsartPutCStr( PSTR("AvcReadMessage: Parity error @ SlaveAddress!\r\n") ); return (AvcActionID)FALSE; } bool forMe = ( SlaveAddress == MY_ADDRESS ); // In point-to-point communication, sender issues an ack bit with value '1' (20us). Receiver // upon acking will extend the bit until it looks like a '0' (32us) on the bus. In broadcast // mode, receiver disregards the bit. if ( forMe ) { // Send ACK. Send1BitWord( 0 ); } else { ReadBits( 1 ); } Control = ReadBits( 4 ); p = ParityBit; if ( p != ReadBits( 1 ) ) { UsartPutCStr( PSTR("AvcReadMessage: Parity error @ Control!\r\n") ); return (AvcActionID)FALSE; } if ( forMe ) { // Send ACK. Send1BitWord( 0 ); } else { ReadBits( 1 ); } DataSize = ReadBits( 8 ); p = ParityBit; if ( p != ReadBits( 1 ) ) { UsartPutCStr( PSTR("AvcReadMessage: Parity error @ DataSize!\r\n") ); return (AvcActionID)FALSE; } if ( forMe ) { // Send ACK. Send1BitWord( 0 ); } else { ReadBits( 1 ); } byte i; for ( i = 0; i < DataSize; i++ ) { Data[i] = ReadBits( 8 ); p = ParityBit; if ( p != ReadBits( 1 ) ) { sprintf( UsartMsgBuffer, "AvcReadMessage: Parity error @ Data[%d]\r\n", i ); UsartPutStr( UsartMsgBuffer ); return (AvcActionID)FALSE; } if ( forMe ) { // Send ACK. Send1BitWord( 0 ); } else { ReadBits( 1 ); } } // Dump message on terminal. if ( forMe ) UsartPutCStr( PSTR("AvcReadMessage: This message is for me!\r\n") ); AvcActionID actionID = GetActionID(); // switch ( actionID ) { // case /* value */: // } DumpRawMessage( FALSE ); LedOff(); return actionID; }
/*----------------------------------------------------------------------------- * Ledzustand ändern */ void LedSet(TLedState state) { uint16_t currTimeMs; TState *pState; GET_TIME_MS16(currTimeMs); switch (state) { case eLedRedOff: LedOff(0); sLedState[0].offTime = FOREVER; break; case eLedRedOn: LedOn(0); sLedState[0].onTime = FOREVER; break; case eLedGreenOff: LedOff(1); sLedState[1].offTime = FOREVER; break; case eLedGreenOn: LedOn(1); sLedState[1].onTime = FOREVER; break; case eLedRedFlashSlow: pState = &sLedState[0]; pState->onTime = FLASH_TIME; pState->offTime = LONG_CYCLE; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(0); break; case eLedGreenFlashSlow: pState = &sLedState[1]; pState->onTime = FLASH_TIME; pState->offTime = LONG_CYCLE; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(1); break; case eLedRedFlashFast: pState = &sLedState[0]; pState->onTime = FLASH_TIME; pState->offTime = SHORT_CYCLE; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(0); break; case eLedGreenFlashFast: pState = &sLedState[1]; pState->onTime = FLASH_TIME; pState->offTime = SHORT_CYCLE; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(1); break; case eLedRedBlinkSlow: pState = &sLedState[0]; pState->onTime = SLOW_BLINK; pState->offTime = SLOW_BLINK; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(0); break; case eLedGreenBlinkSlow: pState = &sLedState[1]; pState->onTime = SLOW_BLINK; pState->offTime = SLOW_BLINK; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(1); break; case eLedRedBlinkFast: pState = &sLedState[0]; pState->onTime = FAST_BLINK; pState->offTime = FAST_BLINK; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(0); break; case eLedGreenBlinkFast: pState = &sLedState[1]; pState->onTime = FAST_BLINK; pState->offTime = FAST_BLINK; pState->cyclic = true; pState->switchTime = currTimeMs; LedOn(1); break; case eLedRedBlinkOnceShort: pState = &sLedState[0]; pState->onTime = FAST_BLINK; pState->offTime = 0; pState->cyclic = false; pState->switchTime = currTimeMs; LedOn(0); break; case eLedRedBlinkOnceLong: pState = &sLedState[0]; pState->onTime = SLOW_BLINK; pState->offTime = 0; pState->cyclic = false; pState->switchTime = currTimeMs; LedOn(0); break; case eLedGreenBlinkOnceShort: pState = &sLedState[1]; pState->onTime = FAST_BLINK; pState->offTime = 0; pState->cyclic = false; pState->switchTime = currTimeMs; LedOn(1); break; case eLedGreenBlinkOnceLong: pState = &sLedState[1]; pState->onTime = SLOW_BLINK; pState->offTime = 0; pState->cyclic = false; pState->switchTime = currTimeMs; LedOn(1); break; default: break; } }
/*---------------------------------------------------------------------------------------------------------------------- Function: LedInitialize Description: Initialization of LED system paramters and visual LED check. Requires: - G_u32SystemTime1ms ticking - All LEDs already initialized to LED_NORMAL_MODE mode ON Promises: - All LEDs in LED_NORMAL_MODE mode with OFF */ void LedInitialize(void) { u32 u32Timer; // u8* pu8Parser; u32 u32Buzzer1Frequency = 4000; u32 u32Buzzer2Frequency = 500; u32 u32StepSize = (u32Buzzer1Frequency - u32Buzzer2Frequency) / 20; static u8 au8LedStartupMsg[] = "LED functions ready\n\r"; #if MPGL2 /* Test code for checking LEDs */ #if 0 LedOn(RED0); LedOn(RED1); LedOn(RED2); LedOn(RED3); LedOn(BLUE0); LedOn(BLUE1); LedOn(BLUE2); LedOn(BLUE3); LedOn(GREEN0); LedOn(GREEN1); LedOn(GREEN2); LedOn(GREEN3); #endif #endif /* MPGL2 */ /* Turn all LEDs on full, then fade them out over a few seconds */ for(u8 i = 20; i > 0; i--) { #if STARTUP_SOUND /* Configure Buzzers to provide some audio during start up */ PWMAudioSetFrequency(BUZZER1, u32Buzzer1Frequency); PWMAudioOn(BUZZER1); #ifdef MPGL1 PWMAudioSetFrequency(BUZZER2, u32Buzzer2Frequency); PWMAudioOn(BUZZER2); #endif /* MPGL1 */ #endif /* STARTUP_SOUND */ /* Spend 40ms in each level of intensity */ for(u16 j = 40; j > 0; j--) { u32Timer = G_u32SystemTime1ms; while( !IsTimeUp(&u32Timer, 1) ); LedUpdate(); } /* Pause for a bit on the first iteration to show the LEDs on for little while */ if(i == 20) { while( !IsTimeUp(&u32Timer, 1500) ); } /* Set the LED intensity for the next iteration */ for(u8 j = 0; j < TOTAL_LEDS; j++) { Leds_asLedArray[j].eRate = (LedRateType)(i - 1); } /* Set the buzzer frequency for the next iteration */ u32Buzzer1Frequency -= u32StepSize; u32Buzzer2Frequency += u32StepSize; } /* Final update to set last state, hold for a short period */ LedUpdate(); while( !IsTimeUp(&u32Timer, 200) ); #if STARTUP_SOUND /* Turn off the buzzers */ PWMAudioOff(BUZZER1); #ifdef MPGL1 PWMAudioOff(BUZZER2); #endif /* MPGL1 */ #endif /* STARTUP_SOUND */ /* Exit with Leds off, NORMAL mode, and the backlight on (white) */ for(u8 i = 0; i < TOTAL_LEDS; i++) { Leds_asLedArray[0].eMode = LED_NORMAL_MODE; } #ifdef MPGL1 LedOn(LCD_RED); LedOn(LCD_GREEN); LedOn(LCD_BLUE); #endif #ifdef MPGL2 LedOn(LCD_BL); #endif /* Final setup and report that LED system is ready */ G_u32ApplicationFlags |= _APPLICATION_FLAGS_LED; DebugPrintf(au8LedStartupMsg); } /* end LedInitialize() */