View previous topic :: View next topic |
Author |
Message |
duke.sir
Joined: 02 Feb 2014 Posts: 4 Location: cameroon
|
using all analogue inputs of pic16f877a for adc |
Posted: Sun Feb 02, 2014 10:57 am |
|
|
hello!
I am designing a system which uses a keypad and an lcd plus pic16f877a MCU
It is to work as such:
If i press say key one on my keypad An0 should be active and is displayed on the lcd such that when i vary its potentiometer it should vary live on lcd.
When i press two, it should leave the loop and enter into the second case and vary live on lcd also.
The problem is when it enters into any case it won't leave the loop until i rerun the simulation.
Please help me with the C statement which can make my circuit work live without restarting my simulation.
I used
Code: |
#include <ADC.h>
#include <KBD_MLB.c>//contains definition for keypad(4by4)
#include <ctype.h>
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>
void main()
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
lcd_init();
kbd_init();
long int a,b,c,d,e,f,g,h;
float i,j,k,l,m,n,o,p;
char key;
while(TRUE)
{
key=kbd_getc();
switch(key)
{
case '1'://the array in KBD_MLB.C USES char for keypad
do{
set_adc_channel(0);
delay_ms(200);
a=read_adc();
i=(float)(5*a)/256;
printf(lcd_putc,"\f ANALOGUE:%01.2fV",i);
printf(lcd_putc,"\n DIGITAL:%ld",a);
}
while(key=='1');
break;
case '2':
do{
set_adc_channel(1);
delay_ms(200);
b=read_adc();
j=(float)(5*b)/256;
printf(lcd_putc,"\f ANALOGUE:%1.2fV",j);
printf(lcd_putc,"\n DIGITAL:%ld",b);
break;
while(key=='2');
/*odd inputs */
case '0':
printf(lcd_putc,"\f invalid input");
break;
case '9':
printf(lcd_putc,"\f invalid input");
break;
case 'O':
printf(lcd_putc,"\f invalid input");
break;
case '*':
printf(lcd_putc,"\f invalid input");
break;
case '+':
printf(lcd_putc,"\f invalid input");
break;
case '/':
printf(lcd_putc,"\f invalid input");
break;
case '-':
printf(lcd_putc,"\f invalid input");
break;
case '=':
printf(lcd_putc,"\f invalid input");
break;
}
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Sun Feb 02, 2014 11:32 am |
|
|
After you have read the key, you never read it again, so it's value will remain the same. So while(key==1) will always be true, and loop for ever. Same applies with every other value you use...
Also learn to use the code buttons, and read the data sheet about when you can use ADC_CLOCK_INTERNAL. You don't tell us your clock rate, but it is almost certainly not legal....
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Feb 02, 2014 11:34 am |
|
|
1) Are you using REAL hardware or computer SIMULATION?
2) Please use the code buttons to retain formatting.
Mike |
|
|
duke.sir
Joined: 02 Feb 2014 Posts: 4 Location: cameroon
|
|
Posted: Mon Feb 03, 2014 7:39 am |
|
|
i am using proteus for the simulation and for this purpose i use 20mhz as my internal clock frequency. |
|
|
duke.sir
Joined: 02 Feb 2014 Posts: 4 Location: cameroon
|
|
Posted: Mon Feb 03, 2014 7:44 am |
|
|
thanks you all for your support atleast am happy.
but really what i need is if some one can suggest a particular c statement that can make me live the loop of each case.
thanks and God bless you!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Mon Feb 03, 2014 8:41 am |
|
|
You are missing the point.
There is no simple C statement, you need to change your code, so it checks the key again. This will depend on how the key data is actually being 'done' (you are receiving it via RS232, but what happens when the key is held down - does the source keep sending characters?).
You can loop for ever, exit immediately, etc. etc.. These are all decisions made by _your_ code. Currently though the code keeps looping, and never tests the key again, so 'of course' it'll loop for ever. Just remove the while statements, and it'll respond to the next character, but will only send the reply once for each key character. If your source sends has 'key repeat', so will keep sending the key value, for as long as the key is held down, this is all you need to do.... |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Feb 03, 2014 9:56 am |
|
|
duke.sir wrote: | i am using proteus for the simulation and for this purpose i use 20mhz as my internal clock frequency. | Waste of time. Do a search on this forum for confirmation.
Mike |
|
|
duke.sir
Joined: 02 Feb 2014 Posts: 4 Location: cameroon
|
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
|
|