C8051f320 ADC调试

C8051F320 ADC调试

记录一下调试心得:
1.包含模块:
ADC:AMX0P配置采样来源,AD转换时钟来源,特别需留意电压基准控制寄存器的相关配置(REF0CN),采用这部分数据类型需注意,用char类型有些地方容易溢出,折腾了好久。
uart,time等配置之前用到过,注意时钟分频。
C8051f320 ADC调试_第1张图片
C8051f320 ADC调试_第2张图片

2.ADC这里采用的是定时器0溢出中断,之前用到该中断都是转向中断服务程序,所以溢出标志位可以自动清0,给自己挖了个坑~~
但是在ADC转换中,通过溢出标志位启动转换,并未进入定时器中断的服务程序,所以该标志位需要手动清 0 !!!!

C8051f320 ADC调试_第3张图片

3.AD值的提取:F320是通过检测电压存储的10位2进制数,需要注意存储数据的对其方式。
2进制数到输出的ASCII之间的转换需留意(‘0’ =0x30),切不能混淆。

C8051f320 ADC调试_第4张图片

#include 

void init_function();
void interrupt_conf();
void adc_amux_select();
void adc_start_conf();
void uart_baud_gen();
void uart_port_conf();
void sys_osc_110592();
void uart_transmit(unsigned char xxx);
void uart_send_str(unsigned char* p);
void delay_ms(int x);
void adc_on();
void adc_off();

unsigned char uart_flag;
unsigned char adc_flag;
unsigned int adc;
float read;
unsigned int adchl[4];			//数据类型折腾了好久
unsigned int uart_xxx[4];
unsigned char temp;
unsigned char j,i;

sbit led1=P2^2;	
sbit led2=P2^3;

void main()
{
	init_function();
	
	/******** led config ************/
	P1MDOUT |= 0x0c; 			//配置led,检测程序运行状态
	P1SKIP |= 0x0c;
	/*******************************/
	adc_flag = 0;
	while(1)
	{	
		while(adc_flag != 0)
		{
			uart_send_str("*****************");
			uart_transmit('\n');	
			
			/****************/
			//2进制转换公式 
			//  V*adc/1024
				
			read = (3.3*adc*1000/1024-776.0)/2.86;	
			/***************/
			
			adchl[0] = read/1000;
			adchl[1] = ((int)read%1000)/100;
			adchl[2] = ((int)read%100)/10;
			adchl[3] = (int)read%10;

			for(j=0;j<4;j++)
			{
				uart_transmit(adchl[j]+0x30);
			}	
			uart_send_str(" C");
			uart_transmit('\n');
			
			
			uart_send_str("*****************");
			uart_transmit('\n');	
			adc_flag = 0;
		}
	}
}


/***************** interrupt service function ************/
void uart_irq() interrupt 4
{
	if(RI0 == 1)
	{
		RI0 = 0;
		temp = SBUF0;
		led1 = ~led1;
		switch(temp)
		{
			case '*':adc_on();
				uart_send_str("adc_on");
				uart_transmit('\n');
				break;
			default: adc_off();				
				uart_send_str("adc_off");
				uart_transmit('\n');
				break;
		}
		
	}
	else
	{
		TI0 = 0;
	}
}

void ADC0_IRQ() interrupt 10
{
	adc_flag = 0;
	led1 = ~led1;
	delay_ms(100);
	adc = (ADC0H << 8) + ADC0L;	
	adc_flag++;
	adc_off();
	uart_send_str("adc_off");
	uart_transmit('\n');

}

/**************** initialization function  ****************/
void init_function()
{
	PCA0MD &= 0x00;
	interrupt_conf();
	adc_amux_select();
	adc_start_conf();
	uart_baud_gen();
	uart_port_conf();
	sys_osc_110592();
}

/*********************************************************/
void delay_ms(int x)
{
	int i,j;
	for(i=1;i0; j--)
		{}
	}
}


/***************** adc on/off ************************/
void adc_on()
{
	TF0 = 1;
	TR0 = 1;
	EIE1 |= 0x08;
}

void adc_off()
{
	TF0 = 0;
	TR0 = 0;
	EIE1 &= 0x00;
}
/***************** interrupt config **********************/
void interrupt_conf()
{
	EA = 1;
	ES0 = 1;  				//uart
	PS0 = 1;   	
	EIE1 |= 0x08;			//	EADC0C = 1;		//ADC
	EIP1 &= 0x00;			//	PADC0 = 0;
}

/***************** adc amux select  **********************/
void adc_amux_select()
{
	// AMX0P |= 0x01;		//P1.1 正输入 选择
	 AMX0P |= 0x1e;			//内部温度传感器
	 
	/******* P1.1 port config ********/
	P1MDIN &= 0xfd;
	P1SKIP |= 0x02;
	/*********************************/
	
	AMX0N |= 0x1f;
	ADC0CF |= 0xf8;		//右对齐
	ADC0CN |= 0x81;		//Tim0溢出
	// REF0CN |= 0X0b;		//temperature off 
	REF0CN |= 0X0f;		//temperature on 
}

/***************** adc start config    *******************/
void adc_start_conf()
{
	TH0 = 0xc3;
	TL0 = 0x50;
	CKCON |=0x40; //0000 0100
	TCON |=0x10;
	TMOD |=0x01;
}


/***************** uart baud rate generate ****************/ 
void uart_baud_gen()
{
	TL1 = 0xcc;
	TH1 = 0xcc;		//12M 115200 Baud
	TCON |= 0x40;	//0100 0000;
	TMOD |= 0x20;	//0010 0000;
	CKCON |= 0x08;	//0000 1000;
}

/***************** uart port config ***********************/ 
void uart_port_conf()
{
	S0MODE = 0;
	REN0 = 1;
	XBR0 |= 0x01;		//P0.4  P0.5 输入
	XBR1 |= 0x40;
}

/***************** system clock config ********************/
void sys_osc_110592()
{
	OSCICN |= 0x83;    	// 配置内部晶振频率为12m
	RSTSRC  = 0x04;  	// 开启丢失时钟检测
						// CLKREC = 0x09;
	// OSCICL = 0x0e; 		//校准 11.0592    目前配置内部12MHz,11.0592MHz不能启用,怀疑始终恢复电路1被使能,CLKREC位值无法确定
	while ((OSCICN & 0x40) == 0);	//输出稳定
}

/****************** 51---computer uart communication ******************/
//transfer a single character
void uart_transmit(unsigned char xxx)		
{
	ES0 = 0;
	// EA = 0;
	SBUF0 = xxx;
	while (TI0 == 0);
	TI0 = 0;
	ES0 = 1;
	// EA = 1;
}

//transfer string
void uart_send_str(unsigned char* p)				
{
	while(*p)
	{
		uart_transmit(*p++);
	}
}

你可能感兴趣的:(单片机C8051)