CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problems with timers and flex_lcd420

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
flyingjoint



Joined: 22 Sep 2010
Posts: 3

View user's profile Send private message Send e-mail

Problems with timers and flex_lcd420
PostPosted: Wed Oct 13, 2010 1:38 pm     Reply with quote

Hello everyone

I just have a couple of questions which i think you may be able to answer.

First of all, I'm developing a project using an encoder and a PIC 16F874A. Basically the PIC make the counts of pulses from the encoder and put them into a counter, it increase the count when it goes CW and decrease in the other way around. I already have the pulse counting. Just to let you know I separated the pulses from each other using some basic TTL devices (two flip-flops D and a logic AND) before the signal goes to the PIC.

Now, the PIC counts just fine, and checked this out making the encoder turns around and showing the count in a 4x20 LCD (which by the way, I'm using your Flex_LCD420). Since my encoder needs to go fast, writing the counter in each count is not helping me out just, as you know, the FLEX_LCD420 driver makes a delay in each lcd_putc() and so on, that means in every writing in can lose some counts if I go fast. So I decided to show the counter every second or half a second at least.

I tried to do that with timer 1. I just wanted to write the counter when timer 1 overflow, that means using the timer 1 interrupt. When I was trying to do so, i realize that the FLEX_LCD driver disable the interrupts to avoid rewriting while you use lcd_putc(). No my question comes:

1.- How can I make the counter appear every half a second at least?. I checked out the datasheet of the PIC but it doesn't say anything about how long (in milliseconds or microsecond) the timer 1 overflow takes, what I was trying to do was this:
Code:

if (get_timer1() == 65 000) // 65 000 just to put a 16 bit number
  {
 
   lcd_gotoxy(5,2);
   printf(lcd_putc,"%Lu",value); //Shows the value of the counter when the timer 1 gets the count i want to (65 000 in this case).
  }

So what can you recommend me to show the counter onto the LCD when timer 1 overflows either in one second or in half second?


2.- Just to be sure, I can't write onto the LCD within an interrupt, am I right? Because I want to make a general counter reset with a push button with an interrupt in Pin B4 that makes the counter turn to ZERO. But when I put 0 to the counter into the interrupt and I want to write the value in that moment I have the same error, that interrupts are disable by FLEX_LCD420.

Thanks in advance

Best regards!
_________________
Let's rock!
sturnfie



Joined: 26 Apr 2010
Posts: 17
Location: Palatine, IL

View user's profile Send private message Visit poster's website

Re: Problems with timers and flex_lcd420
PostPosted: Thu Oct 14, 2010 1:22 pm     Reply with quote

flyingjoint wrote:
Hello everyone
1.- How can I make the counter appear every half a second at least?. I checked out the datasheet of the PIC but it doesn't say anything about how long (in milliseconds or microsecond) the timer 1 overflow takes, what I was trying to do was this:
Code:

if (get_timer1() == 65 000) // 65 000 just to put a 16 bit number
  {
 
   lcd_gotoxy(5,2);
   printf(lcd_putc,"%Lu",value); //Shows the value of the counter when the timer 1 gets the count i want to (65 000 in this case).
  }

So what can you recommend me to show the counter onto the LCD when timer 1 overflows either in one second or in half second?


Check out the first post in this recent thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=39923

The general idea with timers is that they increment to a finite amount, then overflow. The overflow triggers an interrupt. If you start the timer between 0 and this finite amount, and you know how quickly the timer increments, you can configure the timer to overflow after a specific time interval. Follow that person's calculations with your own numbers to figure out the offset value to start your timer at.

I'm a fan of incrementing counter variables inside interrupts (where the interrupts are occurring much more frequently than the action I want taken), up to a defined maximum. In the main loop I check if the variable is at that maximum. If so, clear the variable (allowing the interrupt to re-start incrementing that counting variable) and run the process. This works pretty well with LCD updates. This approach requires the counter variable be defines as volatile (since multiple code paths can con-concurrently modify the variable),

Quote:

2.- Just to be sure, I can't write onto the LCD within an interrupt, am I right?


Technically, you can do whatever you want "in an interrupt", including writing to an LCD. An interrupt is an internal signal that causes a different code path to run. When an interrupt "exits", the usual method is to return to the code path that was originally running. When you disable interrupts, you're really just disabling the monitoring of that internal overflow signal. Yes, you can disable interrupts while "in an interrupt".

However, good programming design dictates that you do not depend on interrupts for anything requiring more than the bare minimum computation. A good practice is to use the main loop of your design for computation, and use interrupts to modify the status variables that determine what computations to run.

As for your other design questions, I'm of the opinion that most of your requirements can be handled in a main loop with standard input checks. What's the purpose of using an external interrupt (B4) to clear a counter, when the main loop is most likely looping 100+ times inside that 1-second before overflow (and LCD update)? I guess this depends on how quickly the encoder counts are coming in.

Lucas
_________________
Lucas Sturnfield
Blog - http://www.sturntech.com/blog
flyingjoint



Joined: 22 Sep 2010
Posts: 3

View user's profile Send private message Send e-mail

Re: Problems with timers and flex_lcd420
PostPosted: Mon Oct 25, 2010 7:33 am     Reply with quote

I followed your advice and you were right, I don't really need to depend on an interrupt for my reset. The oscillator go fast enough, besides I'm using a 20 MHz crystal. About showing up the counts with the timer, in the final implementation of my project I won't need to show up the counts, so is only for design purpose. Thanks a lot.
_________________
Let's rock!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group