Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peripherals and CMSIS STM32F4, Cortex M4 2016.

Similar presentations


Presentation on theme: "Peripherals and CMSIS STM32F4, Cortex M4 2016."— Presentation transcript:

1 Peripherals and CMSIS STM32F4, Cortex M4 2016

2 GPIO configuration } void SUBinit (void) {
GPIO_InitTypeDef GPIO_InitStructure; // declare data structure RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOE, ENABLE); // clock enable GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | // list pins to work with GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // define direction GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // define driver mode GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // define speed GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; // define termination GPIO_Init (GPIOE, &GPIO_InitStructure); } // color code: function call, data structure name, data structure member, TypeDef, parameter, defined as „0xsomething“

3 GPIO use #include "stm32f4xx.h"
#include "stm32f4xx_rcc.c " // these must be included #include "stm32f4xx_gpio.c " // #include "dd.h" void main (void) { // char switches = 0; SWITCHinit (); LEDinit (); while (1) { // switches = GPIOE->IDR; if (switches & S370) LED_GR_ON; else LED_GR_OFF; // S370 = 0x0008 if (switches & S371) LED_OR_ON; else LED_OR_OFF; // S370 = 0x0010 if (switches & S372) LED_RD_ON; else LED_RD_OFF; // S370 = 0x0020 if (switches & S373) LED_BL_ON; else LED_BL_OFF; // S370 = 0x0040 }; } // color code: function call, TypeDef, macro call, defined as „0xsomething“

4 LCD screen 2x16 characters, functions
#include "LCD2x16.c„ // must be included if LCD is used LCD_init (); // init LCD, must run first LCD_string ("LCD test", 0x00); // display title string LCD_uInt16 (counter++, 0x0a, 0x01); // write to LCD, unsigned, 16 bits LCD_sInt16 (counter++, 0x40, 0x01); // write to LCD, signed, 16 bits LCD_uInt32 (counter++, 0x48, 0x01); // write to LCD, unsigned, 32 bits LCD_sInt32 (counter++, 0x40, 0x01); // write to LCD, signed, 32 bits LCD_sInt3DG (counter++, 0x42, 0x01); // write to LCD, unsigned, 3 digits // what to do, (what, where, how); // color code: function call, parameter

5 DAC block diagram X2 !

6 DAC configuration void DACinit (void) {
GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; // DAC outputs are available here GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; // pins must be in analog mode GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // no need, done automatically GPIO_Init (GPIOA, &GPIO_InitStructure); DAC_InitTypeDef DAC_InitStructure; RCC_APB1PeriphClockCmd (RCC_APB1Periph_DAC, ENABLE); DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_Init (DAC_Channel_1, &DAC_InitStructure); DAC_Init (DAC_Channel_2, &DAC_InitStructure); DAC_Cmd (DAC_Channel_1, ENABLE); DAC_Cmd (DAC_Channel_2, ENABLE); } // color code: function call, data structure name, TypeDef, data structure member, parameter, defined as „0xsomething“

7 DAC use #include "stm32f4xx.h" #include "stm32f4xx_rcc.c " // these must be included #include "stm32f4xx_dac.c " // #include "stm32f4xx_gpio.c " // int main () { unsigned int j; DACinit (); while (1) { DAC->DHR12R1 = j & 0xfff; // up ramp, direct write to DAC data register DAC->DHR12R2 = 0xfff - (j & 0xfff); // down ramp , direct write to DAC data register j = (j + 1) & 0x0fff; };

8 ADC block diagram x3 !

9 ADC configuration, part 1
// decision: use two ADCs, simultaneous sampling void ADCinit_SoftTrigger (void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; // we will use ADC inputs here GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; // put pins into analog mode GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // not really needed GPIO_Init (GPIOA, &GPIO_InitStructure); // color code: function call, data structure name, TypeDef, data structure member, parameter, defined as „0xsomething“

10 ADC configuration, part 2
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInit (&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init (ADC1, &ADC_InitStructure); ADC_RegularChannelConfig (ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles); ADC_Init (ADC2, &ADC_InitStructure); ADC_RegularChannelConfig (ADC2, ADC_Channel_2, 1, ADC_SampleTime_3Cycles); ADC_Cmd (ADC1, ENABLE); ADC_Cmd (ADC2, ENABLE); }; // color code: function call, data structure name, data structure member, parameter, defined as „0xsomething“

11 ADC use in program #include "stm32f4xx.h" #include "stm32f4xx_rcc.c " // #include "stm32f4xx_adc.c " // these must be included #include "stm32f4xx_gpio.c " // #include "LCD2x16.c" // result goes to screen this time int main () { unsigned int j; ADCinit_SoftTrigger (); // call configuration software LCD_init(); LCD_string("ADC1=", 0); LCD_string("ADC2=", 0x40); // LCD configure and init while (1) { // endless loop ADC_SoftwareStartConv (ADC1); for (j = 0; j< ; j++){}; // waste some time LCD_uInt16 ( (int)ADC1->DR, 0x06, 1); // write result on LCD, 1st line LCD_uInt16 ( (int)ADC2->DR, 0x46, 1); // write result on LCD, 2nd line }; }

12 COUNTER TIMER block diagram What to count Count processing X14 ! x3 !
Input shaping Count processing Output shaping

13 TIMER as simple counter, configuration, part 1
// decision: use Timer 2, simple counting, port configuration void GPIOAinit_TIM2_ETR (void) { GPIO_InitTypeDef GPIO_InitStructure;   RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; // ETR is available at GPIO_Pin_15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // as an alternate function GPIO_Init (GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig (GPIOA, GPIO_PinSource15, GPIO_AF_TIM2); // pin PA15:TIM2_Ch1/ETR  } // color code: function call, data structure name, TypeDef, data structure member, parameter, defined as „0xsomething“

14 TIMER as simple counter, configuration, part 2
// decision: use Timer 2, simple counting at ETR input pin, timer2 configuration void TIM2init_counter (void) { // counting from ETR TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE); // enable counter; apply clock TIM_TimeBaseInitStructure.TIM_Prescaler = 0; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = ; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit (TIM2, &TIM_TimeBaseInitStructure); TIM_ETRClockMode2Config (TIM2, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0x00); // ETR requires mode 2 TIM_Cmd (TIM2, ENABLE); } // color code: function call, data structure name, TypeDef, data structure member, parameter, defined as „0xsomething“

15 TIMER simple counter, use of
// decision: use Timer 2, simple counting #include "stm32f4xx.h" #include "stm32f4xx_rcc.c„ // must be included #include "stm32f4xx_gpio.c„ // #include "stm32f4xx_tim.c" // #include "LCD2x16.c" // due to writing on the screen int main () { GPIOAinit_TIM2_ETR (); // sub: GPIO clock enable, digital pin definitions TIM2init_counter (); // sub: Timer 2 as counter: ETR input LCD_init (); // due to writing on the screen LCD_string ("TIM2 counter ETR", 0); // write title to LCD while (1) { LCD_uInt32 (TIM2->CNT, 0x40, 0x01); // write counts to LCD for (int i = 0; i < ; i++) {}; // waste time }; } // color code: function call, TypeDef, parameter, defined as „0xsomething“


Download ppt "Peripherals and CMSIS STM32F4, Cortex M4 2016."

Similar presentations


Ads by Google