Exemple #1
0
void main(void)
{
	// Enable Global interrupts (see m8c.h)
	M8C_EnableGInt;
	// Enable GPIO Interrupts (see m8c.h)
	M8C_EnableIntMask(INT_MSK0,INT_MSK0_GPIO);
	
	// Spin here forever
	while(1);
}
void Encoder_Init(void)
{
	encoderCurrentCount.left  = 0;
	encoderCurrentCount.right = 0;
	
	_previousQuadratureStateLeft = _quadrature_left();   // Initialize left encoder state machine
	_previousQuadratureStateRight = _quadrature_right(); // Initialize right encoder state machine

	M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO);         // Enable GPIO interrupts
}
Exemple #3
0
void InitDaisyLink(BYTE *I2CRAM, BYTE manufacturer, BYTE type, BYTE moduleVersion, int ramsize)
{

	// DaisyLink
	DL_Reg.ModuleType    = type;
	DL_Reg.ModuleVersion = moduleVersion;
	DL_Reg.DLVersion     = 4;
	DL_Reg.Manufacturer  = manufacturer;
		
	// Initialize I2C module
	EzI2Cs_SetRamBuffer(ramsize, ramsize,(BYTE *) I2CRAM);
	EzI2Cs_Start();
	
	// enable interrupts
	PRT1IC0 = PIN_DOWNSTREAM;
	PRT1IC1 = PIN_DOWNSTREAM;						// Set downstream pin interrupt type to "change from last read"
	M8C_EnableIntMask( INT_MSK0, INT_MSK0_GPIO );	// Allow GPIO interrupts (but leave interrupt for actual pin disabled until Active state)
	M8C_EnableIntMask( INT_MSK0, INT_MSK0_VC3);		// Enable timer interrupts
	M8C_EnableGInt;									// Enable global interrupts
		
	ResetToInitState();
}
Exemple #4
0
void setup(void) {
	int moi = 0;
	M8C_EnableGInt;
	M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO);
	LCD_Init();
	LCD_Start();
	Timer8UsTrig_EnableInt();
	Timer8UsTrig_Start();
	Timer8Main_EnableInt();
	Timer8Main_Start();
	
	Timer16UsEcho_EnableInt();
	backlight(1);
	for (moi = 0; moi < 9999; moi++);
}
Exemple #5
0
void main(void){
    M8C_EnableGInt;
    M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO);

    I2CHW_Start();
    I2CHW_EnableSlave();
    I2CHW_EnableInt();
    
    AMUX4_Start();
    PGA_1_Start(PGA_1_HIGHPOWER);
    ADCINC_Start(ADCINC_HIGHPOWER);
    TX8_Start(TX8_PARITY_NONE);
    
    LED_DBG_ON();
    TX8_CPutString("I2C slave addr:0x");
    TX8_PutSHexByte(I2CHW_SLAVE_ADDR);
    TX8_PutCRLF();
    LED_DBG_OFF();

    for(;;){
        // I2C
        i2c_status = I2CHW_bReadI2CStatus();
        if(i2c_status & I2CHW_WR_COMPLETE){ // master->slave
            I2CHW_ClrWrStatus();
            I2CHW_InitWrite(buf_rx, BUF_SIZE);
        }
        if(i2c_status & I2CHW_RD_COMPLETE){ // slave->master
            I2CHW_ClrRdStatus();
            I2CHW_InitRamRead(buf_tx, BUF_SIZE);
        }

        // ADC
        for(ad_pin = 0; ad_pin < 4; ad_pin++){
            ad = get_adc(ad_pin);
            weights[ad_pin] = ad;
            TX8_PutChar(ad_pin+'0');
            TX8_CPutString(":");
            TX8_PutString(intToStr(ad,buf));
            TX8_PutCRLF();
        }
        buf_tx[0] = 'a';
        buf_tx[1] = 'b';
        buf_tx[2] = 'c';
        buf_tx[3] = 'd';
    }
}
Exemple #6
0
void main()
{
	configured = 0;			// This integer keeps track of whether or not we have received a valid response.
	//PRT1DR |= 0b00000001;	// Turn the LED off (active low).
	M8C_EnableGInt;			// Turn on global interrupts for the transmission timeout timer.
	
	M8C_EnableIntMask(INT_MSK0,INT_MSK0_GPIO); //activate GPIO ISR
	
	// If this controller is the master, start out in RX mode.
	// Otherwise, this controller is a slave and starts in TX mode.
	if(MASTER)
	{
		commToggle(RX_MODE);	// Toggle into RX mode and wait for a request.
		
		while(!configured)
		{
			if(RECEIVE_cGetChar() == 'M')
			{
				if(RECEIVE_cGetChar() == 'a')
				{
					if(RECEIVE_cGetChar() == 'r')
					{
						if(RECEIVE_cGetChar() == 'c')
						{
							if(RECEIVE_cGetChar() == 'o')
							{
								commToggle(TX_MODE);	// Switch to TX mode.
								
								// Transmit the response.
								TRANSMIT_PutChar('P');
								TRANSMIT_PutChar('o');
								TRANSMIT_PutChar('l');
								TRANSMIT_PutChar('o');
								
								//configured = 1;
								xmitWait();				// Wait for the transmission to finish.
								commToggle(RX_MODE);	// Switch back to receive mode.
							}
						}
					}
				}
			}
		}
	}
	else if(!MASTER)
	{
		// Start response timeout timer and enable its interrupt routine.
		// This will wait one timeout period, and transmit the initial message.
		commToggle(RX_MODE);
		
		while(!configured)
		{
			if(RECEIVE_cGetChar() == 'P')
			{
				if(RECEIVE_cGetChar() == 'o')
				{
					if(RECEIVE_cGetChar() == 'l')
					{
						if(RECEIVE_cGetChar() == 'o')
						{
							// Start response timeout timer and enable its interrupt routine.
							//Timer8_1_DisableInt();
							//Timer8_1_Stop();
							
							// This module is configured, so the next module can now be heard.
							//configured = 1;
							
							// Turn on relay-like object.
						}
					}
				}
			}
		}
	}
	
	//PRT1DR &= 0b11111110;		// Turn the LED on (active low).
	
	while(1)
	{
		// Sit and spin for now, but eventually this is where we will connect to the bus
		// and eavesdrop for more commands.
	}
}
Exemple #7
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");
	}
}