merci mais ce n'est pas la peine !
________________
Ceci est valable pour
tous les Arduino ATMega 328 :+:
-> donc l'arduino UNO et nano par exemple (on trouve la nano sur
ebay à partir de 2$ )
La fréquence de découpage du PWM peut être ajustée au Hz près par le biais du diviseur ICR1
Attention ensuite la valeur du PWM varie sur une plage 0-ICR1 (et non plus la plage habituelle 0-255).
A noter que pour le VNH2SP30 (Motomonster à
18€ sur DX et autres pololu), la fréquence max à ne pas dépasser est de 20kHz (c'est le début de ce que l'on considère être le domaine ultrasonique).
Je vais mettre à jour le code complet sur x-sim.de et xsimulator.net.
Pour la Motomonster : lorsqu'elle est empilée sur l'arduino UNO, les pins de PWM utilisées dépendent d'une horloge qu'il ne vaut mieux pas modifier...
Comme ce sont les pins 9 et 10 de l'arduino qui sont boostées, sachez qu'il n'est plus possible d'empiler la motomonster sur la UNO.
En respectant le câblage ci-dessous,
Arduino /..Motomonster1
......GND <-> GND pin
........5V <-> 5V
.....pin 7 <-> pin 4 inA motor1
.....pin 8 <-> pin 8 inB motor1
.....pin 9 <-> pin 5 pwm motor1
on peut alternativement charger un code ou l'autre pour comparer :
http://www.hostingpics.net/viewer.php?id=754406pwmultrasonictest1.jpgLe code minimaliste normal :
////////////////////////////////////////////////////////////////////////////////
// DECLARATIONS
////////////////////////////////////////////////////////////////////////////////
/* VNH2SP30 pin definitions*/
// to have the wiring compliant with 20kHz pinout, you CANNOT stack the motomonster anymore!
/* Arduino /..Motomonster1
......GND <-> GND pin
........5V <-> 5V
.....pin 7 <-> pin 4 inA motor1
.....pin 8 <-> pin 8 inB motor1
.....pin 9 <-> pin 5 pwm motor1
*/
int inApin = 7; // INA: Clockwise input
int inBpin = 8; // INB: Counter-clockwise input
int pwmpin = 9; // PWM input
#define potR A5
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void setup()
{ // initialization of Arduino's pins
pinMode(inApin, OUTPUT);
pinMode(inBpin, OUTPUT);
pinMode(pwmpin, OUTPUT);
digitalWrite(inApin, HIGH);
digitalWrite(inBpin, LOW);
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Main Loop ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void loop()
{
int sensor;
sensor = analogRead(potR); // range 0-1024
analogWrite(pwmpin, (sensor/4));
}
le code ultrasonique :
////////////////////////////////////////////////////////////////////////////////
// DECLARATIONS
////////////////////////////////////////////////////////////////////////////////
/* VNH2SP30 pin definitions
// with 20kHz PWM pinout is changed, you CANNOT stack the motomonster anymore!
Arduino /..Motomonster1
......GND <-> GND pin
........5V <-> 5V
.....pin 7 <-> pin 4 inA motor1
.....pin 8 <-> pin 8 inB motor1
.....pin 9 <-> pin 5 pwm motor1
*/
int inApin = 7; // INA: Clockwise input
int inBpin = 8; // INB: Counter-clockwise input
int pwmpin = 9; // PWM input
#define potR A5
////////////////////////////////////////////////////////////////////////////////
// INITIALIZATION
////////////////////////////////////////////////////////////////////////////////
void analogWriteSAH_Init( void )
{
// Stop the timer while we muck with it
TCCR1B = (0 << ICNC1) | (0 << ICES1) | (0 << WGM13) | (0 << WGM12) | (0 << CS12) | (0 << CS11) | (0 << CS10);
// Set the timer to mode 14...
//
// Mode WGM13 WGM12 WGM11 WGM10 Timer/Counter Mode of Operation TOP Update of OCR1x at TOV1 Flag Set on
// CTC1 PWM11 PWM10
// ---- ----- ----- ----- ----- ------------------------------- ---- ----------------------- -----------
// 14 1 1 1 0 Fast PWM ICR1 BOTTOM TOP
// Set output on Channel A and B to...
//
// COM1z1 COM1z0 Description
// ------ ------ -----------------------------------------------------------
// 1 0 Clear OC1A/OC1B on Compare Match (Set output to low level).
TCCR1A =
(1 << COM1A1) | (0 << COM1A0) | // COM1A1, COM1A0 = 1, 0
(1 << COM1B1) | (0 << COM1B0) |
(1 << WGM11) | (0 << WGM10); // WGM11, WGM10 = 1, 0
// Set TOP to...
//
// fclk_I/O = 16000000
// N = 1
// TOP = 799
//
// fOCnxPWM = fclk_I/O / (N * (1 + TOP))
// fOCnxPWM = 16000000 / (1 * (1 + 799))
// fOCnxPWM = 16000000 / 800
// fOCnxPWM = 20000
ICR1 = 799;
// Ensure the first slope is complete
TCNT1 = 0;
// Ensure Channel A and B start at zero / off
OCR1A = 0;
OCR1B = 0;
// We don't need no stinkin interrupts
TIMSK1 = (0 << ICIE1) | (0 << OCIE1B) | (0 << OCIE1A) | (0 << TOIE1);
// Ensure the Channel A and B pins are configured for output
DDRB |= (1 << DDB1);
DDRB |= (1 << DDB2);
// Start the timer...
//
// CS12 CS11 CS10 Description
// ---- ---- ---- ------------------------
// 0 0 1 clkI/O/1 (No prescaling)
TCCR1B =
(0 << ICNC1) | (0 << ICES1) |
(1 << WGM13) | (1 << WGM12) | // WGM13, WGM12 = 1, 1
(0 << CS12) | (0 << CS11) | (1 << CS10);
}
void analogWriteD9( uint16_t value )
{
// My variable value varies from 0 to 1024
// but awaited range in OCR1A is from 0 to 799 and nothing else!
OCR1A = constrain(map(value, 0, 1024, 0, 799), 0, 799);
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void setup()
{
analogWriteSAH_Init();
// initialization of Arduino's pins
pinMode(inApin, OUTPUT);
pinMode(inBpin, OUTPUT);
pinMode(pwmpin, OUTPUT);
digitalWrite(inApin, HIGH);
digitalWrite(inBpin, LOW);
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Main Loop ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void loop()
{
int sensor;
sensor = analogRead(potR); // range 0-1024
analogWriteD9(sensor);
}