| | |
how to detect push buttons
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2007
Posts: 2
Reputation:
Solved Threads: 0
Hi..
I'm new in programming microcontroller in c. And I was given an assignment to write a simple program to turn 'ON' and "OFF' the LED by pressing push buttons. The microcontroller i'm using is PIC18F4620. And i'll need 1 LED and 2 push buttons to program this. I have already tried to write the program but it doesnt work. Here is my program:
the underline statement is the one i not sure whether is correct or not. I have being working on this for a week. But still cant get the program run correctly. Hope to get some guide. Thank.
I'm new in programming microcontroller in c. And I was given an assignment to write a simple program to turn 'ON' and "OFF' the LED by pressing push buttons. The microcontroller i'm using is PIC18F4620. And i'll need 1 LED and 2 push buttons to program this. I have already tried to write the program but it doesnt work. Here is my program:
#include <p18f4620.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#define btn_on PORTBbits.RB4
#define btn_off PORTBbits.RB5
#define led PORTAbits.RA0
void on(void);
void off(void);
void main(void)
{
TRISA = 0; //set Port A(LED) as output
PORTAbits.RA0 = 0; //reset LED
while(btn_on != btn_off) //wait for btn press
if (btn_on = 1) //btn on pressed
on();
if (btn_off = 1) //btn off pressed
off();
}
void on(void)
{
while(!btn_on); //wait for btn(RB4) released
PORTAbits.RA0 = 0x0F; //on LED
}
void off(void)
{
while(!btn_off); //wait for btn(RB5) released
PORTAbits.RA0 = 0x00; //off LED
}the underline statement is the one i not sure whether is correct or not. I have being working on this for a week. But still cant get the program run correctly. Hope to get some guide. Thank.
Last edited by Narue; Jun 18th, 2007 at 11:13 am.
>while(btn_on != btn_off) //wait for btn press
I get this tingly sensation that you should be using braces for this loop. That is, of course, if you want both conditional statements to be a part of the loop. If you only care about the first conditional, you're okay.
>if (btn_on = 1) //btn on pressed
Are you really sure you want to assign 1 to btn_on here? This is usually a bug where people mean to use == for comparison.
>if (btn_off = 1) //btn off pressed
Ditto.
I get this tingly sensation that you should be using braces for this loop. That is, of course, if you want both conditional statements to be a part of the loop. If you only care about the first conditional, you're okay.
>if (btn_on = 1) //btn on pressed
Are you really sure you want to assign 1 to btn_on here? This is usually a bug where people mean to use == for comparison.
>if (btn_off = 1) //btn off pressed
Ditto.
Last edited by Narue; Jun 18th, 2007 at 11:14 am.
I'm here to prove you wrong.
C Syntax (Toggle Plain Text)
void main(void)
C Syntax (Toggle Plain Text)
int main( void )
Here's some reading about it.
> void main(void)
Actually, it may be just fine as it is.
> The microcontroller i'm using is PIC18F4620.
This qualifies as a "freestanding" implementation. So long as main, or whatever the documented entry point is, is declared in a manner consistent with that implementation, then it should be good.
http://c-faq.com/ansi/voidmain.html
There's unlikely to be anything in the environment waiting for the result.
Actually, it may be just fine as it is.
> The microcontroller i'm using is PIC18F4620.
This qualifies as a "freestanding" implementation. So long as main, or whatever the documented entry point is, is declared in a manner consistent with that implementation, then it should be good.
http://c-faq.com/ansi/voidmain.html
There's unlikely to be anything in the environment waiting for the result.
![]() |
Similar Threads
- Want to learn LINUX..some help required (Getting Started and Choosing a Distro)
- C++ Help (C++)
- Web Hosts: Helping us navigate through this future techno-world. (IT Professionals' Lounge)
- error message when connecting to internet (Windows NT / 2000 / XP)
- Programming Professor (IT Professionals' Lounge)
Other Threads in the C Forum
- Previous Thread: diference between breadth fisrtsearct and depth first search
- Next Thread: detecting pushbutton
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






