View previous topic :: View next topic |
Author |
Message |
alain.s Guest
|
led on-off help me |
Posted: Tue Jul 17, 2007 1:53 pm |
|
|
Hi i need to code C where a push button and LED is ON then push button and LED turn off....I used a 16f877....thnak you help me please :-)) |
|
|
Guest
|
|
Posted: Tue Jul 17, 2007 1:54 pm |
|
|
i forgot ....used a RA4 as button and RB3 as LED.....i using a microchip picdem2 plus |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
Posted: Wed Jul 18, 2007 12:10 pm |
|
|
THank you, i tryed but the led is on when i push but when i release go to off.....i saw the program that you post me but for example this:
#include <16F819>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock = 4000000)
//==================
void main()
{
char value;
port_b_pullups(TRUE);
while(1)
{
value = input(PIN_B4);
output_bit(PIN_B0, value);
}
while(1);
}
Why use "char value"?
I using a 16f877 and a button on pin R_A4 and a led on R_B3:
show you:
#include "C:\Documents and Settings\Stefano\Desktop\Progetto PIC\Progetto1.h"
void RS232_init(); //inizializza la seriale
void RS232_read(); //legge dalla porta seriale
void RS232_controlla(); //controllo se c'รจ qualcosa da leggere e lo legge
void RS232_OK(); //spedisce un ok con ritorno a capo
void RS232_putc(char ch); // inserisci carattere
#ZERO_RAM
#int_RTCC
void RTCC_isr()
{
// scrivere valore nel registro timer0
set_timer0(0x5A);
//generare frequenza su pin
output_b(U1);
}
#int_RDA
void RDA_isr()
{
RS232_read();
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
char value;
port_a_pullups(FALSE);
while(1)
{
value = input(P1);
output_bit(LED,value);
}
while(1);
// controllo pulsante
// controllo LED
// controllo rx stringhe complete (seriale) e spedizione risposta
}
this is .h
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//tasto
#define P1 PIN_A4
//led
#define LED PIN_B3
//uscita pin della frequenza
#define U1 PIN_A0
#define RS232_PIN_TX PIN_C6 // pin TX
#define RS232_PIN_RX PIN_C7 // pin RX
#define RS232_Buffersize 15 //size del Buffer
#define RS232_Parity N
#define RS232_Baudrate 9600
//#define RS232_CRLF {rs232_putc('\r'); rs232_putc('\n');}
static boolean buffin_ready=false;
static byte buffin_len=0;
static byte bufftemp_len=0;
static char bufftemp[RS232_buffersize];
static char buffin[RS232_buffersize];
static char value;
there is also something about RS232 beacause after i will use this.....sorry for bad english... |
|
|
Guest
|
|
Posted: Wed Jul 18, 2007 12:12 pm |
|
|
i forgot if i put
port_a_pullups(TRUE);
anyway give me ERROR |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 18, 2007 12:39 pm |
|
|
Use the routines in the 2nd link. Do something like this:
Code: |
void main()
{
int8 led_state;
led_state = 0;
output_low(LED_PIN);
while(1)
{
wait_for_keypress();
led_state = !led_state;
output_bit(LED_PIN, led_state);
}
} |
Port A doesn't have any internal pullups on it for your PIC.
That's why that function gives an error message. |
|
|
tony Guest
|
|
Posted: Wed Jul 18, 2007 2:13 pm |
|
|
You do realize RA4 has an open collector output. If you do not have a pullup on the pin the button may never look high. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 18, 2007 2:30 pm |
|
|
He's using pin A4 as an input.
Quote: |
used a RA4 as button
#define P1 PIN_A4
value = input(P1);
|
However, he does need a pull-up on that pin, to use it with a push-button
switch. If he is using a PicDem2-Plus board, it already has one on pin A4. |
|
|
Guest
|
|
Posted: Thu Jul 19, 2007 5:24 am |
|
|
mmmm always the same problem... |
|
|
|