PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
pwm.c
1 /*
2  * @file pwm.c
3  *
4  * @author Andy Lindsay
5  *
6  * @version 0.85
7  *
8  * @copyright Copyright (C) Parallax, Inc. 2012. See end of file for
9  * terms of use (MIT License).
10  *
11  * @brief pwm function source, see simpletools.h for documentation.
12  *
13  * @detail Please submit bug reports, suggestions, and improvements to
14  * this code to editor@parallax.com.
15  */
16 
17 #include "simpletools.h"
18 
19 #ifndef NCO_PWM_1
20 #define NCO_PWM_1 0b00100 << 26
21 #endif
22 
23 void pw(void *par);
24 static unsigned int pwstack[(160 + (50 * 4)) / 4];
25 
26 static volatile unsigned int tCycle, ticksA, ticksB, ctra, ctrb;
27 //static volatile int us;
28 
29 static int pwcog = 0;
30 
31 //static int ctraPin = -1;
32 //static int ctrbPin = -1;
33 
34 int pwm_start(unsigned int cycleMicroseconds)
35 {
36  //us = CLKFREQ/1000000;
37  tCycle = cycleMicroseconds * st_usTicks;
38  pwcog = cogstart(pw, NULL, pwstack, sizeof(pwstack)) + 1;
39  return pwcog;
40 }
41 
42 void pwm_set(int pin, int channel, int tHigh)
43 {
44  if(!channel)
45  {
46  ctra = NCO_PWM_1;
47  ctra |= pin;
48  ticksA = tHigh * st_usTicks;
49  }
50  else
51  {
52  ctrb = NCO_PWM_1;
53  ctrb |= pin;
54  ticksB = tHigh * st_usTicks;
55  }
56 }
57 
58 void pwm_stop(void)
59 {
60  if(pwcog) cogstop(pwcog - 1);
61  pwcog = 0;
62 }
63 
64 void pw(void *par)
65 {
66  FRQA = 1;
67  FRQB = 1;
68  int pin;
69  unsigned int dt = tCycle;
70  unsigned int t = CNT;
71  while(1)
72  {
73  waitcnt(t+=dt);
74  if(ctra != CTRA)
75  {
76  if(ctra != 0)
77  {
78  pin = CTRA & 0b111111;
79  DIRA &= ~(1 << pin);
80  }
81  CTRA = ctra;
82  pin = CTRA & 0b111111;
83  DIRA |= (1 << pin);
84  }
85  if(ctrb != CTRB)
86  {
87  if(ctrb != 0)
88  {
89  pin = CTRB & 0b111111;
90  DIRB &= ~(1 << pin);
91  }
92  CTRB = ctrb;
93  pin = CTRB & 0b111111;
94  DIRA |= (1 << pin);
95  }
96  PHSA = -ticksA;
97  PHSB = -ticksB;
98  }
99 }
100 
PHSB
#define PHSB
Counter B phase accumulation register.
Definition: propeller1.h:175
pwm_stop
void pwm_stop(void)
Shut down PWM process and reclaim cog and I/O pins for other uses.
Definition: pwm.c:58
PHSA
#define PHSA
Counter A phase accumulation register.
Definition: propeller1.h:173
pwm_start
int pwm_start(unsigned int cycleMicroseconds)
Start pulse width modulation (PWM) process in another cog.
Definition: pwm.c:34
FRQA
#define FRQA
Counter A frequency register.
Definition: propeller1.h:169
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
pwm_set
void pwm_set(int pin, int channel, int tHigh)
Set a PWM signal's high time.
Definition: pwm.c:42
FRQB
#define FRQB
Counter B frequency register.
Definition: propeller1.h:171
DIRB
#define DIRB
Unused in P8X32A.
Definition: propeller1.h:163
CTRB
#define CTRB
Counter B control register.
Definition: propeller1.h:167
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
st_usTicks
int st_usTicks
Propeller system clock ticks in 1 microsecond. Changing this value is not recommended because it can ...
Definition: timeTicks.c:23
cogstop
#define cogstop(a)
Stop a COG.
Definition: propeller.h:100
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
NCO_PWM_1
#define NCO_PWM_1
Building block for configuring a cog's counter module to PWM mode. Used by pwm functions....
Definition: simpletools.h:436
cogstart
int cogstart(void(*func)(void *), void *par, void *stack, size_t stacksize)
Start a new propeller LMM function/thread in another COG.
CTRA
#define CTRA
Counter A control register.
Definition: propeller1.h:165