-
Notifications
You must be signed in to change notification settings - Fork 81
Loop not called if attachInterrupt(..LOW) #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Its a PULLUP problem...
|
this implicit PULLUP has same problem
|
AVR8js does not include the logic to deal with pull-up resistors, so the implementation is up to the user of the library. You can add a listener to the relevant GPIO port (by calling the for (let pin = 0; pin < 8; pin++) {
if (port.pinState(pin) === PinState.InputPullUp) {
// Now, if the button is pressed call port.setPin(pin, false);
// Otherwise call port.setPin(pin, true);
}
} You can find a more complete example in the AVR8js Simon Game Sample. The code there assumes there's always a pull-up resistor on the pin (e.g. an external pull-up resistor), so the logic is hard coded: for (const button of buttons) {
const pinIndex = parseInt(button.getAttribute('pin'), 10);
button.addEventListener('button-press', () => {
if (runner) {
runner.portD.setPin(pinIndex, false);
}
});
button.addEventListener('button-release', () => {
if (runner) {
runner.portD.setPin(pinIndex, true);
}
});
} If you need additional help with this, feel free to reopen / add new comments to this thread |
Hi @urish , thanks for the feedback But this is a feature of the AVR at least 328p, according to the datasheet. |
I rum this code in SimulIDE and PicSimLAB (simavr) and both implement this control... void setup() {
Serial.begin(115200);
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(100);
Serial.print("[up]:"); Serial.println(digitalRead(3));
Serial.print("[down]:"); Serial.println(digitalRead(2));
delay(1000);
}
void loop() {
} |
But if i comment, in this example: https://stackblitz.com/edit/avr8js-simon-game-j4rxnx?file=index.ts , not works
|
Thanks for the feedback, Ricardo! It is a design choice to expose the building blocks to the users of the library and let them implement the pin functionality as they wish. AVR8js does let you know if the pull-up for a specific has been enabled or not, but it's up to you to decide how to act upon it. This way, we don't impose any limitations on what you can connect to the pins. For instance, on Wokwi.com, the implementation takes into account external resistors as well, so you can also use an external pull-up or pull-down resistor. Also, a floating INPUT pin currently has a random state. Other implementations can choose to also take the resistor values into account (and this might be the case in Wokwi once wokwi/wokwi-features#203 is done), so by adding a 1k resistor between a pulled-up pin and the ground, you'll get the pin to read 0, but adding a 1m resistor the pin will still read 1. For even more accuracy, the implementation can take the chip's internal temperature into account: One may even integrate AVR8js with a complete SPICE simulation to create a more accurate model of the digital pins, adjusting the pin threshold voltage according to the supply voltage: |
Exactly! Just like the SPI/TWI don't implement the on-wire protocol - they give you some handy callbacks, and you can decide if/how you support these protocols. In Wokwi, the implementation looks at which devices are connected to each MCU pin. Then, whenever the state of any of these devices changes, it checks whether any of the devices have HIGH or LOW output. If there are none, it checks whether there are any pull up or pull down resistors (internal to the MCU or external). If none are found, it uses a random value (high or low). Once it determines the final digital value of the pin, it calls the If Wokwi had an analog simulation engine (similar to falstad), we'd probably model the pull-up resistor using a combination of a 47kΩ resistor (or similar value) + MOSFET that connects/disconnects the resistor according to the MCU pin configuration. What would the suggested flag in AVRIOPort do? How do you imagine it would work? |
I don't know what happens, but online it works, without adding any external components
THIS PRINTS: SUCCESS !! Now on : /avr8js/demo (http://localhost:1234) This prints : FAIL -- I believe the right behavior is: Test using PORTs have sabe result
On SIMAVR, PIND changes !! |
The great reason for existing this pull_up, is to do with digitalwrite (P, high), always keep high, maintaining consistency. But I think I understood the problem in the simulation. Is that if I set a high, but have a pressed button forcing Low. The final state should be "low" by the smallest "resistance". So only the external software can know if it exists or not "something connected"; On the other hand it can reverse logic: |
I tried, implement a test for this case.
|
This is surprising
I think we can come up with an helper function or class that will attach to the AVRIOPort. That way, people who want to bring their own implementation (like Wokwi does), will keep using the existing API, but if someone just needs a basic implementation with support for buttons, they can use the helper function or class. Thoughts? |
Hi,
i'm running some examples of interrupt and I found strange behavior.
Based on the example (which works correctly in wokwi online)
https://wokwi.com/projects/318885209198035539
If I run the same example in Avr8js it will trigger the interrupt (e not call loop())

As the inputs are configured as PULLUP, was this supposed to happen?
I tried simulating the same behavior, removing the buttons from the example, and it still works correctly
https://wokwi.com/projects/328609280331612756
But no work in avr8js-electron-playground , and avr8js demo in my code (which is based on his..)
I wonder if I need to configure something else.
Reproduce:
The text was updated successfully, but these errors were encountered: