Full Version : Using Joystick on Butterfly Board
avr >>BEGINNERS & BUTTERFLIES >>Using Joystick on Butterfly Board


DataDyne- 05-16-2006
Hi, im new to the forums so apologies for posting in the incorrect place if i have done so, also i am new to programming avr butterfly boards and this doesnt help when i have a problem. I am making a small program that when the joystick is pressed up it displays "North" on the lcd display, when i press south it displays "south" on the lcd display and so forth. I am having a problem with using the joystick to do this though, so far i have managed to display "North" but only when the program has loaded, i have trawled throught internet site after internet site looking for any relative information to my problem so i can try to understand what i have done wrong. it would be appreciated if someone could look at my code and give me some tips on how to overcome this problem.

thanks

DDyne

CODE

#include <avr/io.h>

typedef unsigned int u32;
typedef unsigned char u8;

u32 LCD_character_table[] =
{
   0x5559,     // '0'
   0x0118,     // '1'
   0x1e11,     // '2
   0x1b11,     // '3
   0x0b50,     // '4
   0x1b41,     // '5
   0x1f41,     // '6
   0x0111,     // '7
   0x1f51,     // '8
   0x1b51,     // '9'
   0x0000,     // ':' (Not defined)
   0x0000,     // ';' (Not defined)
   0x0000,     // '<' (Not defined)
   0x0000,     // '=' (Not defined)
   0x0000,     // '>' (Not defined)
   0x0000,     // '?' (Not defined)
   0x0000,     // '@' (Not defined)
   0x0f51,     // 'A' (+ 'a')
   0x3991,     // 'B' (+ 'b')
   0x1441,     // 'C' (+ 'c')
   0x3191,     // 'D' (+ 'd')
   0x1e41,     // 'E' (+ 'e')
   0x0e41,     // 'F' (+ 'f')
   0x1d41,     // 'G' (+ 'g')
   0x0f50,     // 'H' (+ 'h')
   0x2080,     // 'I' (+ 'i')
   0x1510,     // 'J' (+ 'j')
   0x8648,     // 'K' (+ 'k')
   0x1440,     // 'L' (+ 'l')
   0x0578,     // 'M' (+ 'm')
   0x8570,     // 'N' (+ 'n')
   0x1551,     // 'O' (+ 'o')
   0x0e51,     // 'P' (+ 'p')
   0x9551,     // 'Q' (+ 'q')
   0x8e51,     // 'R' (+ 'r')
   0x9021,     // 'S' (+ 's')
   0x2081,     // 'T' (+ 't')
   0x1550,     // 'U' (+ 'u')
   0x4448,     // 'V' (+ 'v')
   0xc550,     // 'W' (+ 'w')
   0xc028,     // 'X' (+ 'x')
   0x2028,     // 'Y' (+ 'y')
   0x5009,     // 'Z' (+ 'z')
};

void init (void)
{
//LCD Control and Staus Register B (hexB7 - (below)) - (LCDCS-external asynchronous clock source used)
//(mux1-duty cycle, 1/4 duty, 1/2 or 1/3 Bias, COMO:3 Pin and i/o Port pin-none) PM2-0 - Sets Sement driver to SEG0:24 n no of segments to 25
LCDCRB = (_BV(LCDCS) | _BV(LCDMUX1) | _BV(LCDMUX0) | _BV(LCDPM2) | _BV(LCDPM1) | _BV(LCDPM0)); //external clock,0.25 duty cycle, port mask 25

//LCD Frame Rate Register (hex10 =(LCDpS0) - Sets LCD Prescaler to 8.1KHz
LCDFRR = _BV(LCDPS0);

//LCD Contrast COntroll Register (hexF=(below)) - Sets maximum volatge to 3.35 V
LCDCCR = _BV(LCDCC3) |  _BV(LCDCC2) |  _BV(LCDCC1) |  _BV(LCDCC0);

// LCD Control and Status Register A (hex80=LCDEN) - Enables LCD Controller/Driver.
LCDCRA = _BV(LCDEN);
}


void delay(void)
{  
u32 c;  
for (c = 0; c < 0xffff; c++);
}


