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

Entering ADC while loop

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Entering ADC while loop
PostPosted: Mon Sep 28, 2015 5:18 pm     Reply with quote

Hi, i'm using 'ENTER' key on keyboard to exit ADC while loop for the first time. It can exit the loop and and display "Enter 'Z' for ADC or 'z' for RELAY :". But the main problem here is when I enter 'Z' once again, it cannot enter the ADC loop and keep repeating "Enter 'Z' for ADC or 'z' for RELAY :".
What is wrong with my code.


Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>




main()
   {
      char ch;
      unsigned char key;                   
      float  value, min;
      int8 binary_string[10];

         while(true)
           {
              printf("\n\n");
              printf("Enter 'Z' for ADC or 'z' for RELAY : \n");
              ch=getc();

              if(ch=='Z')
                 {
                     while (key!=13)
                           {

                        setup_port_a( ALL_ANALOG );
                        setup_adc( ADC_CLOCK_INTERNAL );
                        set_adc_channel( 0 );

                             if(kbhit())
                              {key=getch();}

                                value = Read_ADC();
                                min=value*5/1023 ;
                                printf("\r%2.3f",min);
                                delay_ms(1000);
                           }
                 }


              if(ch=='z')
                 {
                     while (1)
                           {
                             printf("Enter Relay Number : \n");
                             get_string(binary_string,9);
                             output_B((int8)strtoul(binary_string,0,2));
                           }
                 }
           }






   }
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Entering ADC while loop
PostPosted: Wed Sep 30, 2015 3:42 pm     Reply with quote

The problem is you do not initialize the "key" variable before checking it at the top of the ADC loop. Once it gets set to 13 it stays set to 13 forever. It can never get changed. Add key=0 just after the 'Z' is recognized.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Thu Oct 01, 2015 8:27 pm     Reply with quote

Dear RLScott,

It works. Thank you very much...
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