Example #1
0
// ---------------------------------------------------------------------------------
// @brief : To initialise UART1 for downloading and debugging
// @param : baudrate
// @retval: none
void UART1_init(int baudrate){

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

	GPIO_Init_Mode(USART1_TX, GPIO_Mode_AF_PP);
	GPIO_Init_Mode(USART1_RX, GPIO_Mode_IN_FLOATING);

	__UART_init(0, baudrate);
}
Example #2
0
// @brief : To initialise port pin for user switch input.
// @param : none
// @retval
void SwitchInit() {
	EXTI_InitTypeDef EXTI_InitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

	// USER_SW_PORT is defined as PA14
	GPIO_Init_Mode(USER_SW_PORT, USER_SW_PIN, GPIO_Mode_IN_FLOATING);

	EXTI_InitStruct.EXTI_Line = EXTI_Line14;
	EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_InitStruct.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStruct);

	NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = SWITCH_PRIORITY;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);

	bSWFlag = FALSE;
	SWstate = SW_LOW;
	SW_PRESS_TIME = 0;

	TimerInit(TIM_SWITCH);
}
Example #3
0
//Main function (execution starts here after startup file)
int main(void)
{
	int i;
	int e;
	uint16_t temperature;

	init_GPIO_pins();
	
	//Short delay during which we can communicate with MCU via debugger even if later user code causes error such as sleep state with no wakeup event that prevents debugger interface working
	//THIS MUST COME BEFORE ALL USER CODE TO ENSURE CHIPS CAN BE REPROGRAMMED EVEN IF THEY GET STUCK IN A SLEEP STATE LATER
	for (i = 0; i < 1000000; i++)
	{
		LED_on();
	}
	
	GPIO_Init_Mode(GPIOA,GPIO_Pin_0,GPIO_Mode_IN_FLOATING); //User button.
	GPIO_Init_Mode(GPIOC,GPIO_Pin_11,GPIO_Mode_IN_FLOATING); //Accelerometer interrupt.
	delay_init();
	LED_off();
	LCDINIT();
	home();
	clear();
	display(); //Surely some of these can be commented out.
	noCursor();
	noBlink();

	standby();

	UART_init();
	humidity_init();
	ADC_init();
	I2C_EEPROM();
	I2C_ACCEL_INIT();
	I2C_EE_LoadConfig();
	logging_timer_init();
	
//	I2C_EE_BufferWrite(Test_Buffer, EEPROM_WriteAddress1, 100);
//I2C_EE_BufferRead(buffer, 0, 100);

	/*while(1){
		if(LEDbyte==512){LEDbyte=1;}
		else {LEDbyte=LEDbyte<<1;}
		setLEDS();
		
	setCursor(0,1);
	temperature=getTemperature();
	writenumber( temperature/100);
	write('.');
	writenumber((temperature/10)%10);
		write(' ');

		write(0xDF);
	write('C');
	setCursor(0,0);
	writenumber(readhumidity(24)); //Needs real temperature
	write(' ');
	write('%');
	write('R');
	write('H');
		delay_ms(50);
		
		check_and_process_received_command();
		
	}*/
	//currentstate=UPLOADING;

	while(1)
	{
		switch (currentstate){
			
		case WAITING:
			if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)) //Polling is probably ok here, since the loop will be very very fast.
			{
				currentstate=LOGGING;
				clear();
				write('S');
				write('t');
				write('a');
				write('r');
				write('t');
				write('i');
				write('n');
				write('g');
				delay_ms(2000);
				clear();
				I2C_EE_StartLog();
				TIM4->CNT=0;
			}
			break;

		case LOGGING:
			if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)){currentstate=WAITING;I2C_EE_FinishLog();break;} //Polling is probably ok here, since the loop will be very very fast.
			LEDbyte|= 1<<8;
			setLEDS();
			if (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) != RESET)
			{
				TIM_ClearFlag(TIM3, TIM_IT_Update);
				temperature = getTemperature();
				LogBuffer[0]=(temperature>>8)&0xFF;
				LogBuffer[1]=temperature&0xFF;
				LogBuffer[2]=readhumidity(LogBuffer[0]);
				I2C_ACCEL_READ();
				I2C_EE_Log(LogBuffer);
				setCursor(0,1);
				writenumber(temperature/100);
				write('.');
				writenumber(temperature%100);
				write(' ');
				write(0xDF);
				write('C');
				write(' ');
				setCursor(0,0);
				writenumber(25); //Needs real temperature
				write(' ');
				write('%');
				write('R');
				write('H');
				write(' ');
				LEDbyte&= ~(1<<8);
				setLEDS();
			}
			break;
		
		case UPLOADING:
			currentstate=WAITING;
			I2C_EE_Upload();
			break;
		
		case ERASING:
			currentstate=WAITING;
			I2C_EE_Erase();
			break;
		
		case STREAMING:
			if (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) != RESET)
			{
				TIM_ClearFlag(TIM3, TIM_IT_Update);
				temperature = getTemperature();
				LogBuffer[0]=(temperature>>8)&0xFF;
				LogBuffer[1]=temperature&0xFF;
				LogBuffer[2]=readhumidity(LogBuffer[0]);
				I2C_ACCEL_READ();
//				I2C_EE_Log(LogBuffer);
				for (e=0;i<ENTRYBYTES;i++)
				{
					UART_send_byte(LogBuffer[e]);
				}
				setCursor(0,1);
				writenumber(temperature/100);
				write('.');
				writenumber(temperature%100);
				write(' ');
				write(0xDF);
				write('C');
				write(' ');
				setCursor(0,0);
				writenumber(25); //Needs real temperature
				write(' ');
				write('%');
				write('R');
				write('H');
				write(' ');
				LEDbyte&= ~(1<<8);
				setLEDS();
			}
		break;
		}