View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
Button command response slow |
Posted: Fri Dec 28, 2012 6:46 am |
|
|
When i use the below program, and once button pressed in the hardware, it increments very fast in the display. It happens because of debounce. But when i change the line
sprintf (msg,"%LU", i); to
sprintf (msg,"%6LU", i);
and once pressed, it increments very slow. Why is it so?
Code: | #include <18F2520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=6144000)
#define EN PIN_A1
#define RS PIN_A2
#define line1 (0x80)
#define line2 (0xC0)
#define KEY1 PIN_C0
void lcd_cmd(CHAR a); // function declaration
void lcd_print(CHAR rstr[]);
CHAR msg[50];
int16 i=0;
void main()
{
output_low (RS); //set LCD to command mode
lcd_cmd (0x38); //set LCD to 2x8 LINE DISPLAY (8 bit)
lcd_cmd (0x01); //clear LCD screen
lcd_cmd (0x02); //move cursor to home position
lcd_cmd (0x0e); //set cursor to blinking
lcd_cmd (0x06); //move cursor right side automatically by 1
WHILE (1)
{
if ( input(key1)==0 )
{
i++;
}
output_low (RS); //set LCD to command mode
lcd_cmd (LINE2);
sprintf (msg,"%LU", i);
lcd_print (msg);
}
}
void lcd_cmd(CHAR a) // TO enaable LCD microcontroler // function definition
{
output_b (a);
delay_ms (10);
output_high (EN);
delay_ms (10);
output_low (EN);
}
void lcd_print(CHAR rstr[])
{
INT i;
FOR (i=0; rstr[i]!='\0'; i++)
{
output_high (rs); //set LCD to data mode
lcd_cmd (rstr[i]); //printing the charact to display LCD
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Dec 28, 2012 7:06 am |
|
|
comment.
dump the listings for the two programs and compare the amount of code the compiler generates for the 2 print functions.
you should then see why.
hth
jay |
|
|
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
|
Posted: Wed Jan 02, 2013 5:36 am |
|
|
this is the asm code i got . Please explain. i dont know assembly
Code: |
.................... sprintf (msg,"%6LU", i);
01A4: CLRF 3A
01A6: MOVLW 05
01A8: MOVWF 39
01AA: MOVLW 01
01AC: MOVWF 3B
01AE: MOVLW 20
01B0: MOVWF 45
01B2: RCALL 0048
01B4: DECFSZ 3B,F
01B6: BRA 01AE
01B8: MOVLW 00
01BA: MOVWF FE9
01BC: MOVFF 38,3D
01C0: MOVFF 37,3C
01C4: BRA 0064
.................... sprintf (msg,"%LU", i);
01A6: CLRF 3A
01A8: MOVLW 05
01AA: MOVWF 39
01AC: MOVLW 10
01AE: MOVWF FE9
01B0: MOVFF 38,3C
01B4: MOVFF 37,3B
01B8: BRA 0066 |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Jan 02, 2013 7:15 am |
|
|
The microchip data sheet usually includes the asm instruction set.
The second code fragment is linear, i.e. runs straight through.
The first includes a loop.
The instruction "01B6: BRA 01AE" is braching back several times to the instruction "01AE: MOVLW 20".
The loop also includes the call "01B2: RCALL 0048" which will also take time.
You could get MPLAB to tell you how many instruction cycles each code fragment takes to execute.
Mike |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jan 02, 2013 8:28 am |
|
|
First of all, learning assembly code will pay off. It is easy to learn, much easier than C, and it allows you to understand underlying problems like why some commands in C take much longer than other commands.
Even without knowing assembly language you can test the time difference between your to programs using the Stopwatch function in the MPLAB Simulator as indicated by Mike.
But then... why do you care?
You said that the counter increases by switch bouncing effects. Shouldn't you just fix this bouncing problem? |
|
|
dorinm
Joined: 07 Jan 2006 Posts: 38
|
|
Posted: Sun Jan 06, 2013 9:32 am |
|
|
what do you mean "very slow"? 10/s? 1/s? how many increments? I assume (from your code) that 1 digit number takes ~20ms to "print", 2 digit ~40ms ... su 6digit will take ~120msec (compared to 1 digit 20msec, you actually CAN see slower increment, if this is what you ask, when you ask the printf to format to a 6 digit (space is also a char and will be printed, right? ))
...? |
|
|
|