/****************************************************************************** * Função: Tarefa5 * Descrição : Função da quinta tarefa * Parâmetros : nenhum * Retorno : nenhum *******************************************************************************/ void Tarefa5(void) { uint16_t LeituraADC; char LeituraADCString[10]; LeituraADC = S12ADC_read(); sprintf(LeituraADCString,"ADC: %04ld",LeituraADC); lcd_display(LCD_LINE6, LeituraADCString); }
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: cmt_callback_readADC * Description : This callback function is to be called from the CMT interrupt. * The function reads the ADC conversion value of the potentiometer * VR1 setting and the time period of the CMT counter is updated * with a new value proportional to that setting. Then the toggle_led * function is called. The result is that the LED toggle rate will * vary based on the pot setting. * Arguments : none * Return value : none *******************************************************************************/ static void cmt_callback_readADC(void) { uint16_t adc_value; cmt_stop(CHANNEL_1); /* Stop the CMT counting. */ /* Read the ADC conversion result. */ adc_value = S12ADC_read(); /* Recalculate the new timer period to make the flash period visible. */ /* Max ADC value is 4096 (12 bits). Timer count must not exceed 16 bit value. */ g_timer_count = (adc_value * 10)+ 2000; /* Scale the delay and insure non-zero value. */ /* Start new count on cmt channel 0. */ cmt_start(CHANNEL_1, g_timer_count); /* Execute the toggle LEDs function */ create_Obstacle(); } /* End of function cmt_callback_readADC(). */
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;}} } }