⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc.c

📁 此例程为将ADC转换的值通过开发版上的UART在超级终端显示出来。用的芯片是Atmel的AT91SAM7S系列
💻 C
字号:
/*********************************************************************************************
* File:	adc.c
* Author:	Embest z.j.zheng
* Desc:	test adc function,super terminal get the digital converted from adc through the uart
* History:	Embest z.j.zheng 2005,05,12
			Usart_init() was modified according to the Usart_init() in the project of 
			interrupt_test.
			Usart_init() in this project is modified as normal mode ,not inerrupt mode
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/*								include files												*/
/*------------------------------------------------------------------------------------------*/
#include "Board.h"	   
#include "AT91SAM7S64a.H"                       /* AT91SAMT7S64 definitions  */
#include "lib_AT91SAM7S64a.h"
/*------------------------------------------------------------------------------------------*/
/*								Global variable										 		*/
/*------------------------------------------------------------------------------------------*/
volatile char message[80];
AT91PS_USART COM0;

/*------------------------------------------------------------------------------------------*/
/*								constent definition											*/
/*------------------------------------------------------------------------------------------*/
#define USART_INTERRUPT_LEVEL		7
#define USART_BAUD_RATE				115200
            
/*********************************************************************************************
* name:		adc_init
* func:		adc init
* para:		none		
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void  adc_init()
{	
	//Hradware triggers are disabled. Starting a conversion is only possible by software
	//8-bit ,set adc clock,the startup time,sample and holding time.
	AT91F_ADC_CfgModeReg(AT91C_BASE_ADC,AT91C_ADC_TRGEN_DIS | AT91C_ADC_LOWRES_8_BIT | AT91C_ADC_SLEEP_NORMAL_MODE
					| AT91C_ADC_PRESCAL | AT91C_ADC_STARTUP | AT91C_ADC_SHTIM);
	AT91F_ADC_EnableChannel(AT91C_BASE_ADC,AT91C_ADC_CH4);
	AT91F_ADC_StartConversion(AT91C_BASE_ADC);
}

/*********************************************************************************************
* name:		Usart_init
* func:		USART initialization
* para:		none		
* ret:		none
* modify:	
* comment:		
*********************************************************************************************/
void Usart_init ( void )
{

	COM0= AT91C_BASE_US0;

        //* Define RXD and TXD as peripheral
        AT91F_US0_CfgPIO() ;

    	// First, enable the clock of the PIOB
    	AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<<AT91C_ID_US0 ) ;

	// Usart Configure
	AT91F_US_Configure (COM0, MCK,AT91C_US_ASYNC_MODE,USART_BAUD_RATE , 0);

	// Enable usart
	COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;


   //* ------ set time out US_RTOR
   //* Arm time out after 255 * 4  bits time
   //* for 115200 bit time = 1/115200 = 8.6 micro sec time out unuti= 4* 8.6 = 34 micro
   //*
	COM0->US_RTOR = 0xFFFF;

}

/*********************************************************************************************
* name:		AT91F_US_Printk
* func:		This function is used to send a string through the US channel
* para:				
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void AT91F_US_Printk( char *buffer) // \arg pointer to a string ending by \0
{
	while(*buffer != '\0') {
		while (!AT91F_US_TxReady(COM0));
		AT91F_US_PutChar(COM0, *buffer++);
	}
}

/*********************************************************************************************
* name:		time_dly
* func:		display code
* para:		dly			--in, delay value
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void  time_dly(int  dly)
{  
	int  i;
	for(; dly>0; dly--) 
		for(i=0; i<500; i++);
}

/*********************************************************************************************
* name:		Main
* func:		main fun
* para:		none
* ret:		none
* modify:
* comment:	
*********************************************************************************************/
int  main(void)
{
	int   j, AD_Data=0,AD_Data1;
	char nADC[3];
	adc_init();
	Usart_init();                                                

	AT91F_US_Printk("\n\r --ADC TEST--\n\r the data output of ADC is 8 bits .\n\r");
	AT91F_US_Printk("changing the input of ADC to get the changed 8-bit data. \n\r\n\r");	

	do 
	{
	AD_Data1 = AD_Data;
	time_dly(5000);	

	// generate a trigger signal by software
	AT91F_ADC_EnableChannel(AT91C_BASE_ADC,AT91C_ADC_CH4);
	AT91F_ADC_StartConversion(AT91C_BASE_ADC);              
	
	for(j=500;j>0;j--);                                         
	AD_Data = AT91F_ADC_GetConvertedDataCH4(AT91C_BASE_ADC);    //get the data after a 5short delay

		// the follow is to change the ADC into ASCII,that can be display on the super terminal
		if( AD_Data1-AD_Data>1||AD_Data1-AD_Data<-1)
		{
		j = AD_Data;
		j = ( j&0xf0 ) >> 4;
		if ((j >= 0 )&(j <= 9 ))
			nADC[0]=0x30+j;
		else nADC[0]=0x41+j-10;
		j = AD_Data;     
		j = j&0x0f;	
		if ((j >= 0 )&(j <=9 )) 
			nADC[1] = 0x30 + j;
		else nADC[1] = 0x41 + j - 10;			 	  			
		nADC[2]='\0';
		
		AT91F_US_Printk("\n\r the curent data output of ADC is:	0x");			
		AT91F_US_Printk(nADC);                                //print the data in ASCII form   
		}
	} while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -