# AVR Rapid Fire Mod… I hit a wall and need some help

Title says it; I'm making a rapid fire mod chip with the 328p family of chips right now (I'm going to scale down to whatever the smallest chip is that will safely hold the final software). I'm a fan of BASIC so I'm using Bascom and a USBASP programmer.

I've hooked up the chip with a 20Mhz clock and 22pF caps, as well as a power\status LED, and an LCD for visual debugging (because it really does make a huge difference). So it's a very basic setup.

Here's the problem: when I'm on the BB with a regular tactile switch, I can get the program to work totally fine. However, when I run wires from the contact points on the controller and hook them up to the corresponding points on the AVR, the program acts as if there's no button present, or rather it almost is looking like it's acting as if I'm just holding the button down, and I'm not. I've tried high side, low side, every possible side, and it isn't working as it should.

Most people use the PIC controllers, and they just run the controller contact points direct (or through 330R) without a problem. I've installed plenty of these for other people, and it's not complicated. So in theory I should be able to do the same, so why is it tripping out?

Here's my Bascom code:

   'Name: Azula Outlaw Mod MCU Firmware
'Version: a1
'Designed by: Dominic M, Luciano
'Development MCU: ATMega328P
'Oscillator: 20 MHz Crystal
'Copyright: (c) 2014-2015 Dominic M. Luciano, Luciano Incorporated, and Azula Microtech

$regfile = "m328pdef.dat"$crystal = 2000000

'PIN\IO INITS
'---------------------------------------
'OUT
'Init LCD Backlight  (For Visual Debugging)
LCDLIGHT Alias PortB.0
Config LCDLIGHT = Output

'Init PWR LED
PWRLED Alias PORTB.1
Config PWRLED = Output

'IN
'Init Function Switch, Start Pull Up
F_SW Alias PinD.2
PortD.2 = 1
Config F_SW = Input

'LCD INIT
'---------------------------------------
'Config the LCD for visual debugging
Config lcd = 16 * 2
Config lcdpin = PIN , DB4 = PORTD.4 , DB5 = PORTD.5 , DB6 = PORTD.6 , DB7 = PORTD.7 , E = PORTD.1 , RS = PORTD.0
Config lcdmode = PORT

'VARIABLE AND SUB CALLS
'---------------------------------------
Declare Sub FClick()
Dim FCount as Byte
Dim FState as Byte                                          'State flag

'INTERRUPTS AND SLEEP\STANDBY
'---------------------------------------

'STARTUP SEQUENCE
'---------------------------------------
LCDLIGHT = 1                                                'Start LCD Backlight
PWRLED = 1

'MAIN LOOP
'---------------------------------------
Main:

Cls
LCD "No Activity"

'Stabilize
Waitms 250

Do

If F_SW = 1 Then

If FState = 0 then
Gosub FClick
ElseIf FState = 1 Then
Waitms 500
FState = 0
End If

End If

Loop

'Subroutine for the function button
'(The button that tells the AVR to take commands)
Sub FClick()

Cls
LCD "Input Settings"

'~2 second wait until reset and goto Main

Dim Flash as Byte

Do
PWRLED = 0
Waitms 500
PWRLED = 1
Waitms 500
Incr Flash
Loop Until Flash = 2

'Change State Flag
Incr FState

'Set Vars
Flash = 0
FCount = 0

Goto Main

End Sub


Sorry about any mess in my code. It's still really rough. I was trying to get one subroutine down because the rest of it is all the same subs basically.

Here's a shot of my workspace. Once again, sorry for the mess:

So if you can offer any help, I'd appreciate it. Thank you!

Not familiar with BASCOM but have done plenty of C coding on Mega328s. I don't see anything that turns on the internal pullup resistor. PB.2 appears to be set as an input, but nothing indicates a pullup. Usually that would involve DDRB but that could be hidden in BASCOM code. Try just adding a 3.3K pullup from the input to Vcc, and connect the switch between the input and ground.

Jim Wagner Oregon Research Electronics

• This line is supposedly supposed to turn on the internal pullups: PortD.2 = 1 But I'm going to try that resistor externally too. I'm honestly so baffled as to why it doesn't work as is since everyone elses chips work. But I'll give this a shot Thank you. By the way, I live in Oregon as well; Southern Oregon area. – Dominic Luciano Feb 19 '15 at 2:02
• Well I took your advice but still no luck. I have determined that it is in fact acting as if there is NO INPUT. I tried another 328P, and a 324P, another controller, tried reading it off the ADC (which just showed 1023 no matter what), and I'm seriously stumped. It should be reading the voltage changes from the controller contact points. And that should change the pin's state with no extra parts; just a direct line. If I can't get a solution, I might as well scrap it because that is literally the whole project. So I'm praying for a solution cause I'd love to finish it. – Dominic Luciano Feb 19 '15 at 6:19

Firstly, it almost looks like you've got the down and left button connections connected to ground on the gamepad, rather than the signal side of the buttons. From what I can see in that image, you will need 4 wires going to the signal side of the buttons (the side not connected to the ground fill), and one other wire connecting the ground on the gamepad to the ground on your AVR.

Second, I suspect your issue is either a wiring or pull up problem, which is easy enough to diagnose by measuring the voltage between ground and your input pins on the AVR. When the button is not pressed, it should read 5V, and drop to 0V when pressed. If the voltage is waving somewhere between, there is likely no resistor pulling that side up to 5V (either externally or by enabling the internal pull ups) - and a floating input like that will just play havok with the AVR behavior.

• I was in the process of testing different sides of the D-Pad contacts to see if that fixed anything. But it didn't. Everyone elses' mod chips use one wire attached to the + side of the buttons. But I just realized I'm using two different power sources with no ground from the controller going anywhere. However, I had them all wired to the USBASP power only first, and it still wasn't working right. Peculiar. – Dominic Luciano Feb 19 '15 at 6:24
• Wiring is sound on the MCU itself, and running wires to contact points is pretty cut and dry. The voltage isn't wavering. So I'm going to assume it's because I didn't connect the ground. I'll to run it all on the same supply again tomorrow and see if that fixes it. Thanks – Dominic Luciano Feb 19 '15 at 6:27
• Alright, gotcha. And that voltage is the same when measured at the input pin to your AVR, correct? The code listed above is checking for If F_SW = 1 Then, though a button press should return a low (F_SW=0), so it is checking for a high level. This leads on to a potential issue - your 2V input. The 328P only guarantees a pin to be read as high when the voltage is above 0.6 of the supply voltage. If your supply is 5V (is it?) this corresponds to a voltage of 3V as your minimum high level, which the input pin will never reach. Powering the 328P with 3.3V instead might solve this issue. – skyrift Feb 22 '15 at 16:52
• When I say the threshold is "0.6 of the supply voltage", I mean VCC*0.6, which equals 3V for a 5V supply, and 1.98V for a 3.3V supply. – skyrift Feb 22 '15 at 17:20
• Section 29.2 on page 303. As far as the crystal frequency, you're right in thinking the 20 MHz won't work at 3.3V properly. TBH the 8MHz internal oscillator is probably easily fast enough for your needs, and a more accurate crystal is often only necessary when you have some very tight timing requirements such as a precise baud rate for serial comms. If you want to use a crystal you'll need to find something around 10MHz or below, 8MHz being one of the most common. The info on max clock speeds is in section 19.3 on page 310. – skyrift Feb 23 '15 at 2:16