Applications moved from app/arm direcotry to app.
[LPC2xxx_and_RobotSpejbl.git] / app / pokusy / pokus.c
blob0ffc424bb6173aa500776f6065eb2d3a0bd2c649
1 #include <lpc21xx.h>
2 #include <types.h>
5 void delay() {
6 unsigned u;
8 for (u = 0; u < 1000000; u++);
11 /*** PWM ***/
13 int PWM_PINSEL[] = {
14 /*nothing*/ 1, /*PWM1*/ 1, 15, 3, 17, /*PWM5*/ 11, /*PWM6*/ 19
17 uint32_t *PWM_MR[] = {
18 (uint32_t*)&(PWMMR0),
19 (uint32_t*)&(PWMMR1),
20 (uint32_t*)&(PWMMR2),
21 (uint32_t*)&(PWMMR3),
22 (uint32_t*)&(PWMMR4),
23 (uint32_t*)&(PWMMR5),
24 (uint32_t*)&(PWMMR6)
27 void pwm_channel(int n, int double_edge) {
28 uint32_t bit;
30 PWMPCR |= (0x100 | (double_edge && n)) << n;
31 if (n == 5) {
32 PINSEL1 |= 0x00000400;
33 PINSEL1 &= 0xfffff7ff;
35 else {
36 bit = 1 << PWM_PINSEL[n];
37 PINSEL0 |= bit;
38 bit = ~(bit >> 1);
39 PINSEL0 &= bit;
43 void pwm_set(int n, uint32_t when) {
44 *PWM_MR[n] = when;
45 PWMLER |= 1 << n;
48 void pwm_set_double(int n, uint32_t from, uint32_t to) {
49 *PWM_MR[n-1] = from;
50 *PWM_MR[n] = to;
51 PWMLER |= 0x3 << (n-1);
54 void pwm_init(uint32_t prescale, uint32_t period) {
55 PWMPR = prescale;
56 PWMMR0 = period;
57 PWMLER |= 0x1;
58 PWMMCR |= 0x00000002;
59 PWMTCR &= ~0x2;
60 PWMTCR |= 0x9;
63 /***********/
65 void motor_drive(float u) {
66 uint32_t d = (float)PWMMR0*(0.5*(1.0+u));
68 pwm_set_double(2, 0, d);
69 pwm_set_double(4, d, 0);
73 int main() {
74 pwm_channel(2, 1);
75 pwm_channel(4, 1);
76 pwm_init(0, 50);
78 motor_drive(0);
80 for (;;);