View previous topic :: View next topic |
Author |
Message |
hadium Guest
|
Trouble reading a button |
Posted: Thu Jul 27, 2006 1:26 am |
|
|
Hi! I have been using C18, and have just changed to CCS. I'm simply trying to toggle an LED using a button input. I can flash an LED through code, but not through the button.
Please see my code below:
[ while (1) {
while(input(PIN_A0)) {
output_high(PIN_D1);
delay_ms(500);
output_low(PIND1);
}
while(!input(PIN_A0)){
output_high(PIN_D2);
delay_ms(500);
output_low(PIN_D2);
}
}
]
Any help understanding why this might not work would be greatly appreciated!
Thanks! |
|
|
Guest
|
|
Posted: Thu Jul 27, 2006 1:28 am |
|
|
Sorry, there was an error in my code. Here is the revised version:
while (1) {
while(input(PIN_A0)) {
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
}
while(!input(PIN_A0)){
output_high(PIN_D2);
delay_ms(500);
output_low(PIN_D2);
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 27, 2006 1:42 am |
|
|
If you want the LEDs to flash, you've got to have a delay for both
the high and the low time. Add the lines shown in bold below.
Quote: |
while(input(PIN_A0))
{
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
delay_ms(500);
}
while(!input(PIN_A0))
{
output_high(PIN_D2);
delay_ms(500);
output_low(PIN_D2);
delay_ms(500);
}
} |
|
|
|
Guest
|
|
Posted: Thu Jul 27, 2006 2:14 am |
|
|
Thanks for your reply. I tried what you suggested, but no luck. I can't make the LEDs trun on at all!!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 27, 2006 2:25 am |
|
|
Post a test program. Show the code that you've already posted,
but also show the main() function, and all #include, #use, and #fuses
statements.
When post the code, highlight all of it and then press the "Code" button.
This will make it be formatted better.
Also, when you post, make that HTML is disabled. Look below the
window where you type in your post. There is a checkbox labeled:
Quote: | Disable HTML in this post |
Make sure it is selected. |
|
|
Guest
|
|
Posted: Thu Jul 27, 2006 2:30 am |
|
|
Thanks so much for your help. Here is my program. I used the project wizard to set it up.
Code: | #include "C:\Documents and Settings\simon\My Documents\Contract Work\DFS\Firmware\Prototyping\Basic Functions\LED Flash on Button Input\Button Input.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void main()
{
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
while (1) {
while(input(PIN_A0)) {
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
delay_ms(500);
}
while(!input(PIN_A0)){
output_high(PIN_D2);
delay_ms(500);
output_low(PIN_D2);
delay_ms(500);
}
}
}
|
Thanks again! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 27, 2006 2:45 am |
|
|
Can you post the #include, #use, and #fuses statements ?
They are likely to be in this file:
Also post your version of the compiler. |
|
|
Guest
|
|
Posted: Thu Jul 27, 2006 5:09 am |
|
|
Here's the .h file:
Code: | #include <18F458.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOCPB //No Boot Block code protection
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9)
|
I ordered the PIC18 version of the compiler today, but have been using the demonstration version until they email me the link to download it.
Thanks so much again for your help! |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Jul 27, 2006 8:08 am |
|
|
Try NOLVP in your fuses. This one bit me when I first started using CCS.
Ronald |
|
|
Guest
|
|
Posted: Thu Jul 27, 2006 5:38 pm |
|
|
Dear Ronald,
Thank you so much for your suggestion - it works now!!!
Thanks again - I really appreciate the help! |
|
|
|