Presentation is loading. Please wait.

Presentation is loading. Please wait.

Praxis I.  Introduction  Hardware  Software Tools  Microcontroller board  Blinky (simple program to flash a led)  References.

Similar presentations


Presentation on theme: "Praxis I.  Introduction  Hardware  Software Tools  Microcontroller board  Blinky (simple program to flash a led)  References."— Presentation transcript:

1 Praxis I

2  Introduction  Hardware  Software Tools  Microcontroller board  Blinky (simple program to flash a led)  References

3  This tutorial consists on a series of classes. ◦ In each one of these classes a simple example will be presented.  The final objective is to build a robot capable of navigation based on a line following strategy.

4  Robot chassis  Power supply  Voltage regulator  Motors/Gearbox with motors  Motor Driver  Microcontroller Board  Sensors

5  Robot chassis ◦ It will support all the electronics and the motors/gearbox. ◦ The robot should have a configuration of two drive wheels plus one caster wheel.  Power supply ◦ No power no moving robot.

6  Voltage regulator ◦ To regulate the input voltage for the values required by the electronics and motors.  Motors/Gearbox with motors ◦ These are controlled by the microcontroller.  Motor Driver ◦ It stands between the microcontroller and the motors ◦ The microcontroller pins can’t provide the voltage nor the current necessary for the motors move as desired.

7  Microcontroller Board ◦ This is the brain of the robot. This where the control is programmed so the robot knows what to do. ◦ It will take the sensors input, process it and determine which adjustments it needs to do on the robots direction (and also possibly speed and others).  Sensors ◦ With these you’ll be able to know if you’re following the line correctly. The data that the sensors provide gives some feedback about the line. Sensors are, basically the eyes of the robot.

8  IDE (Integrated Development Environment) ◦ It includes compiler, editor, etc. ◦ The IDE to be used on this tutorial will be Keil uVision (the free version should be more than enough since it provides 32KB of code limit).  Flash Loader ◦ To upload the code to the flash of the microcontroller. ◦ This tool is provided by ST.

9  ARM 32-bit Cortex™-M3 CPU Core ◦ 72 MHz maximum frequency, ◦ Single-cycle multiplication and hardware division  Memories ◦ 64 or 128 Kbytes of Flash memory ◦ 20 Kbytes of SRAM  Clock, reset and supply management ◦ 2.0 to 3.6 V application supply and I/Os ◦ POR, PDR, and programmable voltage ◦ detector (PVD) ◦ 4-to-16 MHz crystal oscillator ◦ Internal 8 MHz factory-trimmed RC ◦ Internal 40 kHz RC ◦ PLL for CPU clock ◦ 32 kHz oscillator for RTC with calibration  Low power ◦ Sleep, Stop and Standby modes ◦ VBAT supply for RTC and backup registers  2 x 12-bit, 1 µs A/D converters (up to 16 ◦ channels) ◦ Conversion range: 0 to 3.6 V ◦ Dual-sample and hold capability ◦ Temperature sensor  DMA ◦ 7-channel DMA controller ◦ Peripherals supported: timers, ADC, SPIs, ◦ I2Cs and USARTs

10  Up to 80 fast I/O ports ◦ 26/37/51/80 I/Os, all mappable on 16 ◦ external interrupt vectors and almost all ◦ 5 V-tolerant  Debug mode ◦ Serial wire debug (SWD) & JTAG interfaces  7 timers ◦ Three 16-bit timers, each with up to 4 IC/OC/PWM or pulse counter and quadrature (incremental) encoder input ◦ 16-bit, motor control PWM timer with dead-time generation and emergency stop ◦ 2 watchdog timers (Independent and Window) ◦ SysTick timer 24-bit downcounter  Up to 9 communication interfaces ◦ – Up to 2 x I2C interfaces (SMBus/PMBus) ◦ – Up to 3 USARTs (ISO 7816 interface, LIN, IrDA capability, modem control) ◦ – Up to 2 SPIs (18 Mbit/s) ◦ – CAN interface (2.0B Active) ◦ – USB 2.0 full-speed interface  CRC calculation unit, 96-bit unique ID

11  Top view

12  First, you install all the software needed to work with the board.  Get used to the IDE ◦ See how to edit, compile and debug a small example program. This should be a good start.  Get to know the microcontroller board ◦ This means study datasheets, schematics, example projects and everything that will make your life easier when developing for a given platform that is unknown.