void LCD_Write_Digit( u8 character,u8 character2, u8 character3, u8 character4, u8 character5) //Character arrays to hold write digit letters
{
u32 digit,digit2,digit3,digit4,digit5, number1, number2, number3, number4, number5, number; // setting Digits and numbers to hold array data
u32 mask, LCDDRbase; // Setting mask and LCDDRbase
u8 count, i; // setting coutn and i
u8 *ptr; // setting pointer
LCDDRbase = 0xec; //Setting the LCDDRbase to LCDDR0
mask = 0x000f; //Setting mask


character = character - 0x30; // takes away 30 hex from the character to find its position in the array table
character2 = character2 - 0x30;
character3 = character3 - 0x30;
character4 = character4 - 0x30;
character5 = character5 - 0x30;


ptr = (char *) (LCDDRbase); //0xec is the address of LCDDR0


/* Test to see if odd or even. if even then the upper nibble of
LCDDRx is enabled, if odd then lower nibble enabled */

 digit = LCD_character_table [character]; //get character
 digit2 = LCD_character_table [character2]; // get second character
 digit3 = LCD_character_table [character3]; // get third character
 digit4 = LCD_character_table [character4]; // get fourth character
 digit5 = LCD_character_table [character5];



for (i = 0; i < 4; i++)
 {
 
  number1 = digit & mask; //mask off upper nibble of lcddrx
  number2 = digit2 & mask; // mask off upper nibble of lcddrx and sets digit2 to number2
  number2=number2<<4; // shifts number 2 to LCD register to the left
  number= number1 | number2; // ORS number1 and 2 and sets to number
  *ptr = number;  //output value to LCDDRx + ptr
  ptr+=5;   //inctrment to next LCDDRx register
  digit=digit>>=4; // moves digit on
  digit2=digit2>>=4;// etc
 
 }


LCDDRbase = 0xed;
mask = 0x000f;
ptr = (char *) (LCDDRbase);

for (i = 0; i < 4; i++)
 {
  number3 = digit3 & mask; //mask off upper nibble of lcddrx
  number4 = digit4 & mask;
  number4=number4<<4;
  number= number3 | number4;
  *ptr = number;  //output value to LCDDRx + ptr
  ptr+=5;   //inctrment to next LCDDRx register
  digit3=digit3>>=4;
  digit4=digit4>>=4;
 
 }
 
LCDDRbase = 0xee;
mask = 0x000f;
ptr = (char *) (LCDDRbase);

for (i = 0; i < 4; i++)
 {
  number5 = digit5 & mask; //mask off upper nibble of lcddrx
  number= number5;
  *ptr = number;  //output value to LCDDRx + ptr
  ptr+=5;   //inctrment to next LCDDRx register
  digit5=digit5>>=4;
   
 }

}




main()
{
init();
while(1)
{
 LCD_Write_Digit('N','O','R','T', 'H');//Letters to be sent to be taken from array table.
 delay();
 delay();
 delay();
 delay();
 delay();
   
}
 

}





AVR_Admin- 05-16-2006
You are right. I could not find any documentation that mapped the
Butterfly Joystick so I did it by trial-and-error.

I'm an ASM programmer, not a C programmer so I can't give you the code, but I have included part of my ASM program.

Basically the Joystick is connected to TWO ports.
Port B and Port E:

PORT B, PIN 4 - Joystick Centre
PORT B, PIN 6 - Joystick Up (North)
PORT B, PIN 7 - Joystick Down (South)

PORT E, PIN 2 - Joystick Left (West)
PORT E, PIN 3 - Joystick Right (East)

Each Pin is pulled to Ground (0) when joystick is moved.

CODE

OUT PORTB,FF        ;SET PORT B & E FOR INPUT
OUT PORTE,FF

           SBIS    PINB,6      ;JOYSTICK UP?
            RJMP   START_8MHZ
           SBIS    PINB,7      ;JOYSTICK DOWN
            RJMP   START_2MHZ            
           SBIS    PINE,2      ;JOYSTICK LEFT
            RJMP   START_4MHZ
           SBIS    PINE,3      ;JOYSTICK RIGHT
            RJMP   START_1MHZ
           SBIC    PINB,4  ;JOYSTICK CENTRE


DataDyne- 05-17-2006
well thanks to your code, we kinda figured out how it worked in c and we got this to work.

