Beispiel #1
0
void InicializaHardware(void)
{
	S12ADC_init();
	S12ADC_start();
    lcd_initialize();
    lcd_clear();
}
Beispiel #2
0
void Callback_1ms() {
	/* Start the A/D converter */
	S12ADC_start();

	/*!!Safe check: stop timer count if sonar doesn't answer anymore */
	if (sonarGetState() == SONAR_ECHO){
		WDT_Increase(&sonarWDT);
	}

	/* Wait for the conversion to complete */
	while (false == S12ADC_conversion_complete()) {
	}
	/* Fetch the results from the ADC */
	analogRead = S12ADC_read();

}
/*******************************************************************************
* Function name: timer_adc
* Description  : The function configures a ADC unit and a timer unit. After each
*                timer period the callback function cmt_callback_readADC, is 
*                executed.
* Arguments    : none
* Return value : none
*******************************************************************************/
void timer_adc(void)
{
    /* Prepare the ADC unit for continuous conversion of pot VR1 value. */
    S12ADC_init(); 
    /* Start continuous ADC conversion. */
    S12ADC_start();
    
    /* Prepare a CMT channel as a periodic timer. */
    /* Initialize a CMT one-shot timer. */ 
    cmt_init();
    /* Set up the callback function on cmt channel 0 */   
    cmt_callback_set(CHANNEL_1, &cmt_callback_readADC);
    /* Start 100mS count on cmt channel 0. */
    cmt_start(CHANNEL_1, g_timer_count);
            
} /* End of function timer_adc(). */
void main(void)
{    
	//uint16_t adc_count;
	float adc_count;
    /* Initialize LCD */
    lcd_initialize();
	S12ADC_init();
    //UART initialization
	sci_uart_init();
	sci_tx_int_enable();
	sci_rx_int_enable();
	
	
    /* Clear LCD */
    lcd_clear();
    
    /* Display message on LCD */
    //Message will be on Position 1 and it will be stopped
    lcd_display(LCD_LINE1, " Mitch and  ");
	lcd_display(LCD_LINE2, "    Paul    ");
	lcd_display(LCD_LINE3, "Train Track ");
	lcd_display(LCD_LINE4, "  STOPPED   ");
	lcd_display(LCD_LINE5, "       _oo_");
	lcd_display(LCD_LINE6, " Pos1 |[][]|");
	lcd_display(LCD_LINE7, "      |O  O|");
	lcd_display(LCD_LINE8, "      |____|");

    /* 
        STDOUT is routed through the virtual console window tunneled through the JTAG debugger.
        Open the console window in HEW to see the output 
    */
    printf("This is the debug console\r\n");
    
    /* The three pushbuttons on the YRDK board are tied to interrupt lines, set them up here */
    R_SWITCHES_Init();
    
    //Intitalize all global variables
	count=0;
    Forward=0;
	Backword=0;
	Enable=0;
	
	//Character to input from putty
	char new_char;
	
	//Initialize Local variables
	int toggle=0;
	
	//Initialize all LED's starting at position 1
	LED4=LED_ON;
	LED5=LED_ON;
	LED6=LED_ON;
	LED7=LED_ON;
	LED8=LED_OFF;
	LED9=LED_OFF;
	LED10=LED_OFF;
	LED11=LED_OFF;
	LED12=LED_OFF;
	LED13=LED_OFF;
	LED14=LED_OFF;
	LED15=LED_OFF;
	
	while (1)
    {
		S12ADC_start();
		while(false==S12ADC_conversion_complete())
		{}
		new_char= sci_get_char();
		
		//Through the serial communication
		if(new_char =='F' ||new_char=='f')	//If inputted 'F' or 'f' move Forward
		{
			lcd_display(LCD_LINE4, "  FORWARD ");
			Forward=1;
			Enable=1;
			Backword=0;
			sci_put_string("Remote: Forward\n\r");
		}
		if(new_char =='S' ||new_char=='s')	//If inputted 'S' or 's' stop movement
		{
			lcd_display(LCD_LINE4, "  STOPPED   ");
			//Forward=0;
			Enable=0;
			//Backward=0;
			sci_put_string("Remote: Stop\n\r");

		}
		if(new_char =='R' ||new_char=='r')	//If inputted 'R' or 'r' move in reverse
		{
			lcd_display(LCD_LINE4, "  REVERSE  ");
			Forward=0;
			Enable=1;
			Backword=1;
			sci_put_string("Remote: Reverse\n\r");
		}
		
		
		
		adc_count= S12ADC_read();
		
		adc_count=adc_count/4095;
		int adccount=adc_count*100;
		char result[20];
		sprintf(result, "sp%3d%|____|", adccount);
		lcd_display(LCD_LINE8, (const uint8_t *) result);
	//	lcd_display(LCD_LINE8, adc_count);
		if(new_char=='D'||new_char=='d')	//If inputted 'D' or 'd' diplay the current speed
		{
			char speed[20];
			sprintf(speed,"Current Speed: %3d \n\r",adccount);
			sci_put_string(speed);
		}
		
		if(adc_count<=.01 & Enable==1)
		{
			lcd_display(LCD_LINE4, "  STOPPED   ");
			//Forward=0;
			Enable=0;
			//Backward=0;
			sci_put_string("Remote: Stop\n\r");
			while(adc_count<=0.05)
			{
				inloop=1;
				S12ADC_start();
				while(false==S12ADC_conversion_complete())
				{}
				adc_count= S12ADC_read();
				adc_count=adc_count/4095;
				
			}
			if(LastDir==1)
			{
				lcd_display(LCD_LINE4, "  FORWARD ");
				Forward=1;
				Enable=1;
				Backword=0;
				sci_put_string("Forward\n\r");
			}
			else if(LastDir==-1)
			{
				lcd_display(LCD_LINE4, "  REVERSE  ");
				Forward=0;
				Enable=1;
				Backword=1;
				sci_put_string("Reverse\n\r");
			}
			
			inloop=0;
		}
		else if(adc_count<=.25)
		{
			delayMS(1000);
		}
		else if(adc_count<=.5)
		{
			delayMS(500);
		}
		else if(adc_count<=.75)
		{
			delayMS(100);
		}		
		else if(adc_count<=1.0)
		{
			delayMS(10);
		}
		
		
		if(Enable==1)
		{
			trainMove();
			if(toggle==1 && Backword==1)
			{
				lcd_display(LCD_LINE5, "       _**_");
				lcd_display(LCD_LINE7, "      |*  *|");
				toggle=0;
			}
			else if(toggle==0 && Backword==1)
			{
				lcd_display(LCD_LINE5, "       _oo_");
				lcd_display(LCD_LINE7, "      |O  O|");
				toggle=1;
			}
			if(Forward==1)
			{
				lcd_display(LCD_LINE5, "       _oo_");
				lcd_display(LCD_LINE7, "      |*  *|");
			}
		}
		else
		{
			lcd_display(LCD_LINE5, "       _oo_");
			lcd_display(LCD_LINE7, "      |O  O|");
		}
		
		if(Forward==1 && Enable==1)	{count++; 	if(count==12){count=0;}}
		if(Backword==1 && Enable==1){count--;	if(count==-1){count=11;}}
    }
}