Thread: LED switch on when error detected

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    1

    LED switch on when error detected

    Hi

    I'm currently doing a project which will trigger an alarm and light up an LED when a character 'E' is detected from a bluetooth input. I'm supposed to program a pic18f4525 board.

    I'm having troubles with where to start on the codes to enable the LED to light up upon detection of character 'E'. I am new to this sort of programming and I need some assistance.

    I'm using ports RD0-RD3. This is how I initialised them in the main loop.

    Code:
     TRISD = 0b11000000;
    	PORTD = 0xC0;
    Am I doing the initialising right? Also, can someone please give me a kickstart on how to program it to light up when the character's detected?

    Thank you so much for the help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    My advice would be to just start with the program which turns the LED on.

    Then enhance it to turn the LED off again.

    Then enhance it to turn the LED on when some condition is detected.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by g.21 View Post
    I'm using ports RD0-RD3
    Usually, pin number 0 corresponds to the least significant bit. For example, see here. Better yet, look at the documentation provided by your compiler and/or microcontroller manufacturer.

    You're using the other bit labeling order.

    On a PIC, initializing pins RD0 to RD3 can be done using two ways: either pin by pin, or all pins (of port D) at once. TRIS is set to zero for outputs, nonzero (one, or bits set) for inputs. Thus, to set D0..D3 to outputs, you can use
    Code:
    TRISD0_bit = 0;
    TRISD1_bit = 0;
    TRISD2_bit = 0;
    TRISD3_bit = 0;
    or, setting D0..D7 as outputs,
    Code:
    TRISD = 0;
    Similarly, you can set each output pin state, or read each input pin state, either pin by pin, or the entire 8-pin port at once. This changes the pin D2 state:
    Code:
    RD2_bit = !RD2_bit;
    and this sets D0, D2, D4, and D6, and clears D1, D3, D5, and D7:
    Code:
    PORTD = 85; /* 0b01010101 */
    Unlike normal C programs, you must never return from your main(), but loop forever. For example:
    Code:
    void main(void)
    {
        unsigned char state = 0;
    
        /* Set D0 .. D7 as outputs. */
        TRISD = 0;
    
        while (1) {
    
            /* Set new state. */
            PORTD = state;
    
            /* Flip bits */
            state++;
    
            /* Delay 125ms. */
            Delay_ms(125);
         }
    }
    You'll probably have to add an #includes, but it depends on the compiler, environment, and/or PIC type, so I leave that up to you. (Sometimes the delay routines are named Delay_ms() (microc), or delay_ms(), and sometimes you need to include some other header to get it. It just depends on what tools you are using; the names vary a bit, but the logic is pretty much the same.)

    The example should blink D0 four times a second (125ms = 0.125s on, then 0.125 off, so 1/(0.125s+0.125s) = 1/0.25s = 4 s-1 = 4 Hz), D1 twice a second, D2 once a second, D3 once every two seconds, D4 once every four seconds, D5 once every eight seconds, D6 once every 16 seconds, and D7 once every 32 seconds. Well, not exactly "blink", since each pin will be half the time on, then half the time off, so D7 will stay 16 seconds on, then 16 seconds off, and so on.

    If you have one or more LEDs with suitable current-limiting resistors, stick the resistor between the port D pin and the LED's anode (usually the longer leg), and the LED's cathode (usually the shorter leg, and/or side with the flat edge or other mark) to GND.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error detected : glibc
    By Elvio Esposito in forum C++ Programming
    Replies: 14
    Last Post: 06-30-2013, 09:53 PM
  2. GLIBC DETECTED error
    By programhelp in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2010, 10:01 PM
  3. Heap corruption Detected Error
    By mrsirpoopsalot in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2009, 03:59 PM
  4. glibc detected - error
    By mr_m4x in forum C Programming
    Replies: 2
    Last Post: 03-15-2009, 10:29 AM
  5. A S.M.A.R.T. error has been detected...
    By hk_mp5kpdw in forum Tech Board
    Replies: 4
    Last Post: 11-08-2004, 10:10 AM

Tags for this Thread