View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Int rda problem |
Posted: Sun Sep 20, 2015 8:26 pm |
|
|
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: 19588
|
|
Posted: Mon Sep 21, 2015 12:27 am |
|
|
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
|
|
Posted: Mon Sep 21, 2015 1:00 am |
|
|
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
|
|
Posted: Tue Sep 22, 2015 12:01 am |
|
|
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
|
|
Posted: Tue Sep 22, 2015 5:51 am |
|
|
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: 1358
|
|
Posted: Tue Sep 22, 2015 7:00 am |
|
|
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.
This isn't valid. "int_rda" isn't a variable. You need to rethink this. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Tue Sep 22, 2015 5:40 pm |
|
|
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
|
|
Posted: Sun Sep 27, 2015 1:55 pm |
|
|
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
|
|
Posted: Sun Sep 27, 2015 4:55 pm |
|
|
Hi,
Just out of curiosity, what do you guys, art & guy, think this line does:
_________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Mon Sep 28, 2015 1:20 am |
|
|
Return(0) is not recommended in main() 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
|
|
Posted: Mon Sep 28, 2015 2:26 am |
|
|
Dear guy,
Thank you for your code. It works. |
|
|
|