13  Start a new Project and save it ◦ It’s good to create a dedicated folder for the project and save it inside.

14  After a new window will be presented asking to select the target.  The target is the microcontroller for which you will be programming (on this case is the STM32F103RBT6).

15  Next window will ask you if you want to add Start up code to your project. ◦ You should choose no ◦ Later we will add the file

16  Now create the folders on the project explorer view to store the source files, the header files and libraries. Right click on the folder and you’ll see the context menu ◦ It’s a good idea to keep your files organized by category.

17  To add files choose the option “Ad Files to Group…”  Add the startup file: STM32F10x.s. You can find it in: C:\Keil\ARM\Startup\ST

18  A library needs to be added so that the Keil will “include” the necessary header and source files on compilation time.  It cabe found on: C:\Keil\ARM\RV3 1\LIB\ST in a standard install.

19  Create new.c and.h files as needed and save them on the project directory.  You need to add these files the same way as in the last slide.  The.h (header files) will have your function prototypes (the name of the function and parameters that receives and returns) and other declarations and macros.  The.c files will have all your code (all the body of a function is present on this file).  This last two points are not mandatory but it is good to follow this strategy.

20  Here is a view after all these steps

21  Before writing the code you need to select an option so that an.hex file containing the machine code is generated this is the file you will flash to the microcontroller

22  Now write the code to flash a led.  You’ll need these two includes to compile without errors. Needed because other definitions than our own will be used: ◦ #include ◦ #include "platform_config.h“  Basically, it’s necessary to configure the led pin to function as an output and use a delay so it’s visible, else at the frequency that the microcontroller would execute the code, it wouldn’t be possible to see the led blinking.

23 GPIO_InitTypeDef GPIO_InitStructure; int i=0; // Initialize LED PB[8..15] RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE);// Enable clock for GPIO port GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// GPIO speed GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;// GPIO Mode // GPIO pins to be used on the specified GPIO port GPIO_InitStructure.GPIO_Pin =LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN; GPIO_Init(LED_PORT, &GPIO_InitStructure); // Initialize GPIO structure with //previous data

