//------------------------------------------------------------------------------
// MAIN FUNCTION 
//------------------------------------------------------------------------------
void main (void)
{
	unsigned char index, voltage_out;

	initialize_system();

	printf("\n\rKeil Software, Inc.\n\r");	// Display starup message
	printf("MCB517 MCU I²C Example Test Program\n\r");
	printf("Version 1.0.0\n\r");
	printf("Copyright 2000 - Keil Software, Inc.\n\r");
	printf("All rights reserved.\n\n\r");

	printf("P8591 Test Program....Reading from ADC channel 0, Writing to DAC!\n\r");

	while (TRUE)
	{
		for (voltage_out = 0; voltage_out < 0xFF; voltage_out++)
		{

			write_dac(voltage_out);			// Write voltage value to DAC

											// Blink LEDs in sequence
			for (index = 0x01; index < 0x80; index <<=1)
			{
				P4 = index;
				delay_time(DELAY_BLINK);
			}
	  		for (index = 0x80; index > 0x01; index >>=1)
			{
				P4 = index;
				delay_time(DELAY_BLINK);
		   	}

											// Read voltage (ADC 0) and display results
   			printf("DAC output: %3bu     ADC Channel 0: %3bu\n\r", voltage_out, read_adc_channel(0x00));
		}
	}
}
예제 #2
0
/**
 * Returns true if the touchscreen was touched
 */
bool 
sense_touch_screen(
    uint_fast8_t tile_width,
    uint_fast8_t *tile_row_p,
    uint_fast8_t *tile_column_p)
{
#   define XDELTA_MAX	    16 //32 //100
#   define YDELTA_MAX	    12 //24 //100
#   define NUM_SAMPLES	    4 //4 //2
#   define RESOLUTION_BITS  10
 
#   define SETTLING_DELAY() delay_loop(10000)
    /*
     * Approx. 50000 CPU clock cycles. If The CPU clock frequency is 72MHz,
     * there are 72000 CPU clock cycles in 1 ms. So the settling delay is less
     * than 1 ms.
     */

    uint_fast16_t x_samples[NUM_SAMPLES*2];
    uint_fast16_t y_samples[NUM_SAMPLES*2];
    uint_fast16_t x_reading;
    uint_fast16_t y_reading;
    uint_fast8_t i;
    uint_fast16_t yvalue_min = 1023;
    uint_fast16_t yvalue_max = 0;
    uint_fast16_t xvalue_min = 1023;
    uint_fast16_t xvalue_max = 0;
    uint_fast16_t xdelta, ydelta;
    uint_fast16_t ts_max_value = 2 <<(RESOLUTION_BITS-1);

    for (i = 0; i < NUM_SAMPLES; i++)
    {
        /*
         * Power the "x-axis" layer of the touch panel, and use the "y-axis"
         * layer to measure voltage corresponding to the x-axis position:
         */

        touch_screen_polarize_X1_X2();
        select_input_pin_adc_channel(TOUCH_SCREEN_Y_ADC_CHANNEL);
        SETTLING_DELAY();

        x_samples[i*2] =
            read_adc_channel(g_adc_device_p, TOUCH_SCREEN_Y_ADC_CHANNEL);
        
        /*
         * Power the "y-axis" layer of the touch panel, and use the "x-axis"
         * layer to measure voltage corresponding to the y-axis position:
         */

        touch_screen_polarize_Y1_Y2();
        select_input_pin_adc_channel(TOUCH_SCREEN_X_ADC_CHANNEL);
        SETTLING_DELAY();

        y_samples[i*2] = 
            read_adc_channel(g_adc_device_p, TOUCH_SCREEN_X_ADC_CHANNEL);
        
        /*
         * Power the "x-axis" layer of the touch panel, and use the "y-axis"
         * layer to measure voltage corresponding to the x-axis position:
         */

        touch_screen_polarize_X2_X1();
        select_input_pin_adc_channel(TOUCH_SCREEN_Y_ADC_CHANNEL);
        SETTLING_DELAY();

        x_samples[(i*2)+1] = 
            read_adc_channel(g_adc_device_p, TOUCH_SCREEN_Y_ADC_CHANNEL);
        
        /*
         * Power the "y-axis" layer of the touch panel, and use the "x-axis"
         * layer to measure voltage corresponding to the y-axis position:
         */

        touch_screen_polarize_Y2_Y1();
        select_input_pin_adc_channel(TOUCH_SCREEN_X_ADC_CHANNEL);
        SETTLING_DELAY();

        y_samples[(i*2)+1] = 
            read_adc_channel(g_adc_device_p, TOUCH_SCREEN_X_ADC_CHANNEL);
    }
	
    x_reading = 0;
    y_reading = 0;
		
    for (i = 0; i < NUM_SAMPLES; i ++)
    {
        uint_fast16_t tempyval = ts_max_value - y_samples[i*2+1];
        uint_fast16_t tempxval = ts_max_value - x_samples[i*2+1];
        
        x_reading += x_samples[i*2];
        y_reading += y_samples[i*2];
            
        x_reading += (ts_max_value - x_samples[(i*2)+1]);
        y_reading += (ts_max_value - y_samples[(i*2)+1]);
        
        
        if (yvalue_min > y_samples[i*2])
        {
            yvalue_min = y_samples[i*2];
        }

        if (yvalue_max < y_samples[i*2])
        {
            yvalue_max = y_samples[i*2];
        }

        if (yvalue_min > tempyval)
        {
            yvalue_min = tempyval;
        }

        if (yvalue_min < tempyval)
        {
            yvalue_max = tempyval;
        }
        
        if (xvalue_min > x_samples[i*2])
        {
            xvalue_min = x_samples[i*2];
        }

        if (xvalue_max < x_samples[i*2])
        {
            xvalue_max = x_samples[i*2];
        }
        
        if (xvalue_min > tempxval)
        {
            xvalue_min = tempxval;
        }

        if (xvalue_min < tempxval)
        {
            xvalue_max = tempxval;
        }
    }
	
    x_reading /= NUM_SAMPLES*2;
    y_reading /= NUM_SAMPLES*2;

    xdelta = xvalue_max - xvalue_min;
    ydelta = yvalue_max - yvalue_min;
    
    if (x_reading < 1022 && y_reading > 1 &&
        xdelta < XDELTA_MAX && ydelta < YDELTA_MAX)
    {
#if 0 // XXX
        extern volatile uint32_t tile_cursor_index;
        extern volatile uint32_t same_tile_count;

        console_printf(
            "tile_cursor_index: %u, same_tile_count: %u, x_reading: %u, y_reading: %u\n",
            tile_cursor_index, same_tile_count, x_reading, y_reading);

        ATOMIC_POST_INCREMENT_UINT32(&same_tile_count);
#endif // XXX

        map_xy_reading_to_tile(
            x_reading, y_reading, tile_width, tile_row_p, tile_column_p);

        return true;
    }
    
    return false;
}