| View previous topic :: View next topic |
| Author |
Message |
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Fri Dec 31, 2010 12:51 pm |
|
|
| Elie777 wrote: | | Aren't they the same? |
If you're still using your code from the first post, then no. My code is about 20 lines to start debugging, yours is about 200 lines that you're trying to get working all at once.
Why don't you post what you're using now and describe the output and how it differs from what you'd like/expect. |
|
 |
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Sat Jan 01, 2011 5:04 am |
|
|
OK now I'm using this code:
| Code: | while(true)
{
if( (input(pin_A0))&&
(!input(pin_A1))&&
(input(pin_A2))&&
(!input(pin_A3))&&
(input(pin_A4)))
{
output_high(pin_B6);
}
if( (input(pin_A0))&&
(input(pin_A1))&&
(input(pin_A2))&&
(!input(pin_A3))&&
(input(pin_A4)))
{
output_low(pin_B6);
}
}
} |
when the input is 10101 the light is turned on, when this input changes the light turns off. but i want the light to stay on unless my input is 11101. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Sat Jan 01, 2011 5:30 am |
|
|
Why not work directly with the whole port?:
| Code: |
#include <16F84A.h>
//You don't need a device here, it is included in the include file
//However you _do_ need fuses. oscillator, watchdog.
#define ON_MASK (0b10101)
#define OFF_MASK (0b11101)
void main(void) {
int8 tempb;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
while (true) {
temp=input_b() & 0b11111; //Only look at bottom 5 bits
if (temp == ON_MASK) output_high(PIN_B6);
if (temp == OFF_MASK) output_low(PIN_B6);
delay_ms(1000);
}
}
|
Simpler, easier to write, and less likely to lead to getting confused about the logic...
I suspect your existing code _does_ work, but because you are not disabling the watchdog, this is triggering, and you are restarting, which sets B6 as an input, and turns the LED off.
Best Wishes |
|
 |
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Sat Jan 01, 2011 5:44 am |
|
|
You are 100% right. I was enabling the watchdog which was causing the problem. Now it is working like a charm.
Thanks very much for your helpful reply Tttelmah.
Do u think I should change my code and work with the whole port as you indicated. Is there any benefits? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Sat Jan 01, 2011 5:58 am |
|
|
Well, you'd need to use port A, not port B (which I used in my posting by mistake.....).
It is much less likely to be 'wrong' if you need to change the pattern, becomes 'self documenting' as to what you are looking at.
Avoids a problem if bits change between one test and the next, all are sampled 'at once'.
Code will be faster, and smaller (reduces to a total of 22 instructions for the 'bytewide' test, from 58 instructions for the individual bit tests....).
Tidy coding is almost always more reliable, and faster.
Best Wishes |
|
 |
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Sat Jan 01, 2011 6:13 am |
|
|
Thanks for your advice.
Do you think I can turn on and off the light using the same input.
First time turn on, second time turn off.
I tried to use a counter but it didn't work.
Should I enable power up timer or disable it?
Thanks for your time. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Sat Jan 01, 2011 9:22 am |
|
|
To 'toggle' something, use a toggle!. So something like:
| Code: |
#include <16F84A.h>
//You don't need a device here, it is included in the include file
//However you _do_ need fuses. oscillator, watchdog.
#define CHANGE_MASK (0b10101)
void main(void) {
int8 tempb;
int1 toggle=0;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
while (true) {
temp=input_a() & 0b11111; //Only look at bottom 5 bits
if (temp == CHANGE_MASK) {
toggle ^=1; //Toggle 0,1,0,1,0,1 etc....
output_bit(PIN_B6,toggle);
}
delay_ms(1000);
}
}
|
Remember though, that if you hold the pin operated, the light will toggle once per second if done like this. This is then where the idea of checking for 'change' comes:
| Code: |
void main(void) {
int8 tempb;
int1 toggle=0;
int8 last_value;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
last_value=input_a() & 0b11111;
while (true) {
temp=input_a() & 0b11111; //Only look at bottom 5 bits
if ((temp == CHANGE_MASK) && (last_value!=temp)) {
//Here matches mask, and has _changed_ from last reading
delay_ms(10);
temp=input_a() & 0b11111; //Read a second time to avoid 'bounce'
if (temp==CHANGE_MASK) {
toggle ^=1; //Toggle 0,1,0,1,0,1 etc....
output_bit(PIN_B6,toggle);
}
}
last_value=temp;
//No delay really needed.
}
}
|
With this, the input is read, and if it has _changed_ from the last reading, a short delay is applied, then it is read _again_ to verify that it is still saying 'change'. If so, the output is then toggled.
Best Wishes |
|
 |
Elie777
Joined: 30 Dec 2010 Posts: 16
|
|
Posted: Sat Jan 01, 2011 3:31 pm |
|
|
| 10x for your replies they all were helpful. |
|
 |
bkamen
Joined: 07 Jan 2004 Posts: 1619 Location: Central Illinois, USA
|
|
Posted: Sat Jan 01, 2011 10:45 pm |
|
|
Please remember to go back to the original message and edit the topic.
"Help me plz" is a terribly unhelpful subject for those who might be search for a solution to the same problem.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
 |
|