#include <SH79F1612A.H>
#include <intrins.h>
#include <config.h>
typedef unsigned char uint8;
typedef signed char int8;
typedef unsigned short uint16;
typedef signed short int16;
typedef unsigned long uint32;
typedef signed long int32;
uint16 adval;
void delay(uint16 i)
{
while(i--);
}
void Init_IO()
{
P4M0 = 0x00;
P4M1 = 0x04;
P3M0 = 0x00;
P3M1 = 0x04;
}
uint16 Sample_AD(uint8 ch,uint8 smdt)
{
uint16 adval;
if(smdt==0)
{
ADT=Bin(00000000); //ADʱÖÓΪ1¸öCLK ²ÉÑùʱ¼ä1¸öADʱÖÓ
}
else
{
ADT =Bin(11101111); //ADʱÖÓΪ12¸öCLK ²ÉÑùʱ¼ä3¸öADʱÖÓ
}
switch(ch)
{
case 0: //AN0
ADCH=Bin(00000001);
ADCON=Bin(10000001); //ʹÄÜAD0 Æô¶¯×ª»»
break;
case 1: //AN1
ADCH=Bin(00000010);
ADCON=Bin(10000011); //ʹÄÜAD1 Æô¶¯×ª»»
break;
case 2: //AN2
ADCH=Bin(00000100);
ADCON=Bin(10000101); //ʹÄÜAD2 Æô¶¯×ª»»
break;
case 3: //AN3
ADCH=Bin(00001000);
ADCON=Bin(10000111); //ʹÄÜAD3 Æô¶¯×ª»»
break;
case 4: //AN4
ADCH=Bin(00010000);
ADCON=Bin(10001001); //ʹÄÜAD4 Æô¶¯×ª»»
break;
case 5: //AN5
ADCH=Bin(00100000);
ADCON=Bin(10001011); //ʹÄÜAD5 Æô¶¯×ª»»
break;
case 6: //AN6
ADCH=Bin(01000000);
ADCON=Bin(10001101); //ʹÄÜAD6 Æô¶¯×ª»»
break;
case 7: //AN7
ADCH=Bin(10000000);
ADCON=Bin(10001111); //ʹÄÜAD7 Æô¶¯×ª»»
break;
default: //ζȴ«¸ÐÆ÷
break;
}
while((ADCON&Bin(01000000))==0)
{
_nop_();
}
adval=ADDH*4+ADDL;
ADCON=Bin(00000000); //¹Ø±ÕAD
return(adval);
}
/*********************************************************************************************
** º¯ÊýÃû³Æ: main
** º¯ÊýÃèÊö: Ö÷º¯Êý
** ÊäÈë²ÎÊý: ÎÞ
** Êä³ö²ÎÊý: ÎÞ
*********************************************************************************************/
void main()
{
P4_2 = 1;
Init_IO();
//Init_AD();
while(1)
{
Sample_AD(1,1);
//GetADCH_8();
//GetADCL_8();
//GetADC_16();
if(adval >1001001110)
{
P4_2 = 0;
P3_2 = 0;
delay(50000);
P3_2 = 1;
delay(50000);
}
else
{
P4_2 = 1;
}
}
} |