不韋 呂 / UITDSP_ADDA

Dependents:   UITDSP_ADDA_Example UIT2_MovingAv_Intr UIT2_VariableFIR UIT2_VowelSynthesizer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DAC_MCP4921.cpp Source File

DAC_MCP4921.cpp

00001 //------------------------------------------------------
00002 // Class for single DAC in MCP4921
00003 //
00004 // 2015/03/31, Copyright (c) 2015 MIKAMI, Naoki
00005 //------------------------------------------------------
00006 
00007 #include "DAC_MCP4921.hpp"
00008 
00009 namespace Mikami
00010 {
00011     DAC_MCP4921::DAC_MCP4921(PinName mosi, PinName sclk,
00012                              PinName cs, PinName ldac)
00013         : spi_(mosi, NC, sclk),
00014           ld_(ldac, 0), mySpi_((SPI_TypeDef*)NULL)
00015     {
00016         if ( (mosi == PA_7) || (mosi == PB_5) )  mySpi_ = SPI1;
00017         if ( (mosi == PB_15) || (mosi == PC_3) ) mySpi_ = SPI2;
00018         if ( mosi == PC_12 )                     mySpi_ = SPI3;
00019 
00020         // Set SPI format
00021         spi_.format(16, 0);
00022         // Clock source of F401 for SPI1    : 84 MHz,
00023         //                      SPI2, SPI3  : 42 MHz
00024         mySpi_->CR1 = (mySpi_->CR1 & ~SPI_CR1_BR);
00025         if (mySpi_ == SPI1) mySpi_->CR1 += SPI_CR1_BR_0;
00026 #ifdef __STM32F411xE_H
00027         mySpi_->CR1 += SPI_CR1_BR_0;
00028 #endif  // __STM32F411xE_H
00029         
00030         // timer prescaler is set same value of boud rate for SPI
00031         uint16_t psc = (2 << ((mySpi_->CR1 >> 3) & 0x07)) - 1;
00032         if (mySpi_ != SPI1) psc = (psc + 1)*2 - 1;
00033         ss_ = new TIM4_SlaveSelect(psc, 18, cs);
00034     }
00035 
00036     void DAC_MCP4921::ScfClockTim3(uint32_t clock, PinName pin)
00037     {
00038         if ( (pin != PA_6) && (pin != PB_4) && (pin != PB_5) &&
00039              (pin != PC_6) && (pin != PC_7) && (pin != PC_8) && (pin != PC_9) )
00040         {
00041             fprintf(stderr, "\r\nIllegal pin name in DAC_MCP4921::ScfClockTim3()\r\n");
00042             while (true) {}
00043         }
00044 
00045         PwmOut clockSCF(pin);
00046         
00047         TIM3->ARR =  SystemCoreClock/clock - 1;
00048         TIM3->PSC = 0;
00049         // Set capture/compare register 2
00050         if ( (pin == PA_6) || (pin == PB_4) || (pin == PC_6) )
00051             TIM3->CCR1 = (TIM3->ARR + 1)/2;    
00052         if ( (pin == PB_5) || (pin == PC_7) )
00053             TIM3->CCR2 = (TIM3->ARR + 1)/2;    
00054         if (pin == PC_8)
00055             TIM3->CCR3 = (TIM3->ARR + 1)/2;    
00056         if (pin == PC_9)
00057             TIM3->CCR4 = (TIM3->ARR + 1)/2;    
00058     }
00059 }