24 while(1) { LED1_LO();// Led Low for(i=0; i<1000000; i++)// delay ; LED1_HI();// Led High for(i=0; i<1000000; i++)// delay ; }  Obviously this is not all. There is some definitions and functions that are used and were not defined by us. Some of them are in the header file platform_config.h

25  These are some of the definitions on the platform_config.h header file // Define LED PinIO Interface Mask Bit #define LED1_PIN GPIO_Pin_8// LED1 = PB[8] #define LED2_PIN GPIO_Pin_9// LED2 = PB[9] #define LED3_PIN GPIO_Pin_10// LED3 = PB[10] #define LED4_PIN GPIO_Pin_11// LED4 = PB[11] #define LED5_PIN GPIO_Pin_12// LED5 = PB[12] #define LED6_PIN GPIO_Pin_13// LED6 = PB[13] #define LED7_PIN GPIO_Pin_14// LED7 = PB[14] #define LED8_PIN GPIO_Pin_15// LED8 = PB[15] #define LED_PORT GPIOB #define RCC_APB2Periph_GPIO_LEDRCC_APB2Periph_GPIOB #define LED1_HI() GPIO_WriteBit(LED_PORT,LED1_PIN,Bit_SET) #define LED1_LO() GPIO_WriteBit(LED_PORT,LED1_PIN,Bit_RESET)

26  Now compile and check for errors Build all files Build Output

27  After compilation, if there are no presented errors on the build output, we can proceed to debug the code.  There are two types of debug possible: ◦ In hardware: You would be debugging the code directly on the hardware. Requires additional hardware like an emulator/debugger. ◦ In software/simulator: Keil as an simulator of is own and can be used to test the code.  For now we will focus on the simulator since we don’t have an emulator/debugger.

28  To start a debug session you can either go to the debug menu and click on Start/Stop debug session or CTRL+F5

29  You can insert breakpoints (F9 key, debug menu or toolbar) on the code so the debug session will pause on the selected code line. This allows you to check the state of the microcontroller peripherals, registers, pins, etc.  Check the rest of the commands available under the debug menu (you can use the toolbar above the editor for faster access or shortcut keys).  There are commands for: ◦ Run - it only stops if it finds a breakpoint on the code ◦ Step In – Allows to step one line ◦ Step Over – Allows to step over the current line ◦ Step Out – Jumps out of the current function ◦ Step to current – It executes the code until it finds the current cursor line

30  Anyway this is pointless unless you can check out if it affects the microcontroller (registers, pins, etc).  For this, check the menu Peripherals. It presents several peripherals that the microcontroller has.  Other important views are the stack, memory, local variables and watch views.

31  Here is how the GPIOB peripheral window looks like

32  Now we just need to flash microcontroller with the new code.  For this we will use the Flash Loader provided by ST  It uses a serial port to send the code. An USB- RS232 converter can be used (the drivers have to be installed prior to any use).

33  First choose the COM Port and the appropriate settings for it. Refer to picture below.  If it fails to connect you can try to lower the baudrate (can also be other reason).  Then click next...

34  If it connects it will present a screen saying that the target is readable  Press next again

35  Now the window will show some info about the microcontroller and the flash pages (adresses) and if these are protected or not.  You won’t need to do anything on this page so click next

36  This page presents you with some options: ◦ Erase the flash memory ◦ Download to device (downloads the code to the device) ◦ Upload from device (allows to extract the code on the microcontroller to a file on your PC) ◦ Enable/Disable Flash protection ◦ Edit option bytes (to configure the microcontroller)

37  The option to choose is download to device  You have to look for an.hex file on your project directory that was produced by the compiler.  Click next

38  If a message saying that operation finished successfully you just finished to flash the code.  Now push again the BOOT0 switch and after push the reset button and the led should start to blink

39  And that’s it. You should be ready to start your own projects.

40  http://www.st.com/internet/mcu/product/164487.jsp http://www.st.com/internet/mcu/product/164487.jsp ◦ It is the microcontroller page from ST. You can find almost everything for this microcontroller here.  http://www.arm.com/products/processors/cortex- m/cortex-m3.php http://www.arm.com/products/processors/cortex- m/cortex-m3.php ◦ The ARM site for the Cortex-M3 microcontroller. ST uses the cortex-M3 core to design it’s own microcontroller just like many other IC manufacturers.  http://en.wikipedia.org/wiki/ARM_architecture http://en.wikipedia.org/wiki/ARM_architecture ◦ Some general info about ARM.  And read the datasheets that’s the only way to get to know a specific microcontroller. Else, you won’t be able to program it.

41  Structure of a I/O Port bit ◦ I/O port bit as input ◦ I/O port bit as output  Configure a pin ◦ Turn a led on/off with a switch ◦ Toggle the value of a led with a switch  Homework

42

43  The Output Buffer is disabled  The Schmitt Trigger Input is activated  The weak pull-up and pull-down resistors are activated or not depending on input configuration (pull-up, pull-down or floating)  The data present on the I/O pin is sampled into the Input Data Register every APB2 clock cycle  A read access to the Input Data Register obtains the I/O State.

44

45  The Output Buffer is enabled: ◦ Open Drain Mode: A “0” in the Output register activates the N-MOS while a “1” in the Output register leaves the port in Hi-Z. (the P-MOS is never activated) ◦ Push-Pull Mode: A “0” in the Output register activates the N-MOS while a “1” in the Output register activates the P-MOS.  The Schmitt Trigger Input is activated.  The weak pull-up and pull-down resistors are disabled.  The data present on the I/O pin is sampled into the Input Data Register every APB2 clock cycle  A read access to the Input Data Register gets the I/O state in open drain mode  A read access to the Output Data register gets the last written value in Push-Pull mode

46

47 // Initialize LED PB[8..15] // Enable clock for GPIO port RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// GPIO speed GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;// GPIO Mode // GPIO pins to be used on the specified GPIO port GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN | LED8_PIN; // Initialize GPIO structure with previous data GPIO_Init(LED_PORT, &GPIO_InitStructure);

48 //Initialize SW on PA (PA0) RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // GPIO pins to be used on the specified GPIO port GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(LED_PORT, &GPIO_InitStructure);

49 while(1) { // If switch is pushed if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET) LED1_HI();// led on else LED1_LO();// led off }

50 while(1) { // Toogle led // If switch is pushed if((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET) && (SW_prevState == Bit_SET)) { if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) == Bit_RESET) GPIO_SetBits(GPIOB, GPIO_Pin_8); else GPIO_ResetBits(GPIOB, GPIO_Pin_8); SW_prevState = Bit_RESET; } // else if the switch is released clear switch status else if((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_SET) && (SW_prevState == Bit_RESET)) { SW_prevState = Bit_SET; } // delay to ignore the switch pin bouncing for(i=0; i<100000; i++) ; }

51  Shift bits on the led port (GPIOB[8..15]) to turn on a led one at a time. Pressing the switch S4 (GPIOA_Pin_0) toggles the direction. 15141312111098... Switch pushed, shift direction changed

52  Homework review  PWM generation (on timer2)

53 // Turn on led0(GPIOB 8) leds = 0x00000100;// led0 is On dir = 1;// rotate left GPIO_Write(GPIOB, (u16)(leds));// Write to port while(1) { // delay to ignore the switch pin bouncing and make change visible for(i=0; i<100000; i++) ;

54 // If switch is pushed change direction if((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET) && (SW_prevState == Bit_SET)) { // Change direction dir = !dir; SW_prevState = Bit_RESET; } // else if the switch is released clear switch status else if((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_SET) && (SW_prevState == Bit_RESET)) { SW_prevState = Bit_SET; }

55 if(dir)// If true Shift left leds <<= 1; else // else Shift right leds >>= 1; // Correct when shifted left and it's gets to 0x10000 if(leds == 0x00010000 && dir) leds = 0x00000100; // Correct when shifted right and it's gets to 0x0080 else if(leds == 0x00000080 && !dir) leds = 0x00008000; GPIO_Write(GPIOB, (u16)(leds)); }

56 //Initialize PA0 as alternate function push-pull RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP // GPIO pins to be used on the specified GPIO port GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA, &GPIO_InitStructure);

57 RCC->APB1ENR |= RCC_APB1Periph_TIM2;// Enable clk for timer TIM2->ARR = 2500;// Period TIM2->CCR1 = 1000;// duty cycle TIM2->PSC = 7;// prescaler (divides the clk for the timer) TIM2->CCMR1 |= 0x68;// Output compare mode - pwm ; // preload enable; cc1 channel as output TIM2->CCER |= 0x1;// Polarity and CC1 output enable (OC1 signal is output on the pin) TIM2->EGR = 0x1;// Update generation to update the // registers TIM2->CR1 |= 0x85;// clock enable for timer; set update // request source; auto reload preload // enable while(1);

58 // Need these extra vars to initialize using the functions // from the library TIM_TimeBaseInitTypeDef TIM2_TimeBaseInit; TIM_OCInitTypeDef TIM2_OCInit;... RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM2_TimeBaseInit.TIM_Prescaler = 7; TIM2_TimeBaseInit.TIM_CounterMode = TIM_CounterMode_Up; TIM2_TimeBaseInit.TIM_Period = 2500; TIM2_TimeBaseInit.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM2, &TIM2_TimeBaseInit); TIM_ARRPreloadConfig(TIM2, ENABLE); /* TIM Time Base Init structure definition */ typedef struct { u16 TIM_Prescaler; u16 TIM_CounterMode; u16 TIM_Period; u16 TIM_ClockDivision; u8 TIM_RepetitionCounter; } TIM_TimeBaseInitTypeDef;

59 TIM2_OCInit.TIM_OCMode = TIM_OCMode_PWM1; TIM2_OCInit.TIM_OutputState = TIM_OutputState_Enable; TIM2_OCInit.TIM_Pulse = 1000; TIM2_OCInit.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM2_OCInit); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_UpdateRequestConfig(TIM2, TIM_UpdateSource_Regular); TIM_GenerateEvent(TIM2, TIM_EventSource_Update); TIM_Cmd(TIM2, ENABLE); while(1); /* TIM Output Compare Init structure definition */ typedef struct { u16 TIM_OCMode; u16 TIM_OutputState; u16 TIM_OutputNState; u16 TIM_Pulse; u16 TIM_OCPolarity; u16 TIM_OCNPolarity; u16 TIM_OCIdleState; u16 TIM_OCNIdleState; } TIM_OCInitTypeDef;

60


Download ppt "Praxis I.  Introduction  Hardware  Software Tools  Microcontroller board  Blinky (simple program to flash a led)  References."

Similar presentations


Ads by Google