Exemple #1
0
void main(void) {
	setup();

	while(1) {
		if(gpioTick) {
			gpioTick = false;
		}
		
		if(timer8MainTick) {
			timer8MainTick = false;
			lcdUpdate++;
			
			if (lcdUpdate >= 499)
			{
				lcdUpdate = 0;		
				LCD_Control(0x01);
				LCD_PrString(lcdBuffer[0]);
				
				usDistance = (((46400-usRawTimerValue)/2) / 58);
				itoa(lcdBuffer[1], usDistance,10);
				
				itoa(lcdBuffer[1], start, 10);
				LCD_Position(0,10);
				LCD_PrString(lcdBuffer[1]);
				
				itoa(lcdBuffer[1], stop, 10);
				LCD_Position(1,10);
				LCD_PrString(lcdBuffer[1]);
				
				itoa(lcdBuffer[1], usDistance, 10);
				LCD_Position(1,0);
				LCD_PrString(lcdBuffer[1]);
				
			}
			
			if(timer8MainCount >= 99) {
				timer8MainCount = 0;
				usTrigSend();
			}
		}
	}
}
Exemple #2
0
void main(void)
{
	int result;
	float voltage;
	int status;
	
	M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts
	// Insert your main routine code here.
	
	//Start PGA in high power mode
	PGA_Start(PGA_HIGHPOWER);
	
	//Start ADCINC in high power mode
	ADCINC_Start(ADCINC_HIGHPOWER);
	
	//Start LCD
	LCD_Start();
	
	//Run the ADC continuously
	ADCINC_GetSamples(0);
	
	SleepTimer_Start();
   	SleepTimer_SetInterval(SleepTimer_1_HZ);
   	SleepTimer_EnableInt();
	
	while (1)
	{
		SleepTimer_SyncWait(1, SleepTimer_WAIT_RELOAD);
		
		// Wait for data to be ready
		while (ADCINC_fIsDataAvailable() == 0);
		
		// Get Data and clear flag
		result=ADCINC_iClearFlagGetData();
		voltage = result * SCALE_FACTOR;
		
		LCD_Position(0, 0);
		LCD_PrCString("                ");
		LCD_Position(0, 0);
		LCD_PrHexInt(result);
		
		LCD_Position(1, 0);
		LCD_PrCString("                ");
		LCD_Position(1, 0);
		LCD_PrString(ftoa(voltage, &status));
	}
}
Exemple #3
0
void main(void)
{
	//Variables
	char lcdFirstLine[LCD_LENGTH], lcdSecondLine[LCD_LENGTH];
	unsigned int displaymode = 3; // FIXME: sollte 0 sein!
	
	/** init **/
	
	// interrupt and SleepTimer init
	M8C_EnableGInt ;                            // Turn on interrupts
	SleepTimer_Start();
    SleepTimer_SetInterval(SleepTimer_8_HZ);    // Set interrupt to a
    SleepTimer_EnableInt();                     // 8 Hz rate
	
	// init PGA and SAR6 for sun and rain sensor
	PGA_sun_SetGain(PGA_sun_G5_33);  // gain of 5,33
    PGA_sun_Start(PGA_sun_HIGHPOWER);
	//PGA_rain_SetGain(PGA_rain_G8_00);
    //PGA_rain_Start(PGA_rain_MEDPOWER);
	SAR6_sun_Start(SAR6_sun_HIGHPOWER);
	//SAR6_rain_Start(SAR6_sun_MEDPOWER);

	// LCD init
	LCD_Init();
	
	// print welcome screen to LCD
	csprintf(lcdFirstLine,"   Welcome to   ");
	csprintf(lcdSecondLine, " Weatherstation ");
	LCD_Position(0,0);
	LCD_PrString(lcdFirstLine);
	LCD_Position(1,0);
	LCD_PrString(lcdSecondLine);
	
	while(1) {
		// get temp and humidity here
		
		switch(displaymode) {
			case 0:
				// overview();
				break;
				
			case 1:
				// temp();
				break;
				
			case 2:
				// humidity();
				break;
				
			case 3:
				sunsensor(lcdFirstLine, lcdSecondLine);
				break;
				
			case 4:
				// wind();
				break;
		
			default:
				displaymode = 0;
				csprintf(lcdFirstLine,"     Error      ");
				csprintf(lcdSecondLine,"                ");
		}
		
		// lets see what we've got
		LCD_Position(0,0);
		LCD_PrString(lcdFirstLine);
		LCD_Position(1,0);
		LCD_PrString(lcdSecondLine);
		
		// lets sleep for a while
		SleepTimer_SyncWait(8, SleepTimer_WAIT_RELOAD);
	}
}
Exemple #4
0
void main(void)
{

	//Variables
	char lcdFirstLine[LCD_LENGTH], lcdSecondLine[LCD_LENGTH];
	int displaymode = 1;
 	int temperature[5];
	int humidity[5];
	
	
	
	/** init **/
	
	// interrupt and SleepTimer init
	M8C_EnableGInt ;                            // Turn on interrupts
	SleepTimer_Start();
    SleepTimer_SetInterval(SleepTimer_8_HZ);    // Set interrupt to a
    SleepTimer_EnableInt();                     // 8 Hz rate

	// LCD init
	LCD_Init();
	
	// print welcome screen to LCD
	csprintf(lcdFirstLine,"   Welcome to   ");
	csprintf(lcdSecondLine, " Weatherstation ");
	LCD_Position(0,0);
	LCD_PrString(lcdFirstLine);
	LCD_Position(1,0);
	LCD_PrString(lcdSecondLine);


	while(1) {
	
		I2Cm_Start();						//Initialize I2C
		I2Cm_fSendStart( 0x28, 0);			//Send Measuring Request	
		measuring(temperature, humidity); 	//measuring temperature and humidity
	
		
		switch(displaymode) {
			case 0:
				// overview();
				break;
				
			case 1:
				printtemp(lcdFirstLine, lcdSecondLine, temperature); //write temp in the variable for the lcd
				break;
				
			case 2:
				printhum(lcdFirstLine, lcdSecondLine, humidity); //wirte humidity in the variable for the lcd
				break;
				
			case 3:
				// rain();
				break;
				
			case 4:
				// wind();
				break;
		
			default:
				displaymode = 0;
				csprintf(lcdFirstLine,"     Error      ");
				csprintf(lcdSecondLine,"                ");
		}
		
		// lets see what we've got
		LCD_Position(0,0);
		LCD_PrString(lcdFirstLine);
		LCD_Position(1,0);
		LCD_PrString(lcdSecondLine);
		
		// lets sleep for a while
		SleepTimer_SyncWait(8, SleepTimer_WAIT_RELOAD);
		
	}
	}
