PA3040
November 10, 2016, 2:31pm
1
Dear All,
Following Timer1 codes does not fire the interrupt vector
please advice
void setup() {
Serial.begin(9600);
TCNT1H = 0X00;
TCNT1L = 0X00;
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Set up timer at Fcpu/64 ??
TIMSK0 |= (1 << TOIE1); // Enable overflow interrupt??
sei(); // Enable global interrupts??
Serial.println(TCNT1L);
pinMode(13,OUTPUT);
}
void loop() {
Serial.println(TCNT1H);
while (1) {
digitalWrite(13,HIGH);
}
}
ISR(TIMER1_OVF_vect)
{
Serial.println(TCNT1L);
digitalWrite(13,LOW);
}
Please advice
Thanks in advance
Just brief look and I found that you have Serial.print in the ISR. It is function which uses interrupt also and interrupts are disabled inside of ISR by default. Do not use it in this way. Instead of this try to save TCNT into the global variable and to set some flag or another variable for interrupt occurrence. You can print the counter normally in loop().
PA3040
November 10, 2016, 3:24pm
3
Dear Budvar10 Thanks for the reply
I changed the code base on your advice
Please see the codes that isn't working
void setup() {
Serial.begin(9600);
TCNT1H = 0X00;
TCNT1L = 0X00;
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Set up timer at Fcpu/64 ??
TIMSK0 |= (1 << TOIE1); // Enable overflow interrupt??
sei(); // Enable global interrupts??
Serial.println(TCNT1L);
pinMode(13,OUTPUT);
pinMode(5,OUTPUT);
}
void loop() {
Serial.println(TCNT1H);
while (1) {
digitalWrite(13,HIGH);
}
}
ISR(TIMER1_OVF_vect)
{
digitalWrite(5,HIGH);
}
Please advice
Thanks in advance
TIMSK0 |= (1 << TOIE1); // Enable overflow interrupt??
The "??" indicates that you do not bother reading the processor data sheet, which we highly recommend.
TIMSK0 controls TIMER0, not TIMER1, and of course it does not make sense to use a variable defined for TIMER1 to modify the behavior of TIMER0.
PA3040
November 10, 2016, 5:15pm
5
Dear jremington
Thanks for the advice and correction
Now it is working
But I have more questions
it is working with following prescaler
TCCR1B |= ((0 << CS12) | (1 << CS11) |(1 << CS10)); //clkI/O/64 (From prescaler)
But it does not working with following prescaler setting
TCCR1B |= ((1 << CS12) | (0 << CS11) |(1 << CS10));//clkI/O/1024 (From prescaler)
Please advice
Thanks in advance
I wrote that you have to use variable and flag - according the first program you have posted. This:
void loop() {
Serial.println(TCNT1H);
...
cannot guarantee that the TCNT1H was not changed in meantime. Exact value is guaranted only in its ISR while the interrupt is disabled.
volatile byte counter, flag = 0;
void loop() {
// Serial.println(TCNT1H);
if(flag) {
flag = 0; // reset
Serial.println(counter); // value saved in ISR
}
while (1) {
digitalWrite(13,HIGH);
}
}
ISR(TIMER1_OVF_vect)
{
flag = 1; // we need to say to the loop that interrupt occurred and new value is in the counter
counter = TCNT1H; // now register cannot be changed we can save the value
digitalWrite(5,HIGH);
}
PA3040:
TCCR1B |= ((0 << CS12) | (1 << CS11) |(1 << CS10)); //clkI/O/64 (From prescaler)
But it does not working with following prescaler setting
TCCR1B |= ((1 << CS12) | (0 << CS11) |(1 << CS10));//clkI/O/1024 (From prescaler)
Probably the '|=' is a problem. If any of CS bits is set, it stay set after operation. You have to clear them first before OR.
The first one works probably just with luck.
TCCR1B |= ((1 << CS12) | (0 << CS11) |(1 << CS10));//clkI/O/1024 (From prescaler)
This syntax does not set the prescaler for 1024 (101) from an arbitrary state of TCCR1B.
TCCR1B |= (1<<CS12) | (1<<CS10); //set the prescaler bits you want
TCCR1B &= ~(1<<CS11);//clear the prescaler bits you don't want
PA3040
November 10, 2016, 6:57pm
11
Dear cattledog Thanks for the reply and advice
The program is working after your advice
my new question is
The interrupt fire may occur even GIE disable
//sei(); // Enable global interrupts??
//cli();
//SREG |= (1 << 7);
Please advice
Thanks in advance
Please post your current code in full.
PA3040
November 10, 2016, 7:26pm
13
Dear cattledog Thank you so much for your continues support and advice
Please find the code
void setup() {
// Serial.begin(9600);
TCNT1H = 0X00;
TCNT1L = 0X00;
TCCR1B |= (1<<CS12) | (1<<CS10); //set the prescaler bits you want
TCCR1B &= ~(1<<CS11);//clear the prescaler bits you don't want
TIMSK1 |= (1 << TOIE1); // Enable overflow interrupt??
//sei(); // Enable global interrupts??
//cli();
//SREG |= (1 << 7);
//Serial.println(TCNT1L);
pinMode(13,OUTPUT);
pinMode(5,OUTPUT);
// TIFR1 |= (1 << TOV1);
}
void loop() {
//Serial.println(TCNT1H);
while (1) {
//PORTB |=~(1 << PB5);
delay(500);
}
}
ISR(TIMER1_OVF_vect)
{
//digitalWrite(5,!HIGH);
PORTD =(1 << PD5);
}
//sei(); // Enable global interrupts??
//cli();
//SREG |= (1 << 7);
The interrupt fire may occur even GIE disable
I don't know what you mean by GIE? I do not know any register of and AT328 called GIE.
Are you referring to "global interrupt enable"?
The default in the Arduino is for interrupts to be enabled. The Arduino IDE sets the global interrupt enable bit 7 of SREG.
If you have questions about default register values or the register value at any time, you can print it out.
Serial.println(SREG,BIN);
With timer registers in particular, it is often necessary to know the default set up, because when you set bits with out initializing registers (for example TCCR1B = 0) or specifically clearing bits, you can get unexpected results from the default values.
PA3040
November 11, 2016, 4:28am
15
Dear cattledog thanks again again for the reply
Sorry about GIE which I got it from the data sheet
Bit 7 – I: Global Interrupt Enable
New Quentin
Can we able to disable GIE bit which is default enable by the Arduino
Please advice
Thanks in advance
Can we able to disable GIE bit which is default enable by the Arduino
Yes, that is what cli() does. It is the same as noInterrupts().
sei() is the command to enable the global interrupts. It is the same as interrupts().
Here is a list of the 25 interrupts for ATMega 328 which will not working when there is a global disable. Reset will continue to function. Interrupts are globally disabled within an ISR, which is why you were told not to use Serial.print() and to keep your ISR's short. See Nick Gammon's tutorial on interrupts Gammon Forum : Electronics : Microprocessors : Interrupts
and on Timers Gammon Forum : Electronics : Microprocessors : Timers and counters
1 Reset
2 External Interrupt Request 0 (pin D2) (INT0_vect)
3 External Interrupt Request 1 (pin D3) (INT1_vect)
4 Pin Change Interrupt Request 0 (pins D8 to D13) (PCINT0_vect)
5 Pin Change Interrupt Request 1 (pins A0 to A5) (PCINT1_vect)
6 Pin Change Interrupt Request 2 (pins D0 to D7) (PCINT2_vect)
7 Watchdog Time-out Interrupt (WDT_vect)
8 Timer/Counter2 Compare Match A (TIMER2_COMPA_vect)
9 Timer/Counter2 Compare Match B (TIMER2_COMPB_vect)
10 Timer/Counter2 Overflow (TIMER2_OVF_vect)
11 Timer/Counter1 Capture Event (TIMER1_CAPT_vect)
12 Timer/Counter1 Compare Match A (TIMER1_COMPA_vect)
13 Timer/Counter1 Compare Match B (TIMER1_COMPB_vect)
14 Timer/Counter1 Overflow (TIMER1_OVF_vect)
15 Timer/Counter0 Compare Match A (TIMER0_COMPA_vect)
16 Timer/Counter0 Compare Match B (TIMER0_COMPB_vect)
17 Timer/Counter0 Overflow (TIMER0_OVF_vect)
18 SPI Serial Transfer Complete (SPI_STC_vect)
19 USART Rx Complete (USART_RX_vect)
20 USART, Data Register Empty (USART_UDRE_vect)
21 USART, Tx Complete (USART_TX_vect)
22 ADC Conversion Complete (ADC_vect)
23 EEPROM Ready (EE_READY_vect)
24 Analog Comparator (ANALOG_COMP_vect)
25 2-wire Serial Interface (I2C) (TWI_vect)
26 Store Program Memory Ready (SPM_READY_vect)
PA3040
November 11, 2016, 6:06am
17
Dear cattledog Thanks for the reply and info
I go through the links that you provided which will be more helpful for me
I did not sow the Timer0 experiments in the links which Arduino is using mills()
Can we able to disable the timer0 default operation in the Arduino IDE then I can do some experiment with timer 0 for better understands
Please advice
Thanks in advance
Can we able to disable the timer0 default operation in the Arduino IDE then I can do some experiment with timer 0 for better understands
You can do whatever you want with Timer0. Just be aware that changing the prescaler or the mode will break the millis() and micros() functions.
When directly modifying registers and getting away from the standard functions of the IDE, you want the ATmega 328 data sheet close at hand.
http://www.atmel.com/Images/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_datasheet.pdf
PA3040
November 11, 2016, 3:24pm
19
Dear cattledog Thank for the reply
Please check following ISR function isn't working with Timer 0
ISR (TIMER0_OVF0_vect)
{
}
Please advice
Thanks in advance
following ISR function isn't working with Timer 0
Because you are making up syntax and not following any reference material. If you are not willing or able to put in the work to understand what you are doing, you should not be modifying registers. I apologize if I sound harsh, but proceeding with this level of question is not productive.
//ISR (TIMER0_OVF0_vect)
ISR (TIMER0_OVF_vect)
PA3040
November 11, 2016, 4:50pm
21
Dear cattledog Thanks for the reply
Still following ISR Function does not compile with arduini IDE
ISR (TIMER0_OVF_vect){
}