Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: UIT2_MovingAverage UIT2_AllpassReverb UIT2_CombReverb UIT2_FIR_LPF_Symmetry ... more
Revision 20:c49f3b565a87, committed 2015-02-02
- Comitter:
- MikamiUitOpen
- Date:
- Mon Feb 02 08:10:55 2015 +0000
- Parent:
- 19:900efc6796d4
- Child:
- 21:3731753ebf24
- Commit message:
- 21
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DAC_MCP4921.cpp Mon Feb 02 08:10:55 2015 +0000
@@ -0,0 +1,59 @@
+//------------------------------------------------------
+// Class for single DAC in MCP4921
+//
+// 2015/02/02, Copyright (c) 2015 MIKAMI, Naoki
+//------------------------------------------------------
+
+#include "DAC_MCP4921.hpp"
+
+namespace Mikami
+{
+ DAC_MCP4921::DAC_MCP4921(PinName mosi, PinName sclk,
+ PinName cs, PinName ldac)
+ : spi_(mosi, NC, sclk),
+ ld_(ldac, 0), mySpi_((SPI_TypeDef*)NULL)
+ {
+ if ( (mosi == PA_7) || (mosi == PB_5) ) mySpi_ = SPI1;
+ if ( (mosi == PB_15) || (mosi == PC_3) ) mySpi_ = SPI2;
+ if ( mosi == PC_12 ) mySpi_ = SPI3;
+
+ // Set SPI format
+ spi_.format(16, 0);
+ // Clock source of F401 for SPI1 : 84 MHz,
+ // SPI2, SPI3 : 42 MHz
+ mySpi_->CR1 = (mySpi_->CR1 & ~SPI_CR1_BR);
+ if (mySpi_ == SPI1) mySpi_->CR1 += SPI_CR1_BR_0;
+#ifdef __STM32F411xE_H
+ mySpi_->CR1 += SPI_CR1_BR_0;
+#endif // __STM32F411xE_H
+
+ // timer prescaler is set same value of boud rate for SPI
+ uint16_t psc = (2 << ((mySpi_->CR1 >> 3) & 0x07)) - 1;
+ if (mySpi_ != SPI1) psc = (psc + 1)*2 - 1;
+ ss_ = new Tim4_ss(psc, 19, cs);
+ }
+
+ void DAC_MCP4921::ScfClockTim3(uint32_t clock, PinName pin)
+ {
+ if ( (pin != PA_6) && (pin != PB_4) && (pin != PB_5) &&
+ (pin != PC_6) && (pin != PC_7) && (pin != PC_8) && (pin != PC_9) )
+ {
+ fprintf(stderr, "\r\nIllegal pin name in DAC_MCP4921::ScfClockTim3()\r\n");
+ while (true) {}
+ }
+
+ PwmOut clockSCF(pin);
+
+ TIM3->ARR = SystemCoreClock/clock - 1;
+ TIM3->PSC = 0;
+ // Set capture/compare register 2
+ if ( (pin == PA_6) || (pin == PB_4) || (pin == PC_6) )
+ TIM3->CCR1 = (TIM3->ARR + 1)/2;
+ if ( (pin == PB_5) || (pin == PC_7) )
+ TIM3->CCR2 = (TIM3->ARR + 1)/2;
+ if (pin == PC_8)
+ TIM3->CCR3 = (TIM3->ARR + 1)/2;
+ if (pin == PC_9)
+ TIM3->CCR4 = (TIM3->ARR + 1)/2;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DAC_MCP4921.hpp Mon Feb 02 08:10:55 2015 +0000
@@ -0,0 +1,98 @@
+//------------------------------------------------------
+// Class for single DAC in MCP4921 -- Header
+// Fast version
+//
+// Default pin assign
+// D11 SPI Master Out Slave In
+// D13 SPI Serial Clock
+// D10 SPI Slave Select ----------------------- TIM4
+// D12 to MCP4921 LDAC pin
+// D9 clock for Switched-capacitor filter ---- TIM3
+//
+// Argument cs in constructor must be output of TIM4,
+// i.e. D10(PB_6), PB_7, D15(PB_8), or D14(PB_9)
+//
+// Argument pin in function ScfClockTim3() can be
+// PA_6(D12), PB_4(D5), PC_6, PB_5(D4), PC_7(D9),
+// PC_8, or PC_9
+//
+// 2015/02/02, Copyright (c) 2015 MIKAMI, Naoki
+//------------------------------------------------------
+
+#ifndef DAC_MCP4921_HPP
+#define DAC_MCP4921_HPP
+
+#include "mbed.h"
+#include "tim4_slaveSelect.hpp"
+
+namespace Mikami
+{
+ class DAC_MCP4921
+ {
+ public:
+ // Constructor
+ DAC_MCP4921(
+ PinName mosi = SPI_MOSI, // D11
+ PinName sclk = SPI_SCK, // D13
+ PinName cs = SPI_CS, // D10
+ PinName ldac = SPI_MISO); // D12
+
+ // -1.0f <= value <= 1.0f
+ void Write(float value)
+ {
+ if (value < -1.0f) value = -1.0f;
+ if (value > 1.0f) value = 1.0f;
+
+ WriteDac((uint16_t)((value + 1.0f)*2047));
+ }
+
+ // 0 <= value <= 4095
+ void Write(uint16_t value)
+ { WriteDac((value > 4095) ? 4095 : value); }
+
+ // generate LDAC negative-going pulse
+ void Ldac()
+ {
+ ld_.write(0);
+ ld_.write(0); // ensure width of "L" pulse
+ ld_.write(1);
+ }
+
+ // Check busy
+ bool IsBusy()
+ { return (mySpi_->SR & SPI_SR_BSY) == SPI_SR_BSY; }
+
+ // Little wait
+ void Wait()
+ { __NOP(); __NOP(); __NOP(); }
+
+ // Set clock for switched-capacitor filter
+ void ScfClockTim3(uint32_t clock, PinName pin = D9);
+
+ protected:
+ void SlaveSelect() { ss_->SlaveSelect(); }
+ void WriteSpi(uint16_t value) { mySpi_->DR = value; }
+
+ private:
+ SPI spi_; // SPI object of mbed
+ Tim4_ss* ss_;
+ DigitalOut ld_; // for LDAC
+
+ // Pointer of I2C
+ SPI_TypeDef* mySpi_;
+
+ // for inhibition of copy constructor
+ DAC_MCP4921(const DAC_MCP4921&);
+ // for inhibition of substitute operator
+ DAC_MCP4921& operator=(const DAC_MCP4921&);
+
+ // for internal use
+ virtual void WriteDac(uint16_t value)
+ {
+ while (IsBusy()) {}
+ SlaveSelect();
+ WriteSpi(value | 0x3000);
+ }
+ };
+}
+#endif // DAC_MCP4921_HPP
--- a/DAC_MCP4922.cpp Fri Jan 30 07:22:28 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-//------------------------------------------------------
-// Class for single DAC in MCP4922
-//
-// 2015/01/30, Copyright (c) 2015 MIKAMI, Naoki
-//------------------------------------------------------
-
-#include "DAC_MCP4922.hpp"
-
-namespace Mikami
-{
- DAC_MCP4922::DAC_MCP4922(DAC dac, PinName mosi, PinName sclk,
- PinName cs, PinName ldac)
- : wcr_(dac | 0x3000), spi_(mosi, NC, sclk),
- ld_(ldac, 0), mySpi_((SPI_TypeDef*)NULL)
- {
- if ( (mosi == PA_7) || (mosi == PB_5) ) mySpi_ = SPI1;
- if ( (mosi == PB_15) || (mosi == PC_3) ) mySpi_ = SPI2;
- if ( mosi == PC_12 ) mySpi_ = SPI3;
-
- // Set SPI format
- spi_.format(16, 0);
- // Clock source of F401 for SPI1 : 84 MHz,
- // SPI2, SPI3 : 42 MHz
- mySpi_->CR1 = (mySpi_->CR1 & ~SPI_CR1_BR);
- if (mySpi_ == SPI1) mySpi_->CR1 += SPI_CR1_BR_0;
-#ifdef __STM32F411xE_H
- mySpi_->CR1 += SPI_CR1_BR_0;
-#endif // __STM32F411xE_H
-
- // timer prescaler is set same value of boud rate for SPI
- uint16_t psc = (2 << ((mySpi_->CR1 >> 3) & 0x07)) - 1;
- if (mySpi_ != SPI1) psc = (psc + 1)*2 - 1;
- ss_ = new Tim4_ss(psc, 19, cs);
-
- // Set DAC to 0
- WriteDac(0);
- Wait();
- while (IsBusy()) {}
- }
-
- void DAC_MCP4922::ScfClockTim3(uint32_t clock, PinName pin)
- {
- if ( (pin != PA_6) && (pin != PB_4) && (pin != PB_5) &&
- (pin != PC_6) && (pin != PC_7) && (pin != PC_8) && (pin != PC_9) )
- {
- fprintf(stderr, "\r\nIllegal pin name in DAC_MCP4922::ScfClockTim3()\r\n");
- while (true) {}
- }
-
- PwmOut clockSCF(pin);
-
- TIM3->ARR = SystemCoreClock/clock - 1;
- TIM3->PSC = 0;
- // Set capture/compare register 2
- if ( (pin == PA_6) || (pin == PB_4) || (pin == PC_6) )
- TIM3->CCR1 = (TIM3->ARR + 1)/2;
- if ( (pin == PB_5) || (pin == PC_7) )
- TIM3->CCR2 = (TIM3->ARR + 1)/2;
- if (pin == PC_8)
- TIM3->CCR3 = (TIM3->ARR + 1)/2;
- if (pin == PC_9)
- TIM3->CCR4 = (TIM3->ARR + 1)/2;
- }
-}
-
-
--- a/DAC_MCP4922.hpp Fri Jan 30 07:22:28 2015 +0000
+++ b/DAC_MCP4922.hpp Mon Feb 02 08:10:55 2015 +0000
@@ -2,32 +2,17 @@
// Class for single DAC in MCP4922 -- Header
// Fast version
//
-// Default pin assign
-// D11 SPI Master Out Slave In
-// D13 SPI Serial Clock
-// D10 SPI Slave Select ----------------------- TIM4
-// D12 to MCP4922 LDAC pin
-// D9 clock for Switched-capacitor filter ---- TIM3
-//
-// Argument cs in constructor must be output of TIM4,
-// i.e. D10(PB_6), PB_7, D15(PB_8), or D14(PB_9)
-//
-// Argument pin in function ScfClockTim3() can be
-// PA_6(D12), PB_4(D5), PC_6, PB_5(D4), PC_7(D9),
-// PC_8, or PC_9
-//
-// 2015/01/30, Copyright (c) 2014 MIKAMI, Naoki
+// 2015/02/02, Copyright (c) 2015 MIKAMI, Naoki
//------------------------------------------------------
#ifndef DAC_MCP4922_HPP
#define DAC_MCP4922_HPP
-#include "mbed.h"
-#include "tim4_slaveSelect.hpp"
+#include "DAC_MCP4921.hpp"
namespace Mikami
{
- class DAC_MCP4922
+ class DAC_MCP4922 : public DAC_MCP4921
{
public:
enum DAC { DAC_A = 0, DAC_B = 0x8000 };
@@ -38,48 +23,12 @@
PinName mosi = SPI_MOSI, // D11
PinName sclk = SPI_SCK, // D13
PinName cs = SPI_CS, // D10
- PinName ldac = SPI_MISO); // D12
-
- // -1.0f <= value <= 1.0f
- void Write(float value)
- {
- if (value < -1.0f) value = -1.0f;
- if (value > 1.0f) value = 1.0f;
-
- WriteDac((uint16_t)((value + 1.0f)*2047));
- }
-
- // 0 <= value <= 4095
- void Write(uint16_t value)
- { WriteDac((value > 4095) ? 4095 : value); }
-
- // generate LDAC negative-going pulse
- void Ldac()
- {
- ld_.write(0);
- ld_.write(0); // ensure width of "L" pulse
- ld_.write(1);
- }
-
- // Check busy
- bool IsBusy()
- { return (mySpi_->SR & SPI_SR_BSY) == SPI_SR_BSY; }
-
- // Little wait
- void Wait()
- { __NOP(); __NOP(); __NOP(); }
-
- // Set clock for switched-capacitor filter
- void ScfClockTim3(uint32_t clock, PinName pin = D9);
+ PinName ldac = SPI_MISO) // D12
+ : DAC_MCP4921(mosi, sclk, cs, ldac),
+ wcr_(dac | 0x3000) {}
private:
uint16_t wcr_; // write command register
- SPI spi_; // SPI object of mbed
- Tim4_ss* ss_;
- DigitalOut ld_; // for LDAC
-
- // Pointer of I2C
- SPI_TypeDef* mySpi_;
// for inhibition of copy constructor
DAC_MCP4922(const DAC_MCP4922&);
@@ -87,13 +36,12 @@
DAC_MCP4922& operator=(const DAC_MCP4922&);
// for internal use
- void WriteDac(uint16_t value)
+ virtual void WriteDac(uint16_t value)
{
while (IsBusy()) {}
- ss_->SlaveSelect();
- mySpi_->DR = value | wcr_;
+ SlaveSelect();
+ WriteSpi(value | wcr_);
}
};
}
#endif // DAC_MCP4922_HPP
-