| Author |
Message |
|
|
Posted: May 17, 2012 - 11:16 AM |
|

Joined: May 17, 2012
Posts: 4
|
|
I am using timer2 compare match interrupt on Atmega32. The problem is that after executing the interrupt it does not return to the main(). But when the next compare match interrupt arises it goes to the interrupt routine again. Any suggestions? reti() does not help this issue.
my code:
initialization of timer2
Code:
SETBIT(TCCR2,CS22); //prescaling to 1024
SETBIT(TCCR2,CS21); //prescaling to 1024
SETBIT(TCCR2,CS20); //prescaling to 1024
OCR2=100;//gives ~15ms loop //100 gives ~10ms
SETBIT(TIMSK,OCIE2); //compare match interrupt
asm("SEI");
and ISR:
Code:
ISR(TIMER2_COMP_vect){
turnGreenLedOn();
timerCompflag=1;
//reti();
}
|
|
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 11:20 AM |
|


Joined: Mar 27, 2002
Posts: 18530
Location: Lund, Sweden
|
|
|
Quote:
The problem is that after executing the interrupt it does not return to the main()
How are you determining this?
In order to get good answers here, or to actually solve it yourself, this is what you should do: Create a minimal but complete program that builds and runs and that displays the problem. Post that code here.
As for right now you have not even shown the main() that you claim execution does not return to. This leaves us in the usual guessing game, which not unlikely will be a waste of time.
Your call.. |
|
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 11:24 AM |
|

Joined: May 17, 2012
Posts: 4
|
|
I determined it:
1) hardware experiment (with LEDs)
2) debuger
My code:
Code:
/*
* SyncController.c
*
* Created: 2012.05.10. 9:49:33
* Author: xnonamex
*/
#include <avr/io.h>
#include "PWM.h"
#include "LCD.h"
#include "Tachometers.h"
#include "macros.h"
#include "controlBoard.h"
#define F_CPU 10000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
#define READY_STRING "Ready"
#define FAIL "FL"
#define EMPTY " "
unsigned char timerCompflag=0;
ISR(INT0_vect)
{
leftWheelPulses++;
}
ISR(INT1_vect)
{
rightWheelPulses++;
}
ISR(TIMER2_COMP_vect){
turnGreenLedOn();
timerCompflag=1;
//reti();
}
int main(void)
{
setUpPWM();
setUpWheels();
LCD_init();
initTachometer();
setUpControlBoard();
//writeStringToDisplay(READY_STRING);
//_delay_ms(1000);
//lcdClear();
SETBIT(TCCR2,CS22); //prescaling to 1024
SETBIT(TCCR2,CS21); //prescaling to 1024
SETBIT(TCCR2,CS20); //prescaling to 1024
OCR2=100;//gives ~15ms loop //100 gives ~10ms
SETBIT(TIMSK,OCIE2); //compare match interrupt
TCNT2=0;
asm("SEI");
while(1)
{
if(timerCompflag==1)
{ toggleRedLed();
TCNT2=0;
timerCompflag=0;
Control();
}
}
}
|
|
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 11:26 AM |
|


Joined: Jul 18, 2005
Posts: 62228
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
You haven't said which AVR or which C compiler so the following are two wild guesses based on no evidence at all:
1) if it's a mega128 then the M103C fuse has not been cleared
20 if it is avr-gcc being used you have been caught by the "catch all" mechanism documented on this page of the manual:
http://www.nongnu.org/avr-libc/user-man ... rupts.html
If it turns out to be either of these I claim my £5 and also 3,000 points of added kudos for knowing how to operate a crystal ball!
EDIT: our posts crossed so at least we now know it is avr-gcc so it could be (2) but then we don't know what's really happening here:
Code:
setUpPWM();
setUpWheels();
LCD_init();
initTachometer();
setUpControlBoard();
|
_________________
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 11:35 AM |
|

