Butterfly Bug

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.

JC

'-----------------------------------------------------------------------------------------
' 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%


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just for grins, change (or add to) this:

'I tried this, also, it did not work.
   Disable Pcint0
   Disable Pcint1

statements disabling PCINT2 and PCINT3.

I don't think it'll change anything, but if it does, we'll all be amazed.

The Butterfly bootloader doesn't clean up after itself very well, and PCINT2 and PCINT3 are shared with PE3 (joy right) and PE2 (joy left). It's a long shot, but worth trying.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also try to set DIDR0 and DIDR1 to the reset default (0).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Chuck,
Thank you for taking a look at this.

I tried inserting disabling PCint2 and 3, but Bascom did not recognize them as valid interrupts. They do not show up as options on the drop down list for the disable command, and typing them in anyways generates an error.

I open to any others thughts or suggestions!

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello MJThomas,
My Gosh, it suddenly works! :D :D :D

Adding Didr0 = 0 and didr1 =0 fixed it!

Thank you very much to MJThomas, Chuck, and Dean for all taking a look at this. I'm embarassed to say how many hours I've spend trying to read a polled bit! :!:

Frustration is gone and I'm excited once again!

Thank you!

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is an email I just sent to Jay:

Quote:
Jay,

I just chased up the problem you were having - you made me very curious. Diving into the bootloader source shows the line:

// Disable Digital input on PF0-2 (power save)
DIDR1 = (7<<ADC0D);

Which isn't what that will do - those bits don't actually exist in DIDR1. They're actually located in DIDR0, and DIDR1 has only two bits (0 and 1), with the rest reserved. That code has the function of setting two reserved bits in a register, which is generally a bad idea. I suspect the internal circuitry causes collateral damage, disabling other port's digital circuitry instead. Very interesting - and an undocumented way to do...well, nothing constructive other than waste power.

I surmise it's a result of the Butterfly bootloader being a modified version of a MEGA16 bootloader from Atmel - they must have overlooked a few things when porting/coding. Interestingly, I've never experienced such a problem myself, so perhaps it's just in a particular revision that the chip behaves as you are seeing. Fascinating stuff, and I'm glad you emailed me.

Good luck with your AVR adventures!
- Dean

And I do find it fascinating. I've never experienced problems (nor had any reported) with my ButtLoad code, which doesn't change the DIDR registers. Perhaps it only happens in the latest chip revision?

Full disclosure: I was referencing an old GCC port of the Butterfly Bootloader by mjthomas, but I don't think he would have changed the functional part of the code outside the spec of porting it.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dean,
Thank you for the follow up!

As a newbie to AVRs I figured I was missing something... Very frustrating to read 3 of the 5 joystick fuctions correctly, while using the same set up and methodology for all 5.

I figured as ATMEL's beginner's demo board how hard could it be? And surely others have tried this, especially since the manual does not even say which port.pin is affiliated with which direction of the joystick.

I've since tied the Butterfly to an FM radio transitter chip. Works great. I call it the Buttcaster, short for Butterfly Broadcaster, for those without my (warped) sense of humor. I also tied it to a GPS module. The 6 char display is too limited to be useful, but fun to play with. Again, following your lead, I called it the Buttlocator.

My wife thinks I've gone off the deep end.

Thank you, again, to everyone for their help!

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I've since tied the Butterfly to an FM radio transitter chip. Works great. I call it the Buttcaster, short for Butterfly Broadcaster, for those without my (warped) sense of humor. I also tied it to a GPS module. The 6 char display is too limited to be useful, but fun to play with. Again, following your lead, I called it the Buttlocator.

I think you should name the act of your "Buttcaster" broadcasting "Mooning". I agree - as the creator of "ButtLoad", the Butterfly AVRISP replacement - that the Butterfly name is just ripe for rear-related humor. Childish, but we're engineers!

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dean, some time ago (a year ago) I decompiled the Butterfly bootloader and commented it. I just now looked at it and here's what I found:

;	this should be 0x03, not 0x07
	ldi	R16,0x07	; disable digital input buffer
	sts	0x007f,R16	; DIDR1, pg 191

Page 191 refers to the 169 datasheet (the version I have) and as you say, only bits 0 and 1 are defined. Hence my comment of "should be 0x03." This is from a year ago, not just now.

That was from the hex file for the previous version of the bootloader, not the current version, so they didn't just introduce this error.

There are lots of other things the Atmel Butterfly bootloader leaves messy. I had made reference to this in another recent thread (or maybe it was this one - too lazy to go look... yup, this one.).

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Childish, but we're engineers!

From another thread:

Attachment(s): 

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi I am new to this site and I am wondering where you can get the Butterfly Broadcaster code. I am waiting for my parts to come in and I am having trouble finding the file. Or should I just copy the code that is posted at the top of the post?

Thanks!