CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

led on-off help me

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
alain.s
Guest







led on-off help me
PostPosted: Tue Jul 17, 2007 1:53 pm     Reply with quote

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








PostPosted: Tue Jul 17, 2007 1:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 17, 2007 3:12 pm     Reply with quote

Here's a program where the LED follows the state of the push-button:
http://www.ccsinfo.com/forum/viewtopic.php?t=20526&start=3
That's not exactly what you asked for.

Here's a routine that waits for a button to be pressed.
You can toggle a variable every time the button is pressed, and
then output the state of the variable to the LED.
http://www.ccsinfo.com/forum/viewtopic.php?t=19874&start=1
Guest








PostPosted: Wed Jul 18, 2007 12:10 pm     Reply with quote

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








PostPosted: Wed Jul 18, 2007 12:12 pm     Reply with quote

i forgot if i put

port_a_pullups(TRUE);


anyway give me ERROR
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 18, 2007 12:39 pm     Reply with quote

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







PostPosted: Wed Jul 18, 2007 2:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 18, 2007 2:30 pm     Reply with quote

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








PostPosted: Thu Jul 19, 2007 5:24 am     Reply with quote

mmmm always the same problem...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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