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.

Arduino - Input capture programming

Status
Not open for further replies.

Fe(II)man

Member level 1
Joined
Aug 26, 2012
Messages
38
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,288
Location
Durban
Activity points
1,633
Howsit Guys,

my post today is with regard to monitoring the time between two events such as:

The time between a rising edge and a falling edge of a square wave that is connected to the input capture pin of the Arduino uno atmega328.

I wish to set up the micro to trigger upon a falling edge and thereafter save the value stored in the ICR and thereafter restart the timer on the next rising edge.

I can do this in assembly easily, just having a small problem using C in the arduino environment. Newbie in the arduino field.

Any help will be appreciated.

I would like some help in setting up the registers to monitor the ICP pin for the falling edge.

thanks

Atmega 328 datasheet:

http://www.atmel.com/Images/8271s.pdf
 

code I have so far:


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
#include <avr/io.h> 
#include <avr/interrupt.h> 
 
// Set up input capture mode
// Set up timers
// Set up port for output
 
unsigned int Period;
 
void setup(void) 
{ 
  Serial.begin(9600); 
  DDRB |= 0x01; 
  
  Init(); 
  
  Serial.println("Init finished"); 
} 
 void loop(void)
 {
   while(1)
   {
     Serial.print(" ");
   }
 } 
 
void Init(void) 
{ 
    //init Port B0 
    DDRB &= ~0x01;   // make ICP1 pin i/p 
    //init timer registers
    TCCR1A = 0x00;   // Normal Mode
    TCCR1B = 0x05; // prescalar 1024
    TIMSK1 = 0x21;  //-Interrupt enable 
                     // Timer overflow enable 
    TCNT1 = 0; 
    //Clear flags in Timer flag reg 1
    TIFR1  = 0x21;              //0x21; 
} 
 
// ISR for falling edge
ISR(TIMER1_CAPT_vect) 
{ 
  if((PINB & (1<<PORTB0)) == 0)       // capture falling edge 
  { 
    Period = ICR1; 
    TCNT1 = 0;               //Reset timer 1
    TCCR1A = 0x00;
    PORTB = PORTB^0x20;       // toggle PB5 pin 
  } 
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top