Joined: May 17, 2012
Posts: 4
|
|
As I understood catch-all means that you don't have an ISR for an activated interrupt and the result is that the program restarts all the time, which is not the case this time.
setUpPWM:
Code:
void setUpPWM(void){
/*Setup Timer Counter 1 in Fast PWM Mode*/
SETBIT(TCCR1A,WGM11);
SETBIT(TCCR1B,WGM12);
SETBIT(TCCR1B,WGM13);
/*Set the PWM Mode to Non inverted mode for the output OC1A and OC1B*/
SETBIT(TCCR1A,COM1A1);
SETBIT(TCCR1A,COM1B1);
/*pre-scaling*/
SETBIT(TCCR1B,CS10);
/*Set PD5 and PD4 as an output*/
SETBIT(DDRD,PD4);
SETBIT(DDRD,PD5);
ICR1=255; //f=10kk/250=40k
}
setupwheels:
Code:
void setUpWheels(void){
//output on pins for directions
SETBIT(DDRD,PD0);
SETBIT(DDRD,PD1);
}
LCD_init():
Code:
void LCD_init(){
DDRB|=0xFF; //bit 7 down to 0 set to output
_delay_ms(70);
LCD_nibble_transfer(0x0C);
_delay_ms(24);
LCD_nibble_transfer(0x0C);
_delay_ms(24);
LCD_nibble_transfer(0x0C);
_delay_ms(24);
LCD_nibble_transfer(0x04);
_delay_ms(24);
LCD_cmd_write(0x28); // 4 bit data and 2 lines display
_delay_ms(2);
LCD_cmd_write(0x08); //set display
_delay_ms(2);
LCD_cmd_write(0x01); //clear display
_delay_ms(2);
LCD_cmd_write(0x06); //entry mode move cursor to the right
_delay_ms(2);
LCD_cmd_write(0x0e); //display on,and add cursor
_delay_ms(2);
LCD_cmd_write(0x02); //set cursor to first digit; (addr 0x80)
_delay_ms(2);
}
initTachometer:
Code:
void initTachometer(){
SETBIT(MCUCR,ISC11);//Int on rising edge
SETBIT(MCUCR,ISC10);//Int on rising edge
SETBIT(MCUCR,ISC01);//Int on rising edge
SETBIT(MCUCR,ISC00);//Int on rising edge
}
setUpControlBoard:
Code:
void setUpControlBoard(void){
SETBIT(DDRC,DDC0);//set as output
SETBIT(DDRC,DDC1);//set as output
SETBIT(DDRC,DDC2);//set as output
//rest is input
}
|
|
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 11:40 AM |
|


Joined: Jul 18, 2005
Posts: 62228
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
This is a bit like pulling teeth but can we see
Code:
turnGreenLedOn();
next please?
To be honest I'd just follow Johan's advice. Cut the code down to the minimal test program that still shows the fault and post the entire thing. 90% of the time while doing such a cut down you will encounter a "slaps forehead" moment where you finally see what you did wrong anyway.
EDIT: oh wait a minute I'm an idiot this is FAQ#1. Time to use volatile!
(guess why it's FAQ#1 in my signature!). It's also FAQ#1 in the manual for the compiler:
http://www.nongnu.org/avr-libc/user-manual/FAQ.html
and I also wrote some words on this to explain even further:
Optimization and the importance of volatile in gcc
I think we were mislead by the thread title - it DOES return to main(), it's just main() does not "see" the change (to the flag) which was made in the interrupt. |
_________________
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 11:48 AM |
|

Joined: May 17, 2012
Posts: 4
|
|
fml... thanks  |
|
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 12:06 PM |
|


Joined: Jul 18, 2005
Posts: 62228
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
|
|
|
|
Posted: May 17, 2012 - 01:18 PM |
|


Joined: Dec 21, 2006
Posts: 1483
Location: Saar-Lor-Lux
|
|
|
clawson wrote:
Quote:
fml
New one on me.
One of:- Football Manager Live (web community)
- Formal
- Fluorometholone
- Family Medical Leave (US)
- Football Manager Live (video game)
- Field Manipulation Language
- Fancy Markup Language
- Famous Last Words
- Ferret Mailing List
- Fuck my Love
- Fibre Metal Laminate (Delft University of Technology)
- Fix My Life
- Feeling Mighty Low
- Facebook Me Later
- Forget My Life (polite form)
- Forms Markup Language
- Facebook Markup Language
- For My Lady (Moody Blues song)
- Full Maximum Likelihood
- For My Life (trademark of Be in Health)
- Formal Methods Letters
- Follow My Lead
- Funk My Life
- Forgot My Lunch
- Force Module Library
- Flexible Membrane Lining
- Falmouth Memorial Library (Falmouth, ME)
- Fiji Muslim League
- Famecos Mailing List
- Fashion Mailing List
- Frequency Measurement Logic (chip)
- Fast Maximum Likelihood Method
- FAQ (Frequently Asked Questions) Markup Language (an Apache Maven project)
- Found My Lamington
- For My Lover (Tracy Chapman song)
- File Manipulation Language
- Florida Mathematics League
- Free Music Loops (UK)
- Freestyle Markup Language
- Feed Mill License (medication; US FDA)
- Fujitsu Microelectronics Limited
|
|
|
| |
|
|
|
|
|
Posted: May 17, 2012 - 05:32 PM |
|


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA
|
|
|
clawson wrote:
... Time to use volatile!
 |
_________________ Larry
Those afraid to embrace the future will quickly fade into the past. - larryvc
|
| |
|
|
|
|
|