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

Another small problem I could use some help with

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



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

Another small problem I could use some help with
PostPosted: Mon May 03, 2004 12:45 am     Reply with quote

Looking at this

Code:
else if (PB_Plastic == 0)
   {
      Plastic = 1;


How can I make it so that when "Plastic = 1;" I get a count down timer that when it reaches zero in makes "Plastic = 0;" and prints the count down as it happens.


I've tried just asigning a memory address and loading it with a value and then having it decrement the value every 60,000 mS, printing that value, and then set Plastic = 0 when that address reaches zero.

My problem is that while it loads the address, it doesn't decrament. I also can't get it to print the value in that address.

Could somebody point me in the right direction please?[/b]


Last edited by RatFink on Mon May 03, 2004 11:35 am; edited 1 time in total
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon May 03, 2004 5:15 am     Reply with quote

Why are you assigning a memory address to a variable? The nice thing about using a compiler is that the compiler can handle that low level stuff for you.
I would suggest to use a global variable and I guess your problems will disappear.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Mon May 03, 2004 7:52 am     Reply with quote

For you countdown timer you should probably use the PIC's timer2.

Set up the timer to generate an interrupt once per 1ms or 60ms or some even multiple of the 60,000ms you desire.

In the timer interrupt service routine you would have a 16-bit unsigned int (assuming you set the interval to get 1ms resolution, the variable would need to count to 60,000). At each interrupt you increment the counter and when it reaches 60,000 you change the value of your global variable Plastic back to zero.

But, do not enable the timer interrupt yet. You would clear its counter and enable just before your Plastic=1 expression.

As far as printing the time, DON'T DO THAT INSIDE THE INTERRUPT SERVICE ROUTINE. This is important, so I'll say it again, DON'T USE PRINTF INSIDE THE INTERRUPT SERVICE ROUTINE. ISRs should be small and fast. printf is neither small nor fast.

If you must display the count value, do it in your main()'s infinite loop (remember I mentioned using a while(1){} structure). If your loop takes long enough to get through that you can guarantee the timer would have changed, just printf each pass through the loop. Otherwise you can monitor the timer's count variable for change and only print if it has changed.

Keep in mind that the printf statement will take a fairly long time to execute, probably longer than 1ms.

Probably much more information than you wanted, so I'm going to give you even more data to digest...

Examples of timer2 interrupt service routines are found in the CCS example programs. Check the help file for a list of example programs and descriptions. Also this board has a very good search feature. You will get lots and lots of hits if you search on "timer2" or "timer2 setup". And finally, read the Microchip datasheet on the operation of the timers. I think you mentioned before you were using a 16F part so you would also benefit from reading the mid-range reference manual.

You have 30 minutes to complete the exercise, begin! Shocked
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







Re: Another small problem I could use some help with
PostPosted: Mon May 03, 2004 8:32 am     Reply with quote

RatFink wrote:
Looking at this

Code:
else if (PB_Plastic = 0)
   {
      Plastic = 1;


How can I make it so that when "Plastic = 1;" I get a count down timer that when it reaches zero in makes "Plastic = 0;" and prints the count down as it happens.


I've tried just asigning a memory address and loading it with a value and then having it decrement the value every 60,000 mS, printing that value, and then set Plastic = 0 when that address reaches zero.

My problem is that while it loads the address, it doesn't decrament. I also can't get it to print the value in that address.

Could somebody point me in the right direction please?[/b]

Note also, that as written, the code sets 'PB_Plastic', to zero, which will evaluate as 'false' all the time...

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon May 03, 2004 9:26 am     Reply with quote

From a previous post:

Quote:


Posted: Sun May 02, 2004 2:31 pm
--------------------------------------------------------------------------------
Thank you guys, I can't believe I misses the "=" instead of "==", I was tearing my hair out wondering why it wouldn't work.



Hi RatFink,

You get a choice:
1) study/practice a little more "C"
2) or you will get becoming baldness... Very Happy

Hope not being offensive.

Humberto
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Mon May 03, 2004 10:01 am     Reply with quote

Humberto wrote:
From a previous post:

Quote:


Posted: Sun May 02, 2004 2:31 pm
--------------------------------------------------------------------------------
Thank you guys, I can't believe I misses the "=" instead of "==", I was tearing my hair out wondering why it wouldn't work.



Hi RatFink,

You get a choice:
1) study/practice a little more "C"
2) or you will get becoming baldness... Very Happy

Hope not being offensive.

Humberto


Nope not offensive, I've taken on this project thinking that the few things I wanted to do were simple, and I still think they are, but I just lack the experience to see small mistakes and simple paths to do things. getting ponted in the right direction here helps.

I've tried looking through all the examples, and I've searched through the forum but it just got me more confused because I wasn't even sure what exactly I was looking for, but now I have an idea what it is I need.

Thanks to all that showed me what I need to do and what I was doing wrong, I'll go look up all this Timer2 stuff.
AK



Joined: 20 Apr 2004
Posts: 33

View user's profile Send private message

