Class library for internal ADC and DAC connected by SPI. This library support clock generator using TIM3 for switched-capacitor filter to smooth output signal of DAC. This library includes derivative class to support interrupt occured in end of AD conversion. Validated for ST Nucleo F401RE, F411RE. 内蔵 ADC と,SPI 接続の DAC のためのクラスライブラリ.DAC の出力信号を平滑化するためのスイッチトキャパシタフィルタ用のクロックは TIM3 を使用.ST Nucleo F401RE,F411RE で動作を確認.
Dependents: UITDSP_ADDA_Example UIT2_MovingAv_Intr UIT2_VariableFIR UIT2_VowelSynthesizer ... more
DAC_MCP4921.cpp@4:03c4518de836, 2015-03-16 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Mon Mar 16 23:59:24 2015 +0000
- Revision:
- 4:03c4518de836
- Parent:
- 0:2cdd5c91d975
- Child:
- 6:9deca4a06c8c
5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 4:03c4518de836 | 1 | //------------------------------------------------------ |
MikamiUitOpen | 4:03c4518de836 | 2 | // Class for single DAC in MCP4921 |
MikamiUitOpen | 0:2cdd5c91d975 | 3 | // |
MikamiUitOpen | 4:03c4518de836 | 4 | // 2015/02/02, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 4:03c4518de836 | 5 | //------------------------------------------------------ |
MikamiUitOpen | 0:2cdd5c91d975 | 6 | |
MikamiUitOpen | 0:2cdd5c91d975 | 7 | #include "DAC_MCP4921.hpp" |
MikamiUitOpen | 0:2cdd5c91d975 | 8 | |
MikamiUitOpen | 0:2cdd5c91d975 | 9 | namespace Mikami |
MikamiUitOpen | 0:2cdd5c91d975 | 10 | { |
MikamiUitOpen | 0:2cdd5c91d975 | 11 | DAC_MCP4921::DAC_MCP4921(PinName mosi, PinName sclk, |
MikamiUitOpen | 0:2cdd5c91d975 | 12 | PinName cs, PinName ldac) |
MikamiUitOpen | 4:03c4518de836 | 13 | : spi_(mosi, NC, sclk), |
MikamiUitOpen | 4:03c4518de836 | 14 | ld_(ldac, 0), mySpi_((SPI_TypeDef*)NULL) |
MikamiUitOpen | 0:2cdd5c91d975 | 15 | { |
MikamiUitOpen | 0:2cdd5c91d975 | 16 | if ( (mosi == PA_7) || (mosi == PB_5) ) mySpi_ = SPI1; |
MikamiUitOpen | 0:2cdd5c91d975 | 17 | if ( (mosi == PB_15) || (mosi == PC_3) ) mySpi_ = SPI2; |
MikamiUitOpen | 0:2cdd5c91d975 | 18 | if ( mosi == PC_12 ) mySpi_ = SPI3; |
MikamiUitOpen | 0:2cdd5c91d975 | 19 | |
MikamiUitOpen | 0:2cdd5c91d975 | 20 | // Set SPI format |
MikamiUitOpen | 0:2cdd5c91d975 | 21 | spi_.format(16, 0); |
MikamiUitOpen | 0:2cdd5c91d975 | 22 | // Clock source of F401 for SPI1 : 84 MHz, |
MikamiUitOpen | 0:2cdd5c91d975 | 23 | // SPI2, SPI3 : 42 MHz |
MikamiUitOpen | 0:2cdd5c91d975 | 24 | mySpi_->CR1 = (mySpi_->CR1 & ~SPI_CR1_BR); |
MikamiUitOpen | 0:2cdd5c91d975 | 25 | if (mySpi_ == SPI1) mySpi_->CR1 += SPI_CR1_BR_0; |
MikamiUitOpen | 0:2cdd5c91d975 | 26 | #ifdef __STM32F411xE_H |
MikamiUitOpen | 0:2cdd5c91d975 | 27 | mySpi_->CR1 += SPI_CR1_BR_0; |
MikamiUitOpen | 0:2cdd5c91d975 | 28 | #endif // __STM32F411xE_H |
MikamiUitOpen | 0:2cdd5c91d975 | 29 | |
MikamiUitOpen | 4:03c4518de836 | 30 | // timer prescaler is set same value of boud rate for SPI |
MikamiUitOpen | 4:03c4518de836 | 31 | uint16_t psc = (2 << ((mySpi_->CR1 >> 3) & 0x07)) - 1; |
MikamiUitOpen | 4:03c4518de836 | 32 | if (mySpi_ != SPI1) psc = (psc + 1)*2 - 1; |
MikamiUitOpen | 4:03c4518de836 | 33 | ss_ = new Tim4_ss(psc, 19, cs); |
MikamiUitOpen | 0:2cdd5c91d975 | 34 | } |
MikamiUitOpen | 0:2cdd5c91d975 | 35 | |
MikamiUitOpen | 0:2cdd5c91d975 | 36 | void DAC_MCP4921::ScfClockTim3(uint32_t clock, PinName pin) |
MikamiUitOpen | 0:2cdd5c91d975 | 37 | { |
MikamiUitOpen | 0:2cdd5c91d975 | 38 | if ( (pin != PA_6) && (pin != PB_4) && (pin != PB_5) && |
MikamiUitOpen | 0:2cdd5c91d975 | 39 | (pin != PC_6) && (pin != PC_7) && (pin != PC_8) && (pin != PC_9) ) |
MikamiUitOpen | 0:2cdd5c91d975 | 40 | { |
MikamiUitOpen | 4:03c4518de836 | 41 | fprintf(stderr, "\r\nIllegal pin name in DAC_MCP4921::ScfClockTim3()\r\n"); |
MikamiUitOpen | 0:2cdd5c91d975 | 42 | while (true) {} |
MikamiUitOpen | 0:2cdd5c91d975 | 43 | } |
MikamiUitOpen | 0:2cdd5c91d975 | 44 | |
MikamiUitOpen | 0:2cdd5c91d975 | 45 | PwmOut clockSCF(pin); |
MikamiUitOpen | 0:2cdd5c91d975 | 46 | |
MikamiUitOpen | 0:2cdd5c91d975 | 47 | TIM3->ARR = SystemCoreClock/clock - 1; |
MikamiUitOpen | 0:2cdd5c91d975 | 48 | TIM3->PSC = 0; |
MikamiUitOpen | 0:2cdd5c91d975 | 49 | // Set capture/compare register 2 |
MikamiUitOpen | 0:2cdd5c91d975 | 50 | if ( (pin == PA_6) || (pin == PB_4) || (pin == PC_6) ) |
MikamiUitOpen | 0:2cdd5c91d975 | 51 | TIM3->CCR1 = (TIM3->ARR + 1)/2; |
MikamiUitOpen | 0:2cdd5c91d975 | 52 | if ( (pin == PB_5) || (pin == PC_7) ) |
MikamiUitOpen | 0:2cdd5c91d975 | 53 | TIM3->CCR2 = (TIM3->ARR + 1)/2; |
MikamiUitOpen | 0:2cdd5c91d975 | 54 | if (pin == PC_8) |
MikamiUitOpen | 0:2cdd5c91d975 | 55 | TIM3->CCR3 = (TIM3->ARR + 1)/2; |
MikamiUitOpen | 0:2cdd5c91d975 | 56 | if (pin == PC_9) |
MikamiUitOpen | 0:2cdd5c91d975 | 57 | TIM3->CCR4 = (TIM3->ARR + 1)/2; |
MikamiUitOpen | 0:2cdd5c91d975 | 58 | } |
MikamiUitOpen | 0:2cdd5c91d975 | 59 | } |