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

gear check using 2 switches
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Dec 26, 2012 4:17 am     Reply with quote

You're still ignoring most of the advice you've been given and the requests for more information.

1) Still no schematic.
2) No detail on what happens to switches when gear lever is pressed.
3) No account taken of switch bounce.
4) ...............

OK, you've found out how to drive the code button.
That's now revealed that you have not learned how to indent code to make it more readable.

Code is easier to trace if after each opening {, the following code is further indented 'til it meets the closing }.

It's still not easy to follow yours because you have to scroll.
Try to write each module/routine so that it is all viewable at once on the screen.
The whole thing is still too complex.
You have to learn how to debug code one section at a time.
Break the problem into small easy to manage sections.
Get each section to work on it's own.
Then put them together.
Have a look with a 'scope at what happens when you depress a switch.
You're PIC has a hardware UART. Use that to tell a PC what's going on.

You could try something like this:-
Code:

Main()

{
   initialise();               // set up oscillator etc
   while (1)                   // loop here forever   
   {
       read_switches();        // sets new_switch_flag on a valid switch change
       if (new_switch)
       {
          find_new_gear();     // work out new gear
          display_gear();      // shows gear
       }
       delay_ms(1);            // provides time-keeping
   }
}


Loop goes round roughly once per millisecond

Code read_switches(), reads switches and compares to state last time round.
With two switches, there are only four switch_states 0,1,2,3.
If state is same for a few ms in succession, then switch_state is valid (i.e. stopped bouncing).
If valid switch_state is different from previous one then sets new_switch_flag.

Code find_new_gear() uses switch_state and current gear to determine new gear, and reset new_switch_flag.

Display_gear turns ALL LEDs off, then lights ONE only.

Mike

EDIT I fear this project is going nowhere fast.
[email protected]



Joined: 23 Jun 2012
Posts: 22

View user's profile Send private message

PostPosted: Thu Dec 27, 2012 11:04 am     Reply with quote

I think switch bouncing and delay is the problem in my code.
Is it possible by using analog inputs?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Dec 27, 2012 11:17 am     Reply with quote

[email protected] wrote:
I think switch bouncing and delay is the problem in my code.
Is it possible by using analog inputs?
Of course it's possible to use analogue inputs.

A switch is essentially a digital device, so what makes you think that using the A2D will magically solve your problem?

I've already outlined how to deal with switch bounce.

A search on this forum will yield loads of other suggestions, code samples, explanations etc.

Where in your system do you think there is a delay issue?

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9240
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Dec 27, 2012 11:22 am     Reply with quote

Of course you 'could' use analog inputs BUT that opens up a HUGE problem concerning analog 'noise' which can easily be worse than simple switch debounce noise!!!
Mike , and others have patiently provided you with a lot of valuable knowledge and tips on how to do this simple task.
Simply apply the 'words of wisdom' and build a simple 2 switches--PIC setup on your bench.Don't even think about trying on the '2wheeler'' until the bench setup works flawlessly.
Do more research either on the Web,PIC sites,the examples CCS supplies,etc. you're NOT the first one trying to get 2 switches to interface to a PIC.

hth
jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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