📄 adc_example.c
字号:
/***********************************************************************
* $Workfile: adc_example.c $
* $Revision: 1.1 $
* $Author: WellsK $
* $Date: Oct 28 2003 10:51:54 $
*
* Project: ADC driver example
*
* Description:
* A simple ADC driver example using polled mode.
*
* Revision History:
* $Log: //smaicnt2/pvcs/VM/sharpmcu/archives/sharpmcu/software/csps/lh7a404/bsps/sdk7a404/examples/adc_ts_polled/adc_example.c-arc $
*
* Rev 1.1 Oct 28 2003 10:51:54 WellsK
* Updated to use common LCD parameters file.
*
* Rev 1.0 Oct 15 2003 16:37:38 WellsK
* Initial revision.
*
*
***********************************************************************
* SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
* OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
* AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES,
* SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
*
* SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
* FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A
* SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
* FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
*
* COPYRIGHT (C) 2001 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
**********************************************************************/
#include "abl_types.h"
#include "abl_irq_fiq.h"
#include "abl_swim.h"
#include "abl_swim_font.h"
#include "abl_arm922t_cp15_driver.h"
#include "lh7a404_adc_driver.h"
#include "lh7a404_clcdc_driver.h"
#include "lh7a404_gpio_driver.h"
#include "sdk7a404_cpld_driver.h"
/* Pick only one display! */
#define LCD_DISPLAY sharp_lq035
//#define LCD_DISPLAY sharp_lq039
//#define LCD_DISPLAY sharp_lq057
//#define LCD_DISPLAY sharp_lq064
//#define LCD_DISPLAY sharp_lq104
//#define LCD_DISPLAY sharp_lq121
/* ADC and LCD device handles */
STATIC INT_32 adcdev, lcddev;
/* Sample enumeration type */
typedef enum {PENDOWN, XCOORD, YCOORD} SAMPLE_T;
/* Sample array */
UNS_16 samples[YCOORD + 1];
/* Physical address of LCD frame buffer */
#define FBPHY 0xC0000000
/***********************************************************************
*
* Function: make_hex_str
*
* Purpose: Convert a number to a hex string
*
* Processing:
* Using successive nibble computation, compute the base16 value of
* a number into a string and return the string value to the caller.
*
* Parameters:
* str : Where to place the generated string
* iteration: Number to generate a string from
*
* Outputs: None
*
* Returns: Nothing
*
* Notes: None
*
**********************************************************************/
void make_hex_str(CHAR *str,
UNS_16 iteration)
{
INT_32 cnt = 0;
UNS_8 li;
while (cnt < 4)
{
li = (UNS_8) ((iteration >> ((3 - cnt) * 4)) & 0xF);
if (li > 9)
{
str[cnt] = 'A' + li - 10;
}
else
{
str[cnt] = '0' + li;
}
cnt++;
}
str[cnt] = '\0';
}
void corv(unsigned short *x_res, unsigned short *y_res)
{
unsigned short x, y;
x=*x_res;
y=*y_res;
x=x/4.2;
y=y/3.2;
x=240-x;
//y=320-y;
if ((x<0 )||(x>240)) x=0;
if ((y<0)||(y>320)) y=0;
*x_res=x;
*y_res=y;
}
/***********************************************************************
*
* Function: c_entry
*
* Purpose: ADC example
*
* Processing:
* This example will configure the Touchscreen controller (ADC) and
* ADC driver to sample the pendown status, touchscreen X
* coordinate, and touchscreen Y coordinate in polling/software
* triggered mode. This mode is simple to implement in that it does
* not use interrupts and can be easily periodically performed from
* a timer interrupt or other periodic source.
*
* Parameters: None
*
* Outputs: None
*
* Returns: Always returns 1
*
* Notes:
* This example assumes that the touchscreen controller is
* configured as follows:
* Pendown status is read on AN0 using weak pullup FET B12
* AN0/AN1 drive X grid, X value is read on AN2
* AN2/AN3 drive Y grid, Y value is read on AN0
*
**********************************************************************/
void v_start(void)
{
INT_32 idx, del;
COLOR_T *fblog;
INT_32 ch[3];
ADC_CH_CONV_T chdata;
SWIM_WINDOW_T win1,win4;
CHAR str[32];
/* Disable interrupts */
disable_irq_fiq();
/* Initialize CPLD */
cpld_init();
/* Set virtual address of MMU table (needed for VIC driver
functions) */
//cp15_set_vmmu_addr((UNS_32 *) cp15_get_ttb());
/* Setup LCD muxing for all 16 data bits */
gpio_lcd_signal_select(GPIO_LCDV_0_15);
/* Setup LCD paramaters in the LCD controller */
lcddev = lcd_open(CLCDC, (INT_32) &LCD_DISPLAY);
/* Enable LCD controller and power signals */
lcd_ioctl(lcddev, LCD_PWENABLE, 1);
/* Also make sure that the CPLD_JTAG_OE signal is in the
correct state */
gpio_set_data_dir(GPIO_PORT_A, 0x04, GPIO_OUTPUT);
gpio_data_write(GPIO_PORT_A, 0x04);
gpio_set_data_dir(GPIO_PORT_H, 0xff, GPIO_OUTPUT);
gpio_data_write(GPIO_PORT_H, 0x0);
/* Set color depth to 16 bits per pixel */
lcd_ioctl(lcddev, LCD_SET_BPP, 16);
/* For displays that require more bandwidth, set DMA to request
a transfer on 4 words empty instead of the default 8. This may
help prevent 'display tearing' due to a starved LCD controller */
idx = lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_XSIZE) *
lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_YSIZE) *
sizeof (COLOR_T);
if (idx >= (800 * 600 * 2))
{
/* Displays of 800x600 pixels and 16-bits of color (or larger)
will use faster DMA requests */
lcd_ioctl(lcddev, LCD_DMA_ON_4MT, 1);
}
/* HRTFT/TFT panel board initialization only */
if ((lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == HRTFT) ||
(lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == ADTFT) ||
(lcd_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == TFT))
{
/* Enable power to the LCD panel (sets VDDEN on PC3) */
gpio_set_data_dir(GPIO_PORT_C, 0x08, GPIO_OUTPUT);
gpio_data_write(GPIO_PORT_C, 0x08);
}
else
{
/* Other displays - do nothing (yet) */
;
}
/* Set frame buffer address */
lcd_ioctl(lcddev, LCD_SET_UP_FB, FBPHY);
/* Get virtual address of frame buffer */
//fblog = cp15_map_physical_to_virtual(FBPHY);
fblog = (COLOR_T*)(FBPHY);
gpio_data_write(GPIO_PORT_H, 0xff);
/* Enable LCD controller and power signals */
lcd_ioctl(lcddev, LCD_PWENABLE, 1);
/* Create SWIM window for output data */
swim_window_open(&win1, LCD_DISPLAY.pixels_per_line,
LCD_DISPLAY.lines_per_panel, fblog, 0,
0, (LCD_DISPLAY.pixels_per_line - 1),
(LCD_DISPLAY.lines_per_panel - 1), 1, WHITE, BLACK, DARKGRAY);
swim_set_title(&win1, "Touchscreen coordinates", GREEN);
/* Open ADC */
if ((adcdev = adc_open(ADC, 0)) == 0x00000000)
{
return 0;
}
/* Reset the ADC and driver to a known 'clean' state. This isn't
needed directly after open, but is good practice */
adc_ioctl(adcdev, ADC_SET_STATE, ADC_MODE_RESET);
/* Set ADC clock speed to 400KHz */
adc_ioctl(adcdev, ADC_SET_CLOCK, 400000);
/* Setup idle channel (not used, but should be setup) */
chdata.settle_clks = 100;
chdata.inpc = ADC_ANALOG_1;
chdata.innc = ADC_REFN_VREFN;
chdata.vrefp = ADC_REFP_VREF;
chdata.vrefn = ADC_REFN_VREF;
chdata.bias = (ADC_FET_AN0_VDDA1K | ADC_FET_AN3_GND);
adc_ioctl(adcdev, ADC_SET_IDLE_CH, (INT_32) &chdata);
/* Pendown status channel */
chdata.settle_clks = 100;
chdata.inpc = ADC_ANALOG_1;
chdata.innc = ADC_REFN_VREFN;
chdata.vrefp = ADC_REFP_VREF;
chdata.vrefn = ADC_REFN_VREF;
chdata.bias = (ADC_FET_AN0_VDDA1K | ADC_FET_AN3_GND);
ch[0] = adc_ioctl(adcdev, ADC_ADD_CHANNEL, (INT_32) &chdata);
/* X coordinate channel */
chdata.settle_clks = 100;
chdata.inpc = ADC_ANALOG_0;
chdata.innc = ADC_REFN_VREFN;
chdata.vrefp = ADC_REFP_AN2;
chdata.vrefn = ADC_REFN_AN3;
chdata.bias = (ADC_FET_AN2_VDDA | ADC_FET_AN3_GND);
ch[1] = adc_ioctl(adcdev, ADC_ADD_CHANNEL, (INT_32) &chdata);
/* Y coordinate channel */
chdata.settle_clks = 100;
chdata.inpc = ADC_ANALOG_2;
chdata.innc = ADC_REFN_VREFN;
chdata.vrefp = ADC_REFP_AN0;
chdata.vrefn = ADC_REFN_AN1;
chdata.bias = (ADC_FET_AN0_VDDA | ADC_FET_AN1_GND);
ch[2] = adc_ioctl(adcdev, ADC_ADD_CHANNEL, (INT_32) &chdata);
/* Enable internal reference voltage */
adc_ioctl(adcdev, ADC_ENABLE_IREF, 1);
// adc_ioctl(adcdev,ADC_INT_DISABLE,0);
/* Clear the FIFO of any garbage */
adc_ioctl(adcdev, ADC_CLEAR_FIFO, 0);
/* Set sample mode to software triggered */
adc_ioctl(adcdev, ADC_SET_STATE, ADC_SW_TRIGGERED);
{
unsigned long *tmp_set;
tmp_set=0x80001314;
//tmp_set+=0x14;
*tmp_set=0;
}
/* Do samples */
idx = 0;
while (idx < 1000)
{
/* Trigger a software sample sequence */
adc_ioctl(adcdev, ADC_START_SAMP, 1);
/* Wait for 3 samples */
del = (INT_32) PENDOWN;
while (del <= (INT_32) YCOORD)
{
del = del + (adc_read_polled(adcdev, &samples[del],
sizeof(samples)) / 2);
}
/* Use SWIM to display output data only if touchscreen was
pressed */
if (ADC_READ_OUTPUT(samples[PENDOWN]) < 0x080)
{
unsigned short x_res,y_res;
swim_window_open(&win1, LCD_DISPLAY.pixels_per_line,
LCD_DISPLAY.lines_per_panel, fblog, 0,
0, (LCD_DISPLAY.pixels_per_line - 1),
(LCD_DISPLAY.lines_per_panel - 1), 1, WHITE, BLACK, DARKGRAY);
swim_set_title(&win1, "Touchscreen coordinates", GREEN);
swim_put_text_xy(&win1, "PD: ", 5, 5);
make_hex_str(str, ADC_READ_OUTPUT(samples[PENDOWN]));
swim_put_text(&win1, str);
swim_put_text(&win1, " -X: ");
make_hex_str(str, ADC_READ_OUTPUT(samples[XCOORD]));
swim_put_text(&win1, str);
swim_put_text(&win1, " -Y: ");
make_hex_str(str, ADC_READ_OUTPUT(samples[YCOORD]));
swim_put_text(&win1, str);
swim_put_text(&win1, " ");
adc_ioctl(adcdev, ADC_CLEAR_FIFO, 0);
#if 1
x_res=ADC_READ_OUTPUT(samples[XCOORD]);
y_res=ADC_READ_OUTPUT(samples[YCOORD]);
corv(&x_res,&y_res);
//x_res=200;
//y_res=300;
swim_window_open(&win4,
LCD_DISPLAY.pixels_per_line,LCD_DISPLAY.lines_per_panel,
fblog,
x_res,y_res,x_res+10,y_res+10,
0,
RED,RED, RED);
#endif
idx++;
}
else
{
swim_put_text_xy(&win1, "PD: ", 5, 5);
make_hex_str(str, ADC_READ_OUTPUT(samples[PENDOWN]));
swim_put_text(&win1, str);
swim_put_text(&win1, " ");
}
}
/* Close ADC and LCD */
adc_close(adcdev);
adc_close(lcddev);
do {}while(1);
return 1;
}
#ifndef __GNUC__
/* With ARM and GHS toolsets, the entry point is main() - this will
allow the linker to generate wrapper code to setup stacks, allocate
heap area, and initialize and copy code and data segments. For GNU
toolsets, the entry point is through __start() in the crt0_gnu.asm
file, and that startup code will setup stacks and data */
int main(void)
{
return c_entry();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -