Voulant utiliser le Timer/Counter dans un Arduino Uno, je parviens à écrire dans le TCNT1 mais pas du tout dans le OCR1A.Ni écrire ni lire. Même en utilisant séparéremnt
OCR1AH et OCR1AL.
Dans le OCR0A j'y parviens.
Y a-t-il une explication à mon problème ?
Merci d'avance.
Coxyde
Un grand merci à vous deux.
Il faut absolument mettre le TCCR1A AVANT OCR1A. Ce qui n'était pas expliqué dans le manuel d'Atmel bien que'j'avais aussi essayé de clear l'interrupt principal SREG.
Bien cordialement.
coxyde
Il me semble que les opérations de lecture/écriture dans OCR1A dépendent du "Waveform Generation Mode" , bits WGM10 & 11 de TCCR1A et WGM12 & 13 de TCCR1B.
/*
https://www.avrfreaks.net/forum/ocr1a-16-bit-register-treated-8-bit
The Arduino startup code (in init(), in wiring.c) preconfigures all of an AVR's timers.
TIMER1 gets set to mode 1 (8-bit phase correct PWM), with prescaler /64.
Mode 1 is selected with WGM1[3:0]=0b0001, and WGM10 is in TCCR1A, which you left untouched.
You then set WGM12 in TCCR1B, selecting mode 5 (not 4 as you'd expected), which is 8-bit fast PWM.
In the 8-bit PWM modes, OCRnx are truncated to 8 bits (in the 9-bit and 10-bit modes, they are truncated to 9 and 10 bits, respectively).
The moral of the story is that if you're configuring timers yourself within an Arduino sketch,
don't take for granted that the registers will be in the post-reset default states.
BPJ : on dirait que quand ( Cf Table 20-6. Waveform Generation Mode Bit Description )une valeur fixe
(0xFFFF , 0x03FF, 0x01FF, 0X00FF ) est utilisée por TOP , il y a un & logique effectué
entre cette valeur et OCR1A [ OCR1A = OCR1A & x03FF par exemple]
*/
unsigned char sreg;
const unsigned int c_OCR1Aa = 0xFFFF; //0x1356; // 0x1256;
volatile unsigned int i_OCR1A;
int mode = -15;
unsigned int ok = 0;
byte j = 0;
byte a = 0;
byte b = 0;
void setup() {
Serial.begin(1000000);
delay(100);
//ICR1 = 0xFFFF; //pour tester les cas où TOP = ICR1
Serial.print("ICR1 = 0x");
Serial.print(ICR1, HEX);
Serial.print(" OCR1Aa = 0x");
Serial.print(OCR1A, HEX);
Serial.print(" TCCR1Aa = 0x");
Serial.print(TCCR1A, HEX);
Serial.print(" TCCR1Ba = 0x");
Serial.println(TCCR1B, HEX);
Serial.println(" ************ fin valeurs au boot ************");
// TCCR1A = 0;
// TCCR1B = 0;
// fait varier les bits WGM de TCCR1B & TCCR1A
// pour faire varier le Waveform Generation Mode
// cf Table 20-5. Compare Output Mode, Phase Correct and Phase and Frequency Correct PWM
for (b = 0; b <= 3 ; b++) { //fait varier WGM12 & 13 de TCCR1B
TCCR1B = (8 * b) + 1; //éventuellement ajouter 1 pour prescaler timer1
//bits CS10, CS11, CS12 de TCCR1B
for ( a = 0; a <= 3 ; a++) { //fait varier WGM10 & 11 de TCCR1A
mode = (4 * b) + a ; //n° Waveform Generation Mode
TCCR1A = a;
sreg = SREG; //atomic read
cli(); //atomic read
OCR1A = c_OCR1Aa;
i_OCR1A = OCR1A ;
SREG = sreg; //atomic read
////if ( i == c_OCR1Aa) {
ok = ok + 1;
Serial.print("ok = ");
Serial.print(ok);
Serial.print(" b = ");
Serial.print(b);
Serial.print(" a = ");
Serial.print(a);
Serial.print(" mode = ");
Serial.println(mode , HEX);
Serial.print("TCCR1Ab = 0x");
Serial.print(TCCR1A, HEX);
Serial.print(" TCCR1Bb = 0x");
Serial.println(TCCR1B, HEX);
Serial.print("OCR1Aa = 0x");
Serial.print(OCR1A, HEX);
Serial.print(" i_OCR1Aa = 0x");
Serial.print(i_OCR1A, HEX);
Serial.print(" OCR1AH = 0x");
Serial.println(OCR1AH, HEX);
Serial.println("--------------");
////}
}
}
Serial.print("ICR1 = 0x");
Serial.println(ICR1, HEX);
}
void loop() {
}