Cours:TPS 2103 tp positionnementMoteur : Différence entre versions
| Ligne 7 : | Ligne 7 : | ||
=Esclave (target) i2c= | =Esclave (target) i2c= | ||
| + | On donne les exemples suivant pour qu'un µcontroleur atmegaxxx se comporte comme un esclave i2c avec différents registres | ||
| + | {| | ||
| + | |- | ||
| + | | | ||
| + | <source lang=cpp> | ||
| + | // ******* | ||
| + | // ******* | ||
| + | // i2c target | ||
| + | // ******* | ||
| + | // ******* | ||
| + | // ne pas modifier !!! | ||
| + | // ******* | ||
| + | // ******* | ||
| + | // Include the required Wire library for I2C | ||
| + | #include <Wire.h> | ||
| + | |||
| + | const int8_t i2cAddress = 0x1A; | ||
| + | |||
| + | volatile uint16_t valPotar; | ||
| + | |||
| + | void setup() { | ||
| + | // Start the I2C Bus as Slave on address | ||
| + | Wire.begin(i2cAddress); | ||
| + | Wire.onRequest(requestEvent); | ||
| + | } | ||
| + | |||
| + | |||
| + | void requestEvent() | ||
| + | { | ||
| + | Wire.write(valPotar>>2); | ||
| + | } | ||
| + | |||
| + | |||
| + | void loop() { | ||
| + | valPotar=analogRead(A0); | ||
| + | _delay_ms(100); | ||
| + | } | ||
| + | </source> | ||
| + | || | ||
| + | <source lang=cpp> | ||
| + | // ******* | ||
| + | // ******* | ||
| + | // i2c target | ||
| + | // ******* | ||
| + | // ******* | ||
| + | // ne pas modifier !!! | ||
| + | // ******* | ||
| + | // ******* | ||
| + | |||
| + | // Include the required Wire library for I2C | ||
| + | #include <Wire.h> | ||
| + | |||
| + | const int8_t i2cAddress = 9; | ||
| + | |||
| + | |||
| + | void setup() { | ||
| + | DDRD=0xFF; | ||
| + | // Start the I2C Bus as Slave on address | ||
| + | Wire.begin(i2cAddress); | ||
| + | Wire.onReceive(receiveEvent); | ||
| + | } | ||
| + | |||
| + | void receiveEvent(int nbBytes) | ||
| + | { | ||
| + | PORTD=Wire.read(); | ||
| + | } | ||
| + | |||
| + | |||
| + | void loop() { | ||
| + | _delay_ms(100); | ||
| + | } | ||
| + | |||
| + | </source> | ||
| + | |} | ||
=Interface écran/bouton= | =Interface écran/bouton= | ||
Version du 13 avril 2023 à 20:36
Pensez à mettre sur la 1ère ligne de votre code : // Compiler: Avrgcc device: nomDuMicrocontroleur
Asservissement en position du moteur CC
Esclave (target) i2c
On donne les exemples suivant pour qu'un µcontroleur atmegaxxx se comporte comme un esclave i2c avec différents registres
// *******
// *******
// i2c target
// *******
// *******
// ne pas modifier !!!
// *******
// *******
// Include the required Wire library for I2C
#include <Wire.h>
const int8_t i2cAddress = 0x1A;
volatile uint16_t valPotar;
void setup() {
// Start the I2C Bus as Slave on address
Wire.begin(i2cAddress);
Wire.onRequest(requestEvent);
}
void requestEvent()
{
Wire.write(valPotar>>2);
}
void loop() {
valPotar=analogRead(A0);
_delay_ms(100);
}
|
// *******
// *******
// i2c target
// *******
// *******
// ne pas modifier !!!
// *******
// *******
// Include the required Wire library for I2C
#include <Wire.h>
const int8_t i2cAddress = 9;
void setup() {
DDRD=0xFF;
// Start the I2C Bus as Slave on address
Wire.begin(i2cAddress);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int nbBytes)
{
PORTD=Wire.read();
}
void loop() {
_delay_ms(100);
}
|