Applications moved from app/arm direcotry to app.
[LPC2xxx_and_RobotSpejbl.git] / app / test_ARMBoard_pwm / pwm.c
blobe6460c6eb5086da5012750056b2764f8eb884b38
1 /*
2 * C Implementation: pwm
4 * Description: configuration of PWM unit
7 * Author: LAGARRIGUE <glagarri@etud.insa-toulouse.fr>, (C) 2005
9 * Copyright: See COPYING file that comes with this distribution
14 #include <LPC210x.h>
15 #include "config.h"
16 #include "pwm.h"
18 #define MINFREQ (PCLK/(0xFFFFFFFF))
20 /**
21 * Initializes PWM frequency with match0 register
22 * @param freq frequency in Hz
24 void Init_PWM(int freq)
26 if ((freq <= PCLK) | (freq >= MINFREQ))
28 PWMPR = 0x00000000; //no prescaler
29 PWMMCR = 0x00000002; // on match with MATCH0 Register, reset the counter
30 PWMMR0 = (int)(PCLK/(freq));
36 /**
37 * Initilizes and sets the duty cycle of the desired pwm channel
38 * @param channel = channel number (1,2,3,4,5,6)
39 * @param duty_cycle = float between 0 and 1
41 void Set_PWM (int channel, float duty_cycle)
43 int Pwmmatch;
44 Pwmmatch = (int) (duty_cycle * PWMMR0);
48 //PINSEL0 |= ...; /* Enable P0.7, 8 and P0.9 as alternate PWM outputs functions*/
49 //PINSEL0 &= ...;
51 //PWMPCR = ...; /* single edge control only*/
53 //PWMMRX = 0x500; /* set falling edge of PWM channel X */
55 //PWMLER = 0x..; /* enable shadow latch for match 1 - 6 */
58 switch (channel)
62 case 1 :
63 PWMMR1 =Pwmmatch;
64 PWMPCR |= 0x0200;
65 PWMLER |= 0x02;
66 PINSEL0 |= 0x00000002;
67 PINSEL0 &= 0xFFFFFFFE;
68 break;
70 case 2 :
71 PWMMR2 =Pwmmatch;
72 PWMPCR |= 0x0400;
73 PWMLER |= 0x04;
74 PINSEL0 |= 0x00008000;
75 PINSEL0 &= 0xFFFFBFFF;
76 break;
78 case 3 :
79 PWMMR3 =Pwmmatch;
80 PWMPCR |= 0x0800;
81 PWMLER |= 0x08;
82 PINSEL0 |= 0x00000008;
83 PINSEL0 &= 0xFFFFFFFB;
84 break;
85 case 4 :
86 PWMMR4 =Pwmmatch;
87 PWMPCR |= 0x1000;
88 PWMLER |= 0x10;
89 PINSEL0 |= 0x00020000;
90 PINSEL0 &= 0xFFFEFFFF;
91 break;
93 case 5 :
94 PWMMR5 =Pwmmatch;
95 PWMPCR |= 0x2000;
96 PWMLER |= 0x20;
97 PINSEL1 |= 0x00000400;
98 PINSEL1 &= 0xFFFFF7FF;
99 break;
101 case 6 :
102 PWMMR6 =Pwmmatch;
103 PWMPCR |= 0x4000;
104 PWMLER |= 0x40;
105 PINSEL0 |= 0x00080000;
106 PINSEL0 &= 0xFFFBFFFF;
107 break;
116 * Resets the counter and runs the PWM unit
117 * @param
119 void Run_PWM (void)
121 PWMTCR = 0x2; /* Reset counter and prescaler */
122 PWMTCR = 0x9; /* enable counter and PWM, release counter from reset */
127 * Stops PWM unit and resets the timer
128 * @param
130 void Stop_PWM (void)
132 int i;
133 for (i=1 ; i < 7 ; i++)
135 Set_PWM (i, 0);
137 PWMTCR = 0x2;