I purchased two AVR Butterflies to learn about AVRs. As I do not know C, and haven't done any assembly language for years, I purchased Bascom-AVR.
I can flash an LED, display text on the LCD, and use I2C to read a DS1624 Temp chip. Off to a good start. :D
I am having trouble, however, reading the Left/Right Joystick activation, (Port E bits 2 & 3). :( I CAN read the Up/Down/Press Inwards functions, (Port B, Bits 4, 6, & 7).
I'm attempting to do a simple read, simply polling the bits, which are pulled high by the internal pull up resistors, and go low with activation. I am not using any interrupts or debounce at this time.
My joystick test program thinks that the Left/Right functions are always pressed, (low). The program should display the port and pin when the joystick is pressed and held in a given position. It currently loops, as it otherwise locks up reading Left/Right, which it thinks are always pressed.
The code is shown below, I would appreciate it if another Butterfly owner could load it in their Butterfly, and see if it properly reads the ports/pins only when pressed, or fails on their Butterfly also. (Fail = Shows Left / Right pressed when they are not.)
The original Butterfly code works, so I believe the hardward is fine. I O'scoped the pins, they are high, going low with activation, as they should.
The Butterfly has its original bootloader, and I download the hex file via the Butterfly's built-in serial port, using AVR Studio 4.
The LCD library is interrupt driven, and is in the Bascom demos. The original Butterfly program, which does correctly read the joystick, is, I believe, interrupt drive.
Why can I not read the Port E joystick fuctions?
Any thoughts would be much appreciated.
'----------------------------------------------------------------------------------------- ' File Name: BFJoyStick.bas 12/07 JEC 'Tests reading ATMEL AVR Butterfly Joystick, polled, no interrupts, no debounce. 'Shows CAN READ Port B joystick activation. 'Shows CAN NOT READ Port E joystick activation. 'This began as MCS Demo m169_ldc.bas to demo LCD driver for Butterfly 'I used it as starting point for further experimentation 'LCD Driver is from MCS. 'Purpose: Test reading the Butterfly 5-Position JoyStick 'Set bits for input, read them, be able to use JoyStick as a user interface. 'Keep it simple: 'Polled reading of the joystick input port pins 'No interrupts, No debounce 'LCD Demo counts and displays counter on LCD 'With each time through the loop scan the Joystick pins and write the 'Port & Bit number on the LCD for any activated joystick value. 'Hardware is as defined below. 'Set PORT's directions 'Enable the internal pull-up resistors. 'Read Pin's input 'Joystick switches are normally high, activated = low. 'BF is on a breadboard, with a clean 3.3 V power supply. 'JoyStick Test: 'Make the known ports and bits inputs, and test for activation. 'Determine which port/bit is which direction, as it isn't 'spelled out in the manual. 'JS inputs are Normally High, Activated goes Low 'Up B6 'Down B7 'In B4 'Rht E (Right or Left ?) 'Lft E (Right of Left ?) 'BUG: 'Reading the Butterfly Joystick is proving to be problematic. 'I can read 3 of the 5 positions, by polling the bit status,as expected. 'These 3 are all on port B. 'I can not (yet) read the the two (L & R) events, on Port E. 'It acts as if the internal pull ups are not active. 'Of interest, the original Butterfly program works, ie can read the joystick, but 'it does it using interrupts. It shows the hardware is functinal, however. 'Hardware Definition: 'Port B: 'Bit 0 = LED High = ON (My addition, now removed.) 'Bit 1 = PB Switch, Normally Open, High, Push = Low (My addition) 'Bit 2 = 'Bit 3 = 'Bit 4 = JoyStick, Push In 'Bit 5 = Speaker, Timer1, PWM, Output A, PB5 'Bit 6 = JoyStick, Push Up 'Bit 7 = JoyStick, Push Down 'Port E: 'Bit 2 = JoyStick, Push (R/L ?) 'Bit 3 = JoyStick, Push (R/L ?) 'Port F: 'Bit 0 = NTC Thermister on ADC0 (Neg Temp Coef Thermister) 'Bit 1 = V ADC Input, ADC1, 0-5 V, PF1 'Bit 2 = LDR Input, Light Sensor Input (I added my own photocell) 'Bit 3 = 'Bit 4 = 'Bit 5 = 'Bit 6 = 'Bit 7 = '----------------------------------------------------------------------------------------- $regfile = "m169def.dat" ' specify the used micro $crystal = 8000000 ' used crystal frequency $baud = 19200 ' use baud rate $hwstack = 32 ' default use 32 for the hardware stack $swstack = 10 ' default use 10 for the SW stack $framesize = 40 ' default use 40 for the frame space $lib "lcd_butterfly.lbx" 'Butterfly LCD Driver 'BF LCD Setup: Dim _butterfly_digit As Byte Dim S As String * 6 Dim Bb As Bit 'Testing reading joystick values... Config Aci = Off 'Force OFF the Analog Comparator 'Made no difference 'Write a 1 to the Port & Pin, on INPUTs, to Enable Internal Pull-Up resistors: Set Portb.1 'Set it before config as input, turns on Int Pull Up Set Portb.4 'Joystick Pull-UP Enabled Set Portb.6 'Joystick Pull-UP Enabled Set Portb.7 'Joystick Pull-UP Enabled Set Porte.2 'Joystick Pull-UP Enabled Set Porte.3 'Joystick Pull-UP Enabled 'Now Configure the Port's Pins for Input or Output mode. '0 = Input, 1 = Output 'For Port E am currently interested in pins 2 & 3, (Joystick) Config Portb = &B00100001 'Now Config PORT Input/Output Config Porte = &B00000000 'Guessing for Port E other than bits 2, 3 'Alternative Port E Pin Configuration, this also did not work, (Sets just the pins of interest): 'Config Porte.2 = Input 'JoyStick 'Config Porte.3 = Input 'Joystick 'Tried writing to the INPUT Pins after defining the ports input and output mode 'to be sure internal pull up resistors are on. 'This made no difference, still does not work. Set Porte.2 'Repeat, after port setup , no difference Set Porte.3 'Or PortE.3 = 1 either is OK '.................................... 'Deans suggestion: Eimsk = 0 Eifr = 0 '............................. 'I tried this, also, it did not work. Disable Pcint0 Disable Pcint1 '....................................... 'Butterfly LCD Start Up message: S = "AVR" 'Display String on LCD Waitms 100 Lcd "HELLO" 'Display Start Up Up Mesage Waitms 250 Sound Portb.5 , 500 , 100 'Start Up Beep Dim J As Byte Myloop: For J = 0 To 100 Lcd " " 'Cls LCD by overwrite with spaces Lcd J 'Display count Waitms 100 Gosub Joystick 'Test JoyStick Next J Goto Myloop 'Loop Forever for testing Joystick: 'Test the Butterfly JoyStick 'Pins should be normally high, low when joystick is activated 'Tried enabling the internal pull up resistors here, also, without effect, 'just before the pins are actually read. 'LCD Displays the Port/Pin if the joystick is activated, then moves on Set Porte.2 Set Porte.3 If Pinb.4 = 0 Then 'Joystick PortB.4 works, JS is Pushed Inwards Cls Lcd "B4" Waitms 250 End If If Pinb.6 = 0 Then 'Joystick PortB.6 works, JS is Pushed Upwards Cls Lcd "B6" Waitms 250 End If If Pinb.7 = 0 Then 'Joystick PortB.7 works, JS is Pushed Downwards Cls Lcd "B7" Waitms 250 End If If Pine.2 = 0 Then 'Joystick PortE.2 Fails Always reads input as activated, (Low) Cls 'Always reads input as activated, (Low) Lcd "E2" Waitms 250 End If If Pine.3 = 0 Then 'Joystick PortE.2 Fails Cls 'Always reads input as activated, (Low) Lcd "E3" Waitms 250 End If 'Alternative, assign pin value to a variable, did not make a difference 'Bb = Pine.2 'Read State of INput Pin, made no difference 'If Bb = 0 Then ' Cls ' Lcd "E22" ' Waitms 250 'End If Return Lcd_butterfly_data: Data 0% ' space Data 0% ' ! Data 0% '"" Data 0% ' # Data 0% '$ Data 0% ' % Data 0% ' & Data 0% ' ' Data 0% ' ( Data 0% ' ) Data 0% ' * Data 0% ' + Data 0% ' , Data &H0A00% ' - Data &H8000% ' . Data 0% '/ Data &H5559% ' 0 Data &H0118% '1 Data &H1E11% ' 2 Data &H1B11% ' 3 Data &H0B50% ' 4 Data &H1B41% ' 5 Data &H1F41% '6 Data &H0111% ' 7 Data &H1F51% ' 8 Data &H1B51% '9 Data 0% ': Data 0% '; Data 0% '< Data &H0004% '= Data 0% '> Data 0% '? Data 0% '@ Data &H0F51% ' A Data &H3991% , &H1441% , &H3191% , &H1E41% , &H0E41% , &H1D41% , &H0F50% , &H2080% , &H1510% , &H8648% , &H1440% , &H0578% Data &H8570% , &H1551% , &H0E51% , &H9551% , &H8E51% , &H9021% , &H2081% , &H1550% , &H4448% , &HC550% , &HC028% , &H2028% , &H5009%