View previous topic :: View next topic |
Author |
Message |
nickomalley
Joined: 16 Aug 2011 Posts: 3
|
Pulse counter affected by delay in LCD write |
Posted: Tue Aug 16, 2011 3:44 pm |
|
|
Hello,
Would it be possible to have a look at the following code, I have this working fine counting pulses without a problem.
With the LCD running and no delay after the printf the fastest pulse it can count is approx 120Hz, if I comment out the printf it can count a significantly faster pulse (low kHz), if I put in the recommended 200ms delay after the printf it struggles to read pulses faster than 10hz.
Is there a more efficient way to write this to the LCD?
thanks for any help
Nick
Code: |
#include <16f877.h>
#device *=16
#fuses HS,NOPROTECT,PUT,BROWNOUT,NOLVP,NOWDT
#use delay (clock=20000000) //Set clock rate of 10Mhx - needed for rs232 commands
#include <lcd.c>
#include <ds1621.c>
/* Use #byte to assign variables to a RAM locations */
#byte PORT_B=6 //this is the port the motors are on
unsigned int16 counter; // count variable for pulse counter
unsigned int occurrence; //count how many time 0 was recorded, only want to count if equal to 1
enum pin0_portb {pin1=PIN_B1};
#define OFF 0 //IR receiver connected to ground
#define ON 1 //IR receiver connected to ground
//-----------------------------------------------
void clear_some_pins () {
//Resets state variables allowing a new message to be received.
}
void testlcd()
{
printf(LCD_PUTC, "\fHarry is COOL");
printf(LCD_PUTC, "\nCheese is nice 2");
delay_ms(3000);
}
///////////////////////// Main ////////////////////////
void main ()
{
lcd_init();
delay_ms(6);
printf(LCD_PUTC, "\f"); //clear the lcd
delay_ms(500);
testlcd();
//port_b_pullups(TRUE);
occurrence=0;
counter = 0; //reset the counter variable on program start
set_tris_b(0b00000000); // define B as all inputs except int port
while (true)
{
if(input_state(pin_A4)==OFF) //check if the pin is held high eg 5volts internal pullups
{
occurrence=0; //if it is, reset the occurence to 0
bit_clear(PORT_B,1); // Turn LED off
}
if (input_state(pin_A4)== ON)
{
if (occurrence ==255) // this is necessary as it will overflow to 0 and pass the test below, which doesn't help
occurrence = 2;
else
occurrence++;
if (occurrence<=1)
{
counter++;
bit_set(PORT_B,1); //Turn LED connected to this pin ON to show count happened
}
}
printf(lcd_putc,"\fWinding counter: F\nTurns\1 %6lu",counter);
//delay_ms(200);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 16, 2011 3:53 pm |
|
|
Quote: | printf(lcd_putc,"\fWinding counter: F\nTurns\1 %6lu",counter); |
The majority of your LCD text doesn't have to be written every time.
Just write it once before the loop starts. Then at the end of the loop,
use lcd_gotoxy() to put the lcd cursor in the correct position to display
your number. Then just update the number on the lcd.
Quote: | set_tris_b(0b00000000); // define B as all inputs except int port |
The comment doesn't match the parameter. |
|
|
Wilksey
Joined: 14 Aug 2011 Posts: 36 Location: Somerset, UK
|
|
Posted: Tue Aug 16, 2011 5:08 pm |
|
|
The clock doesn't match the comment either. |
|
|
nickomalley
Joined: 16 Aug 2011 Posts: 3
|
|
Posted: Wed Aug 17, 2011 3:34 am |
|
|
Thanks for this i'll give it a go.
re printf(lcd_putc,"\fWinding counter: F\nTurns\1 %6lu",counter);
is there a more efficient way of sending this to the lcd, i read somewhere that printf is very processor time hungry
Re the errors in the clock and tris I apologise, its my sloppy commenting.
I tend to copy code from other programs and tweak as necessary but sometimes forget to update the comments |
|
|
Wilksey
Joined: 14 Aug 2011 Posts: 36 Location: Somerset, UK
|
|
Posted: Wed Aug 17, 2011 12:50 pm |
|
|
You can do as PCM suggested.
you can use fputc on individual characters, not sure how much you will save with that, or you can write / read the registers directly.
All about trying what is the best solution for your setup. |
|
|
nickomalley
Joined: 16 Aug 2011 Posts: 3
|
|
Posted: Wed Aug 17, 2011 3:23 pm |
|
|
Thanks I'll give it a go and post outcome to the forum
regards
Nick |
|
|
|