« Micro contrôleurs AVR/Travail pratique/Télécommande NRF24L01 pour Robot » : différence entre les versions

Contenu supprimé Contenu ajouté
Ligne 1 112 :
{{Solution|contenu=
<big>'''Solution non vérifiée donc à vérifier au plus vite !'''</big>
 
'''Question 1°'''
<syntaxhighlight lang="C">
Ligne 1 126 ⟶ 1 127 :
DDRC |= 0x0F; // PCO/A0, PC1/A1, PC2/A2 et PC3/A3 en sortie
</syntaxhighlight>
 
'''Question 2°'''
 
<syntaxhighlight lang="C">
#define MAXPWM 100
// question 32°
void deplaceToi(int16_t pwmD, int16_t pwmG) {
if (pwmD > MAXPWM) pwmD = MAXPWM;
Ligne 1 136 ⟶ 1 139 :
if (pwmG < -MAXPWM) pwmG = -MAXPWM;
if (pwmD > 0) {
digitalWritePORTC |= (A2,HIGH1<<PC2);
digitalWritePORTC &= ~(A3,LOW1<<PC3);
analogWrite(6,OCR0A = pwmD);
//analogWrite(6,pwmD);
}
if (pwmD == 0) {
digitalWritePORTC |= (A2,HIGH1<<PC2);
digitalWritePORTC |= (A3,HIGH1<<PC3);
analogWrite(6,OCR0A = pwmD);
//analogWrite(6,pwmD);
}
if (pwmD < 0) {
digitalWritePORTC &= ~(A2,LOW1<<PC2);
digitalWritePORTC |= (A3,HIGH1<<PC3);
analogWrite(6,OCR0A = -pwmD);
//analogWrite(6,-pwmD);
}
if (pwmG > 0) {
digitalWritePORTC |= (A0,LOW1<<PC1);
digitalWritePORTC &= ~(A1,HIGH1<<PC0);
analogWrite(5,OCR0B = pwmG);
//analogWrite(5,pwmG);
}
if (pwmG == 0) {
digitalWritePORTC |= (A0,HIGH1<<PC1);
digitalWritePORTC |= (A1,HIGH1<<PC0);
analogWrite(5,OCR0B = pwmG);
//analogWrite(5,pwmG);
}
if (pwmG < 0) {
digitalWritePORTC |= (A0,HIGH1<<PC0);
digitalWritePORTC &= ~(A1,LOW1<<PC1);
analogWrite(5,OCR0B = -pwmG);
//analogWrite(5,-pwmG);
}
}
</syntaxhighlight>
 
'''Question 3°'''
int main() {
 
uint16_t joystick_x,joystick_y,pwmD,pwmG,pwm,deltaPWM;
<syntaxhighlight lang="C">
intnt main() {
uint16_tint16_t joystick_x,joystick_y,pwmD=60,pwmG,pwm,deltaPWM;
uint8_t rx_buffer[RX_PLOAD_WIDTH],status1,cas=0;
// setup
//serialInit();
//ADC_Init();
SPIMasterInit();
SetRX_Mode();
// ****** gestion du timer
//Configuration (6 et 5 pour NANO)
DDRD |= ((1<<DDD6)|(1<<DDD5));
// Set the Timer Mode to PWM fast
TCCR0A |= ((1 << WGM01) | (1<<WGM00));
// clear OC0A on compare match set OC0A at TOP and same for OCOB
TCCR0A |= ((1 << COM0A1)|(1 << COM0B1));
// set prescaler to 256 and start the timer
TCCR0B |= (1 << CS01)|(1 << CS00);
// gestion du sens d'avancement
DDRC |= 0x0F; // PCO/A0, PC1/A1, PC2/A2 et PC3/A3 en sortie
// loop
while (1) {
status1=nRF24L01_RxPacket(rx_buffer);
if (status1 & (1<<RX_DR)) {
joystick_xjoystick_y = (rx_buffer[1]<<8)+rx_buffer[0];
joystick_yjoystick_x = (rx_buffer[3]<<8)+rx_buffer[2];
serialWrite16Deci(joystick_x);serialWrite(' ');serialWrite('-');serialWrite(' ');
serialWrite16Deci(joystick_y);
serialWrite(0x0D);serialWrite(0x0A);
// question 3° Exo 7
pwm = joystick_y-513;
// calcul de deltaPWM
deltaPWM = joystick_x-504;
// gestion de différentes possibilités avec la variable cas
if (pwm > 30) cas |= 0x08; else cas &= 0x07;
if (pwm < -30) cas |= 0x04; else cas &= 0x0B;
Ligne 1 202 ⟶ 1 228 :
default : pwm = 0; deltaPWM=0;
} // switch
pwmG = pwm - deltaPWM;
pwmD = pwm + deltaPWM;
deplaceToi(pwmD,pwmG);