Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
roberthroseroo authored Jun 19, 2018
1 parent 89204ba commit d30289d
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
52 changes: 52 additions & 0 deletions timer/IRQ_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*********************************************************************************************************
**--------------File Info---------------------------------------------------------------------------------
** File name: IRQ_timer.c
** Last modified Date: 2014-09-25
** Last Version: V1.00
** Descriptions: functions to manage T0 and T1 interrupts
** Correlated files: timer.h
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
#include "lpc17xx.h"
#include "timer.h"
#include "../Potenciometer/potenciometer.h"


/******************************************************************************
** Function name: Timer0_IRQHandler
**
** Descriptions: Timer/Counter 0 interrupt handler
**
** parameters: None
** Returned value: None
**
******************************************************************************/

void TIMER0_IRQHandler (void)
{
LPC_TIM0->IR = 1; /* clear interrupt flag */
/*once the timer is done the A/D converter starts again*/
LPC_ADC->ADCR |= (0<<24); // starts the A/D converter
LPC_ADC->ADCR |= (1<<24); // starts the A/D converter
return;
}


/******************************************************************************
** Function name: Timer1_IRQHandler
**
** Descriptions: Timer/Counter 1 interrupt handler
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void TIMER1_IRQHandler (void)
{
LPC_TIM1->IR = 1; /* clear interrupt flag */
return;
}

/******************************************************************************
** End Of File
******************************************************************************/
58 changes: 58 additions & 0 deletions timer/funct_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*********************************************************************************************************
**--------------File Info---------------------------------------------------------------------------------
** File name: funct_timer.h
** Last modified Date: 2014-09-25
** Last Version: V1.00
** Descriptions: High level functions, possibly exploiting lib_*.c functions
** Correlated files: timer.h
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
#include "lpc17xx.h"
#include "timer.h"

/*****************************************************************************
** Function name: delayMs
**
** Descriptions: Start the timer delay in milo seconds
** until elapsed
**
** parameters: timer number, Delay value in milo second
**
** Returned value: None
**
** Comments [PB]: Not a very good functions because it is
** keeping the processor control while waiting
** count completion
*****************************************************************************/
void delayMs(uint8_t timer_num, uint32_t delayInMs)
{
if ( timer_num == 0 )
{
LPC_TIM0->TCR = 0x02; /* reset timer */
LPC_TIM0->PR = 0x00; /* set prescaler to zero */
LPC_TIM0->MR0 = delayInMs * (9000000 / 1000-1);
LPC_TIM0->IR = 0xff; /* reset all interrrupts */
LPC_TIM0->MCR = 0x04; /* stop timer on match */
LPC_TIM0->TCR = 0x01; /* start timer */

/* wait until delay time has elapsed */
while (LPC_TIM0->TCR & 0x01);
}
else if ( timer_num == 1 )
{
LPC_TIM1->TCR = 0x02; /* reset timer */
LPC_TIM1->PR = 0x00; /* set prescaler to zero */
LPC_TIM1->MR0 = delayInMs * (9000000 / 1000-1);
LPC_TIM1->IR = 0xff; /* reset all interrrupts */
LPC_TIM1->MCR = 0x04; /* stop timer on match */
LPC_TIM1->TCR = 0x01; /* start timer */

/* wait until delay time has elapsed */
while (LPC_TIM1->TCR & 0x01);
}
return;
}

/******************************************************************************
** End Of File
******************************************************************************/
161 changes: 161 additions & 0 deletions timer/lib_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*********************************************************************************************************
**--------------File Info---------------------------------------------------------------------------------
** File name: lib_timer.h
** Last modified Date: 2014-09-25
** Last Version: V1.00
** Descriptions: atomic functions to be used by higher sw levels
** Correlated files: lib_timer.c, funct_timer.c, IRQ_timer.c
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
#include "lpc17xx.h"
#include "timer.h"

/******************************************************************************
** Function name: enable_timer
**
** Descriptions: Enable timer
**
** parameters: timer number: 0 or 1
** Returned value: None
**
******************************************************************************/
void enable_timer( uint8_t timer_num )
{
if ( timer_num == 0 )
{
LPC_TIM0->TCR = 1;
}
else
{
LPC_TIM1->TCR = 1;
}
return;
}

/******************************************************************************
** Function name: disable_timer
**
** Descriptions: Disable timer
**
** parameters: timer number: 0 or 1
** Returned value: None
**
******************************************************************************/
void disable_timer( uint8_t timer_num )
{
if ( timer_num == 0 )
{
LPC_TIM0->TCR = 0;
}
else
{
LPC_TIM1->TCR = 0;
}
return;
}

