|
|
View previous topic :: View next topic |
Author |
Message |
pretyh8machin
Joined: 18 Oct 2011 Posts: 4
|
ADC / Keypad help |
Posted: Mon Nov 07, 2011 1:56 am |
|
|
Hey CCS forum users,
So I have been working on this project for class where I have to control the direction of a motor using a H-bridge circuit connected to with my PIC. I have a keypad that input that changes these values just fine. I broke down my code into sections so I can determine where I am getting any errors or problems with the code. I got my ADC to display a value on my LCD too so I know my ADC is working fine also. For some reason when I combined the code altogether I found out that as soon as I initialized my adc converter my keypad stopped responding. I couldn't figure out why. Heres my code:
Code: |
#include <18f4580.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP, HS, MCLR
#use delay(clock=8000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
#include <flex_kbd.c>
#include <flex_lcd.c>
#define BRAKE PIN_C4
#define DIRECTION PIN_C5
//!#int_rda
//!
//!void rda_isr(void)
//!{
//! lcd_init(); // Always call this first.
//! kbd_init();
//! long value1;
//! setup_port_a(ALL_ANALOG);
//! setup_adc(adc_clock_internal);
//! setup_ccp1(CCP_PWM);
//! setup_timer_2(T2_DIV_BY_1, 127, 1);
//! set_adc_channel(0);
//! delay_us(10);
//! value1=read_adc();
//! lcd_gotoxy(1, 1);
//! printf(lcd_putc,"\f%2X ", value1);
//! set_pwm1_duty(value1);
//! delay_ms(100);
//!}
void housekeeping();
void startmenu();
void optionbegin();
void displaybegin();
void menudisplay();
void operation();
void adc();
long value1;
void main() {
housekeeping();
//enable_interrupts(INT_RDA);
//enable_interrupts(GLOBAL);
setup_port_a(ALL_ANALOG);
setup_adc(adc_clock_internal);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 127, 1);
//startmenu();
//menudisplay();
//adc();
operation();
}
void housekeeping(){
lcd_init(); // Always call this first.
kbd_init();
output_high (BRAKE);
}
void startmenu(){
displaybegin();
optionbegin();
}
void displaybegin(){
lcd_putc( "\fPress 1 to begin");
}
void optionbegin(){
char enter;
int i=0;
while (i!=1){
enter=kbd_getc();
if (enter=='1') {
i=1;
} else {
i=0;
}
}
}
void menudisplay(){
int i;
lcd_gotoxy(1, 1);
lcd_putc( "\fPress '1'\n");
lcd_gotoxy(1, 2);
lcd_putc( "to start brake");
delay_ms(1000);
lcd_gotoxy(16, 2);
for(i = 0; i < 20; i++)
{
printf(lcd_putc," \b\b");
delay_ms(150);
}
lcd_gotoxy(16, 1);
for(i = 0; i < 20; i++)
{
printf(lcd_putc," \b\b");
delay_ms(150);
}
lcd_gotoxy(1, 1);
lcd_putc( "\fPress '2'\n");
lcd_gotoxy(1, 2);
lcd_putc( "to stop brake");
delay_ms(1000);
lcd_gotoxy(16, 2);
for(i = 0; i < 20; i++)
{
printf(lcd_putc," \b\b");
delay_ms(150);
}
lcd_gotoxy(16, 1);
for(i = 0; i < 20; i++)
{
printf(lcd_putc," \b\b");
delay_ms(150);
}
lcd_gotoxy(1, 1);
lcd_putc( "\f'3' and '4'\n");
lcd_gotoxy(1, 2);
lcd_putc( "change direction");
delay_ms(1000);
lcd_gotoxy(16, 2);
for(i = 0; i < 20; i++)
{
printf(lcd_putc," \b\b");
delay_ms(150);
}
lcd_gotoxy(16, 1);
for(i = 0; i < 20; i++)
{
printf(lcd_putc," \b\b");
delay_ms(150);
}
}
void operation(){
char k;
//! value1=read_adc();
//! set_pwm1_duty(value1);
//! lcd_putc( "\fPress '1'\n");
while (TRUE){
k=kbd_getc();
if(k!=0){
if(k=='2')
output_low (BRAKE);
if(k=='1')
output_high (BRAKE);
if(k=='4')
output_low (DIRECTION);
if(k=='3')
output_high (DIRECTION);
}
}
}
void adc(){
setup_port_a(ALL_ANALOG);
setup_adc(adc_clock_internal);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 127, 1);
}
|
I commented out the irrelevant sections because thats just a menu display. Any help is appreciated. Thnx in advance. The Keypad works up until I initialize the ADC. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Mon Nov 07, 2011 3:22 am |
|
|
The 'key thing' you have not shown, is the start of the flex_kbd driver, defining which pins are used for the keypad.
Don't initialise the ADC at multiple places. Do it _once_, either at the start of main, or in an init routine. Then 'think'. What ADC pins are you using? (again you don't say). Does it make sense to setup _all_ the ADC pins (on your chip that means AN0 to AN10, the whole of portA, and B0,B1 & B4). If any of these pins are are used as digital inputs, they _won't_ function after this is done....
I'd guess at least one of these pins is being used for the keypad....
Since you only seem to be reading AN0, use
setup_adc_ports(AN0);
Also, at your clock rate, ADC_CLOCK_INTERNAL, is _not_ recommended look at table 19-1 in the data sheet, and use:
setup_adc(ADC_CLOCK_DIV_16);
Best Wishes |
|
|
|
|
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
|