CODE
#include <avr/io.h>
typedef unsigned int u32;
typedef unsigned char u8;

u32 LCD_character_table[] =
{
   0x5559,     // '0'
   0x0118,     // '1'
   0x1e11,     // '2
   0x1b11,     // '3
   0x0b50,     // '4
   0x1b41,     // '5
   0x1f41,     // '6
   0x0111,     // '7
   0x1f51,     // '8
   0x1b51,     // '9'
   0x0000,     // ':' (Not defined)
   0x0000,     // ';' (Not defined)
   0x0000,     // '<' (Not defined)
   0x0000,     // '=' (Not defined)
   0x0000,     // '>' (Not defined)
   0x0000,     // '?' (Not defined)
   0x0000,     // '@' (Not defined)
   0x0f51,     // 'A' (+ 'a')
   0x3991,     // 'B' (+ 'b')
   0x1441,     // 'C' (+ 'c')
   0x3191,     // 'D' (+ 'd')
   0x1e41,     // 'E' (+ 'e')
   0x0e41,     // 'F' (+ 'f')
   0x1d41,     // 'G' (+ 'g')
   0x0f50,     // 'H' (+ 'h')
   0x2080,     // 'I' (+ 'i')
   0x1510,     // 'J' (+ 'j')
   0x8648,     // 'K' (+ 'k')
   0x1440,     // 'L' (+ 'l')
   0x0578,     // 'M' (+ 'm')
   0x8570,     // 'N' (+ 'n')
   0x1551,     // 'O' (+ 'o')
   0x0e51,     // 'P' (+ 'p')
   0x9551,     // 'Q' (+ 'q')
   0x8e51,     // 'R' (+ 'r')
   0x9021,     // 'S' (+ 's')
   0x2081,     // 'T' (+ 't')
   0x1550,     // 'U' (+ 'u')
   0x4448,     // 'V' (+ 'v')
   0xc550,     // 'W' (+ 'w')
   0xc028,     // 'X' (+ 'x')
   0x2028,     // 'Y' (+ 'y')
   0x5009,     // 'Z' (+ 'z')
};

void init (void)
//for LCD
{
//LCD Control and Staus Register B (hexB7 - (below)) - (LCDCS-external asynchronous clock source used)
//(mux1-duty cycle, 1/4 duty, 1/2 or 1/3 Bias, COMO:3 Pin and i/o Port pin-none) PM2-0 - Sets Sement driver to SEG0:24 n no of segments to 25
LCDCRB = (_BV(LCDCS) | _BV(LCDMUX1) | _BV(LCDMUX0) | _BV(LCDPM2) | _BV(LCDPM1) | _BV(LCDPM0)); //external clock,0.25 duty cycle, port mask 25

//LCD Frame Rate Register (hex10 =(LCDpS0) - Sets LCD Prescaler to 8.1KHz
LCDFRR = _BV(LCDPS0);

//LCD Contrast COntroll Register (hexF=(below)) - Sets maximum volatge to 3.35 V
LCDCCR = _BV(LCDCC3) |  _BV(LCDCC2) |  _BV(LCDCC1) |  _BV(LCDCC0);

// LCD Control and Status Register A (hex80=LCDEN) - Enables LCD Controller/Driver.
LCDCRA = _BV(LCDEN);
}

void init2(void)
//for joystick
{
PORTB = _BV(PINB7) | _BV(PINB6) | _BV(PINB4);
PORTE =_BV(PINE3) | _BV(PINE2);
#define PINB_MASK ((1<<PINB4)|(1<<PINB6)|(1<<PINB7))
#define PINE_MASK ((1<<PINE2)|(1<<PINE3))
#define BUTTON_A    6   // UP
#define BUTTON_B    7   // DOWN
#define BUTTON_C    2   // LEFT
#define BUTTON_D    3   // RIGHT
#define BUTTON_O    4   // PUSH
PCMSK0 = PINE_MASK;
EIMSK = (1<<PCIE0)|(1<<PCIE1);
   
}

void delay(void)
{  
u32 c;  
for (c = 0; c < 0xffff; c++);
}


