#include "2440.h"
#include <stdarg.h>
#include <stdio.h> // vsprintf defined
void Port_Init(void)
{
//GPIO GPH2->TXD0 GPH3->RXD0
rGPHCON |= (0xa<<4);
rGPHUP |= (0x3<<2); // The pull up function is disabled GPH[10:0
}
void Delay(int val)
{
int i;
for(i=0;i<val;i++);
}
void ChangeClockDivider(int hdivn,int pdivn)
{
// hdivn,pdivn FCLK:HCLK:PCLK
// 0,0 1:1:1
// 1,1 1:2:4
// 3,1 1:3:6
// 2,1 1:4:8
rCLKDIVN = (hdivn<<1) | pdivn;
}
//*************************[ MPLL ]*******************************
void ChangeMPllValue(int mdiv,int pdiv,int sdiv)
{
rMPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
}
//*************************[ UART ]*******************************
void Uart_TxEmpty(int ch)
{
if(ch==0)
while(!(rUTRSTAT0 & 0x4)); //Wait until tx shifter is empty.
}
static int whichUart=0;
void Uart_Init(int ch,int pclk,int baud)
{
whichUart = ch;
if(pclk == 0)
pclk = PCLK;
rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable
rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable
rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
rUCON0 = 0x245; // Control register
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); //Baud rate divisior register 0
Uart_TxEmpty(whichUart);
}
//=====================================================================
void Uart_SendByte(int data)
{
if(whichUart==0)
{
if(data=='\n')
{
while(!(rUTRSTAT0 & 0x2));
Delay(0x10000); //because the slow response of hyper_terminal
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
Delay(0x10000);
WrUTXH0(data);
}
}
void Uart_SendString(char *pt)
{
while(*pt)
Uart_SendByte(*pt++);
}
void Uart_Printf(const char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}
//---------------------------------------->
void TS_ISR()
{
// Uart_Printf("This is TS_ISR\n");
/*--------clear interrupt----------*/
rINTPND = rINTPND;
rSRCPND |= (0x1<<31);
rSUBSRCPND |= (0x1<<9);
/*---------------------------------*/
rADCTSC = 0x0c;//auto x/y mode
rADCCON |= (0x1<<0); //start adc
}
void ADC_ISR()
{
int datx, daty;
// Uart_Printf("This is ADC_ISR\n");
/*--------clear interrupt----------*/
rINTPND = rINTPND;
rSRCPND |= (0x1<<31);
rSUBSRCPND |= (0x1<<10);
/*------------read data--------------*/
datx = (unsigned int)(rADCDAT0 & (0x3FF));
daty = (unsigned int)(rADCDAT1 & (0x3FF));
/*----------------------------------*/
Uart_Printf("x=%d, y=%d\n",datx,daty);
rADCTSC = 0xD3;//waiting mode
}
__irq void ADC_TS_ISR(void)
{
if(rSUBSRCPND & (1<<9))//TS_ISR
TS_ISR();
if(rSUBSRCPND & (1<<10))//ADC_ISR
ADC_ISR();
}
void ADC_init(void)
{
//ADCCON[14]=1 ---> f
rADCCON |= (0x1 << 14);
//ADCCON[13:6]=44 --->300KSPS
rADCCON &= ~(0xff << 6);
rADCCON |= (44 << 6);
//ADCCON[5:3]=2 -->CHANNEL
rADCCON |= (2 << 3);
/*delay */
rADCDLY = 100000;
/*narmal mode*/
rADCCON &= ~(0x1<<2);
/*waiting mode*/
rADCTSC = 0xD3;
}
void INT_init()
{
rINTMSK &=~(0x1<<31); //INT_ADC
rINTSUBMSK &=~(0x1<<10); //INT_ADC_S
rINTSUBMSK &=~(0x1<<9); //INT_TC
pISR_ADC = (unsigned int)ADC_TS_ISR;
}
void xmain(void)
{
ChangeClockDivider(3,1);
ChangeMPllValue(127,2,1);
Port_Init();
Uart_Init(0, 0, 115200);
Uart_Printf("the main is running\n");
INT_init();
ADC_init();
while(1);
}
裸机触摸屏定位程序
最新推荐文章于 2024-11-10 13:00:44 发布