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

📄 main.c

📁 (珍藏)51单片机开发板原理图应用范例、PCB图
💻 C
字号:
#include <reg52.h>
#include <INTRINS.H>
#include <math.H>
#include <stdio.H>	
#include "Mini51B.H"
#include "LCD1602.h"

#define UART_ASK 0xA5

sbit GATE=P3^2;
sbit OPEN=P1^0;
sbit CLRP=P3^5;
sbit BEEP=P1^7;

unsigned char xdata REG0 _at_ 0xfff0;
unsigned char xdata REG1 _at_ 0xfff1;
unsigned char xdata REG2 _at_ 0xfff2;
unsigned char xdata REG3 _at_ 0xfff3;

unsigned char str_buff[30];
unsigned char counter;




void rs232_port_init(void)
{
	SCON|=0x50;	//串口工作在方式1,异步模式
	PCON|=0x80;	//波特率翻倍
	TMOD|=0x20;  //定时器1工作在方式2			
	TH1=0xff; 	//波特率115200,晶振为22.1184MHz
	TL1=0xff;						  	
	TR1 = 1;  //启动定时器,产生波特率
	RI  = 0;  //接收标志位清零
	TI  = 0;  //发送标志位清零
//	ES  = 1;  //中断允许
//	EA  = 1;
}

void timer0_init()
{

	TMOD|=0x09;
	ET0=1;
	EA=1;
	TR0=0;	
}


void uart_putc(unsigned char c)
{
	SBUF = c;
	while(!TI);
	TI = 0;
}

unsigned char uart_getc()
{
	while(!RI);
	RI = 0;
	return SBUF;
}

void uart_puts(unsigned char *s)
{    
    while (*s) 
    {
        uart_putc(*s);
        s++;		
    }
	uart_putc(0x0d);
	uart_putc(0x0a);
}



main()
{
	double fx,temp;

	lcd1602_init();
	rs232_port_init();
	timer0_init();
	OPEN=0;
    BEEP=0;
    delay_ms(100);
    BEEP=1;

	while(1)
	{	
		
		OPEN=0;
		while(GATE);
		CLRP=1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		CLRP=0;

		TH0=0;
		TL0=0;
		counter=0;
		TR0=1;
		OPEN=1;
		while(counter<1);
		OPEN=0;
		while(GATE);
		TR0=0;		

//uart_putc(REG0);	 		

		temp=counter*65536+TH0*256+TL0;		
		fx=1843200/temp;
		fx=fx*(REG2*65536+REG1*256+REG0);		
		sprintf(str_buff,"%-8.3fHz",fx);
		lcd_put_xys(1,1,"f=");	
		lcd_put_xys(3,1,str_buff);	
		uart_puts(str_buff);		  		
		/*
		uart_putc(REG2);
		uart_putc(REG1);
		uart_putc(REG0);		
		uart_putc(0x55);
		uart_putc(0x55);		*/

		delay_ms(1000);	 
	}
}

void timer0_int(void) interrupt 1
{
	counter++;
}



//串口接收中断
void serial_int(void) interrupt 4
{ 
    unsigned char temp;
	if (RI) 
    {
  		temp = SBUF;
		RI = 0;
		switch (temp)
		{
			case 0:
				//Add your code
				P1 = ~uart_getc();
				break;
			case 1:
				//do one task

				break;
				//...
				//...  

			case UART_ASK:
				uart_putc(UART_ASK);
				break;
			default:
				break;
		} 
    }
}

⌨️ 快捷键说明

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