简体   繁体   English

嵌入式 C 计数器在两个值之间

[英]Embedded C counter between two values

Please help with my issue.请帮助解决我的问题。

I am trying to avoid going out of the limit between 0 to 100 for the count up/down value in the program below;我试图避免在下面程序中的向上/向下计数值超出 0 到 100 之间的限制;

I am using an 8051 microcontroller and 2x16 LCD to display a value between 0 and 100. when pressing the UP button the number increased by one, while when pressing down button it decreased by one.我正在使用 8051 微控制器和 2x16 LCD 显示 0 到 100 之间的值。按下 UP 按钮时,数字增加一,而按下按钮时,数字减少一。

my code keeps incrementing the value above 100 and less 0.我的代码不断将值递增到 100 以上且小于 0。

// This is a code in Embedded C written on MikroC compiler for 8051 microcontroller
// The microcontroller shall count up / down on the LCD when press up / down button. 

unsigned int cntr=0; //  counter value 
char lcdv[6];       // Value to displau on lcd

sbit UP at P3.B7;   // declare button UP at port 3 Bit 7.
sbit DN at P3.B6;   // declare button UP at port 3 Bit 6.

// LCD module connections

sbit LCD_RS at P2_0_bit;      // Declare LCD reset pin.
sbit LCD_EN at P2_1_bit;      // Declare LCD Enable pin.
sbit LCD_D4 at P2_2_bit;      // Declare LCD D4 pin.
sbit LCD_D5 at P2_3_bit;      // Declare LCD D5 pin.
sbit LCD_D6 at P2_4_bit;      // Declare LCD D6 pin.
sbit LCD_D7 at P2_5_bit;      // Declare LCD D7 pin.

// End LCD module connections

char text[16]; // this is stored in RAM



 void main() {                      // Main program


  P3 = 255;         // Configure PORT3 as input

  Lcd_Init();       // Initialize LCD

cntr=0;   // Starting counter value


  Lcd_Cmd(_LCD_CLEAR); 
  Lcd_Cmd(_LCD_CURSOR_OFF);

  while(1) {

   while ((cntrset<=100)&&(cntrset>=0))  // Not sure how to limit Min and Max value.

 {
   wordTostr(cntrset,volset);

   LCD_Out(2,1,volset);

  if (UP==0)
  cntrset ++;
  while (UP==0);

  if (DN==0)
  cntrset=--;
  while (DN==0);
                     }


                }    
                }

if (UP==0 && cntrset < 100 ) cntrset++; 
while (UP==0); 

if (DN==0 cntrset > 0 ) cntrset--; 
while (DN==0); 

You may still have an issue with switch bounce causing a single press to result in the counter changing by more than one count.您可能仍然会遇到开关弹跳的问题,导致单次按下会导致计数器更改超过一个计数。 But that is a different question.但这是一个不同的问题。

Regarding comment: If the increment is not by-one and the current value need not be a multiple of the increment, then it is easier to apply saturation instead:关于评论:如果增量不是一倍并且当前值不需要是增量的倍数,那么更容易应用饱和度:

if( UP == 0 ) cntrset += increment; 
while (UP==0); 

if( DN == 0 ) cntrset -= increment ; 
while (DN==0); 

if( cntrset < 0 ) cntrset = 0 ;
else if( cntrset > MAX_CNTRSET ) cntrset = MAX_CNTRSET ;

For that to work however you must change cntrset to signed int .但是,要使其正常工作,您必须将cntrset更改为signed int If you'd rather not do that then (assuming 16 bit unsigned ):如果您不想这样做(假设 16 位unsigned ):

...

if( (cntrset & 0x8000u) != 0 ) cntrset = 0u ;
else if( cntrset > MAX_CNTRSET ) cntrset = MAX_CNTRSET ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM