Software Support > Automated Projects

12 position rotary switch > 2 keyboard encoder inputs, simple hardware solution?

<< < (4/5) > >>

melvinbates:
Ok, finished the first draft of the code.  I haven't tested it yet.  I'll get a circuit put together soon to test it.  Posting it here in case you are interested.


--- Code: ---#include <avr/io.h>
#include <util/delay.h>

void ioinit(void); //Initializes IO
void Go(void); //Main Program
void Signal(uint8_t direction); //Signals line 0 R, 1 L
uint8_t GetDirection(void); //process curline and lastline returns direction

uint8_t curLine = 0;
uint8_t lastLine = 0;
uint8_t testLine = 0;

int main (void)
{
    ioinit(); //Setup IO pins and defaults

    _delay_ms(100);
    curLine = ((PINB & 0x07) ^ 0x07);
    lastLine = curLine;

    while(1)
    {
Go();
    }
   
    return(0);
}
 
void Go(void)
{
//Code assumes it is hooked up to a rotary switch configured in a 4 wire configuration (1 ground, 3 signal)

testLine = PINB & 0x07;
if (testLine != 0x07) //See if any lines have been pulled low.
{
if ((testLine ^ 0x07) != curLine) //check if line was already low.
{
curLine = (testLine ^ 0x07); //update curLine low with line currently pulled low
Signal(GetDirection());
lastLine = curLine;
}
}
}
uint8_t GetDirection(void)
{
if (lastLine == 0x01)
{
if (curLine == 0x02)
return 0;
else if (curLine == 0x04)
return 1;
}

if (lastLine == 0x02)
{
if (curLine == 0x04)
return 0;
else if (curLine == 0x01)
return 1;
}

if (lastLine == 0x04)
{
if (curLine == 0x01)
return 0;
else if (curLine == 0x02)
return 1;
}
return 2; //something unexpected happened.
}

void Signal(uint8_t direction)
{
if (direction == 0)
PORTB = 0x17; //Pull PB3 low
else if (direction == 1)
PORTB = 0x0f; //Pull PB4 Low

_delay_ms(20);
PORTB = 0x1f; //set PB3 and PB4 back
}


void ioinit (void)
{   
DDRB = 0b00011000;  //set PB3,4 to output
PORTB = 0b00011111; //set internal pullups on PB0-2 and set PB3,4 high
}
--- End code ---

<Update code to final version>

BadMouth:
I should be ready to play test next week.  Guess I should have ordered some microprocessors and programmer already.  I should have the other parts on hand.

Thanks!

melvinbates:
Here is the proposed board layout and schematic.  Once I test them and confirm they are working I'll get the boards made and send you one along with a programmer (in case the code doesn't work as intended, I'm still waiting on these from China though... I ordered some along with some diodes I needed).  Turn around to get a board made is generally 2-3 weeks, though I've had it as fast as 1 week.

If you decide not to wait and get your own programmer and such, I'd also be happy to help you get it setup and answer any questions you have.


Basic schematic, with the current code you could use an attiny25/45/85.  though it's a bit tight on the attiny25.


Quick render of the board layout.  It's actually quite small and less than an inch square.

melvinbates:
Ok, everything finally arrived and I've assembled and tested the circuit.  I had to make a minor modification to the code, which I've updated above.  It seems to be working exactly like I hoped it would, pulsing the R line when a sequence like 12 is found and the L line when 32.  So I think for the intent of this adventure it's done.

BadMouth:
I received the rotary encoders today.  Wow, they look professional!

I probably won't have time to try them until this weekend, but I am looking forward to it.  (Also looking forward to learning how to use the programmer you sent, as I'm sure I'll come up with other projects once I get a taste).

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version