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

📄 cd_ad.c

📁 Keil ,LPC932系列环境下开发的频率测量单片机程序,用定时器来计脉冲数从而得出频率.
💻 C
字号:
#include "reg922.h"
#include "stdio.h"
#include "stdlib.h"
#include "cd_ad.h"
#include "stdarg.h"

//cclk=osc/2 = 7.3728M/2 = 3.6864 M;	PCLK = CCLK/2 = 1.8432M 
#define STAND_RC_OSC 1843200UL 
#define RC_OSC 1866800UL

void init_para()
{
	count  = 0 ; 
	Frequence = 0 ;
}

//初始化系统时钟,内部RC振荡器
void init_clock()
{
	DIVM=1;				//cclk=osc/2 = 7.3728M/2 = 3.6864 M;	PCLK = CCLK/2 = 1.8432M ;
	TRIM |= 0x40 ;
}

void init_io()
{
   	PT0AD=0x3E;			//P0.1--P0.5 数字输入禁能
	P0= 0xFF;

	P0M1= 0x3e;			//P0.0,P0.6,P0.7 准双向 00110110
	P0M2= 0x00;

	P1M1=0; 			//P1准双向
	P1M2=0;

	P3M1=0;				//P3准双向
	P3M2=0;

	P1M1 |= 0x0c; //SCL SDA开漏 
	P1M2 |= 0x0c;
}

void init_uart()
{
//	AUXR1 |= 0x40; 		//enable reset on break detect
	SCON = 0x50; 		//select the BRG as UART baud rate source
	SSTAT = 0x00;
	BRGR0 = 0x70; 		//9600 BAUD at (7.3728)/2 MHZ 
	BRGR1 = 0x01;
//	BRGR0 = 0xf0; 		//4800 BAUD at (7.3728)/2 MHZ 
//	BRGR1 = 0x02;
	BRGCON = 0x03; 		//enable BRG
	TI=1;
}

void int_i2c(void)
{
    I2ADR=0xae;     //从 ADR 装入
	I2CON=0X44;     //EN I2C WAIT 从ADD 
	EI2C=1;         //EN I2C INTERRUPT
}

void init_ext1()
{
	IT1 = 1 ;		//设置外部中断为边沿触发 
	EX1 = 1 ;		//外部中断1使能
}

//用做系统定时,25ms中断一次 25ms/(1/PCLK) = 36864 16位模式 
void init_time0()
{
	unsigned int value ;
	value = RC_OSC/40;
	value = 65535 - value ;

	sys_th0 = value/256;
	sys_tl0 = value%256;
	TH0 = sys_th0;
	TL0 = sys_tl0;
//	TH0=0x6F;				//定时器初值 65535 - 36864 = 28671 = 0x6FFF 
//	TL0=0xFF; 
	TMOD &= 0xF0;			//T0 16位定时器
	TMOD |= 0x01 ; 
	ET0 = 1 ; 	//使能T0中断
}

void init_time1()
{
	TH1 = 0 ;
	TL1 = 0 ;
	TMOD &= 0x0F;
	TMOD |= 0x50 ; 
	ET1 = 1 ;
}

void i2c_interrupt() interrupt 6
{
	SI = 0 ; 
}

//计数器
void time1_interrupt(void) interrupt 3
{
//	count ++ ;
	TF1 = 0 ; 
}

//25ms 定时
void time0_intrrupt(void) interrupt 1
{
	TR0 = 0 ; //关定时器
	test_pin = ~test_pin ;
	systimer ++ ;
	if(systimer>=40)	//1s
	{
		TR1 = 0 ;//停止记数
		count=TH1<<8;
		count += TL1 ; 
		Frequence = count ;
		TH1 = 0 ;
		TL1 = 0 ; 
    	count = 0 ; 	//清零计数
		systimer = 0 ;
		TR1 = 1 ; 	//重新计数
	}
	TH0=0x49;//sys_th0;				//定时器初值 65535 - 36864 = 28671 = 0x6FFF 
	TL0=0xE1;//sys_tl0;

	TF0 = 0 ;
	TR0 = 1 ;	//重新定时20ms
}

void ext1_interrupt(void) interrupt 2
{
	IE1 = 0 ;
}

float measure()
{
	int freq ;
	float RH;
	freq = Frequence ;
	if(freq >=7351 ) 
	{
		RH = 0.0 ;
	}
	else if(freq >= 7224)
	{
		RH = 10*(freq-7224)/(7351.0-7224.0);
	}
	else if(freq >= 7100)
	{
		RH = 10*(freq-7100)/(7224.0-7100.0)+10;
	}
	else if(freq >= 6976)
	{
		RH = 10*(freq-6976)/(7100.0-6976.0)+20;	
	}
	else if(freq >= 6853)
	{
		RH = 10*(freq-6853)/(6976.0-6853.0)+30;		
	}
	else if(freq >= 6728)
	{
		RH = 10*(freq-6728)/(6853.0-6728.0)+40;		
	}
	else if(freq >= 6600)
	{
		RH = 10*(freq-6600)/(6728.0-6600.0)+50;		
	}
	else if(freq >= 6468)
	{
		RH = 10*(freq-6468)/(6600.0-6468.0)+60;	
	}
	else if(freq >= 6330)
	{
		RH = 10*(freq-6330)/(6468.0-6330.0)+70;	
	}
	else if(freq >= 6186)
	{
		RH = 10*(freq-6186)/(6330.0-6186.0)+80;	
	}
	else if(freq >= 6033)
	{
		RH = 10*(freq-6033)/(6186.0-6033.0)+90;		
	}
	else
	{
		RH = 100.0 ;
	}
	return RH;
}

void Uart_Printchar(char c)
{
	unsigned int idata j;
	while(!TI);
	TI=0;
	SBUF=c;
	for(j=0;j<100;j++) ;
}

void Uart_SendString(char *s)
{	
	while(*s)
	{
		Uart_Printchar(*s++);
	}
}

void Uart_Printf( char *fmt,... )
{
  va_list ap;
  char string[20];

  va_start(ap,fmt);
  vsprintf(string,fmt,ap);
  Uart_SendString(string);
  va_end(ap);
}

void main()
{
	int i;

	init_para();
	init_clock();
	init_io();
	int_i2c();
	init_uart();
//	init_ext1();	
	init_time0();		
	init_time1();

	EA=1;	//中断开
	TR0 = 1 ;	//开始记时
	TR1 = 1 ;	//开始计数

	Uart_Printf("System initialization!\r\n");
	while(1)
	{
//		P0 &= 0x7F;
		for(i=0;i<9000;i++) ;
//		P0 |= 0x80;
		RH = measure();
		Uart_Printf("F = %d\r\n" , Frequence);
		Uart_Printf("RH = %.1f\r\n" , RH);
	}

}

⌨️ 快捷键说明

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