PostPosted: Mon May 03, 2004 11:14 am     Reply with quote

The else if statement should be:

Code:
else if (PB_Plastic == 0)
   {
      Plastic = 1;
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Mon May 03, 2004 11:34 am     Reply with quote

AK wrote:
The else if statement should be:

Code:
else if (PB_Plastic == 0)
   {
      Plastic = 1;


Yup, I got that in my other thread, when I posted this thread I just copy/pasted the code from the other thread without the fix. I just fixed it in my first post so people would quit seeing how stupid I am Shocked
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Tue May 04, 2004 8:27 am     Reply with quote

Ratfink -

Keep trying. I know I have lots of scars from doing stupid things when I started learning C and again later when I started working with microcontrollers. Make mistakes, just learn from them so you can make new and better mistakes later.

I can only imagine how scarred and disfigured PCM Programmer and RJHamlet (and Neutone and dozens of others on this forum) are with their deep understaning of C & ASM on the PIC. I'm sure it came at a heavy price. Wink
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Tue May 04, 2004 5:41 pm     Reply with quote

Rob is right on the spot.

"There is only one thing more painful than learning from experience and that is not learning from experience."
Archibald McLeish
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Wed May 05, 2004 1:00 am     Reply with quote

Thanks guys, I got it to work and it was much easier than I thought it would be, it works fine but you guys tell me how ugly this is;

Code:
 else if (PB_Plastic == 0)
   {
      PORTD =0x13; 

   }
      /* Timer Function "Plastic" */

      if (Timed1 == 1)   /*I assigned this earlier "#bit Timed1 = PORTD.4"*/
      {

      int T_Plastic = 20;



      while (T_Plastic > 0)
      {

         printf("%d\n", T_Plastic);
         --T_Plastic;

        delay_ms (60000);

      }

      if (T_Plastic == 0)
      {
     
      PORTD =0x0C; 
     
      }
      }

      /* End Timer Function "Plastic" */


Now when the button is pressed, the heat coil and fan activates and the time is displayed. it then counts down and 20 minutes later the heat coil turns off, the fan kicks into high speed and the vent baffle opens.

Now I have to hook up an LCD and make it work Shocked
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Wed May 05, 2004 7:56 am     Reply with quote

This portion will prevent you from servicing any button presses unless you do them as interrupts. And for that matter, the PIC can't be doing anything else unless it is an interrupt service routine. It is also going to have some accuracy problems, I predict it will tend to run a bit on the long side if for no other reason than the printf statement takes some finite amount of time so your delay is really (60,000 + time-printf + time-to-decrement + loop-overhead) * T_Plastic

Code:
while (T_Plastic > 0)
      {

         printf("%d\n", T_Plastic);
         --T_Plastic;

        delay_ms (60000);

      }


But, if it works and does what you need with the accuracy you need then fine. You have plenty of things to learn how to do and keep you busy.

You mentioned an LCD, if you are planning on a "standard" character based LCD with driver, the example programs supplied with the compiler are very good. You may need to re-arrange your port assignments so you can use the example code though. I think it is designed for PORTB and uses 6 of the 8 available lines. d
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed May 05, 2004 10:07 am     Reply with quote

Hi RatFink,

I�m feeling a kind of embarrassment regarding my previous comments, it wasnt
my intention to be ofensive in any way, just to steal a smile...

I want to compensate such fault. Feel free to drop me a line at
my email if you want some help with your project regarding the LCD upgrade.

Sincerely,

Humberto
RatFink



Joined: 01 May 2004
Posts: 49

View user's profile Send private message

PostPosted: Wed May 05, 2004 11:24 am     Reply with quote

rwyoung wrote:
But, if it works and does what you need with the accuracy you need then fine. You have plenty of things to learn how to do and keep you busy.


Running a few seconds off will not be a problem here, and according to my watch it seems to be pretty dang close to 20 minutes, but I did think about that having noticed you mentioned it in a previous post. This just worked out easiest for me at this time, I may make some changes if I have time later. It just makes me happy know that it does what it's supposed to.

rwyoung wrote:
You mentioned an LCD, if you are planning on a "standard" character based LCD with driver, the example programs supplied with the compiler are very good. You may need to re-arrange your port assignments so you can use the example code though. I think it is designed for PORTB and uses 6 of the 8 available lines. d


I know about the conflict between my inputs on PORTB and the LCD driver, but for some reason I'm having any other port just work, they all have some wierdness that requires extra hardware or something. I will have to deal with it though, it would be easier for me to change my 4 inputs than to try messing around with the default LCD driver.



Humberto wrote:
Hi RatFink,

I�m feeling a kind of embarrassment regarding my previous comments, it wasnt
my intention to be ofensive in any way, just to steal a smile...

I want to compensate such fault. Feel free to drop me a line at
my email if you want some help with your project regarding the LCD upgrade.

Sincerely,

Humberto


Don't worry, I took no offense to your post, and saw it as you just goofing around, but I may take you up on your offer of help w/ the LCD if I have trouble. Thank you for the offer.
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