/*******************************************************************************************************
* 文件名:main.c
* 功 能:底层驱动模块(触摸屏)
* 作 者:Andy Jiang
* 日 期:2008年12月23号
|
| main.c
| by Andy jiang
| 2008.12.123
|
| CS8920 software group
| www.midoriya.co.jp
|
********************************************************************************************************/
#include "config.h"
#define baud_rate 1200
#define ADC0 18
#define ADC1 21
#define UART0 6
#define UART1 7
#define TIMER0 4
#define EXTRN 14
#define TIMER1 5
#define RTC 13
uint32 adresult;
uint32 voltage1;
uint32 voltage2;
//uint8 a[100]={0};
uint8 send_counter;
uint32 state;
/************************************************************/
/************function prototypes*****************************/
/************************************************************/
void EXTRN_Init(void);
void Port_Init( void );
void Timer0_Init( void );
void UART1_Init( void );
void UART0_Init( void );
void RTCI_Init( void );
void UART0_Sendbyte( uint8 sendbyte);
void UART0_Senddata( uint8 send_bytes, uint8 *buffer );
uint8 UART0_Recebyte( void );
void UART0_Recdata( uint8 rece_bytes, uint8 *buffer );
void UART1_Senddata(uint8 send_bytes, uint8 *buffer );
void Timer0_Int( void ) __irq ;
void ADC0_Int ( void ) __irq ;
void Uart0_Int ( void ) __irq ;
void Uart1_Int ( void ) __irq ;
void EXTRN_Int ( void ) __irq ;
void RTC_Int ( void ) __irq ;
/************************************************************/
/***********************serial0 driver***********************/
/************************************************************/
void RTC_Int ( void ) __irq
{
VICVectAddr=0xFF;
}
void ADC0_Int( ) __irq //AD0 i nterrupt service programme
{
VICVectAddr=0xFF;
}
void ADC1_Int( ) __irq //AD1 interrupt service programme
{
VICVectAddr=0xFF;
}
void EXTRN_Int( ) __irq //EXTRN interrupt service programme
{
IO0SET=0X00;
while((EXTINT&0X01)!=0X01)
{
EXTINT=0X01;
}
EXTINT=0X01;
VICVectAddr=0xFF;
}
void Timer0_Int( ) __irq //Timer0 interrupt service programme
{
T0IR=0X01;
VICVectAddr=0xFF;
}
void Uart0_Int( ) __irq //UART0 interrupt service programme
{
VICVectAddr = 0xFF;
}
void Uart1_Int( ) __irq //UART1 interrupt service programme
{
VICVectAddr = 0xFF;
}
void Port_Init( void ) // Port initialize
{
PINSEL0=0X00;
PINSEL1=0X00000001;
// PINSEL2=0X00; //BE CARE FOR THIS CODE!!!!!!!
IO0DIR= 0X00;
IO1DIR= 0X00;
IO2DIR= 0X00;
IO3DIR= 0X00;
IO0SET= 0X00;
IO0CLR= 0X00;
IO1SET= 0X00;
IO1CLR= 0X00;
EXTMODE= 0X00;
}
void Timer0_Init( void)
{
T0TC= 0;
T0PR= 0;
T0MCR= 0X03;
T0MR0= Fpclk/2;
T0TCR= 0X01;
VICIntSelect= 0x00000000; /*dispatch as IRQ */
VICVectCntl3= 0x20|TIMER0; /*Timer0 as IRQ slot1 */
VICVectAddr3= (uint32)Timer0_Int; /*get timer0 inter service */
VICIntEnable= 1<<TIMER0; /*timer0 Enable */
}
void UART0_Init( void )
{
PINSEL0= PINSEL0&(~0x0F)|0x05; /* Enable RxD0 and TxD0 */
U0LCR= 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL= 625%256; /* 1200 Baud Rate @ 12MHz VPB Clock */
U0DLM= 625/256;
U0LCR= 0x03; /*LOCK*/
U0IER= 0X07;
// U0LCR=0X81; /*INT ENABLE*/
VICVectCntl0= 0x20|UART0; /*UART0 as IRQ slot0*/
VICVectAddr0= (uint32)Uart0_Int; /*get UART0 inter service*/
VICIntEnable= 1<<UART0;
}
void UART1_Init( void )
{
PINSEL0= PINSEL0&(~(0x0F<<16))|(0x05<<16); /* Enable RxD1 and TxD1 */
U1LCR= 0x83; /* 8 bits, no Parity, 1 Stop bit */
U1DLL= 65535%256; /* 1200 Baud Rate @ 12MHz VPB Clock */
U1DLM= 65535/256;
U1LCR= 0x03;
U1IER= 0X03;
VICVectCntl0= 0x20|UART1; /*UART0 as IRQ slot0*/
VICVectAddr2= (uint32)Uart1_Int; /*get UART0 inter service*/
VICIntEnable= 1<<UART1;
}
uint16 ADC0_Init( void)
{
uint16 adresult;
VICIntSelect=0x00000000; //dispatch as IRQ
VICVectCntl0=0x20|ADC0; //AD0 as IRQ slot0
VICVectAddr0=(uint32)ADC0_Int; //get ad inter service
VICIntEnable=1<<ADC0;
return (adresult);
}
uint16 ADC1_Init( void)
{
uint16 adresult;
VICIntSelect=0x00000000; //dispatch as IRQ
VICVectCntl0=0x20|ADC1; //AD0 as IRQ slot0
VICVectAddr0=(uint32)ADC1_Int; //get ad inter service
VICIntEnable=1<<ADC1;
return (adresult);
}
void EXTRN_Init( void)
{
VICIntSelect=0x00000000; //dispatch as IRQ
PINSEL0=0X00000001;
VICVectCntl0=0x20|EXTRN; //EXT0 as IRQ slot0
VICVectAddr0=(uint32)EXTRN_Int; //get ad inter service
VICIntEnable=1<<EXTRN;
}
void UART0_Sendbyte(uint8 sendbyte)
{
U0THR= sendbyte;
while( U0LSR&0X40== 0);
}
void RTC_Init( void)
{
PREINT=Fpclk/32768-1;
PREFRAC=Fpclk-(Fpclk/32768)*32768;
CIIR=0X01;
VICIntSelect= 0x00000000; /*dispatch as IRQ */
VICVectCntl3= 0x20|RTC; /*Timer0 as IRQ slot1 */
VICVectAddr3= (uint32)RTC_Int; /*get timer0 inter service */
VICIntEnable= 1<<RTC; /*timer0 Enable */
}
void UART0_Senddata(uint8 send_bytes, uint8 *buffer)
{
uint8 i ;
for(i=0; i<send_bytes; i++)
{
U0THR=*buffer;
buffer++;
while( U0LSR&0x40== 0);
}
}
uint8 UART0_Recebyte( void )
{
uint8 Recebyte;
while(!(U0LSR&0X01));
Recebyte=U0RBR;
return (Recebyte);
}
void UART0_Recdata( uint8 rece_bytes,uint8 *buffer)
{
uint8 i ;
for(i=0; i<rece_bytes; i++)
{
while(!(U0LSR&0X01));
*buffer=U0RBR;
buffer++;
}
}
/************************************************************/
/***********************serial1 driver***********************/
/************************************************************/
void UART1_Senddata(uint8 send_bytes, uint8 *buffer)
{
uint8 i ;
for(i=0;i<send_bytes;i++)
{
U1THR=*buffer;
buffer++;
while( U1LSR&0x40==0);
}
}
void UART1_Recdata( uint8 rece_bytes,uint8 *buffer)
{
uint8 i ;
for(i=0;i<rece_bytes;i++)
{
while(!(U1LSR&0X01));
*buffer=U1RBR;
buffer++;
}
}
void delayms(uint8 time)
{
uint16 i;
uint16 j;
uint8 k;
for(i=0;i<=time;i++)
{
for(j=0;j<=10000;j++)
{
for(k=0;k<=10000 ;k++)
{
;
}
}
}
}
void touchgetx( void )
{
uint8 adtime;
IOSET=IOSET|0X02;
IO0CLR=IO0CLR|0X02;
delayms(10);
for( adtime=0;adtime<=20;adtime++)
;
}
/**********************************************************/
/***************中断服务程序*******************************/
/**********************************************************/
/***************************************************************/
/************************主程序*********************************/
/***************************************************************/
int main ( )
{
uint8 volt_high,volt_low,volt_midd;
// IRQEnable( ); //IRQ enable
Port_Init( );
Timer0_Init( );
UART0_Init( );
UART1_Init( );
ADC0_Init( );
ADC1_Init( );
// while(1) ;
// UART0_Init( );
// while(1)
// {
// UART0_Sendbyte(0x30);
// while(1);
// }
/***************************************************************/
/************************AD SUB PROG****************************/
/***************************************************************/
// PINSEL1=PINSEL1&(~(0X03<<26))|(0X01<<26); //pin mapped as ad in
// AD0CR=(1<<2) | //channel select
// (0XFF<<8) | //convert clock frequency
// (0<<16) | //software converter
// (0<<17) | //clks=0,,11clock con
// (1<<21) | //pdn=1;
// (0<<22) | //test1:0=00
// (1<<24) | //start=1
// (0<<27);
// while(1);
// while((ADDR&0X80000000)==0);
// adresult=(ADDR>>6)&0x3ff;
while(1)
{
// ADCR|=(1<<24) ;
// while((AD0DR&0X80000000)==0);
PINSEL1=0X00000001;
// EXTMODE= 0X00;
// VICIntSelect=0x00000000; //dispatch as IRQ
// VICVectCntl0=0x20|14; //AD0 as IRQ slot0
// VICVectAddr0=(uint32)ADC0_Int; //get ad inter service
// VICIntEnable=1<<14;
// EXTINT=0X01;
adresult=(ADDR>>6)&0x3ff;
voltage1=adresult*330/1023;
volt_high=voltage1/100;
volt_midd=voltage1%100/10;
volt_low=voltage1%10;
voltage2=(volt_high<<8)|(volt_midd<<4)|volt_low;
//EXTINT=0X01;
}
//return 0;
}