微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

长按同时按住不放设定的温度应在pic微型控制器中连续增加/减少

如何解决长按同时按住不放设定的温度应在pic微型控制器中连续增加/减少

在这里,我提供了使用两个按钮来递增和递减温度的代码

sbit sw1 at RB0_bit;
sbit sw2 at RB1_bit;

sbit sw1_dir at TRISB0_bit;
sbit sw2_dir at TRISB1_bit;
// End Keypad module connections



// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections

#define HEATER PORTD.RD0
#define COOLER PORTD.RD1
#define LED PORTD.RD3
#define ENTER 15
#define CLEAR 13
#define ON 1
#define OFF 0


void main(){
  unsigned short Txt[14];
  float Temp_Ref ;      //  Reference Temperature
  unsigned char inTemp;
  unsigned int temp;
  unsigned int pressed;
  unsigned int x;
  float mV,ActualTemp;

  sw1_dir=sw2_dir=1;

  PORTC.F0 = 0;                 // Configure PORTC as digital I/O
  PORTB.F0 = 0;                 // Configure PORTB as digital I/O
  PORTD.F0= 0;                 // Configure PORTD as digital I/O
  TRISA0_bit = 1;              //Configure AN0 (RA0) as input
  TRISC = 0;                  //PORTC are outputs (LCD)
  TRISD0_bit=0;               //RD0 is output (Heater)
  TRISD1_bit=0;               //RD1 is output (Cooler)
  TRISD3_bit=0;               //RD3 is output (LED)


  Lcd_Init();                  // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  lcd_Out(1,4,"Automatic");
  lcd_Out(2,2,"Temp Control");
  delay_ms(2000);                          //2s delay

  HEATER = OFF;
  COOLER = OFF;

这是我单按按钮所使用的代码

    START:

    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"Enter Temp Ref");

    Lcd_Out(2,"Temp Ref: ");
    Temp_Ref=0;

while(1){

if(sw1==1 && Temp_Ref<31){
 for(x=0;Temp_Ref<31;x++)
 {
         Temp_Ref+=0.5;

 
 }
 while (sw1==0){
 Lcd_Cmd(_LCD_CLEAR);          // Clear display
         Lcd_Out(1,"Temp Ref: ");
         FloatToStr( Temp_Ref,Txt);      //Convert to String
         inTemp=Ltrim(Txt);
         Lcd_Out_CP(inTemp);}
}
if(sw2==0 && Temp_Ref>0){
 for(x=0;Temp_Ref>0;x++)
 {
         Temp_Ref-=0.5;


 }   
 }
 
 }

}

我想修改代码以在 长按(按住时)设定温度时获得输出 连续增加/减少。

解决方法

/*
 * Project name:  Automatic Temperature Control
 * Copyright:
     (c) www.studentcompanion.co.za,2018.
 * Test configuration:
     MCU:             PIC16F877
                      http://ww1.microchip.com/downloads/en/DeviceDoc/41412D.pdf
     Oscillator:      HS-PLL,32.00000 MHz
 */

// Keypad module connections

sbit sw1 at RB0_bit;
sbit sw2 at RB1_bit;

sbit sw1_dir at TRISB0_bit;
sbit sw2_dir at TRISB1_bit;
// End Keypad module connections



// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;

sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections

#define HEATER PORTD.RD0
#define COOLER PORTD.RD1
#define LED PORTD.RD3
#define ENTER 15
#define CLEAR 13
#define ON 1
#define OFF 0


void main(){
  unsigned short Txt[14];
  float Temp_Ref ;      //  Reference Temperature
  unsigned char inTemp;
  unsigned int temp;
  unsigned int pressed;


  PORTC.F0 = 0;                 // Configure PORTC as digital I/O
  PORTB.F0 = 0;                 // Configure PORTB as digital I/O
  PORTD.F0= 0;                 // Configure PORTD as digital I/O
  TRISA0_bit = 1;              //Configure AN0 (RA0) as input
  TRISC = 0;                  //PORTC are outputs (LCD)
  TRISD0_bit=0;               //RD0 is output (Heater)
  TRISD1_bit=0;               //RD1 is output (Cooler)
  TRISD3_bit=0;               //RD3 is output (LED)


  Lcd_Init();                  // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);                     // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);                // Cursor off
  lcd_Out(1,4,"Automatic");
  lcd_Out(2,2,"Temp Control");
  delay_ms(2000);                          //2s delay

  HEATER = OFF;
  COOLER = OFF;


    START:

    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,"Enter Temp Ref");

    Lcd_Out(2,"Temp Ref: ");
    Temp_Ref=0;



 while(1){
 if(sw1==0 && Temp_Ref<31){
 while (sw1==0)
 {
  Lcd_Cmd(_LCD_CLEAR);          // Clear display
    Lcd_Out(1,"Temp Ref: ");
    FloatToStr( Temp_Ref,Txt);      //Convert to String
    inTemp=Ltrim(Txt);
    Lcd_Out_CP(inTemp);
 
 
 }

    if(sw1==1){
    delay_ms(1000);
    for(;Temp_Ref<31;){
    Temp_Ref=Temp_Ref+0.5;
    delay_ms(1000);
    Lcd_Cmd(_LCD_CLEAR);          // Clear display
    Lcd_Out(1,Txt);      //Convert to String
    inTemp=Ltrim(Txt);
    Lcd_Out_CP(inTemp);
    pressed = sw1;
    if(pressed ==1){
    break;
    }
    else{
    continue;

     }

    }
    }
    }



if(sw2==1 && Temp_Ref>0){
 while (sw2==1)
 {
  Lcd_Cmd(_LCD_CLEAR);          // Clear display
    Lcd_Out(1,Txt);      //Convert to String
    inTemp=Ltrim(Txt);
    Lcd_Out_CP(inTemp);


 }

    if(sw2==0){
    delay_ms(1000);
    for(;Temp_Ref>0;){
    Temp_Ref=Temp_Ref-0.5;
    delay_ms(1000);
    Lcd_Cmd(_LCD_CLEAR);          // Clear display
    Lcd_Out(1,Txt);      //Convert to String
    inTemp=Ltrim(Txt);
    Lcd_Out_CP(inTemp);
    pressed = sw2;
    if(pressed ==0){
    break;
    }
    else{
    continue;

     }

    }
    }
    }
    }

 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。