MIKROPROCESORY PRO VÝKONOVÉ SYSTÉMY Special HW pheripheries and their programming in C 1. díl České vysoké učení technické Fakulta elektrotechnická A1B14MIS Mikroprocesory pro výkonové systémy 08 Ver.1.10 J. Zděnek, 2017 1
Special HW pheripheries Microcontroller for electric drive require special pheripheries. Block digaram of controlled electric drive follows on next screen, however from the drive is required: Communication with operator. Communication Controller diferent types CAN, Ethernet, UART. Control of switches input/output pins of controller. Control of transistors in converter. Have to be generated precisely. A1B14MIS Mikroprocesory pro výkonové systémy 08 2
Special HW pheripheries Measurement of analogue signals (voltage, current, temperature, ). (ADC Analog to Digital Converter). Measurement of speed from incremental sensor. (Capture Unit) Allways (Interrupt Controller). All HW parts are connected to interrupt controller. For generation of time intervals (Timer) A1B14MIS Mikroprocesory pro výkonové systémy 08 3
Drive controlled by computer Electric Drive UDC Operator Microcontroller Converter Motor Software Measurement A1B14MIS Mikroprocesory pro výkonové systémy 08 4
Special HW pheripheries The following is a block summary of the special HW blocks in a microcontroller designed for real-time control, for example, controlling the electrical drive by a computer. In the lab we will use manuals for the PIC18F87J11 microcontroller A1B14MIS Mikroprocesory pro výkonové systémy 08 5
Special HW pheripheries Control Computer (Hardware) CPU (Processor) Program & Data Memory Modulator (PWM) Impuls Outputs Higher Level System Communication (UART, Ethernet, CAN) Timer Interrupt Controller ADC Capture Unit Logic Inputs & Outputs Logic Outputs Analog Signal (Voltage) Impuls Signal Logic Inputs A1B14MIS Mikroprocesory pro výkonové systémy 08 6
Memory Mapped Special Function Registers (SFRs) A1B14MIS Mikroprocesory pro výkonové systémy 08 7
Analogue to digital conversion The computer can not handle analogue variables directly. The only thing that can do is to process numeric values. Therefore, the analog quantities must be expressed by numbers. For the numerical expression (approximation) of the analog magnitude, two steps are taken: (1) Sampling: An analogue variable must be sampled. Continuous period sampling is used. When sampling, information is lost because the analog value between the samples is unknown. Here we refer to the "Sampling Theorem", which tells how to select sampling periods relative to the maximum frequency contained in the analog signal so that the samples can be reconstructed again. (2) Quantification: The samples taken must be adjusted according to the number of bits of the transmission. There is a further loss of information here (we will not deal with the number of bits involved) The following steps demonstrate the steps below. For simplicity of display, 4 bits (ie 16 possible values) are used. In fact, 8 or 10 or 12 (sometimes 16 bits) A1B14MIS Mikroprocesory pro výkonové systémy 08 8
Analogue signal A1B14MIS Mikroprocesory pro výkonové systémy 08 9
Sampled analogue signal A1B14MIS Mikroprocesory pro výkonové systémy 08 10
Sampled and quantified analogue signal A1B14MIS Mikroprocesory pro výkonové systémy 08 11
Analogue to digital conversion A typical analog-to-digital conversion scheme is in the following screen. Filter: Recall that the filter block that limits the upper signal frequency (anti-aliasing filter) is analog and out of the computer. The filter ensures compliance with the "Sampling Theorem" along with the correct sampling period of the computer. Sampling circuit: To measure higher frequencies, the actual conversion time of the analogue number may be too long (the signal would change too much during the conversion). Therefore, a sampling circuit is placed at the input, which quickly removes the sample from the analog signal and holds its value during its own analog-to-digital conversion. Analog-to-digital converter: The conversion method is a large amount; the "sequential approximation" method is typically used in the microcontroller, see below. A1B14MIS Mikroprocesory pro výkonové systémy 08 12
Zpracování analogových veličin počítačem A1B14MIS Mikroprocesory pro výkonové systémy 08 13
Zpracování analogových veličin počítačem Skutečné zapojení analogově-číslicového převodu: Analogový přepínač (Analog Multiplexer): Aby bylo možné snadno měřit analogové veličiny z více vstupů, je typicky na začátku analogověčíslicového bloku umístěn analogový přepínač, který se ovládá z počítače (programu). Vzorkovací obvod: Pro rychlý odběr vzorku ADC: Analogově-číslicový převodník (Analog to Digital Converter) je mikrokontroléru jeden (někdy dva). Pokud je ADC v mikrokontroléru jen jeden, pak ovšem lze v daný okamžik převádět jen jeden analogový vstup a pak je možné přepnou analogový přepínač na jiný vstup a převádět ten. Blokové zapojení analogově-číslicového bloku je na následujícím snímku. Obsahuje časovač vzorkovače, časovač ADC převodníku, registru pro volbu analogového kanálu, registru pro ovládání (spuštění) ADC převodníku (často společný se stavovým registrem) a registr výsledku (číslo) převodu. Blok generuje též signál žádosti o přerušení na konci převodu. A1B14MIS Mikroprocesory pro výkonové systémy 08 14
ADC Clock ADC Timer Sample&Hold Timer Multiplexer Analog Inputs ADC D Sample&Hold CH Channel AC ADC Command ADC Result AR Program Program Int Program A1B14MIS Mikroprocesory pro výkonové systémy 08 15
Successive Aproximation Method ADC uses mostly (Successive Aproximation Method). Principle is on following screen The ADC contains a digital-to-analog converter (DAC) and a comparator. In the first step of the DAC transmission, the half-voltage of the total ADC range is generated. This voltage is compared with the converted voltage Ux. If less than Ux is retained in the next step, and half of the remaining range is added and the sum is compared again with Ux. If the sum greater than Ux the added voltage is not added in the next step, otherwise it is left. In the next step, add half of the remaining range, etc., until the end of the transfer. Transfer Time: It does not depend on the size of the input voltage Ux It depends on the number of bits being converted Input voltage polarity Ux: The converter accepts only positive voltages typically within the supply voltage of the microcontroller). Negative voltages must be moved to the ADC range of the external circuit (Operational Amplifier). A1B14MIS Mikroprocesory pro výkonové systémy 08 16
Successive Aproximation Method A1B14MIS Mikroprocesory pro výkonové systémy 08 17
Successive Aproximation Method The following screen shows the block connection of the converter with the succesive approximation method. The digital to analog Converter (DAC) requires a reference voltage for its operation. It can be connected: For less accuracy of conversion from the supply voltage of the microcontroller For greater accuracy of conversion from an external precision reference voltage source (semiconductor component with ambient temperature compensation) For the ADC converter : Resolution, ie how many bits have an output converted number (typically 8 or 10 or 12 sometimes 16 bits) The accuracy, ie the lowest bit is stable at a stable Ux. The accuracy of the transducer strongly depends on the design of the external circuits (both by connection and design of the printed circuit board). A1B14MIS Mikroprocesory pro výkonové systémy 08 18
Successive Aproximation Method A1B14MIS Mikroprocesory pro výkonové systémy 08 19
Generation of precise time stamps In a various programs, it requires to generate accurate time stamps in the program (by requesting an interrupt). This can be done in several ways. One of these is shown in the following screens. Generating of accurate time stamps require the request to be set up always after the required interval and no delay over time. The set request can then be served in the ISR with a certain delay due to the action during ISR call and due to other requests. Timer with Autoload: The following screen shows the principle of evaluating the period in hardware. Here, the timer steps upwards and automatically reaches zero when it reaches the preset value (default period) and sets the user request (Int) A1B14MIS Mikroprocesory pro výkonové systémy 08 20
Generování přesných časových značek do programu Clock (Takt) Frequency Prescaler (Dělič kmitočtu) Timer (Časovač) Overflow (Přetečení) CP Count Prescaler (Předvolba) Program Int Žádost o obsluhu A1B14MIS Mikroprocesory pro výkonové systémy 08 21
Generování přesných časových značek do programu Following screen show procedure of interrupt requests Own ISR service can have some delay beacause of other interrupts Interrupt flag is set periodicaly with same delay A1B14MIS Mikroprocesory pro výkonové systémy 08 22
Generování přesných časových značek do programu Ttimer Ttimer T1 Int T2 Int time INIT ISR Tactual ISR CP Ttimer Calculation Calculation RET RET RET ISR = Interrupt Service Routine (Obslužný program přerušení) Ttimer = čas měřený časovačem Tactual = čas volání ISR A1B14MIS Mikroprocesory pro výkonové systémy 08 23
Capture Unit Precise period To determine the speed of rotation of the motor, a pulse sensor (incremental encoder) is often used to generate a pulse signal with a period dependent on the speed of rotation. Slow rotation (long period) can usually be measured by reading a input port (sampling based on timer request). The error is tolerable for low speeds. High speeds already require support hardware called the "Capture Unit". An overview of the principle and timing is in the following screen. Two more screens then show the situation in detail. A1B14MIS Mikroprocesory pro výkonové systémy 08 24
Capture Unit - Přesné měření periody Capture unit obsahuje (viz následující tři snímky): Referenční časovač (Timer) Čítač předvolby (Prescaler) pro vhodné nastavení hodin pro Timer Detektor události (Event Detektor) měřený impulsní se přivádí právě do detektoru události. Detektor událostí umí detekovat hranu v signálu. V některých mikrokontrolérech lze požadovanou událost přednastavit na náběžnou, sestupnou nebo obě hrany (v registru Event Mode) Předdělička událostí (není na následujícím snímku zobrazena) je připojena na výstup Detektoru událostí a umožňuje snížit frekvenci detekovaných událostí pro velmi rychlé průběhy. Oddělovací obvod výstup detektoru událostí ovládá oddělovací obvod připojený mezi referenční čítač a záchytný registr (Capture Register). Při detekci událostí se oddělovací obvod otevře a stav čítače se sejme od záchytného registru. Zároveň se nastaví žádost o obsluhu (int) Záchytný registr (Capture Register) slouží k uložení stavu časovače (času) v okamžiku detekce události. V obsluze ISR se pak záchytný registr přečte a vyhodnotí vzhledem k minulému čtení a tím se přesně určí perioda i když ISR je voláno s určitým časovým zpožděním vlivem akci při přechodu do ISR případně obsluhou ISR s vyšší prioritou A1B14MIS Mikroprocesory pro výkonové systémy 08 25
Capture Unit - principle Clock Prescaler Timer Input Event Detector EM Event Mode CR Capture Register Program Int Program T T Input T1 Int T2 Int time INIT EM T 0 RET ISR X CR RET ISR Y X X CR T X - Y RET A1B14MIS Mikroprocesory pro výkonové systémy 08 26
Capture Unit block diagram A1B14MIS Mikroprocesory pro výkonové systémy 08 27
Capture Unit Capture unit use principle (see next screen): After the event is detected (set here to rising edges), the reference timer status (edge time) is stored in the Capture Register and the Interrupt request is set. Upon calling the ISR, the Capture Record register will be read. The registers remember the time of instatns of the past event (edges). By subtracting the new record and the previous time we get the value of the measured period. The new readout is saved in the register as the previous one and the ISR ends. If the reference timer overflow does not matter once and the past and new readings are valid if the size of the previous time variable is the same as the Capture registry size (usually 16 bits). The calculation is done in this case by modulo 16 and the difference is valid (verify). A1B14MIS Mikroprocesory pro výkonové systémy 08 28
Capture Unit principle A1B14MIS Mikroprocesory pro výkonové systémy 08 29
Speed sensor for rotation direction Measurement of speed, direction of rotation and shaft position: With a single channel impulse sensor, only the rotation speed can be measured. In many cases, it is sufficient for the direction of rotation to be apparent from other data (for example, setting the drive for the forward or reverse - locomotive). For measuring the direction of rotation and position, a sensor with a socalled quadrature signal is used. Quadrature incremental sensor is three-channel. Two channels (A, B) for measuring the period and direction of rotation and one channel (so-called Index) for determining the shaft going trough a zero position. Channels A and B are offset by 90o. When rotating in one direction, the edges of channel A of the channel edge B are preceded. When rotating in the opposite direction, the order of edges of channels A and B is changed (see the following reference) Some microcontrollers have a Capture Unit already modified in hardware and can process a quadrature signal directly. A1B14MIS Mikroprocesory pro výkonové systémy 08 30
Speed sensor for rotation direction A1B14MIS Mikroprocesory pro výkonové systémy 08 31
Compare Unit - principle Compare Unit: is a supporting hardware that creates a PWM modulator together with the software (that is the main part). Compare unit consists of: Timer with prescaler Comparator - compares two numbers Event Comparator Register Event Generator - generates a preset edge at the output of the microcomputer. Event Registerer (Event Register) Interrupt Requests (Int) Compare Register and Event Register are available from the program The digital comparator compares the timer value and the Compare register before each change of the reference timer value (Timer). When equal, it sets the interrupt request and generates the preset event at the microcontroller output. A1B14MIS Mikroprocesory pro výkonové systémy 08 32
Compare Unit diagram Program TC Timer Count Clock Prescaler Timer Comparator Event Generator Output CR Compare Register Event Register ER Program Int Program T0 T1 T2 T3 Output T1 Int T2 Int T3 Int time INIT ISR ISR ISR ER ER ER ER CR TC+T0 CR X+T1 CR X+T2 CR X+T3 X TC+T0 X X+T1 X X+T2 X X+T3 RET RET RET RET A1B14MIS Mikroprocesory pro výkonové systémy 08 33
Compare Unit detail A1B14MIS Mikroprocesory pro výkonové systémy 08 34
Compare Unit Compare Unit - Principle: The unit allows you to preset from the software the time in the future when an event (the preset edge) is to be generated at the output. The hardware then accurately generates an edge at the preset time and asks for an Interrupt. Preset time is saved in the register. In the ISR from the Compare Unit, the software calculates the next requested time of the new event (with saved time and new value eg. from the controller) and saves the time again in the Compare Unit and presets the new event Register). The timing diagram of the activity described above is in the following screen. Delays in transition to ISR do not matter if there are additional events within the preset time. The event (edge) is generated precisely by hardware. A1B14MIS Mikroprocesory pro výkonové systémy 08 35
Compare Unit A1B14MIS Mikroprocesory pro výkonové systémy 08 36
Tlačítko Potenciometr Čidlo rychlosti Vstup RJx Vstup RFx Vstup RJx Vstup RJx ADC PIC Výstup RHx Výstup RHx Výstup RHx Výstup RHx Výstup RHx A1B14MIS Mikroprocesory pro výkonové systémy 08 37
Hlavní smyčka programu RESET Init Vars Init ISR RCONbits.IPEN INTCON2bits.TMR0IP PIE1bits.ADIE IPR1bits.ADIP Str 119-134 Init Vstup TRISJ TRISH... Str. 135 Interrupt enable INTCON0 = 0b1110000 Str. 121 Init ADC ADCON0 ADCON1 Str 309, 310 While(1) Init Timer0 T0CON Str 193 Běh na pozadí Svícení LED PORTHbits.XX = CitImp = prevod>>2 If (CitImp >= 255) CitImp = 255 If (CitImp <= 0) CitImp = 0 A1B14MIS Mikroprocesory pro výkonové systémy 08 38
Obsluha přerušení 1ms ISRLOW Uložení Context INTCONbits.T MR0IF == 1 PIR1bits.ADIF == 1 Čti stav tlačítka Prev = ADRESH<<8+AD RESL Čti stav kanál A PIR1bits.ADIF = 0 Čti stav kanál B Obnov Context INTCONbits.TMR 0IF = 0 A1B14MIS Mikroprocesory pro výkonové systémy 08 39
Stavový diagram tlačítko PORTJbits.RJ7 == 0 0 PORTJbits.RJ7 == 1 PORTJbits.RJ7 == 0 4 PORTJbits.RJ7 == 0 1 PORTJbits.RJ7 == 1 PORTJbits.RJ7 == 1 5 2 PORTJbits.RJ7 == 0 PORTJbits.RJ7 == 1 6 VirtTlacitko = 0 3 VirtTlacitko = 1 PORTJbits.RJ7 == 0 PORTJbits.RJ7 == 1 A1B14MIS Mikroprocesory pro výkonové systémy 08 40
Stavový diagram tlačítko /* Start podprogramu obsluhy preruseni. */ if (INTCONbits.TMR0IF == 1) { switch(stavtl) { case 0: { if (PORTJbits.RJ7 == 1) stavtlnext = 1; else stavtlnext = 4; break; } case 1: { if (PORTJbits.RJ7 == 1) stavtlnext = 2; else stavtlnext = 4; break; } case 2: { if (PORTJbits.RJ7 == 1) stavtlnext = 3; else stavtlnext = 4; break; } case 3: { if (PORTJbits.RJ7 == 1) virttl = 1; else stavtlnext = 4; break; } case 4: { if (PORTJbits.RJ7 == 0) stavtlnext = 5; else stavtlnext = 1; break; } case 5: { if (PORTJbits.RJ7 == 0) stavtlnext = 6; else stavtlnext = 1; break; } case 6: { if (PORTJbits.RJ7 == 0) virttl = 0; else stavtlnext = 1; break; } } stavtl = stavtlnext; INTCONbits.TMR0IF = 0; } A1B14MIS Mikroprocesory pro výkonové systémy 08 41
Stavový diagram Snímač S5 PORTJbits.RJ7 == 0 0 PORTJbits.RJ7 == 1 PORTJbits.RJ7 == 0 4 PORTJbits.RJ7 == 0 1 PORTJbits.RJ7 == 1 PORTJbits.RJ7 == 1 5 2 PORTJbits.RJ7 == 0 PORTJbits.RJ7 == 0 PORTJbits.RJ7 == 1 PORTJbits.RJ7 == 1 VirtStopaA = 0 6 VirtStopaA = 0 If(virtStopaB == 0) citimp++; else citimp--; 3 VirtStopaA = 1 If(virtStopaB == 1) citimp++; else citimp--; A1B14MIS Mikroprocesory pro výkonové systémy 08 42
MIKROPROCESORY PRO VÝKONOVÉ SYSTÉMY Speciální obvody a jejich programování v C 1. díl KONEC České vysoké učení technické Fakulta elektrotechnická A1B14MIS Mikroprocesory pro výkonové systémy 08 43