Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Please provide a sample IAR AVR C Code for LED Blinking using Timer1 interrupt

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
Please provide a sample IAR AVR C Code for LED Blinking using Timer1 interrupt. I am new to IAR AVR Compiler. I have done LED Blinking project using delays and it works fine. I need code with Timer interrupts for ATMega32.
 

Hi,

it´s not that difficult:
Imagine you want to blink it with 1Hz. then you need to toggle output every 500ms.
If you use a 1MHz clock, then you need to setup your timer to interrupt every 500.000 clock cycles.
for best precision use a 16 bit timer. it can count up to 65535. To find the prescaler: divide 500000/65536 = 7.6, use the next higher available presacler. Here it is 8.
With a prescaler of 8 the timer/counter increments every 8 clock cycles.
then calculate the top value: divide 500000 / 8 = 62500. (The timer needs to increment 62500 times for a delay of 500 ms.)
But you have to take the the counter value "0" into account, so TOP value should be 62499 (counting 0 to 62499)
Now setup your timer/counter for CTC mode (4) with OCR1A used for the TOP value. Then the counter counts from 0 to 62499 and automatically restarts with 0.
Now store "62499" to the OCR1A registers.
Enable timer.
(Timer setup finished)
Setup LED port as output.

Additionally you need to write the code for the ISR:
just toggle the LED port pin
and return.
(ISR finished)


Klaus
 

@KlausST

My problem is not with doing the maths for Timer Calculation. I am new to IAR AVR Compiler and I don't know how to write the ISR function definition in IAR AVR and also how to set bit of a register.

I have written a code for LED Blinking in mikroC PRO AVR Compiler and it works fine. It interrupts every 500 ms. I need to write the same code in IAR AVR to see if IAR AVR generated better .hex file compared to mikroC PRO AVR.

Here is my code. I just don't know how to write the ISR routine in IAR AVR and also how to set the #pragma directive and also set the OCIE1A_bit. Also I need to set SREG_I_bit. I don't know how to do these in IAR AVR.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iom32.h>
#include <intrinsics.h>
 
// structure to allow bit field operations, name conversions: PORTA.0 -> PORT_A.b0  PORTB.7 -> PORT_B.b7
typedef struct{ char b0:1,
                     b1:1,
                     b2:1,
                     b3:1,
                     b4:1,
                     b5:1,
                     b6:1,
                     b7:1; } bits;
 
// define all the ports of your microcontroller, add more ports depending on the available mcu ports
#define PORT_A (* (volatile bits *) &PORTA)
#define PIN_A (* (volatile bits *) &PINA)
#define DDR_A (* (volatile bits *) &DDRA)
 
#define PORT_B (* (volatile bits *) &PORTB)
#define PIN_B (* (volatile bits *) &PINB)
#define DDR_B (* (volatile bits *) &DDRB)
 
#define PORT_C (* (volatile bits *) &PORTC)
#define PIN_C (* (volatile bits *) &PINC)
#define DDR_C (* (volatile bits *) &DDRC)
 
#define PORT_D (* (volatile bits *) &PORTD)
#define PIN_D (* (volatile bits *) &PIND)
#define DDR_D (* (volatile bits *) &DDRD)
 
void Delay_ms() {
  unsigned int i, j;
  
  for(i = 0; i < 127; i++) 
      for(j = 0; j < 1275; j++);
 
}
 
#pragma vector = TIMER1_COMPA_vect
 
void interrupt_ISR_TIMER1_COMPA_vect()
{
PORTB = ~PORTB; // Toggle pins on Port B
}
 
 
int main( void )
{
  
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD = 0xFF;
  
//global interrupts are enabled
__enable_interrupt();
 
 
  SREG_bit7 = 1; 
  TCCR1A = 0x80;
  TCCR1B = 0x09;
  OCR1AH = 0x0F; 
  OCR1AL = 0x9F; 
  OCIE1A_bit = 1; 
 
 
 
  while(1) {
       
  }
  
  return 0;
}

 

Hi,

Ok i see.
With the IAR syntax i can´t help.

Do you know avrfreaks forum?
This is especially for AVR hardware and software....

Hope this helps

Klaus
 

Ok. I made this code which compiles fine in IAR AVR Compiler but I don't see PORTB toggling.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iom32.h>
#include <intrinsics.h>
 
#pragma vector = TIMER1_COMPA_vect
 
__interrupt void irqHandler( void )
{
PORTB = ~PORTB; // Toggle pins on Port B
}
 
 
int main( void )
{
  
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD = 0xFF;
 
  SREG_Bit7 = 1; 
  TCCR1A = 0x80;
  TCCR1B = 0x09;
  OCR1AH = 0x0F; 
  OCR1AL = 0x9F; 
  TIMSK_Bit4 = 1; 
  
  //global interrupts are enabled
__enable_interrupt();
 
 
 
  while(1) {
       
  }
  
  //return 0;
}

 

I fixed the timer code and added one more line for setting F_OSC and it works fine now.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iom32.h>
#include <intrinsics.h>
 
#define F_CPU 4000000UL
 
#pragma vector = TIMER1_COMPA_vect
 
__interrupt void irqHandler( void )
{
PORTB = ~PORTB; // Toggle pins on Port B
}
 
 
int main( void )
{
  
  DDRA = 0xFF;
  DDRB = 0xFF;
  DDRC = 0xFF;
  DDRD = 0xFF;
 
  SREG_Bit7 = 1; 
  TCCR1A = 0x80;
  TCCR1B = 0x0B;
  OCR1AH = 0x7A; 
  OCR1AL = 0x11;
  TIMSK_Bit4 = 1; 
  
  //global interrupts are enabled
__enable_interrupt();
 
 
 
  while(1) {
       
  }
  
  //return 0;
}

 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top