 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Tue Feb 21, 2006 7:39 am |
|
|
| Quote: |
Well after understanding how the open / close ports are operating i have problems with code form mr. Humberto.
|
Using open / close when talking of inputs / outputs is unsuitable and confuse.
rwyoung kindly posted an easy explanation for the right use and applications of these words.
Please do not use open / close when you refer to a microcontroller's actions.
| Quote: |
if(microswitch_state & 0B00000010) // Read input(A1)
{
}
and if the jumper is set at A1, there is no check.
Same goes for some combination... Why?
|
How do you know that there is no check ? There is no code inside the brackets { ... }
Could you tell us how did you connected the jumper in A0..A1.. etc ?
Could you tell us how did you configure the Port_A ?
Could you post full code + headers ?
Humberto |
|
 |
Guest
|
|
Posted: Tue Feb 21, 2006 11:34 am |
|
|
| Code: | #include <16F84a.h>
#fuses XT, NOWDT, NOPROTECT
#use delay (clock=4000000)
set_tris_b(0b11111000); // b0,b1,b2 -- b5
set_tris_a(0b00011111); //a0-a4
#byte PortA = 5
#bit TRISP_0 = 0x85.4
//-----------------------------------------------------
int tipkaPRESSED()
{
static int1 laststate=0;
if (!bit_test(PortA,4) )
{
delay_ms(50);
if (laststate) {
laststate = 0;
return(1);
}
} else
laststate=1;
return (0);
}
int tipkaSTATE()
{
if (!bit_test(PortA,4) )
return 1;
else
return 0;
}
void tipkaINIT()
{
#asm
BSF TRISP_0
#endasm
}
//---------------------------------------
void main()
{
int mikro_state, on;
while (1){
tipkaINIT();
if (tipkaPRESSED())
//output_high(PIN_B0);
//delay_ms(500);
//output_high(PIN_B1);
mikro_state = input_a();
if(mikro_state & 0B00000001) // A0
{
output_high(pin_B1);
delay_ms(1000);
on =1;
}
if(mikro_state & 0B00000101) // A0,A2
{
output_high(pin_B1);
delay_ms(1000);
on=1;
}
}
} |
----------------------
I have connected the switch to B5 so i can test it, but there is no action so i have used switch from the picdem board ... A4.
---------------------
Answers:
| Quote: | | Could you tell us how did you connected the jumper in A0..A1.. etc ? |
On the same way that they are connected in end device. One part to 5V and other part to A port pins ( A0-A3 ). There are no resistors. Direct connection.
| Quote: | | Could you tell us how did you configure the Port_A ? |
As inputs
------------------------
I can not check any microswitch combination and microswitches are connected like i wrote, and i can not change this. This HW is what i have get. |
|
 |
Michel Guest
|
|
Posted: Sat Feb 25, 2006 5:27 am |
|
|
| come on guys, any suggestions ? |
|
 |
Ttelmah Guest
|
|
Posted: Sat Feb 25, 2006 8:01 am |
|
|
The reason for the lack of replies, is that you have been answered already.
Put a voltmeter on your input pins. What happens to the voltage?. Nothing...
You need _pulldown_ resistors on the pins (since your switches connect to 5v). Then when the switch is open, the inputs will go to 0v. Without these, the signals will simply float, and stay where they were for a long time.
Also, the code is about 10* the size it needs for what you are doing. You do not need to have assembler to access TRIS. It is available from the C, However the 'set_tris' lines need to actually be 'in' the program, not disconnected as you show.
Best Wishes |
|
 |
Guest
|
|
Posted: Mon Feb 27, 2006 5:35 am |
|
|
| Quote: | | You need _pulldown_ resistors on the pins (since your switches connect to 5v). | OK. I have done that but there is no result.
q1.
If i set_tris an this way:
| Code: | void mani (){
set_tris_a(0b00011111); // a0-a4
set_tris_b(0b11111000); // b0,b1,b2 -- b5
}
|
when i run the program i get lighted B0 and B1 LED ( not the B2 ) and i can not make check of the microswitches.
q2.
Regarding to your last replay (the code is about 10* the size ) im going in wrong direction. How to reduce the code to get the same effect. ? |
|
 |
Ttelmah Guest
|
|
Posted: Mon Feb 27, 2006 6:59 am |
|
|
I have added a load of comments:
| Code: |
#include <16F84a.h>
#fuses XT, NOWDT, NOPROTECT
#use delay (clock=4000000)
//-----------------------------------------------------
int tipkaPRESSED() {
static int1 laststate=FALSE;
//If your descripton of the wiring is right, and you have pull down
//resistors
//on the inputs, and switches connecting them to +5v, then the logic is
//reversed
if (!bit_test(PortA,4) )
//Clear laststate here
laststate=FALSE;
return FALSE;
else {
//debounce.
delay_ms(10);
if (laststate) {
//If you clear 'laststate' here, the code will retrigger if the key
//remains
//pressed. Normally only clear it when key is released.
return TRUE;
}
laststate=TRUE;
return(0);
}
}
//---------------------------------------
void main() {
int mikro_state, on;
while (1){
//You don't actually need this, since the pin is always set as an input,
//but this is how to turn the tris bit to '1' in C
output_float(PIN_A4);
//As posted, if 'tipkaPRESSED' goes true, it executes the
//mifro_state=input_a() line...
//put back demo code to avoid this.
if (tipkaPRESSED()) {
output_high(PIN_B0);
delay_ms(500);
output_high(PIN_B1);
}
mikro_state = input_a();
//You realisise that it'll stop checking 'tipkaPRESSED' while it is
//delaying...
//These two tests dplicate each other. A bitwise '&', is 'true' if any
//bit in the
//result is non-zero. The first test will evaluate true, and then the
//second will
//also evaluate as true. If you want to test for either bit, just use
//the second
//test if you want to find specific _patterns_, then use A & B == B.
if(mikro_state & 0B00000101) { // True if _either_ bit is on ...
{
output_high(pin_B1);
delay_ms(1000);
on=1;
}
}
}
|
Get the inputs changing level when the switches are active, first...
Best Wishes |
|
 |
|
|
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
|