Exemple #5
0
void main(void)
{	
	M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts
	M8C_EnableIntMask(INT_MSK1, INT_MSK1_DBB01); // Enable DBB01 Interrupt for TempCounter
	M8C_EnableIntMask(INT_MSK1, INT_MSK1_DBB11); // Enable DBB01 Interrupt for MotorDriver
	M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO); // Enable GPIO interrupt for Tout
	
	// Start the UART(with no parity), LCD, TempCounter and MotorDriver
	UART_Start(UART_PARITY_NONE);
	LCD_Start();
	TempCounter_EnableInt(); // Enable interrupts for counter
	TempCounter_Start();
	MotorDriver_EnableInt(); // Enable interrupts for counter
	
	// Start I2CHW
	I2CHW_Start();
	I2CHW_EnableMstr();
	I2CHW_EnableInt();
	
	WriteI2C(slaveAddress, 0xAC, 1, 0x02); // Write to access config, sets mode to cooling(POL = 1), also turns 1-SHOT off, continuous conversions
	
	WriteI2C(slaveAddress, 0xA1, 2, (setTemp + tolerance), 0x00); // Sets initial high temp to be setTemp + tolerance
	WriteI2C(slaveAddress, 0xA2, 2, (setTemp - tolerance), 0x00); // Sets initial low temp to be setTemp - tolerance
	WriteI2C(slaveAddress, 0xEE, 0); // This tells the temperature IC to start converting the temperatures
	
	// Writes initial string to LCD. When LCD is updated, only the numbers will be changed
	LCD_Position(0,0); LCD_PrCString("CUR: 00 OFF     ");
	LCD_Position(1,0); LCD_PrCString("SET: 00 FAN OFF ");
	
	// This is the command usage string
	UART_CPutString("#################### Heating/Cooling Stepper Motors ##################\r\n\
#	S ##\r\n\
#		S - Set the desired Temperature\r\n\
#		## - Desired temperature in celsius\r\n\
#\r\n\
#	T ##\r\n\
#		T - Set the desired tolerance\r\n\
#		## - Desired tolerance in celsius\r\n\
#\r\n\
#	M X\r\n\
#		M - Change the mode of the thermostat\r\n\
#		X - C is for cool, H is for heat, F is for off\r\n\
#\r\n\
#	F X S\r\n\
#		F - Change the mode of the fan\r\n\
#		X - A is for automatic fan control, M is for always on\r\n\
#		S - Speed of the fan, H = high, M = medium, L = low\r\n\
#####################################################################\r\n");
	while (1)
	{
		char *cmd;
		char *params;
		
		if (GetLine(buf, &strPos, 79)) // Only process the data if GetLine returns true
		{
			cmd = Lowercase(cstrtok(buf, " ")); // Lowercase the first word from the inputted string
			
			if (strlen(cmd) == 1 && cmd[0] == 's') // If the person entered s
			{	
				int temp;
			
				params = cstrtok(0x00, " "); // Read next word 							
				// If next word isnt number or isnt 1 or 2 characters long, then return error
				if (!IsNumber(params) || strlen(params) < 1 || strlen(params) > 2 || csscanf(params, "%d", &temp) != 1) goto error;
				
				// If there is additional data at end of string or if number is not within 0-99, return error
				if (cstrtok(0x00, " ") != 0x00) goto error;
				if ( temp > 99 || temp < 0) goto error; 
				
				setTemp = temp;
				WriteI2C(slaveAddress, 0xA1, 2, (setTemp + tolerance), 0x00); // Sets high temp to be setTemp + tolerance
				WriteI2C(slaveAddress, 0xA2, 2, (setTemp - tolerance), 0x00); // Sets low temp to be setTemp - tolerance
				updateLCD = TRUE; // Update the LCD
			}
			else if (strlen(cmd) == 1 && cmd[0] == 't') // If the person entered t
			{	
				int tol; 
			
				params = cstrtok(0x00, " "); // Read next word					
				// If next word isnt number or isnt 1 or 2 characters long, then return error
				if (!IsNumber(params) || strlen(params) < 1 || strlen(params) > 2 || csscanf(params, "%d", &tol) != 1) goto error;
				
				// If there is additional data at end of string or if number is not within 0-10, return error
				if (cstrtok(0x00, " ") != 0x00) goto error;
				if (tol < 0 || tol > 10) goto error;
				
				tolerance = tol;
				
				WriteI2C(slaveAddress, 0xA1, 2, (setTemp + tolerance), 0x00); // Sets high temp to be setTemp + tolerance
				WriteI2C(slaveAddress, 0xA2, 2, (setTemp - tolerance), 0x00); // Sets low temp to be setTemp - tolerance
				updateLCD = TRUE; // Update the LCD
				
			}
			else if (strlen(cmd) == 1 && cmd[0] == 'm') // If the person entered m
			{	
				char mode;
			
				params = cstrtok(0x00, " "); // Read next word
				
				// If next word isnt 1 character long, return error
				if (strlen(params) != 1 || csscanf(params, "%c", &mode) != 1) goto error;
				// If there is additional data at end of string, return error
				if (cstrtok(0x00, " ") != 0x00) goto error;
				
				mode = tolower(mode); // Lowercase the character
				
				switch (mode)
				{
					case 'h':
						thermostatMode = 1; // Set mode to heating
						WriteI2C(slaveAddress,0xAC, 1, 0x00); // Change access config on DS1621 to heating(POL = 0)
						break;
						
					case 'c':
						thermostatMode = 2; // Set mode to cooling
						WriteI2C(slaveAddress, 0xAC, 1, 0x02); // Change access config on DS1621 to cooling(POL = 1)
						break;
						
					case 'f': 
						thermostatMode = 0; // Set mode to off
						break;
						
					default:
						goto error; // Invalid character entered, goto error
				}
				CheckFan(); // Check the fan to see if it should be on
			}
			else if (strlen(cmd) == 1 && cmd[0] == 'f') // If the person entered f
			{	
				char mode;
				char speed;
			
				params = cstrtok(0x00, " "); // Read next word
				// If next word isnt 1 character long, then return error
				if (strlen(params) != 1 || csscanf(params, "%c", &mode) != 1) goto error;
				
				params = cstrtok(0x00, " "); // Read next word
				// If next word isnt 1 character long, then return error
				if (strlen(params) != 1 || csscanf(params, "%c", &speed) != 1) goto error;
				// If there is additional data at end of string, return error
				if (cstrtok(0x00, " ") != 0x00) goto error;
				
				speed = tolower(speed); // Lowercase the speed and mode characters entered
				mode = tolower(mode);
				
				switch (mode)
				{
					case 'm':
						fanMode = 0; // Set fan mode to manual
						break;
						
					case 'a':
						fanMode = 1; // Set fan mode to automatic
						break;
						
					default: // Otherwise go to error
						goto error;
				}
				
				MotorDriver_Stop(); // Stop the motor to change the period values
				switch (speed)
				{
					case 'l':
						fanSpeed = 0; // Set fan speed to low
						MotorDriver_WritePeriod(49999); // See report for where these numbers came from
						MotorDriver_WriteCompareValue(25000);
						break;
						
					case 'm':
						fanSpeed = 1; // Set fan speed to medium
						MotorDriver_WritePeriod(9999); // See report for where these numbers came from
						MotorDriver_WriteCompareValue(5000);
						break;
						
					case 'h':
						fanSpeed = 2; // Set fan speed to high
						MotorDriver_WritePeriod(1999); // See report for where these numbers came from
						MotorDriver_WriteCompareValue(1000);
						break;
						
					default: // Otherwise go to error if invalid input entered
						goto error;
				}
				CheckFan(); // Check the fan to see if it should be on
			}
			else 
				goto error;
		}
			
		if (checkTemp) // Check the temperature
		{	
			char buf[2];
			
			ReadI2C(slaveAddress, 0xAA, 2, buf); // Read the temperature from IC, returns 2 bytes
			curTemp = buf[0]; // We just care about the first byte
			checkTemp = FALSE; // Turn flag off so it doesnt keep doing this
		}
		
		if (updateLCD) // Update the LCD
		{	
			char buf[3];
			
			NumToStr(buf, curTemp, 2); // Convert current temp to str
			LCD_Position(0, 5); LCD_PrString(buf); // Print it
			
			LCD_Position(0, 8);
			switch(thermostatMode) // Print thermostat mode
			{
				case 0: LCD_PrCString("OFF "); break;
				case 1: LCD_PrCString("HEAT"); break;
				case 2: LCD_PrCString("COOL"); break;
			}
			
			NumToStr(buf, setTemp, 2); // Convert set temp to str
			LCD_Position(1, 5); LCD_PrString(buf); // Print it
			
			LCD_Position(1, 12);
			if (fanMode == 1 && thermostatMode == 0) LCD_PrCString("OFF"); // Print current fan state
			else if (fanSpeed == 0) LCD_PrCString("LOW");
			else if (fanSpeed == 1) LCD_PrCString("MED");
			else if (fanSpeed == 2) LCD_PrCString("HI ");
			updateLCD = FALSE;
		}
		
		continue;
		error:	
			UART_CPutString("# Invalid format entered. Valid formats are:\r\n\
#	S ##\r\n\
#		S - Set the desired Temperature\r\n\
#		## - Desired temperature in celsius\r\n\
#\r\n\
#	T ##\r\n\
#		T - Set the desired tolerance\r\n\
#		## - Desired tolerance in celsius\r\n\
#\r\n\
#	M X\r\n\
#		M - Change the mode of the thermostat\r\n\
#		X - C is for cool, H is for heat, F is for off\r\n\
#\r\n\
#	F X S\r\n\
#		F - Change the mode of the fan\r\n\
#		X - A is for automatic fan control, M is for always on\r\n\
#		S - Speed of the fan, H = high, M = medium, L = low\r\n\
#####################################################################\r\n");
	}
}
Exemple #6
0
void main(void)
{
	unsigned long temp_ulong;
	INT temp_int, temp_int2;
	BYTE temp_byte;
	AMUX4_0_InputSelect(AMUX4_0_PORT0_1);        
   	AMUX4_1_InputSelect(AMUX4_1_PORT0_0);
   	INSAMP_Start(INSAMP_LOWPOWER); 
    ADCINC_Start(ADCINC_HIGHPOWER);      
   	DAC9_Ia_Start(DAC9_Ia_HIGHPOWER);
	DAC6_VGND_Start(DAC6_VGND_MEDPOWER);
	DAC6_VGND_WriteStall (31);
    PWM8_Vout_DisableInt();  
    PWM8_Vout_Start();     
    PWM8_Heater_DisableInt();  
    PWM8_Heater_Start();
	PWM8_NB_Out_DisableInt();  
    PWM8_NB_Out_Start(); 
	ADCINC_GetSamples(0);
	SleepTimer_Start();  
   	SleepTimer_SetInterval(SleepTimer_512_HZ);  
   	SleepTimer_EnableInt();   
	M8C_EnableGInt ;  
	LCD_Start();                  // Initialize LCD
	LCD_InitBG(LCD_SOLID_BG);

	while(1) {
		temp_ulong++;
		if ((ADC_IF & 1) == 1) {
			ADC_IF = ADC_IF & 254;
			Ri_Min = IIR_Int(Ri_Min_x1*2,Ri_Min,Ri_Filter_Strength);
			Ri_Delta = Ri_Max - Ri_Min;
			Ri_Mid = (Ri_Max + Ri_Min) / 2;
		}

		if ((ADC_IF & 2) == 2) {
			ADC_IF = ADC_IF & 253;
			Ri_Max = IIR_Int(Ri_Max_x1 * 2, Ri_Max, Ri_Filter_Strength);
			Ri_Delta = Ri_Max - Ri_Min;
			Ri_Mid = (Ri_Max + Ri_Min) / 2;
		}

		if ((ADC_IF & 4) == 4) {
			ADC_IF = ADC_IF & 251;
			ip = IIR_Int(ip_x1 * 2, ip, ip_Filter_Strength);
		}

		Ia_PID_Counter += Sleep_Counter;
		Heater_PID_Counter += Sleep_Counter;
		Heatup_Counter += Sleep_Counter;
		Vout_Lookup_Counter += Sleep_Counter;
		LCD_Counter += Sleep_Counter;
		Sleep_Counter = 0;

		if (Ia_PID_Counter > Ia_PID_Counter_Set) {
			Ia_PID_Counter = 0;
			Ia_PID();
		}

		if (Heater_PID_Counter > Heater_PID_Counter_Set) {
			Heater_PID_Counter = 0;
			Heater_PID();
		}

		if (Vout_Lookup_Counter > Vout_Lookup_Counter_Set) {}
			Vout_Lookup_Counter = 0;
			temp_int = ip - ip_to_Vout_Lookup_Start;
			if (temp_int < 0) {
				temp_int = 0;
			} else if (temp_int > (ip_to_Vout_Lookup_Size - 1)) {
				temp_int = (ip_to_Vout_Lookup_Size - 1);
			}
			PWM8_Vout_WritePulseWidth(ip_to_Vout_Lookup[temp_int]);
			
			#ifdef NB_Out
				temp_byte = 23;//0.45v
				if (ip < 251) { // 251 =0.9797787392968
					temp_byte = 46; //0.9v		
				} else if (ip > 259) { //259 = 1.02295956968912
					temp_byte = 0; //0v
				}
				PWM8_NB_Out_WritePulseWidth(temp_byte);
			#endif
		}

		if (LCD_Counter > LCD_Counter_Set) {
			LCD_Counter = 0;
			
			#ifdef LCD_Lambda_Text
				temp_int = ip - ip_to_Lambda_Lookup_Start;
				
				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (ip_to_Lambda_Lookup_Size - 1)) {
					temp_int=(ip_to_Lambda_Lookup_Size - 1);
				}

				Lambda_x100 = ip_to_Lambda_Lookup[temp_int];
				temp_int = Lambda_x100;
				LCD_Position(0,0);
				temp_int2 = temp_int / 100;
				Str1[9] = btoa(temp_int2);
				temp_int = temp_int - (100 * temp_int2);
				temp_int2 = temp_int / 10;
				Str1[11] = btoa(temp_int2);
				temp_int = temp_int - (10 * temp_int2);
				Str1[12] = btoa(temp_int);
				LCD_PrString(Str1);
			#endif

			#ifdef LCD_AFR_Text
				temp_int = ip - ip_to_Lambda_Lookup_Start;

				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (ip_to_Lambda_Lookup_Size - 1)) {
					temp_int = (ip_to_Lambda_Lookup_Size - 1);
				}

				Lambda_x100=ip_to_Lambda_Lookup[temp_int];
				temp_int = (INT) Lambda_x100 * 147;
				LCD_Position(0,0);
				temp_int2 = temp_int / 1000;
				Str1[6] = btoa(temp_int2);
				temp_int = temp_int - (1000 * temp_int2);
				temp_int2 = temp_int / 100;
				Str1[7] = btoa(temp_int2);
				temp_int = temp_int - (100 * temp_int2);
				temp_int2 = temp_int / 10;
				Str1[9] = btoa(temp_int2);
				temp_int = temp_int - (10 * temp_int2);
				Str1[10] = btoa(temp_int);
				LCD_PrString(Str1);
			#endif
			
			#ifdef LCD_Lambda_Graph
				temp_int = ip - ip_to_Lambda_Lookup_Start;
				
				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (ip_to_Lambda_Lookup_Size-1)) {
					temp_int = (ip_to_Lambda_Lookup_Size - 1);
				}

				Lambda_x100 = ip_to_Graph_Lookup[temp_int];
				LCD_DrawBG(0, 0, 16, Lambda_x100);
			#endif
			
			#ifdef LCD_Temperature_Text
				temp_int = Ri_Delta-Ri_Delta_to_Temperature_C_Start;

				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (Ri_Delta_to_Temperature_C_Size - 1)) {
					temp_int = (Ri_Delta_to_Temperature_C_Size - 1);
				}

				LSU_Temperature_C = Ri_Delta_to_Temperature_C[temp_int] + Ri_Delta_to_Temperature_C_Offset;
				temp_int = LSU_Temperature_C;
				LCD_Position(1,0);           
				temp_int2 = temp_int / 100;
				Str2[7] = btoa(temp_int2);
				temp_int = temp_int - (100 * temp_int2);
				temp_int2 = temp_int / 10;
				Str2[8] = btoa(temp_int2);
				temp_int = temp_int - (10 * temp_int2);
				Str2[9] = btoa(temp_int);
				LCD_PrString(Str2);
			#endif
			
			#ifdef LCD_Temperature_Graph
				temp_int = Ri_Delta - Ri_Delta_to_Temperature_C_Start;

				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (Ri_Delta_to_Temperature_C_Size - 1)) {
					temp_int = (Ri_Delta_to_Temperature_C_Size - 1);
				}

				LSU_Temperature_C = Ri_Delta_to_Graph[temp_int];
				LCD_DrawBG(1, 0, 16, LSU_Temperature_C);
			#endif
		}

		if (Heatup_Heater_Output < 255) {   
			if (Heatup_Counter > Heatup_Counter_Set) {
				Heatup_Counter = 0;
				Heatup_Heater_Output++;
			}

			if ((Ri_Min > 50) && (Ri_Max < 475) && (Ri_Delta < Ri_Delta_Target)) {
				Heatup_Heater_Output = 255;
				Ri_Delta_Error_Sum = 0;
			}
		}
	}