Coding Microcontroller Program for Spinning LED Display

Discussion in 'Programmer's Corner' started by dannybeckett, Feb 21, 2011.

  1. dannybeckett

    dannybeckett Thread Starter Member

    Joined:
    Dec 9, 2009
    Messages:
    153
    Hi guys,

    Im in the middle of making a spinning LED display using a PIC16F88. Save a long winded explanation of the device the following is a video of something very simalar to mine:

    http://www.youtube.com/watch?v=fUrwy3l0XfA

    And pictures:

    http://hackedgadgets.com/2008/12/12/rotating-blue-led-display-2/

    Our setup is basically the same as that, but I am not an experienced PIC programmer. Would anyone be able to throw me in the right direction with regards to programming the microcontroller in a simalar project? I will have an optical sensor triggering every revolution, going into the uC, the spinning portion of the project has been created, the next step is basically programming the chip.

    My thoughts -

    The program has a constant within it which specifies the number of horizontal 'pixels' that occupy the circumference of the spinning arm. The program measures the time it takes for 1 revolution of the arm and divides that time by the pixel number. This provides the time it takes the arm to rotate from one specific pixel to the one neighbouring it. Using this time you can flash the LEDs on and off accordingly, to create an image.

    Does this sound reasonable to anyone? Maybe there is another way I have not thought of that this can be done? Any help at all is massively appreciated.
  2. John P

    John P Senior Member

    Joined:
    Oct 14, 2008
    Messages:
    1,323
    Location:
    Massachusetts
    Sounds right to me.

    It didn't look as if the unit in that video had a sensor, and the result was that the "location" of the display was wandering around. But sync it to the motion of the arm, and that would be cured.

    You'd better have a good plan to get the power and signals onto the moving arm. Or maybe all your electronics is out there rotating, and you run it off batteries?
  3. dannybeckett

    dannybeckett Thread Starter Member

    Joined:
    Dec 9, 2009
    Messages:
    153
    The electronics are spinning with the arm, only the power is needed to be brushed to the arm, so +5V and ground. Do you have any idea on how I could begin to code the microcontroller for this kind of project? I'm quite stuck!

    Thanks again
  4. Markd77

    Markd77 Senior Member

    Joined:
    Sep 7, 2009
    Messages:
    2,798
    Location:
    Birmingham
    What code have you got so far?
    If you haven't started yet I'd suggest trying for a line first (just turn on all the LEDs for a very short time every revolution).
    The speed will be fairly constant once it has got going so you can use a constant time for the pixel widths to start with and then see if you need to make it more complicated.
  5. spinnaker

    spinnaker Well-Known Member

    Joined:
    Oct 29, 2009
    Messages:
    3,650
    Location:
    Pittsburgh, PA U.S.A.
    What experience do you have with writing code for a microchip? This is not an easy project.

    It is on my list of things to to.
  6. dannybeckett

    dannybeckett Thread Starter Member

    Joined:
    Dec 9, 2009
    Messages:
    153
    Markd77:
    Yes this is a great start. As soon as I have got the pic development board that's coming in the post I will try light 1 line of the display. I have a feeling I am going to need to use interrupts for the RPM signal?

    spinnaker:
    Very limited if im honest. Ive written a few things in assembly and don't really know how to code for microcontrollers in C. I am willing to learn (and will do very quicky) if you know of any decent resources knocking around the internet?
  7. spinnaker

    spinnaker Well-Known Member

    Joined:
    Oct 29, 2009
    Messages:
    3,650
    Location:
    Pittsburgh, PA U.S.A.
    Then I would start off small. Basically what you need to do with this project is to be able to turn on 8 or 10 LEDs individually. I would start there. Once you have that then you can move on.

    The next step would be to determine when the lights will be turned on. This will require a timer interrupt and is going to be the most difficult part of your project. The problem is going to be it that the board will be spinning and there will be no easy way to debug it. I was thinking of using two microchips and wireless communication. The stationary one would do all of the work and the spinning one would just interpret commands and turn on the LEDs.
  8. spinnaker

    spinnaker Well-Known Member

    Joined:
    Oct 29, 2009
    Messages:
    3,650
    Location:
    Pittsburgh, PA U.S.A.
    Once you get you leds to light up and you start the spinning portion, a good step would be to get the line of leds to light up at a specific position.
  9. bertus

    bertus Administrator Staff Member

    Joined:
    Apr 5, 2008
    Messages:
    12,634
    Location:
    Amsterdam,Holland (GMT + 1)
    Hello,

    You could also use a slot photo interupter for synchronisation.
    You start the pixel stream after the interupt has been.
    That way the picture will stand still.

    Bertus
  10. John P

    John P Senior Member

    Joined:
    Oct 14, 2008
    Messages:
    1,323
    Location:
    Massachusetts
    Here's how I would do it. First, set up a repeating timer interrupt at something like 4KHz, and make it increment a counter for each occurrence. Set up another interrupt triggered by the optical switch. When that one operates, turn the timer on and zero both the time interval and the count of total number of timer intervals.

    What you'll be doing is counting time intervals, and for each one you'll show a new output on the LEDs. Start with a single occurrence of all LEDs on for just one period, to verify that it happens at the same place each revolution--it should look like a narrow vertical bar. Then change the counting so that the LEDs are on for (say) 100 counts, and that'll give you a scale for how much display time you have over the time of one revolution. I'd expect that you want 1 horizontal pixel per timer interval, so once you've established this scale you might want to change the timer interval to match. And then, all you have to do is make up the actual display in terms of what gets shown for each successive timer interval. Dead easy.
  11. spinnaker

    spinnaker Well-Known Member

    Joined:
    Oct 29, 2009
    Messages:
    3,650
    Location:
    Pittsburgh, PA U.S.A.
    If you say so. You certainly have me confused but maybe the OP is a lot smarter than me and understands, and that is what counts. :)
  12. John P

    John P Senior Member

    Joined:
    Oct 14, 2008
    Messages:
    1,323
    Location:
    Massachusetts
    Oh surely not! But here's a pseudo-C version of the interrupts, assuming that there are 8 LEDs in a column, controlled by output_port. This leaves the LEDs on for 100 cycles, then off:

    Code:
    //Global variables
    int16 timer_count;
    
    void int_from_optical_switch(void)
    {
      timer_count = 0;
      timer_register = 0;      // Do this so it waits a full cycle till next interrupt
      bit_clear(timer_interrupt_flag);     // If timer int was pending, kill it
    }
    
    void int_from_timer(void)    // Interrupt occurs 4000 times a second
    {
      if (timer_count < 100)
      {
        output_port = 0xFF;      // Lights on
        timer_count++;            // Increment count till it hits 100
      }
      else                             // Then no more increments
        output_port = 0;          // Lights off
    }
    
    main()
    {
      while (1) ;          // Just sit in a loop in main()
    }
    
  13. dannybeckett

    dannybeckett Thread Starter Member

    Joined:
    Dec 9, 2009
    Messages:
    153
    John your help is amasing! Ill post back in this thread when i have my pic dev board, compiler installed and a vague plan of how to go about things, so I'm not just asking very general questions about what to do.
  14. dannybeckett

    dannybeckett Thread Starter Member

    Joined:
    Dec 9, 2009
    Messages:
    153
    Hello again!

    Right ive had the board for about 4 days and I've been messing non stop! It's great fun. I have a plan to use the CCP module to measure the rotation speed in seconds. I am now configuring Timer1 on the PIC16F887. My problem is that running at 8mhz, and even with the prescaler set to max the max division (Fosc/8) the time it takes to count the full 65536 is far too quick. I tried getting the onboard timer1 oscillator to act as the clock for the timer bit I couldnt get it going! My code is here :

    http://codepad.org/jCoKUWsl

    Works fine when TMR1CS is cleared, but like I said its far too fast! How can i get this internal 32.768KHz crystal oscillator working?
  15. John P

    John P Senior Member

    Joined:
    Oct 14, 2008
    Messages:
    1,323
    Location:
    Massachusetts
    Generally it's not useful to read the registers of a timer while it's running. A program usually responds to the interrupts that the timer generates when it overflows (or hits a comparator, in the case of TMR2 in PIC processors). Then you either restart the timer for the desired interval, or let it run again if it's automatic, which TMR2 is.

    You don't necessarily have to make your program jump to the interrupt service routine. If you prefer, you can have your main() routine check the interrupt flag by polling; for TMR1 that's TMR1IF or PIR1.0. What you can do when the interrupt occurs is to increment a counter, telling the program how many times the interrupt has been seen. That way, you can effectively extend the 16-bit count to 24 bits, or as many as you want if you add more bytes to the count.

    Note that if you merely poll the interrupt flag, your software must clear it if it's found. If you use the compiler's interrupt-service routine, that would be done automatically.
Similar Threads
Forum Title Date
Programmer's Corner Need help with coding microcontroller based LED matrix Jan 25, 2009
Programmer's Corner Help Arduino Coding Oct 8, 2013
Programmer's Corner Decoding response from modbus device Apr 18, 2013
Programmer's Corner gabor wavelet coding Jul 4, 2012
Programmer's Corner Help for Slot Machine Coding in VHDL May 2, 2012

Share This Page