06-25-2015, 06:40 AM
(06-25-2015, 06:24 AM)SCN-3_NULL Wrote:(06-25-2015, 06:08 AM)Segolia Wrote: Using a clock of some kind is probably preferable to busy-waiting. Is there a configurable clock (like, with a register?). I'd do it something like this:I'm not sure about the configurable clock but I do finf TMR1,TMR2 and a few TRM in the special registers, are those what I'm looking for?
-Configure clock to desired delay (eg. 500ms)
-Store LED bitmask in some byte in memory or use a register (like "2_00000" where each digit corresponds to an LED in the sequence)
-Initialise the bitmask with some value (eg. "2_00001")
-On clock interrupt, circular shift right, use bitmask to determine which LED to light up and send a signal to the corresponding pin (alternatively you could save some hassle, say if your device has ports with multiple pins which share a single data register, just store the LED bitmask in the data register and shift that, then just hook LEDs up to appropriate pins)
-Write an interrupt handler for the button press, simply make it toggle the direction of the shift which happens on the clock interrupt.
and yes the device has ports, I'm planning on assembling them like this, but with 5LEDs and 4MHz clock
And a code example would be really useful, dont see these in my slides, will put those in the comments just for reminder.
I can't give you a code example easily. I'd have to read through the datasheet for your device to even work out pinouts and registers properly. Not to mention I'm a bit rusty and I've never worked with PIC (I learned assembly with an ARM architecture).
It should be pretty straightforward, you know how to write interrupt handlers right? psuedocode would look something like
Quote:Main:
-configure clock registers (for desired delay)
-configure GPIO pins/ports/whatever for LED
-Store LED bitmask (0x01) in memory or register
-Store Direction flag/bit (0x0) in memory or register
-enable port/pins
-enable clock
Clock interrupt handler:
-read LED bitmask into register
-Clear all current LED pins
-Perform some logic to write to appropriate pin (eg. write value of LED bitmask AND 2_00000001 to LED 1, bitmask AND 2_00000010 to LED 2 etc.)
-read direction flag/bit
-Circular shift (aka rotate no carry) either left or right based on value of direction bit (eg. left if 0, right if 1)
-store new LED bitmask in memory
Button interrupt handler:
-read direction flag/bit
-XOR 0x1 to toggle
-store updated direction flag/bit
And remember if you're using registers to store those values to push them onto the stack before running your interrupt handler.