View previous topic :: View next topic |
Author |
Message |
khairul786
Joined: 11 Feb 2009 Posts: 5 Location: High City .jb
|
Problem with library of utility functions- CCS Example |
Posted: Wed Apr 13, 2011 1:26 am |
|
|
Compile no 0 error and 2 warnings but after run it does not work. It should be, Check that with each button press, the LEDs increment in a binary number 0-7 as shown here. LED on binary sequence, 001,010...
Hope somebody can help. Thank You
Code: |
EX5
....................................
#include <prototype.h>
#include <utility.c>
void main() {
int count = 0;
//int n = 1;
while(TRUE) {
show_binary_on_leds(count);
wait_for_one_press();
count++;
}
}
.........................................
prototype.h
........................................
#include <18f4520.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#define GREEN_LED PIN_A0
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_B1
................................................
utility.c
.........................................
wait_for_one_press() {
while(input(PUSH_BUTTON)) ;
while(!input(PUSH_BUTTON)) ;
}
show_binary_on_leds(int n) {
output_high(GREEN_LED);
output_high(YELLOW_LED);
output_high(RED_LED);
if( bit_test(n,0) )
output_low(GREEN_LED);
if( bit_test(n,1) )
output_low(YELLOW_LED);
if( bit_test(n,2) )
output_low(RED_LED);
}
|
thank you _________________ bla -bla bla |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Apr 13, 2011 1:50 am |
|
|
What are the warnings?
What does it do?
Your problem is most likely due to bounce.
Do you find that when you press the button that the LEDS count up realy fast, prob too fast to notice and then stop at some random value?
This is because as you press the button it doesn't do a clean make/break but will do several, possibly 100s within a very short time. you need to account for this:-
Code: |
while(input(PUSH_BUTTON)); // Wait for press
delay_ms(10); // ignore bounce
while(!PUSH_BUTTON); // Wait for lift
delay_ms(10); // ignore bounce
|
Something like that.
But then again it could be your hardware.
Without knowing the symptoms it is hard to say. |
|
![](templates/subSilver/images/spacer.gif) |
khairul786
Joined: 11 Feb 2009 Posts: 5 Location: High City .jb
|
|
Posted: Wed Apr 13, 2011 3:20 am |
|
|
I try that all already. The warning
1. Utility.c "Line 2[1,19]: Function not Void and does not return a value wait for one press.
2. Utility.c "Line 11[1,20]:Function not valid and does not return a value show_binary_on_leds.
Hardware is verify for program with out call function, it ok. [/url] _________________ bla -bla bla |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Apr 13, 2011 5:22 am |
|
|
So if you dont call the wait_for_one_press() function in main the leds flash ?
I suspect your putton is not working, prob due to hardware.
how have you got it wired up?
Pull UP, Pull down? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Wed Apr 13, 2011 7:40 am |
|
|
The warnings, are not your problem.
They say exactly 'why' they are occurring. You have declared all your functions without any 'return' type. By default in this case, C assumes a function returns an int. Yet you then return nothing. So the warnings.
There is a 'void' return type to specifically say 'this function does not return a value'. Use it.
Code: |
void wait_for_one_press(void) {
while(input(PUSH_BUTTON)) ;
while(!input(PUSH_BUTTON)) ;
}
|
However these are just _warnings_, not errors. They are saying 'you are not declaring this right - may matter or not'. Since you don't use the return value, it doesn't matter at all.
I have to agree with Wayne. Most likely problem is the button. Either the pull up resistor you have used is not pulling the pin properly high (you _do_ have a pull up resistor on the input.....), or the button is not pulling the signal low.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
khairul786
Joined: 11 Feb 2009 Posts: 5 Location: High City .jb
|
|
Posted: Wed Apr 13, 2011 10:03 pm |
|
|
Thank you very much.. IT solve after put VOID... ![Razz](images/smiles/icon_razz.gif) _________________ bla -bla bla |
|
![](templates/subSilver/images/spacer.gif) |
|