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

Int rda problem

 
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

Int rda problem
PostPosted: Sun Sep 20, 2015 8:26 pm     Reply with quote

Hi, i'm trying to use int_rda function to exit ADC loop , but its not working. Whats wrong with my code?

Here is my code:


Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#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>

#int_rda
void rx_isr()
{
char c;
c=getc();
if(c=='Y')
{break;}


 main()
{
   float min, value;
   int8 binary_string[10];
   char ch;
 
   printf("Enter "Z" for ADC or "z" for binary number : \n");
       
        if(ch=='x')
        return(0);
        switch(ch)
        {


case 'Z':
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
    setup_port_a(ALL_ANALOG);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);

    do  {
           
                  value = Read_ADC();
                  min=value*5/1023;
                  printf(" Voltage : %2.3f V   NO: %3.0f ", min);
            }while(TRUE);


case 'z':   

   while(true)
             {       
               printf("Enter 8 binary number : \n");
               get_string(binary_string,9);
                     if(binary_string=='A')
                       {break; }
               output_D((int8)strtoul(binary_string,0,2));
             }


}
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19469

View user's profile Send private message

PostPosted: Mon Sep 21, 2015 12:27 am     Reply with quote

Layout your brackets logically, and the first fault becomes obvious:
Code:

#int_rda
void rx_isr(void)
{
   char c;
   c=getc();
   if(c=='Y')
   {
      break;
   }
////Duh. Where is the terminating bracket for the interrupt handler.....


In fact the 'break' does nothing. The code always exits for every character...

You need to set a flag to say "I have a 'Y'", and then test for this in the main. The logic is flawed.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Sep 21, 2015 1:00 am     Reply with quote

Dear ttelmah,

Can you show the code how to put 'Y' flag. Please...i really don't have idea.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Sep 22, 2015 12:01 am     Reply with quote

I have modified the code, but when i type 'Y' , " ADC STOP " not appear. The code can be compile. What should i do?


Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#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>

#int_rda
void rx_isr()
{
char c;
c=getc();
}


 main()
{
   float min, value;
   int8 binary_string[10];
   char ch;
 
   printf("Enter "Z" for ADC or "z" for binary number : \n");
       
        if(ch=='x')
        return(0);
        switch(ch)
        {


case 'Z':
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
    setup_port_a(ALL_ANALOG);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);

    do  {
           
                  value = Read_ADC();
                  min=value*5/1023;
                  printf(" Voltage : %2.3f V   NO: %3.0f ", min);
                  delay_ms(1000);
                  if((int_rda)=='Y')
                  printf(" ADC STOP \n");

            }while(TRUE);


case 'z':   

   while(true)
             {       
               printf("Enter 8 binary number : \n");
               get_string(binary_string,9);
                     if(binary_string=='A')
                       {break; }
               output_D((int8)strtoul(binary_string,0,2));
             }


}
}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Tue Sep 22, 2015 5:51 am     Reply with quote

Hi,

Do you have real hardware, or is this a Proteus exercise?

It's pretty clear from your program structure that you've done no troubleshooting! My advice is to greatly simplify your program to the point that you can receive and display individual characters. Only then should you attempt to make conditional decisions based on what you receive!
_________________
John

If it's worth doing, it's worth doing in real hardware!
jeremiah



Joined: 20 Jul 2010
Posts: 1342

View user's profile Send private message

PostPosted: Tue Sep 22, 2015 7:00 am     Reply with quote

Code:
switch(ch)


Take a look at the "ch" variable listed in the switch. Where do you set the ch variable? Does it have a known value in it? It's not the same as you interrupt's variable. I don't think your switch is gonna work.

Code:
if((int_rda)=='Y')

This isn't valid. "int_rda" isn't a variable. You need to rethink this.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Sep 22, 2015 5:40 pm     Reply with quote

Hi jeremiah,

I have test this code using real hardware. The switch(ch) is functioning. The only problem i cannot exit the adc loop.

Code:

  printf("Enter "Z" for ADC or "z" for binary number : \n");
       ch=getc();
        if(ch=='x')
        return(0);
        switch(ch)
        {


Sorry for mistyping.
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Sun Sep 27, 2015 1:55 pm     Reply with quote

Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#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>

char newCh=0;   //// global variable - accessible from ISR and Main

#int_rda
void rx_isr()
{
newCh=getc();      //// store new char in newCh
}


 main()
{
   float min, value;
   int8 binary_string[10];
   char ch;
 
   printf("Enter "Z" for ADC or "z" for binary number : \n");
       
        if(ch=='x')
        return(0);
        switch(ch)
        {


case 'Z':
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
    setup_port_a(ALL_ANALOG);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(0);

    do  {
           
                  value = Read_ADC();
                  min=value*5/1023;
                  printf(" Voltage : %2.3f V   NO: %3.0f ", min);
                  delay_ms(1000);
                  if(newCh=='Y')             //// check global variable
                  printf(" ADC STOP \n");

            }while(TRUE);


case 'z':   

   while(true)
             {       
               printf("Enter 8 binary number : \n");
               get_string(binary_string,9);
                     if(binary_string=='A')
                       {break; }
               output_D((int8)strtoul(binary_string,0,2));
             }


}
}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun Sep 27, 2015 4:55 pm     Reply with quote

Hi,

Just out of curiosity, what do you guys, art & guy, think this line does:

Code:

Return(0);

_________________
John

If it's worth doing, it's worth doing in real hardware!
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Mon Sep 28, 2015 1:20 am     Reply with quote

Return(0) is not recommended in main() Cool Nowhere to return to.
Art's question was: The only problem i cannot exit the adc loop. I tried to help him with his problem.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Sep 28, 2015 2:26 am     Reply with quote

Dear guy,

Thank you for your code. It works.
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