STC12C5201AD AD采样+串口发送模板


 

#include<reg52.h>    



sfr	ADC_CONTR = 0xBC; //ADC control register

sfr ADC_RES    = 0xBD; //ADC 8-bit result register						  

sfr P1ASF      = 0x9D; //P1 secondary function control register



/* Define ADC operation const for ADC_CONTER */

#define ADC_POWER 0x80

#define ADC_FLAG  0x10

#define ADC_START 0x08

#define ADC_SPEEDLL 0x00

#define ADC_SPEEDL 0x20

#define ADC_SPEEDH 0x40

#define ADC_SPEEDHH 0x60 



unsigned char ADC_ch = 0;

void Delay(unsigned int n);



/*Initial ADC sfr*/

void initADC()

{

	P1ASF = 0xff; //Set All P1 as analog input port

	ADC_RES = 0;  //Clear previous result

	ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ADC_ch;

	Delay(2);     //ADC power-on delay and Start A/D conversion



	//IE = 0xa0;    //Enable ADC interrupt and Open master interrupt switch

}



  

void initSCI(void)  

{  

    SM0 = 0;      /*Step 1 :  确定串口通信方式*/    

    SM1 = 1;    

        

    TMOD = 0x20;  /*step 2: 根据串口通信方式 确定是否设置波特率*/     

    TH1  = 0xfd;    

    TL1  = 0xfd;    

        

    TR1  = 1;     /*step 3 :启动*/    

    REN  = 1;    

    

    EA = 1;       /*step 4: 是否启用中断函数*/    

    ES = 1;   

}  

  

void putChar (unsigned char chr)//发送一个字符  

{  

    SBUF = chr;  

    while(!TI); TI=0;                                                   

}  

  

void putStr(unsigned char str[], unsigned char n)  

{  

    while( n-- )  

    {  

        SBUF = str[n];  

        while(!TI); TI=0;  

    }  

}  



void Delay(unsigned int n)

{

	unsigned int x;

	while( n-- ){

		x = 5000;

		while(x--);

	}

}    





int main()    

{    

    initSCI();   

    initADC();

	    

    while(1)  

    {  

        //operation;    

    }      

    return 0;     

}  

  

/* ADC interrupt service routine */

void adc_isr() interrupt 5 using 1    

{

	ADC_CONTR &= !ADC_FLAG; // Clear ADC interrupt flag



	//SendData(ADC_ch);	  //Show Channel NO.

	//SendData(ADC_RES);  //Get ADC 8-bit result and Send to UART

	

	if(++ADC_ch > 7) ADC_ch = 0;  //switch to next channel

	ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ADC_ch;

}



void ser() interrupt 4    

{    

    if( RI )    

    {    

        RI = 0;    

        /*Operation SBUF*/    

    }    

    else if( TI )    

    {    

        TI = 0;     

    }    

    return ;    

}    


 

 

你可能感兴趣的:(模板)