本文主要是介绍【TB作品】msp430g2553单片机,家用可燃气体监测报警器,MQ-2,MQ5,PWM风扇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
功能
//家用可燃气体监测报警器
//硬件:MQ-2 MQ5 OLED 蜂鸣器 按键 风扇
//1 OLED显示天然气、液化气浓度
//2 OLED显示可燃气体报警临界值
//3 2个按键用于修改可燃气体报警临界值
//4 MQ2检测到的浓度或者MQ7检测到的浓度 高于临界值时,蜂鸣器报警+风扇打开
//line:
//mq2 vcc-3.3v gnd-gnd data-p6.0
//mq5 vcc-3.3v gnd-gnd data-p6.1
//oled vcc-3.3v gnd-gnd scl-p2.0 sda-p2.2
//buzzer vcc-3.3v gnd-gnd data-p3.6
//fan vcc-3.3v gnd-gnd data-p3.5
//key1-p1.1 key2-p2.1 nocare
部分程序
int main(void)
{unsigned char dis[10];unsigned char time_cyc = 0;int set_value = 50; //设置临界值int adc_prec[4]; //adc结果的百分比结果WDTCTL = WDTPW + WDTHOLD; /* Stop WDT *//* 初始化显示 */OLED_Init(); /* OLED初始化 */OLED_ShowString(0, 0, "MQ-2:");OLED_ShowString(0, 2, "MQ-5:");OLED_ShowString(0, 4, " SET:");dis[0] = set_value % 100 / 10 + '0';dis[1] = set_value % 10 + '0';dis[2] = 37; //这表示百分号dis[3] = 0;OLED_ShowString(8 * 6, 4, dis);/*按键端口 */P1DIR &= ~BIT1;P1SEL &= ~BIT1;P1REN |= BIT1;P1OUT |= BIT1;P2DIR &= ~BIT1;P2SEL &= ~BIT1;P2REN |= BIT1;P2OUT |= BIT1;/* 风扇端口 P35 */P3DIR |= BIT5;/* 蜂鸣器端口P3.6 PWM 频率是(1MHZ/1000) 占空比是TBCCR6/TBCCR0 */P3SEL |= BIT6; // P3.5-7.6 option select_EINT();while (1){//显示MQ2 MQ5delay_ms(10);time_cyc = (time_cyc + 1) % 10;if (time_cyc == 1) /* 10 个10ms执行一次 */{adc_prec[0] = (int) ((float) (ADCresults[0]) * 1.0 / 4096.0 * 100); //结果转化成百分比dis[0] = adc_prec[0] % 100 / 10 + '0';dis[1] = adc_prec[0] % 10 + '0';dis[2] = 37; //这表示百分号dis[3] = 0;OLED_ShowString(8 * 6, 0, dis);//第一排显示MQ2adc_prec[1] = (int) ((float) (ADCresults[1]) * 1.0 / 4096.0 * 100); //结果转化成百分比dis[0] = adc_prec[1] % 100 / 10 + '0';dis[1] = adc_prec[1] % 10 + '0';dis[2] = 37; //这表示百分号dis[3] = 0;OLED_ShowString(8 * 6, 2, dis);//第一排显示MQ5}//按键1检测处理if (!(P1IN & BIT1)){while (!(P1IN & BIT1));if (set_value < 95)set_value += 1; //临界值加1adc_prec[0] = (int) ((float) (ADCresults[0]) * 1.0 / 4096.0 * 100); //结果转化成百分比dis[0] = set_value % 100 / 10 + '0';dis[1] = set_value % 10 + '0';dis[2] = 37; //这表示百分号dis[3] = 0;OLED_ShowString(8 * 6, 4, dis);}//按键2检测处理if (!(P2IN & BIT1)){while (!(P2IN & BIT1));if (set_value > 5)set_value -= 1; //临界值-1adc_prec[0] = (int) ((float) (ADCresults[0]) * 1.0 / 4096.0 * 100); //结果转化成百分比dis[0] = set_value % 100 / 10 + '0';dis[1] = set_value % 10 + '0';dis[2] = 37; //这表示百分号dis[3] = 0;OLED_ShowString(8 * 6, 4, dis);}//临界值判断if (adc_prec[0] > set_value || adc_prec[1] > set_value) //MQ2>临界值 或者 MQ7>临界值{TBCCR6 = 500; //打开蜂鸣器P3OUT &= ~BIT5;//打开风扇}else{TBCCR6 = 0; //关闭打开蜂鸣器P3OUT |= BIT5;//关闭风扇}}
}#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR(void)
{switch (__even_in_range(ADC12IV, 34)){case 12: // Vector 12: ADC12IFG3ADCresults[0] = ADC12MEM0; // Move A0 results, IFG is clearedADCresults[1] = ADC12MEM1; // Move A1 results, IFG is clearedADCresults[2] = ADC12MEM2; // Move A2 results, IFG is clearedADCresults[3] = ADC12MEM3; // Move A3 results, IFG is cleareddefault:break;}
}
全部程序
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2
这篇关于【TB作品】msp430g2553单片机,家用可燃气体监测报警器,MQ-2,MQ5,PWM风扇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!