7.10. PROGRAMMING THE TIMER SYSTEM IN C 201
OCR1BH = 0x00;
OCR1BL = (unsigned char)(PWM_duty_cycle);//Set PWM duty cycle CH B to 0%
//Ramp up to Open Speed in 1.6s
OCR1BL = (unsigned char)(PWM_duty_cycle);//Set PWM duty cycle CH B
while (PWM_duty_cycle < Open_Speed_int)
{
if(PWM_duty_cycle < Open_Speed_int) //Increment duty cycle
PWM_duty_cycle=PWM_duty_cycle + PWM_open_incr;
//Set PWM duty cycle CH B
OCR1BL = (unsigned char)(PWM_duty_cycle);
}
//Gate continues to open at specified upper speed (PA3)
:
:
:
//*************************************************************************
7.10.3 INPUT CAPTURE MODE IN C
This example was developed by Julie Sandberg, BSEE and Kari Fuller, BSEE at the University of
Wyoming as part of their senior design project. In this example, the input capture channel is being
used to monitor the heart rate (typically 50-120 beats per minute) of a patient. The microcontroller
is set to operate at an internal clock frequency of 1 MHz. Timer/Counter channel 1 is used in this
example.
//*************************************************************************
//initialize_ICP_interrupt: Initialize Timer/Counter 1 for input capture
//*************************************************************************
void initialize_ICP_interrupt(void)
{
TIMSK=0x20; //Allows input capture interrupts
SFIOR=0x04; //Internal pull-ups disabled
TCCR1A=0x00; //No output comp or waveform
//generation mode
TCCR1B=0x45; //Capture on rising edge,
//clock prescalar=1024
202 7. TIMING SUBSYSTEM
TCNT1H=0x00; //Initially clear timer/counter 1
TCNT1L=0x00;
asm("SEI"); //Enable global interrupts
}
//*************************************************************************
void Input_Capture_ISR(void)
{
if(first_edge==0)
{
ICR1L=0x00; //Clear ICR1 and TCNT1 on first edge
ICR1H=0x00;
TCNT1L=0x00;
TCNT1H=0x00;
first_edge=1;
}
else
{
ICR1L=TCNT1L; //Capture time from TCNT1
ICR1H=TCNT1H;
TCNT1L=0x00;
TCNT1H=0x00;
first_edge=0;
}
heart_rate(); //Calculate the heart rate
TIFR=0x20; //Clear the input capture flag
asm("RETI"); //Resets the I flag to allow
//global interrupts
}
//*************************************************************************
void heart_rate(void)
{
if(first_edge==0)
{

Get Arduino Microcontroller, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.