//---------------------------------------------------------------------- // Write-Protects the 1-Wire clock so that the real-time clock and alarm // registers cannot be changed. This function can also be used to set the // expiration mode on the device. TRUE sets the expiration to read-only, // meaning that the nv ram of the part becomes read-only after expiration, and // FALSE sets the expiration to "destruct", meaning only the part's serial // number can be read (and nothing else) after expiration. // // Parameters: // portnum The port number of the port being used for the // 1-Wire network. // SNum The 1-Wire address of the 1-Wire device. // RTCProtect Sets the write-protect bit for the real-time clock // and alarms located in the control register. If set // to TRUE, the part's Real-Time Clock and Alarm registers // can no longer be written, including the WPR bit. If // set to FALSE (if possible), the part will not be write- // protected. // RTCAExpire Sets the expiration bit (RO) of the control register. // If set to TRUE, the part expires and all memory // becomes read-only. If FALSE, the only thing that can // be accessed is the 1-Wire Net Address. // // Returns: TRUE if the write worked. // FALSE if there was an error in writing to the part. // SMALLINT setWriteProtectionAndExpiration(int portnum, uchar * SNum, SMALLINT RTCProtect, SMALLINT RTCAExpire) { if (setControlRegister(portnum, SNum, -99,-99,-99,-99,RTCAExpire,-99,-99,RTCProtect) == FALSE) { return FALSE; } return TRUE; }
//---------------------------------------------------------------------- // Sets the oscillator bit in the control register of the 1-Wire // clock. It can be turned on (1) or off (0). // // Parameters: // portnum The port number of the port being used for the // 1-Wire network. // SNum The 1-Wire address of the device to communicate. // OscEnable Sets the Oscillator enable bit of the control register. A // TRUE will turn the oscillator one and a FALSE will turn // the oscillator off. // // Returns: TRUE if the write worked. // FALSE if there was an error in writing to the part. // SMALLINT setOscillator(int portnum, uchar * SNum, SMALLINT OscEnable) { if (setControlRegister(portnum,SNum,-99,-99,-99,OscEnable,-99,-99,-99,-99) == FALSE) { return FALSE; } return TRUE; }
pwm::pwm(GPIO_TypeDef* bank, uint16_t pin, TIM_TypeDef* timer, uint8_t controlRegister, uint32_t inputFrequency) { frequency = inputFrequency; uint8_t prescaler = 1; //scales the timer clock to be the same as the internal clock timerNum = timer; //Saves the value of the timer inputted by the user controlRegisterNum = controlRegister; //Saves the value of the control register number inputted by the user timerFactor(timer); //gets the correct pulse length for the timer timerToClock(timer); //Initiates the clock for the given timer bankToClock(bank); //Initiates the clock for a given bank GPIO_InitTypeDef GPIO_InitStructure; //structure used by stm in initializing pins. // Initialize the given pin for pwm GPIO_InitStructure.GPIO_Pin = pin; //specifies which pins are used GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //assigns the pins to use their alternate functions GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(bank, &GPIO_InitStructure); //initializes the structure GPIO_PinAFConfig(bank, getPinSource(pin), gpioAltFunc); uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / (84000000 / prescaler)) - 1; uint16_t PreCalPeriod = ((84000000 / prescaler) / frequency) - 1; period = PreCalPeriod; //assigns the calculated period to the global instance variable that will store the period TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //structure used by stm in initializing the pwm // Setup timebase for the given timer TIM_TimeBaseStructure.TIM_Period = PreCalPeriod; //sets the period of the timer TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; //sets the pre scaler which is divided into the cpu clock to get a clock speed that is small enough to use for timers TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(timer, &TIM_TimeBaseStructure); //initializes this part of the code setControlRegister(controlRegister, timer); // Enable the given time peripheral Preload register on ARR. TIM_ARRPreloadConfig(timer, ENABLE); TIM_Cmd(timer, ENABLE); //Enable the timer given }