Hi! Someone can explain me how to use the sensor HC SR04 using the internal register of Arduino? (using PORTx, DDRx ecc...) Thanks!
you will need a timer to play with your module.
PORTX and other register command allow you to set or clear Pin on GPIO. I propose you to read the manuel user where every one are detailled.
Then, you could be able to set a specific pin with a command like:
PORTA |= 0b00000001
assuming the pin to set is the first in the corresponding register. It is a (fancy) quick method to play on pin but to put the right TRIGGER SIGNAL (10ms) and listen for the ECHO (between 5 and 30ms), you will need a timer involved, and maybe an interrupt to detect the ECHO.
You can see the Register command as a substitute for digitalWrite()
but it won't make the job alone.
Thanks, but actually I have no idea how to start writing the code. I have been using Arduino for a short time, I am able to use the sensor normally, but not using the registers.
If you already have it working, why do you need to change to a low-level register technique?
that will not the perfect example I want for you, but here is a (french written) article explaining how to set timer (the missing part for your module!) with Register commands
https://www.locoduino.org/spip.php?article88
see this example:
void setup () {
pinMode (Led, OUTPUT);
pinMode (Led2, OUTPUT);
cli(); // Désactive l'interruption globale
bitClear (TCCR2A, WGM20); // WGM20 = 0
bitClear (TCCR2A, WGM21); // WGM21 = 0
TCCR2B = 0b00000110; // Clock / 256 soit 16 micro-s et WGM22 = 0
TIMSK2 = 0b00000001; // Interruption locale autorisée par TOIE2
sei(); // Active l'interruption globale
}
TCCR2B and TIMSK2 are two register's adresses. By directly change its value (like TIMSK2 = 0b00000001), you change the settings of the function.
To be able to use this, the register mapping is mandatory (user manual of the chip). For the rest, it is a very classical "attribute a value to a memory".
In the example and my fisrt answer, I talk about WRITING on a register, but the opposite is true, and you can read the register value (for example a Led state plugged in GPIO).
Thanks, I will try in this way
Because I'm a university student and I need to use registers for an exam.
And study nowadays is asking in a forum for help?
always better than payed enginner asking for an ever written code.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.