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 support@ccsinfo.com

problem reading many switches with ELSE IF

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
meo tseto



Joined: 28 Feb 2010
Posts: 4
Location: montreal

View user's profile Send private message Send e-mail

problem reading many switches with ELSE IF
PostPosted: Sun Mar 21, 2010 1:03 pm     Reply with quote

Hi, I am new in code. I am trying to understand why my code is not working. In regards of everything I can read on many books and on the help file it should work. Sometime a little dot could change everything.
I am using a old version of PCWHD 4.084 I start to think maybe that could be part of my problem. My problem is on the hyperterminal I have for result only "SORTIE B0" or SORTIE B1" when I press on A0. The other switch have no impact.
Thank you
Code:

#include <16F628A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)
#use rs232(baud=9600,xmit=pin_B2,rcv=pin_B1,bits=8,parity=n)
#zero_ram


void main()
{
// set_tris_b(0x00); //set port b as outputs
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   while (True)
      {
       output_b (0x00);     
      if (input(PIN_A0))
         printf ("sortie B0\n\r");
      else if (input(PIN_A1))
         printf ("sortie B1\n\r");
      else if (input(PIN_A2))
         printf ("sortie B2\n\r");
      else if (input(PIN_A3))
         printf ("sortie B3\n\r");
      else if (input(PIN_A4))
         printf ("sortie B4 \n\r");
      else
         printf ("toute les sorties aux niveaux bas\n\r");
      }

}

_________________
Loi d’Incompréhension de Cooper

Si dans un article scientifique un mot vous échappe, ignorez-le.
Le texte est tout aussi compréhensible sans ce mot.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 21, 2010 1:44 pm     Reply with quote

Quote:

I am using a old version of PCWHD 4.084 I start to think maybe that
could be part of my problem.

That compiler version probably works OK with the 16F628.

Quote:

My problem is on the hyperterminal I have for result only "SORTIE B0"
or SORTIE B1" when I press on A0. The other switch have no impact.

Describe the external circuits that you have connected to the pins on
Port A.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Sun Mar 21, 2010 4:35 pm     Reply with quote

Shouldn't that be "Toutes les sorties"?

For this to work, you'd need pull-down resistors on all the pins you're testing, with pushbuttons connected between the pins and Vcc. Pullup resistors and buttons connecting to Gnd is a more common arrangement. As PCMP says, you need to explain what your external circuit is.
meo tseto



Joined: 28 Feb 2010
Posts: 4
Location: montreal

View user's profile Send private message Send e-mail

switch connectic
PostPosted: Sun Mar 21, 2010 6:27 pm     Reply with quote

Here are how my switch are connected.
The hardware work ok if I am doing a regular IF and ELSE (only one choice) But when I want to use many ELSE IF (many choices) that is when it doesn't work.
Code:


         VDD 5V   
            | 
            |     
            |           
            | 
            < 
            >  10K
            <     
            |
            |                         __________________
            |                         |               
            |                         |   PIC16F628A 
            |                         |   
            |_________________________|   Pin A0   
            |                         |   
            |                         |   
              |                       | 
              |   SWITCH NO           |   
              |                       |   
            |                         |   
            |                         |   
            |                         |
            |                         |   
           ----                       |   
            --                        |
            -                         |           

Thank you
_________________
Loi d’Incompréhension de Cooper

Si dans un article scientifique un mot vous échappe, ignorez-le.
Le texte est tout aussi compréhensible sans ce mot.
meo tseto



Joined: 28 Feb 2010
Posts: 4
Location: montreal

View user's profile Send private message Send e-mail

PostPosted: Sun Mar 21, 2010 7:21 pm     Reply with quote

Thank you for your fast answer. The answer from John P make me think to look more at how my switches are connected and I realised my switches were returning a state 1 instead of 0.
Code:

void main()
{
// set_tris_b(0x00); //set port b as outputs
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   while (True)
      {
       output_b (0x00);     
      if (!input(PIN_A0))
         printf ("sortie B0\n\r");//pin 17
      else if (!input(PIN_A1))
         printf ("sortie B1\n\r");//pin 18
      else if (!input(PIN_A2))
         printf ("sortie B2\n\r");//pin 01
      else if (!input(PIN_A3))
         printf ("sortie B3\n\r");//pin 02
      else if (!input(PIN_A4))
         printf ("sortie B4 \n\r");//pin 03
      else
         printf ("toute les sorties aux niveaux bas\n\r");
      }
}

So I correct the code but now I am reacting with a delay of one step, meaning when I press PIN AO I should read SORTIE B0 and when I press PIN A1 I should read SORTIE B1 and so.
Instead I have one step behind, meaning when I press PIN A1 I get SORTIE B0 and when I press PIN A2 I get SORTIE B1. I noticed also the switch PIN A0 is inop.
So with your help I resolve one but create another one Surprised
_________________
Loi d’Incompréhension de Cooper

Si dans un article scientifique un mot vous échappe, ignorez-le.
Le texte est tout aussi compréhensible sans ce mot.
meo tseto



Joined: 28 Feb 2010
Posts: 4
Location: montreal

View user's profile Send private message Send e-mail

It WORKING
PostPosted: Sun Mar 21, 2010 7:30 pm     Reply with quote

Hi guys,

Big thank you for your support everybody.
I just find a miss routed wire, shame on me Embarassed (pitcher error).

So now the code is working, I can wait to be no more a green and be able to help other like you and payback to the forum.

Have a great day Very Happy
_________________
Loi d’Incompréhension de Cooper

Si dans un article scientifique un mot vous échappe, ignorez-le.
Le texte est tout aussi compréhensible sans ce mot.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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