dacctr library  v0.85
Performs D/A conversion (sets voltages) with a cog's counter modules
Data Structures | Macros | Typedefs | Functions
dacctr.h File Reference

This file provides convenience functions for digital to analog conversion using using counter modules (each cog has two).

Currently supports LMM and CMM memory models. More...

#include "simpletools.h"

Go to the source code of this file.

Data Structures

struct  DacControl
struct  DacAddr
struct  DacCogMemory

Macros

#define DUTY_SE   (6 << 26)
#define NEW_COG   2

Typedefs

typedef struct DacControl dac
typedef struct DacAddr daca
typedef struct DacCogMemory dacmem

Functions

dac dac_setup (int pin, int channel, int bits)
 Configure an I/O pin to set a voltage with digital to analog (D/A) conversion.
void dac_set (dac *da, int value)
 Set the D/A converter's output.
void dac_close (dac *da)
 Clear a D/A converter that's running in the same cog and reclaim the I/O pin and counter module for other uses.
int dac_start (dacmem mem, int sampleRate, dac *da0, dac *da1)
 Start D/A converter in new cog that sets voltages on either one or two pins.
int dac_stop (int cogid)
 Stops a dac cog and frees up a cog and all its resources for other uses.

Detailed Description

This file provides convenience functions for digital to analog conversion using using counter modules (each cog has two).

Currently supports LMM and CMM memory models.

Author
Andy Lindsay
Version
0.85

The current cog's counters can be used for D/A conversion, or one or more new cogs can be launched, each with two D/A channels. The output of this type of D/A converter can go straight to an LED circuit for variable brightness, or to a filter and into an op amp to drive other circuits at certain voltages. See Propeller Activity Board schematic for an example. P26 and P27 are connected to a filter, op amp, and also to LEDs.

Example in same cog:

dac myDac = dac_setup(3, 0, 8); // P3, channel 0, 8-bits (256ths of 3.3 V)
dac_set(&myDac, 128); // Voltage to 128/256ths of 3.3 V = 1.65 V
// Call dac_set whenever you want to change the voltage
Core Usage
A call to either dac_start will launch 1 additional core that repeatedly updates DAC outputs.

Example in another cog:

// P4, channel 0, 10-bits (1024ths of 3.3 V).
dac cogDac = dac_setup(4, NEW_COG, 10);
// Optionally, pre-set the voltage (to 256/1024ths of 3.3 V = 0.825 V).
dac_set(&cogDac, 256);
// Set aside memory for the processor.
dacmem mem;
// Launch the process (we are using just one of the two available channels here).
// In another cog, you have to specify the sampling rate. We are using 44100.
int myCog = dac_start(mem, 44100, &cogDac, NULL);
Help Improve this Library
Please submit bug reports, suggestions, and improvements to this code to edito.nosp@m.r@pa.nosp@m.ralla.nosp@m.x.co.nosp@m.m.

Function Documentation

void dac_close ( dac da)

Clear a D/A converter that's running in the same cog and reclaim the I/O pin and counter module for other uses.

Parameters
dacStructure corresponding to the I/O pin that's sending the dac signal you want to close.
void dac_set ( dac da,
int  value 
)

Set the D/A converter's output.

After a call to dac_setup, you can use the dac structure it returns to set the D/A converter's voltage. Again, see examples at the top of this file.

Parameters
daThe pointer to the dac structure that dac_setup function returned.
valueThe output value for the D/A converter. (Like the number of 256ths of 3.3 V if you set up an 8-bit D/A converter with dac_setup.
dac dac_setup ( int  pin,
int  channel,
int  bits 
)

Configure an I/O pin to set a voltage with digital to analog (D/A) conversion.

Sets up a dac structure for you. You can then pass the address of this dac structure to other functions (like dac_set and/or dac_start) do things like set voltages or launch other cogs to perform D/A conversion. The technique this library uses is called duty modulation, and each D/A channel uses one of a cog's two counter modules. If you need to launch more than one D/A channel, you can use the dac structure this function returns in a call to dac_start to launch the D/A process into another cog.

Parameters
pinNumber of the I/O pin to perform D/A conversion.
channelUse 0 for the cog's CTRA or 1 for the cog's CTRB counter module. You can also use NEW_COG if you are going to use dac_start.
bitsThe resolution of your D/A converter in bits. If you set it to 8, you will be able to set voltages in terms of 256ths of 3.3 V, from 0 to 255. If you set it to 10, you will be able to set voltages in terms of the number of 1024ths of 3.3 V. More generally, the fraction of 3.3 V is 2^bits.
Returns
dac structure that you can pass by address to other functions for updating the output or launching into another cog. See examples at top of this file.
int dac_start ( dacmem  mem,
int  sampleRate,
dac da0,
dac da1 
)

Start D/A converter in new cog that sets voltages on either one or two pins.

Launches up to two D/A converters in a new cog. Example included near the top of this file.

Parameters
memA dacmem structure that you initialize with dacmem myStructureName;
sampleRateThe sample rate you want the dac to update values at. Top speed is 46 kHz, 44.1 kHz is recommended.
da0Address of the dac structure the cog's channel 0 will run. If you do not want to use this channel, pass NULL.
da1Address of the dac structure the cog's channel 1 will run. If you do not want to use this channel, pass NULL.
Returns
dac cog ID, the number of the cog the process was launched into. You can save this in case you want to use dac_stop to shut down the cog later.
int dac_stop ( int  cogid)

Stops a dac cog and frees up a cog and all its resources for other uses.

Parameters
cogidThe dac cog's ID that dac_start returned.