PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
dac.c
Go to the documentation of this file.
1 /*
2  * @file dac.c
3  *
4  * @author Andy Lindsay
5  *
6  * @version 0.86
7  *
8  * @copyright Copyright (C) Parallax, Inc. 2012. See end of file for
9  * terms of use (MIT License).
10  *
11  * @brief dac 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 void dac_ctr_cog(void *par);
20 
21 #ifndef DUTY_SE
22 #define DUTY_SE (0b110 << 26)
23 #endif
24 
25 static unsigned int stack[44 + 16];
26 
27 static int dacCtrBits;
28 static unsigned int cog;
29 static volatile int ctra, ctrb, frqa, frqb;
30 
31 void dac_ctr_res(int bits)
32 {
33  dacCtrBits = bits;
34 }
35 
36 void dac_ctr(int pin, int channel, int dacVal)
37 {
38  if(dacCtrBits == 0) dacCtrBits = 8;
39  int dacBitX = 32 - dacCtrBits;
40 
41  if(!cog) cog = cogstart(dac_ctr_cog, NULL,
42  stack, sizeof(stack)) + 1;
43  if(!channel)
44  {
45  ctra = (DUTY_SE + pin);
46  frqa = (dacVal << dacBitX);
47  }
48  else
49  {
50  ctrb = (DUTY_SE + pin);
51  frqb = (dacVal << dacBitX);
52  }
53 }
54 
55 void dac_ctr_cog(void *par)
56 {
57  int pin;
58  while(1)
59  {
60  if(ctra != CTRA)
61  {
62  if(CTRA != 0)
63  {
64  pin = CTRA & 0b111111;
65  DIRA &= ~(1 << pin);
66  }
67  CTRA = ctra;
68 
69  if(ctra != 0)
70  {
71  pin = CTRA & 0b111111;
72  DIRA |= (1 << pin);
73  }
74  }
75 
76  if(ctrb != CTRB)
77  {
78  if(CTRB != 0)
79  {
80  pin = CTRB & 0b111111;
81  DIRA &= ~(1 << pin);
82  }
83  CTRB = ctrb;
84 
85  if(ctrb != 0)
86  {
87  pin = CTRB & 0b111111;
88  DIRA |= (1 << pin);
89  }
90  }
91  FRQA = frqa;
92  FRQB = frqb;
93  }
94 }
95 
96 void dac_ctr_stop(void)
97 {
98  if(cog) cogstop(cog - 1);
99  cog = 0;
100 }
101 
102 
DUTY_SE
#define DUTY_SE
Building block for configuring a cog's counter module to DUTY_SE mode. Used by dac functions....
Definition: simpletools.h:460
dac_ctr
void dac_ctr(int pin, int channel, int dacVal)
Set D/A voltage.
Definition: dac.c:36
dac_ctr_res
void dac_ctr_res(int bits)
Set D/A voltage resolution.
Definition: dac.c:31
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,...
FRQB
#define FRQB
Counter B frequency register.
Definition: propeller1.h:171
CTRB
#define CTRB
Counter B control register.
Definition: propeller1.h:167
cogstop
#define cogstop(a)
Stop a COG.
Definition: propeller.h:100
DIRA
#define DIRA
Use to set pins to input (0) or output (1).
Definition: propeller1.h:161
dac_ctr_stop
void dac_ctr_stop(void)
Stop the cog that's transmitting the DAC signal(s).
Definition: dac.c:96
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