Esempio n. 1
0
/************************************************************************************************
** Function name :			
** Description :
** 
** Input :
** Output :
** Return :
** Others :
** 
************************************************************************************************/
void INIT(void)
{
	 SystemInit();
	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA 
                              |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
                              |RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE );  
                                                                                                                                                    						                                                    
	 NVIC_Configuration();
	 GPIO_Configuration();
	 USART1_config();
	 Usart_DMA_config();
}
int main(void){
uint16_t pwm=0;
uint8_t portVal=0;


	//1.  config stuffs
	USART1_config(USART1_MY_UBBRN,USART_DATA_FORMAT_8BITS|USART_STOP_BITS_1,USART_TRANSMIT_ENABLE|USART_RECEIVE_ENABLE| USART_INTERRUPT_ENABLE);
	
	ADC_init(ADC_ENABLE,ADC_REF_VCC,ADC_calcPreScaler(ADC_MAX_FREQ));
	configGPIO();
	schedulerInit();
	pwmInit();
	
	
	
	// call the memory read here
	
	paramLoadDefaultParameters(); // 
	controlInit();
	
	
	USART1_sendStr("Hello\n\r");
	
	
	state= STATE_IDLE; // by default
	IDLE_LED_ON;
	PRGM_LED_OFF;
	
	//2. enable interrups
	sei();
	
	flagCurrentEnable =1;
	
	// set run status
	
	
	
	//3. loop
    while(1){
		
		/*
		OCR1B=pwm;
		pwm +=100;
		if(pwm >CURRENT_MAXPWM) pwm=0;
		_delay_ms(10);
		*/
		 
		//1. buttons read
		if(flagTaskReadButtons){
				
			uint8_t codeNew;
			//portVal=0; // just
			portVal = (~(PINC & 0x07)&0x07); // Handle inverted logic
			
			codeNew = decodeButton(portVal);
			codeNew = debounceKey(codeNew);
		
			
			#ifdef DEBUG
			sprintf(bufferDummy,"%x\n\r",codeNew);
			USART1_sendStr(bufferDummy);
			#endif	
			
			stateMachine(codeNew); 
	
			
			flagTaskReadButtons=0;
		}
		
		
		if(flagSaveParameters){
			
			paramSavetoEeprom();
			
			flagSaveParameters=0;
		}
		
		
		
		// 2. Control loop to be esecuted
		if(flagTaskControl){
	
			pwm = controlLoop();
			flagTaskControl=0;
			
			/*#ifdef DEBUG
			sprintf(bufferDummy,"%x\n\r",pwm);
			USART1_sendStr(bufferDummy);
			#endif*/
			
		
			
		}
		
		if(flagCurrentEnable){
			// Update ocr 16 bits
			OCR1B= pwm;
		
		}else OCR1B =0;
		
		//4. new message arrive
		if(flagTaskUsartMessage){
			
			// extrct the message
			
			// update Pid parameters
			PID_setPid(pidP,pidI,pidD);
			PID_setLimitsPerr(pidPerrMin,pidPerrMax);
			PID_setLimitsIerr(pidIerrMin,pidIerrMax);
			flagTaskUsartMessage=0;
		}
       
    }
}
Esempio n. 3
0
// main loop
int main(void){
	
	uint8_t stateDS18=DS18B20_STATUS_READY;
	
	#ifdef USE_WDT
	WDT_init(WDTO_8S); 
	#endif
	
	
	initGPIO();
	LCD_init();
	
	showLcdSplash();
		
	setLcdInitialFields();
		
	paramLoadFromEeprom();
		
	USART1_config(USART1_MY_UBBRN,USART_DATA_FORMAT_8BITS|USART_STOP_BITS_1,USART_TRANSMIT_ENABLE|USART_RECEIVE_ENABLE| USART_INTERRUPT_ENABLE);
	
	USART1_sendStr("Hello");
	
	schedulerInit();
	
	// check for the default values
	#ifdef USE_WDT
	WDT_init(WDTO_8S);
	#endif
	
	
	sei(); //enable interrups
	
	// loop while	
    while(1){
		
		
		// cintrol zone
		if(flagTaskControl){
			#ifdef USE_WDT
			WDT_reset();
			#endif
			
			
			LED_RUN_OFF;
			// fire the DS
			if(stateDS18==DS18B20_STATUS_READY){
				DS18B20_startConv();
				stateDS18=DS18B20_STATUS_CONV;
			
			// check if convertion ended	
			}else if(stateDS18==DS18B20_STATUS_CONV){
				if(DS18B20_convComplete()){
					stateDS18=DS18B20_STATUS_END_CONV;
				}
			
			// convertion ready	
			}else if (stateDS18==DS18B20_STATUS_END_CONV){
				LED_RUN_ON;
				currentTemp=DS18B20_getTemp();
				stateDS18=DS18B20_STATUS_READY;
				
				currentStatus = checkTempError(currentTemp,currentTempSetPoint,currentHistSetPoint); // chec the out
				setOutputRelay(currentStatus);
				_delay_ms(50);
				LED_RUN_OFF;
					
				
			}
			
			flagTaskControl=0;
		}
		
		// user bottons area
		if(flagTaskReadButtons){
			#ifdef USE_WDT
			WDT_reset();
			#endif
			
			uint8_t portVal = readButtons();
			uint8_t code = decodeButton(portVal);
			code = debounceKey(code);
			
			#ifdef MAIN_DEBUG
			sprintf(debugBuffer,"Key %d",code);
			USART1_sendStr(debugBuffer);
			#endif
			
			stateMachine(code); // go to machine
			
			flagTaskReadButtons=0;
		}
		
		 
		 // lcd update area
		 if(flagTaskUpdateLcd){
			 #ifdef USE_WDT
			 WDT_reset();
			 #endif
			 //showLcdSavedMessage();
			 updateLcd(); // update the lcd
			 
			 flagTaskUpdateLcd=0;
		 }
		 
		 // save to eeprom
		 if(flagSaveParametersEeprom){
			 #ifdef USE_WDT
			 WDT_reset();
			 #endif
			 
			 paramSavetoEeprom(); // save value sto eeprom
			 showLcdSavedMessage();
			 setLcdInitialFields();
			 updateLcd();
			 flagSaveParametersEeprom=0;
		 }
		 
    }
}
Esempio n. 4
0
void ConsoleConfig(void){
	USART1_config(115200);
	commandInit();
}