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

📄 main.c

📁 用单片机M8和传感器H10来测量温湿度
💻 C
字号:
#include 		"main.h"

uint8	bcd_temp[6],bcd_humi[6];
uint16	t_c, t_c2,rh_true,rh_true2;
void port_init(void)
{
 PORTB = 0x00;//
 DDRB  = 0x00;//输入
 PORTC = 0x00; //m103 output only
 DDRC  = (1<<4);
 PORTD = 0x00;
 DDRD  = 0x00;
}


//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts置位
 port_init();
 uart0_init();
 shtxx_init();
 
 SFIOR &= 0XFB;	//开启上拉电阻

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void main()
{
uint16	*shtxx_temp, *shtxx_humi,error=0;
uint8	*shtxx_crc,j;
uint8	i=0;
uint32	test;

*shtxx_temp=*shtxx_humi=0;

 init_devices();
 delay_1second();
 while(1)
 	{
	if (!shtxx_measure(shtxx_temp,shtxx_crc, SHTXX_MODE_TEMP))
	{
		//*shtxx_temp=4000;
		t_c = *shtxx_temp;
		bcd_temp[5]=0;
		if(t_c<4000)	//零度以下
		{
			if(t_c<=0)
			{
				uart_send_string("Temperature is too low,or something is wrong.");
				shtxx_softrst();
				Delay_100millisecond();
				//break;
			}
			bcd_temp[5]='-';
			t_c2=4000-t_c;
		}
		else if(t_c>17000)//高于130 度,复位SHT
		{
			uart_send_string("Temperature is too high.\r\dReconecting...");
			if(!shtxx_softrst())  	
				{
				Delay_100millisecond();
				//*********************************************************
				t_c2=0;
				}
		}
		else if(t_c>14000)
		{
			t_c2=t_c-14000;
			bcd_temp[5]='1';
		}
		else
		{
			t_c2=t_c-4000;
		}
		hex_bcd(t_c2,bcd_temp);
		t_c=*shtxx_temp-4000;
		
		
		//串口发送温度
		uart_send_string("Temperature: ");
		if(bcd_temp[5] != 0)
			uart_send_char(bcd_temp[5]);
		else
			uart_send_char(' ');
		for(j=0;j<4;j++)
		{
			if(j==2) 
			uart_send_char('.');
			uart_send_char(bcd_temp[3-j]+0x30);
		}
		uart_send_char(0x0a);
		uart_send_char(0x0d);
		
	}
	else			//无法连接
	{
		uart_send_string("Can't connect SHT.");
		error=0;
	}
	delay_1second();

	if (!shtxx_measure(shtxx_humi,shtxx_crc, SHTXX_MODE_HUMI))
	{
		rh_true = shtxx_calc_humi(shtxx_humi);
		hex_bcd(rh_true,bcd_humi);
		//串口发送湿度
		uart_send_string("Humility: ");
		
		if(bcd_humi[4] != 0)
			uart_send_char(bcd_humi[4]+0x30);
		else
			uart_send_char(' ');
		for(j=0;j<4;j++)
		{
			if(j==2) 
			uart_send_char('.');
			uart_send_char(bcd_humi[3-j]+0x30);
		}
		uart_send_char(0x0a);
		uart_send_char(0x0d);
		
	}
	else		//无法连接
	{
		uart_send_string("Can't connect SHT.");
		error=0;
	}
	delay_1second();

 }
}


⌨️ 快捷键说明

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