功能
电风扇模拟控制系统设计
基本要求:
用LED/LCD 显示电风扇的工作状态 (1,2,3,4 四档风力),
显示风类:“自然风”、“常风”和“睡眠风”。
设计 “自然风”“常风”和“睡眠风” 三个风类键用于设置风类
设计一个“摇头”键用于控制电机摇头。
设计一个“定时”键,用于定时时间长短设置:
发挥部分:
设计过热检测与保护电路
若电风扇电机过热(用内存参数模拟温度,用键),则电机停止转动,
蜂鸣器报警电机冷却后(用键模拟) 电机又恢复转动。
显示格式:
LED 数码管/LCD 屏显示方法自定
效果
部分代码
void main(void)
{char t_j = 0;WDTCTL = WDTPW + WDTHOLD; /* 关闭看门狗 */P1DIR = 0xFF; /* 设置方向 */P1SEL = 0; /* 设置为普通I/O 口 */P2DIR = 0xFF; /* 设置方向 */P2SEL = 0; /* 设置为普通I/O 口 */P2OUT = 0x00;P1OUT = 0x00;delay_ms(200);LCD_init();//P4.1 设置为PWM 输出P4DIR |= BIT1;P4SEL |= BIT1;TBCCR0 = 1000; //PWM 周期TBCCR1 = 0; //PWM 占空比TBCCTL1 = OUTMOD_7; //PWM 输出模式TBCTL = TBSSEL_2 + MC_1 + ID_3; //SMCLK, UP 模式CCTL0 = CCIE; // CCR0 interrupt enabledCCR0 = 32768 - 1;TACTL = TASSEL_1 + MC_1; // ACLK, upmodeP5DIR = 0X00; //其余的都是按键P5REN |= 0XFF;P5OUT = 0XFF;//P4.4输出给beepP4DIR |= BIT4;P4OUT &= ~BIT4;//P4.3输出给摇头电机P4DIR |= BIT3;P4OUT &= BIT3;//P4.2 继电器P4DIR |= 0X04;P4OUT |= 0X04; //高电平是关闭继电器//P4.5输出P4DIR |= BIT5;P4OUT &= ~BIT5;__bis_SR_register( GIE);while (1){delay_us(10);t_j++;if (t_j > 100){if (Countdown_Flag){tmp_r = (hour * 3600) + (minute * 60) + second;if (tmp_r>0){TBCCR1 = fengli[fengli_Flag_dangwei]; //每一秒钟检测一次风扇的转速}else{fengli_Flag_dangwei = 0;TBCCR1 = fengli[fengli_Flag_dangwei]; //每一秒钟检测一次风扇的转速}}else{TBCCR1 = fengli[fengli_Flag_dangwei]; //每一秒钟检测一次风扇的转速}Read_DS18B20(&TAM_KISIM, &ONDALIK_KISIM);if ((negative == 0) && (limited == 0)){cnt = 0;disp[cnt++] = 't';disp[cnt++] = '=';disp[cnt++] = '0' + (TAM_KISIM % 1000 / 100);disp[cnt++] = '0' + (TAM_KISIM % 100 / 10);disp[cnt++] = '0' + (TAM_KISIM % 10);disp[cnt++] = '.';disp[cnt++] = '0' + (ONDALIK_KISIM % 10000 / 1000);disp[cnt++] = '0' + (ONDALIK_KISIM % 1000 / 100);disp[cnt++] = '0' + (ONDALIK_KISIM % 100 / 10);disp[cnt++] = '0' + (ONDALIK_KISIM % 10);disp[cnt++] = 'C';disp[cnt++] = 0;LCD_dsp_string(0, 2, disp); /* 在第一行第一列显示"LCD TEST" */}if (TAM_KISIM > 50){//P4.4输出给beepP4OUT &= ~BIT4; //鸣叫P4OUT &= 0XFB; //低电平是打开继电器,是关闭风扇}else{P4OUT |= BIT4; //不鸣叫P4OUT |= 0X04; //高电平是关闭继电器,是正常风扇工作}update_disp();}//按键检测if ((P5IN & BIT0) == 0) //0档位风力{
// delay_ms(10);if ((P5IN & BIT0) == 0){fengli_Flag_dangwei = 0;update_disp();while ((P5IN & BIT0) == 0);}}if ((P5IN & BIT1) == 0) //1档位风力{
// delay_ms(10);if ((P5IN & BIT1) == 0){fengli_Flag_dangwei = 1;update_disp();while ((P5IN & BIT1) == 0);}}if ((P5IN & BIT2) == 0) //2档位风力{
// delay_ms(10);if ((P5IN & BIT2) == 0){fengli_Flag_dangwei = 2;update_disp();while ((P5IN & BIT2) == 0);}}if ((P5IN & BIT3) == 0) //3档位风力{
// delay_ms(10);if ((P5IN & BIT3) == 0){fengli_Flag_dangwei = 3;update_disp();while ((P5IN & BIT3) == 0);}}if ((P5IN & BIT4) == 0) //摇头电机{
// delay_ms(10);if ((P5IN & BIT4) == 0){if (shake_Flag == 1)shake_Flag = 0;elseshake_Flag = 1;if (shake_Flag == 1)P4OUT |= BIT3;elseP4OUT &= ~BIT3;update_disp();while ((P5IN & BIT4) == 0);}}if ((P5IN & BIT5) == 0) //加倒计时,一次加10分钟,小时不能多于12小时{
// delay_ms(10);if ((P5IN & BIT5) == 0){tmp_r = hour * 60 + minute;tmp_r += 10;hour = tmp_r / 60;minute = tmp_r % 60;update_disp();while ((P5IN & BIT5) == 0);}}if ((P5IN & BIT6) == 0) //减倒计时,一次减10分钟,分钟不能小于0{
// delay_ms(10);if ((P5IN & BIT6) == 0){tmp_r = hour * 60 + minute;if (tmp_r >= 10){tmp_r -= 10;hour = tmp_r / 60;minute = tmp_r % 60;}else{fengli_Flag_dangwei = 0;hour = 0;minute = 0;second=0;}update_disp();while ((P5IN & BIT6) == 0);}}if ((P5IN & BIT7) == 0) //定时模式的开关{
// delay_ms(10);if ((P5IN & BIT7) == 0){if (Countdown_Flag){Countdown_Flag = 0;hour=0;minute=0;second=0;fengli_Flag_dangwei=0;}else{Countdown_Flag = 1;}update_disp();while ((P5IN & BIT7) == 0);}}}
}
全部代码
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2