|
|
View previous topic :: View next topic |
Author |
Message |
aymanoo
Joined: 26 Aug 2011 Posts: 6
|
timer 0 help |
Posted: Wed Sep 07, 2011 5:44 am |
|
|
The program below is about to press a push button as quickly just after the led turn on ......there's something I don't get in the code below.
Because as the code says the timer start after we push the button while(input(pin_b3)==1, but I mean the timer should stop and be read when the input is one, not start when the input is one. And another question, the while(input(pin_b3 ) will be executed once only because this is a push button...am I right ?...thx
Code: |
#include<16f887.h>
#use delay (clock=4000000)
void main()
{
unsigned char cnt8ms;
setup_timer_0(rtcc_internal|rtcc_div_32);
while (true)
{
output_b(0);
delay_ms(2000);
output_high(pin_b2);
cnt8ms=0;
while(input(pin_b3)==1&&cnt8ms<1000/8)
{
set_timer0(0);
while(get_timer0()<800/32);
++cnt8ms;
}
if(cnt8ms<200/80)
output_high(pin_b1);
delay_ms(1000);
}
} |
_________________ AYMAN |
|
|
aymanoo
Joined: 26 Aug 2011 Posts: 6
|
problem solved |
Posted: Thu Sep 08, 2011 7:08 am |
|
|
I solved my problem ..it was that when we say input(bin_b0)==1 this means the switch is open ....i thought that 1 means switch close +5v _________________ AYMAN |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Sep 08, 2011 8:33 am |
|
|
Most hardware makes simple switches, including push buttons, "active low" like that. The switch is between 0V and the input port, with a pull-up resistor that pulls up the input when the switch is disconnected to +5V (or +3.3V or whatever your suply votlage is). This is common and pretty much standard. it dates back a long way to the TTL years and before when logic generally had asymetric drive capability. TTL would pull down to close to 0V well, at typically 24mA for logic 0, but would only source a tenth of that, 2.4mA for logic 1. Also unconnected inputs would naturally and predictably float high. Thus the high state was naturally taken as the inactive state, and low as the active state. Leaving most signals high, generally resulted in a little lower power dissapation too. Also, if there was a fault the inputs floated high, so active low logic resulted in a greater degree of failsafe behaviour. So active low logic was common amongst professional logic designers.
CMOS, as in modern PICS has a symmetric drive, i.e. outputs can source or sink the same current, and inputs float all over the place. So there is no inherent bias towards active high or active low logic circuits. Designers, especially the younger ones, tend therefore to use the more intuitive active high logic, and assume, like you, that "1" is "on". As you've found, that may not always be the case.
Incidentally "active low" logic still shows itself in PICs. MCLR is active low, and has to be pulled up externally to allow the PIC to run. I2C is also "active low" in a sense - actually its open collector/open drain. Also RS232 involves inversion, the logic level version is generally inverted compared to the true RS232 signal: the drivers do the inversion and the "space" state, the idle state, is negative, i.e. the input to the inverting driver is high. That's classic active low logic. A so-called break is when the line is driven to the mark state for greater than a character time or three. It requires the RS232 driver to be actively driven.
Also input(Pin) gives a boolean result, 1 or 0, true or false. There's no point in comparing it to 1... or 0: it already IS 1 or 0. Therefore you can use it directly in ifs:
Code: |
if (input(pin))
{
// Do stuff if pin is high.
}
|
I even, when I started my latest job encountered this:
Code: |
if ((input(pin) == 1) == TRUE)
{
}
|
...err someone didn't understand something... did they? Luckily the compiler DID understand it and produced the same code as if they'd have written:
Code: |
if (input(pin))
{
}
|
RF Developer. |
|
|
|
|
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
|