void Install_Timer(uint32_t ms, uint32_t timer, uint32_t prio, uint8_t mtchstop){ #ifndef AVR #if defined(STM32F10X_MD) || defined(STM32F30X) TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, DISABLE); EnableClk(timer+6); ///< Just to use created functions // RCC_APB1PeriphClockCmd(1<<(timer-2), ENABLE); TIM_TimeBaseStructure.TIM_Prescaler = (ms-1); TIM_TimeBaseStructure.TIM_Period = ((SystemCoreClock) / 1000) - 1;//(ms); TIM_TimeBaseInit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), &TIM_TimeBaseStructure); TIM_ClearITPendingBit(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update); //TODO: Install Interrupt here? Well it actually SHOULD be initialized before activating TIM_IT //but that would force me to create more doubles. Keep an eye on this TIM_ITConfig(((TIM_TypeDef *) (APB1PERIPH_BASE + ((timer-2)*0x400))), TIM_IT_Update, ENABLE); #else // configure timer TIM_TIMERCFG_Type tc; TIM_ConfigStructInit(TIM_TIMER_MODE, &tc); TIM_Init((( TIM_TypeDef *) timers[timer]), TIM_TIMER_MODE, &tc); // set up match register TIM_MATCHCFG_Type mc; mc.MatchChannel = 0; mc.IntOnMatch = ENABLE; mc.StopOnMatch = mtchstop; mc.ResetOnMatch = ENABLE; mc.ExtMatchOutputType = 0; #ifdef _LPC23XX_ //Yes it actually IS running at one Mhz. This isn't right but I don't have time to spend days in the lpc23xx bible again. mc.MatchValue = (((1000000/1000)*ms)-1); #else mc.MatchValue = (ms * CLKPWR_GetPCLK(CLKPWR_PCLKSEL_TIMER0)) / 1000; #endif TIM_ConfigMatch((( TIM_TypeDef *) (timers[timer])), &mc); // InstallINT(timer, prio); // TIM_Cmd((( TIM_TypeDef *) (timers[timer])), ENABLE);// enable timer #endif //Must really do something neater than this if(mtchstop==0) mtchstop=1; else mtchstop=0; InstallINT(timer, prio); TIM_Cmd((( TIM_TypeDef *) (timers[timer])), mtchstop); #endif }
int main (void) { TIM_TIMERCFG_Type timerCfg; uint8_t joyState = 0; /* Initialize devices */ // initialize timer TIM_ConfigStructInit(TIM_TIMER_MODE, &timerCfg); TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timerCfg); console_init(); joystick_init(); while(1) { joyState = joystick_read(); if (joyState & JOYSTICK_UP) { console_sendString((uint8_t*)"Up "); } if (joyState & JOYSTICK_DOWN) { console_sendString((uint8_t*)"Down "); } if (joyState & JOYSTICK_LEFT) { console_sendString((uint8_t*)"Left "); } if (joyState & JOYSTICK_RIGHT) { console_sendString((uint8_t*)"Right "); } if (joyState & JOYSTICK_CENTER) { console_sendString((uint8_t*)"Center "); } if (joyState != 0) { console_sendString((uint8_t*)"\r\n"); } TIM_Waitms(200); } }
void startTimerInt(uint8_t matchRegister, uint32_t us) { TIM_MATCHCFG_Type timerMatchCfg; TIM_TIMERCFG_Type timerCfg; uint32_t ticks = us; //Initialize timer for delays and interrupts TIM_ConfigStructInit(TIM_TIMER_MODE, &timerCfg); /* initialize timer config struct */ TIM_Init(LPC_TIM1, TIM_TIMER_MODE, &timerCfg); /* initialize timer0 */ timerMatchCfg.ExtMatchOutputType = TIM_EXTMATCH_NOTHING; timerMatchCfg.IntOnMatch = ENABLE; timerMatchCfg.MatchChannel = matchRegister; timerMatchCfg.MatchValue = ticks; timerMatchCfg.ResetOnMatch = DISABLE; timerMatchCfg.StopOnMatch = DISABLE; TIM_ConfigMatch(LPC_TIM1, &timerMatchCfg); TIM_ResetCounter(LPC_TIM1); NVIC_EnableIRQ(TIMER1_IRQn); TIM_Cmd(LPC_TIM1, ENABLE); }
int main(void) { //Variable Declarations initSolenoid(); /* initialize solenoid valve */ TIM_TIMERCFG_Type timerCfg; initTimeStruct(); RTC_TIME_Type* watertime = malloc(sizeof(RTC_TIME_Type)); uint8 fed = 0; uint8 watered = 0; watertime->HOUR = 5; watertime->MIN = 0; //Initialize timer0 for delays TIM_ConfigStructInit(TIM_TIMER_MODE, &timerCfg); /* initialize timer config struct */ TIM_Init(LPC_TIM0, TIM_TIMER_MODE, &timerCfg); /* initialize timer0 */ //Initialize Real Time Clock RTC_Init(LPC_RTC); RTC_Cmd(LPC_RTC, ENABLE); RTC_ResetClockTickCounter(LPC_RTC); // Initialize Peripherals INIT_SDRAM(); /* initialize SDRAM */ servoInit(); /* initialize FSR servo motor for panning camera */ initStepper(); /* initialize stepper motor for dispensing food */ initFSR(); /* initialize force sensitive resistor circuit for food and water full signals */ initWiFi(AUTO_CONNECT); /* initialize WiFi module -- must be attached*/ audio_initialize(); audio_reset(); //audio_test(); audio_setupMP3(); int i = 0, retval; uint32 length; /* length variable for photo */ printf("Entering while loop\n\r"); //audio_storeVoice(); // Enter an infinite loop while(1) { if(STATE == DISPENSING_FOOD){ printf("Entering food dispense state\n\r"); /* Execute commands to dispense food */ //spinUntilFull(); spinStepper(300); reverseSpin(250); STATE = CONNECTED; } if(STATE == DISPENSING_WATER){ printf("Entering water dispense state\n\r"); /* Execute commands to dispense water */ fillWater(); STATE = CONNECTED; } if(STATE == CAPTURING){ printf("Entering camera taking state\n\r"); /* Initialize camera and set it up to take a picture */ if(cameraInit()) printf("Camera not initialized!\n\r"); retval = stopFrame(); length = getBufferLength(); printf("length: %i\n\r", length); /* Send length to Android application */ int temp_len = length; while(temp_len){ uart1PutChar(temp_len % 10); temp_len = temp_len / 10; } /* Send photo and finish set up */ getAndSendPhoto(length); resumeFrame(); STATE = CONNECTED; } if(STATE == TALKING1){ audio_playVoice(1); STATE = CONNECTED; } if(STATE == TALKING2){ audio_playVoice(2); STATE = CONNECTED; } if(STATE == TALKING3){ audio_playVoice(3); STATE = CONNECTED; } if(STATE == PAN_LEFT){ /* Execute commands to pan servo left */ panServo(LEFT); STATE = CONNECTED; } if(STATE == PAN_RIGHT){ /* Execute commands to pan servo right */ panServo(RIGHT); STATE = CONNECTED; } if(STATE == SCHEDULING){ /* Execute commands to schedule a feeding time */ STATE = CONNECTED; } /* Scheduling */ RTC_GetFullTime(LPC_RTC, time); //Fill water bowl at predetermined time if (time->HOUR == watertime->HOUR + 1 && watered == 1) watered = 0; if (watertime->HOUR == time->HOUR && watertime->MIN < time->MIN && watered == 0) { fillWater(); watered = 1; } //Feed dog on schedule if any cannot feed dog two consecutive hours for(i = 0; i < scheduled_feeds; i++) { if (time->HOUR == feedtime[i]->HOUR + 1 && fed == 1) fed = 0; if (feedtime[i]->HOUR == time->HOUR && feedtime[i]->MIN < time->MIN && fed == 0) { spinUntilFull(); fed = 1; } } } return 0; }