void LCD_Write_Digit( u8 character,u8 character2, u8 character3, u8 character4, u8 character5) //Character arrays to hold write digit letters
{
u32 digit,digit2,digit3,digit4,digit5, number1, number2, number3, number4, number5, number; // setting Digits and numbers to hold array data
u32 mask, LCDDRbase; // Setting mask and LCDDRbase
u8 count, i; // setting coutn and i
u8 *ptr; // setting pointer
LCDDRbase = 0xec; //Setting the LCDDRbase to LCDDR0
mask = 0x000f; //Setting mask


character = character - 0x30; // takes away 30 hex from the character to find its position in the array table
character2 = character2 - 0x30;
character3 = character3 - 0x30;
character4 = character4 - 0x30;
character5 = character5 - 0x30;


ptr = (char *) (LCDDRbase); //0xec is the address of LCDDR0


/* Test to see if odd or even. if even then the upper nibble of
LCDDRx is enabled, if odd then lower nibble enabled */

 digit = LCD_character_table [character]; //get character
 digit2 = LCD_character_table [character2]; // get second character
 digit3 = LCD_character_table [character3]; // get third character
 digit4 = LCD_character_table [character4]; // get fourth character
 digit5 = LCD_character_table [character5];



for (i = 0; i < 4; i++)
 {
 
  number1 = digit & mask; //mask off upper nibble of lcddrx
  number2 = digit2 & mask; // mask off upper nibble of lcddrx and sets digit2 to number2
  number2=number2<<4; // shifts number 2 to LCD register to the left
  number= number1 | number2; // ORS number1 and 2 and sets to number
  *ptr = number;  //output value to LCDDRx + ptr
  ptr+=5;   //inctrment to next LCDDRx register
  digit=digit>>=4; // moves digit on
  digit2=digit2>>=4;// etc
 
 }


LCDDRbase = 0xed;
mask = 0x000f;
ptr = (char *) (LCDDRbase);

for (i = 0; i < 4; i++)
 {
  number3 = digit3 & mask; //mask off upper nibble of lcddrx
  number4 = digit4 & mask;
  number4=number4<<4;
  number= number3 | number4;
  *ptr = number;  //output value to LCDDRx + ptr
  ptr+=5;   //inctrment to next LCDDRx register
  digit3=digit3>>=4;
  digit4=digit4>>=4;
 
 }
 
LCDDRbase = 0xee;
mask = 0x000f;
ptr = (char *) (LCDDRbase);

for (i = 0; i < 4; i++)
 {
  number5 = digit5 & mask; //mask off upper nibble of lcddrx
  number= number5;
  *ptr = number;  //output value to LCDDRx + ptr
  ptr+=5;   //inctrment to next LCDDRx register
  digit5=digit5>>=4;
   
 }

}




main()
{
init();
init2();
while(1)
{
 char buttons;
     buttons = (~PINB) & PINB_MASK; // setting PINS for UP, DOWN
    buttons |= (~PINE) & PINE_MASK;// setting PINS for LEFT, RIGHT
 
 
 // Output virtual keys
     if (buttons &(1<<BUTTON_A))
 {
  LCD_Write_Digit('N','O','R','T', 'H');//Letters to be sent to be taken from array table.
 }
 
           else if (buttons &(1<<BUTTON_B))
 {
  LCD_Write_Digit('S','O','U','T', 'H');//Letters to be sent to be taken from array table.
 }

          else if (buttons & (1<<BUTTON_C))
               {
  LCD_Write_Digit('W','E','S','T', '@');//Letters to be sent to be taken from array table.
        }

         else if (buttons & (1<<BUTTON_D))
        {
  LCD_Write_Digit('E','A','S','T', '@');//Letters to be sent to be taken from array table.
      }

           else if (buttons & (1<<BUTTON_O))
      {
  LCD_Write_Digit('P','W','N','E', 'D');//Letters to be sent to be taken from array table.
      }

             else
 {
  LCD_Write_Digit('@','@','@','@', '@');//Letters to be sent to be taken from array table.
 }
       
 

}
 

}





thanks again DDyne

AVR_Admin- 05-17-2006
Well that's fantastic, I'm glad you got it to work.

Be sure to drop back and upload you're finished code 'cause now you've got me curious about what you are trying to do.



Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.