rogerclarkmelbourne / Arduino_STM32

Arduino STM32. Hardware files to support STM32 boards, on Arduino IDE 1.8.x including LeafLabs Maple and other generic STM32F103 boards
Other
2.52k stars 1.26k forks source link

ADC thereshold detect & interrupt #824

Closed AntonioFromBrazil closed 3 years ago

AntonioFromBrazil commented 3 years ago

I am trying to generate an interrupt when limits defined by setAnalogWatchdog(channel, HighLimit, LowLimit) are crossed but I think that function is not working well since no interrupts are generated. Maybe I forgot something... I would suggest an example of how to work with analog watchdog...

stevstrong commented 3 years ago

Can you post a simple sketch you have tried?

AntonioFromBrazil commented 3 years ago

Sure... thanks stevtrong. I tried many different variations over this attached example but no way !!! I will appreciate any help.

include

STM32ADC myADC(ADC1);

uint8 ana =PA0; // analogic input pin

void setup() { Serial1.begin(115200); pinMode(PA0, INPUT_ANALOG); myADC.calibrate(); myADC.setSampleRate(ADC_SMPR_1_5); myADC.setPins(&ana, 1); myADC.setAnalogWatchdog(0, 2100, 1900); //high low limit at channel 0 (PA0) myADC.attachAnalogWatchdogInterrupt(adc_int); myADC.setContinuous(); myADC.startConversion(); }

void adc_int() { Serial1.println("int"); }

void loop() { }

AntonioFromBrazil commented 3 years ago

in the example there is no difference if I use myADC.attachAnalogWatchdogInterrupt(adc_int); or myADC.attachInterrupt(adc_int, ADC_AWD); or both...

dewhisna commented 3 years ago

@AntonioFromBrazil -- you do realize that you aren't using the same serial port in setup() and adc_int(), right? A quick grep through the framework source shows that Serial is USBSerial and Serial1 is HardwareSerial (i.e. one of the USARTs).

So what port are you monitoring?

AntonioFromBrazil commented 3 years ago

Serial1. There is only a typewriter mistake on example... I have already edited and corrected it. Thanks

stevstrong commented 3 years ago

Your application has two errors.

First, if you start continuous conversion, then the ADC will convert in loop. This means that the interrupt comes regularly in very short intervals and will be nested because the ADC ISR cannot run faster than the ADC conversion time. After some time the CPU will simply hang, this is why you don't see anything on the serial monitor.

The workaround to this is to disable the conversion or the watchdog alone in the ISR, and restart it in the main loop. Or better use single conversion and restart it each time in the main loop after you printed it on the serial monitor. PLease check the single interrupt example.

Second, the AWD interrupt is not yet implemented in the lib. Although the functions are there, but behind them the IRQ will not be enabled.

I am currently making some amendments on ADC lib to solve these issues, I hope I will finish it today or tomorrow, I will then post here.

AntonioFromBrazil commented 3 years ago

Woow... it is great ! Do you think would be a good idea to have one awd code example as part of this library ?

stevstrong commented 3 years ago

Yes, I will make one.

AntonioFromBrazil commented 3 years ago

stevstrong, If there is anyway I can help in that amendments you have mentioned, just tell me. I am available to work on it if you give me some directions. Thanks

stevstrong commented 3 years ago

I need to commit some changes in the core files.

stevstrong commented 3 years ago

I just committed some changes in the lib, and added an example how to handle the AWD. Please check it out and send me your comments.

AntonioFromBrazil commented 3 years ago

I did not find any examples of AWD usage... Please tell me where the example you have added is located. Thanks

stevstrong commented 3 years ago

Ooops, I forgot to added to the commit list. Now you can find it here: https://github.com/rogerclarkmelbourne/Arduino_STM32/blob/master/STM32F1/libraries/STM32ADC/examples/AnalogWatchdogInterrupt/AnalogWatchdogInterrupt.ino

AntonioFromBrazil commented 3 years ago

It works perfectly... I think that example will help many others. Thanks a lot !!!

stevstrong commented 3 years ago

Issue solved.