/******************************************************************************
** Function name: reset_timer
**
** Descriptions: Reset timer
**
** parameters: timer number: 0 or 1
** Returned value: None
**
******************************************************************************/
void reset_timer( uint8_t timer_num )
{
uint32_t regVal;

if ( timer_num == 0 )
{
regVal = LPC_TIM0->TCR;
regVal |= 0x02;
LPC_TIM0->TCR = regVal;
}
else
{
regVal = LPC_TIM1->TCR;
regVal |= 0x02;
LPC_TIM1->TCR = regVal;
}
return;
}

uint32_t init_timer ( uint8_t timer_num, uint32_t TimerInterval )
{
if ( timer_num == 0 )
{
LPC_TIM0->MR0 = TimerInterval;

//*** <<< Use Configuration Wizard in Context Menu >>> ***
// <h> timer0 MCR
// <e.0> MR0I
// <i> 1 Interrupt on MR0: an interrupt is generated when MR0 matches the value in the TC. 0
// <i> 0 This interrupt is disabled
// </e>
// <e.1> MR0R
// <i> 1 Reset on MR0: the TC will be reset if MR0 matches it.
// <i> 0 Feature disabled.
// </e>
// <e.2> MR0S
// <i> 1 Stop on MR0: the TC and PC will be stopped and TCR[0] will be set to 0 if MR0 matches the TC
// <i> 0 Feature disabled.
// </e>
// <e.3> MR1I
// <i> 1 Interrupt on MR1: an interrupt is generated when MR0 matches the value in the TC. 0
// <i> 0 This interrupt is disabled
// </e>
// <e.4> MR1R
// <i> 1 Reset on MR1: the TC will be reset if MR0 matches it.
// <i> 0 Feature disabled.
// </e>
// <e.5> MR1S
// <i> 1 Stop on MR1: the TC and PC will be stopped and TCR[1] will be set to 0 if MR1 matches the TC
// <i> 0 Feature disabled.
// </e>
// <e.6> MR2I
// <i> 1 Interrupt on MR2: an interrupt is generated when MR2 matches the value in the TC.
// <i> 0 This interrupt is disabled
// </e>
// <e.7> MR2R
// <i> 1 Reset on MR2: the TC will be reset if MR2 matches it.
// <i> 0 Feature disabled.
// </e>
// <e.8> MR2S
// <i> 1 Stop on MR2: the TC and PC will be stopped and TCR[2] will be set to 0 if MR2 matches the TC
// <i> 0 Feature disabled.
// </e>
// <e.9> MR3I
// <i> 1 Interrupt on MR3: an interrupt is generated when MR3 matches the value in the TC.
// <i> 0 This interrupt is disabled
// </e>
// <e.10> MR3R
// <i> 1 Reset on MR3: the TC will be reset if MR3 matches it.
// <i> 0 Feature disabled.
// </e>
// <e.11> MR3S
// <i> 1 Stop on MR3: the TC and PC will be stopped and TCR[3] will be set to 0 if MR3 matches the TC
// <i> 0 Feature disabled.
// </e>
LPC_TIM0->MCR = 3;
// </h>
//*** <<< end of configuration section >>> ***

NVIC_EnableIRQ(TIMER0_IRQn);
return (1);
}
else if ( timer_num == 1 )
{
LPC_TIM1->MR0 = TimerInterval;
LPC_TIM1->MCR = 3; /* Interrupt and Reset on MR1 */

NVIC_EnableIRQ(TIMER1_IRQn);
return (1);
}
return (0);
}

/******************************************************************************
** End Of File
******************************************************************************/
30 changes: 30 additions & 0 deletions timer/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*********************************************************************************************************
**--------------File Info---------------------------------------------------------------------------------
** File name: timer.h
** Last modified Date: 2014-09-25
** Last Version: V1.00
** Descriptions: Prototypes of functions included in the lib_timer, funct_timer, IRQ_timer .c files
** Correlated files: lib_timer.c, funct_timer.c, IRQ_timer.c
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
#ifndef __TIMER_H
#define __TIMER_H

/* da capire quanto vale in millisecondi ms */
#define TIME_INTERVAL (9000000/100 - 1)

/* funct_timer.c */
extern void delayMs(uint8_t timer_num, uint32_t delayInMs);
/* init_timer.c */
extern uint32_t init_timer( uint8_t timer_num, uint32_t timerInterval );
extern void enable_timer( uint8_t timer_num );
extern void disable_timer( uint8_t timer_num );
extern void reset_timer( uint8_t timer_num );
/* IRQ_timer.c */
extern void TIMER0_IRQHandler (void);
extern void TIMER1_IRQHandler (void);

#endif /* end __TIMER_H */
/*****************************************************************************
** End Of File
******************************************************************************/

0 comments on commit d30289d

Please sign in to comment.