CS代考 GY 6483 Real Time Embedded Systems – cscodehelp代写
PERIPHERALS
EL-GY 6483 Real Time Embedded Systems
PERIPHERALS
Copyright By cscodehelp代写 加微信 cscodehelp
• Buttons and keypads
• Sensors (temperature, light, microphone, pressure)
• Actuators (motors, e.g. servo)
• Displays (individual LEDs, seven segment displays, pixel displays)
• Communication (SPI, I2C, UART, USB, Ethernet/WiFi/Bluetooth/Zigbee)
• Memory-mapped I/O: the memory and registers of the I/O devices are mapped to address values (uses same address bus), same instructions used for accessing memory are used for accessing I/O devices.
• Port-mapped (isolated) I/O: write to the registers of the I/O interface through the data (not address) bus, which later sends the data to the actual I/O device. Often has special instructions for accessing I/O devices.
EXAMPLE: BLINKY
On MK20DX256VLH7 (Cortex M4):
• Program memory (Flash) begins at 0x00000000
• SRAM (stack) begins at 1FFC0000 (0x20000000 – 0x40000000)
• Peripherals begin at 0x40000000
• GPIOD port uses 0x400FF000 – 0x400FF114
EXAMPLE: BLINKY
r0 0x400FF094 1074786452
r1 0x00000020 32
r2 0x00300000 3145728
sp 0x1fff803c lr 0x00003fd pc 0x000040f
GPIO pin: Generic pin whose behavior, including whether it is an input or output pin, can be controlled by the user at run time.
A GPIO port is a group of GPIO pins.
HARDWARE ABSTRACTION LAYER (HAL)
A hardware abstraction layer is a programming API to interact with peripherals.
• CMSIS (Cortex Microcontroller Software Interface Standard) is a vendor independent HAL for using peripherals on the Cortex-M processor series.
• Environments may provide several HALs for using peripherals on Arm Cortex series microcontrollers.
GPIO on M0+ (ATSAMD21)
• PORT Block Diagram
Alternative Functions on M0+ (ATSAMD21)
• Pin Muxing Cross Referrence
PORT Function on M0+ (ATSAMD21)
• Pin Muxing Cross Referrence
Pin Configuration
Pin Configuration
Pin Control REGISTERS – DIR
Pin Control REGISTERS – DIR
Pin Control REGISTERS
// set pin 5 on port 0 to Output mode
*REG_PORT_DIR0 |= 0x00000100; //See HAL
Pin Control REGISTERS
ADDITIONAL PIN CONFIGURATION
ADDITIONAL PIN CONFIGURATION
USING GPIO
1. Set input/output
2. Set other configuration
3. Read/write
EXAMPLE: USING GPIO
void setup() { // put your setup code here, to run once: //We know that the LED is at Port 0 pin 5 *REG_PORT_DIR0 |= (1U << 5); //data direction reg }
void loop() { // put your main code here, to run repeatedly *REG_PORT_OUT0 |= (1U << 5); //on
delay(1000);
*REG_PORT_OUT 0&= ~(1U << 5); //off
delay(1000);}
EXAMPLE: USING GPIO
void setup() { // put your setup code here, to run once: //We know that the LED is at Port 0 pin 5 *REG_PORT_DIR0 |= (1U << 5); //data direction reg }
void loop() { // put your main code here, to run repeatedly *REG_PORT_OUTSET0 |= (1U << 5); //on
delay(1000);
*REG_PORT_OUTCLR0 |= (1U << 5); //off
delay(1000);}
EXAMPLE: USING GPIO
void setup() { // put your setup code here, to run once: //We know that the LED is at Port 0 pin 5 *REG_PORT_DIR0 |= (1U << 5); //data direction reg }
void loop() { // put your main code here, to run repeatedly *REG_PORT_OUTTGL0 ^= (1U << 5); //Toggle delay(1000);
SPECIFIC PERIPHERAL CONCEPTS
ARM Timers
Definition?
Timers are hardware Counters that can be configured to run from a variety of internal or external clocks.
They run concurrently with CPU and can be configured in software to provide a variety of functions.
Counter Modes (Example)
What about our Mo+?
• Selectable configuration
• 8-, 16- or 32-bit TC, with compare/capture channels
• Waveform generation
• Frequency generation
• Single-slope pulse-width modulation
• Input capture
• Event capture
• Frequency capture
• Pulse-width capture
• One input event
• Interrupts/output events on:
• Counter overflow/underflow • Compare match or capture
• Internal prescaler
• Can be used with DMA and to trigger DMA transactions
Timer Block Diagram
Example: LPC2148
• 2 x 32 Bit Identical Timer blocks Either Timer or Counter
• 2 Important Registers:
TC (Timer Counter)
PR (Prescaler)
• If timer is reset and enabled:
2. TC++, every PR+1 clock ticks
3. TC resets to zero at 0xFFFF FFFF
• 4 Match Registers and 4 Capture Registers for each Timer Do I have a match??
Stop the Timer, reset the Timer, generate an interrupt, or do whatever else the Match Control Register is configured for....
What about Capture?
Configure a pin to store current counter value in a capture register.
Example: LPC2148
• Prescale Register (PR):
Prescale Register specifies the maximum value for the Prescale Counter.
When the Prescale Counter (PC) is equal to PR, the TC in incremented on the next clock and also PC is cleared.
• Prescale Counter Register (PC):
Prescale Counter increments on every peripheral clock to the value stored in the PR. When the value in the PC is equal to the value in the PR, PC is reset and TC is incremented by 1 on the next clock cycle.
For example, if PR = 2, then TC is incremented on every third cycle of the peripheral clock.
Example: LPC2148
• Timer Control Register (TCR):
Timer Control Register is used to control the functions of timer / counter. It is used to enable / disable or reset the Timer Counter.
Bit 0 = 0, Timer Counter and Prescale Counter are disabled.
Bit 0 = 1, the counters are enabled.
Bit 1 = 1, both the counters (Timer Counter and Prescale Counter) are reset on the
next positive edge of the peripheral clock. They remain reset until Bit 1 = 0.
• Count Timer Control Register (CTCR):
Sets Timer/Counter Mode.
If Counter Mode is selected the counter pin and the edges (rising, falling or both) can be selected using CTCR.
Example: LPC2148
Match Control Register (MCR):
The Match Control Register is used control the actions to be performed when the value in the Match Register (MR) matches with the value in the Timer Counter (TC).
.Bit 0: When this bit is 1, an interrupt is triggered when MR0 is equal to TC. When this bit is 0, the interrupts is disabled.
Bit 1: When this bit is 1, TC is reset when MR0 is equal to TC. When this bit is 0, this feature is disabled.
Bit 2: When this bit is 1, the Timer Counter (TC) and Prescale Counter (PC) are stopped when the value in MR0 is equal to TC. Also, the TC is reset to 0.
Bit 3-5, Used for MR1 etc...
Example: LPC2148
If the clock = 60 MHz
T0CTCR = 0x0; // Timer Mode is selected. T0PR = 59999; // PR is set to this value T0TCR = 0x02; // Reset the Timer
How often will TC be incremented? Is the Timer running?
Example: LPC2148
T0TCR = 0x01; // Enable the Timer
while (T0TC < time_in_milliseconds); //Wait till the TC reaches the desired delay
T0TCR = 0x00; // Disable the Timer
Example: LPC2148
void delay (int d)
{T0TCR=0x02; T0TCR=0x01; while (T0TC < d); T0TCR=0x00;
}int main()
/* Initialize the Timer */ T0CTCR=0x00; T0PR=59999; T0TCR=0x02;
Example:Timer3 Toggles LED at One Second intervals.
// Assume CPU configured to run at system clock frequency 24 MHz.
// GPIO driving LED has been configured for output and its clock enabled. #include "stm32f10x.h“ // Contains definitions of Timer structure
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // enable clock for timer TIM3 :
TIM3->PSC = 23999; TIM3->ARR = 1000; TIM3->CR1 = TIM_CR1_CEN;
// Set pre-scaler to 24 000 (PSC + 1) // Auto reload value 1000
// Enable timer
Example:Timer3 Toggles LED at One Second intervals.
// When timer reaches the ARR value, the UIF flag will be set. Check this flag, clear it // and toggle an LED :
if(TIM3->SR & TIM_SR_UIF)
TIM3->SR &= ~TIM_SR_UIF; LED_BLUE_GPIO->ODR ^= (1 << LED_BLUE_PIN);
Capture and Compare Register (CCR)
CCR is based on a Timer Register plus additional hardware. CCR can be configured for either Capture or Compare modes:
• Capture mode operates as a stop watch – You start the stop watch
– The stop watch runs while you wait for an event(s) to happen – Once the event(s) happen the stop watch is stopped and the
time is recorded
• Compare mode operates as a comparator -You set a predefined value
- Start the register to count incoming pulses
- Once the pre-defined value is reached it generates an output event
Pulse Width Modulation (PWM)
PWM (PULSE WIDTH MODULATION)
PWM is based on switching between a small number of voltage levels (typically two voltage levels vmin and vmax, e.g., 0 V and 5 V).
APPROXIMATE CONTINUOUS RANGE
INTERSECTIVE PWM METHOD
In the intersective PWM method, a sawtooth waveform m(t) is generated and the signal u(t) is compared (at each time instant) with the sawtooth waveform m(t). The output PWM signal uPWM(t) is defined as follows:
If u(t) > m(t) at a time t, then at that time, uPWM = vmax. If u(t) < m(t) at a time t, then at that time, uPWM = vmin.
ILLUSTRATION OF INTERSECTIVE PWM METHOD
Example of intersective PWM method. Approximation of an analog signal
u(t) = 2.5 + 2 sin(t) using a PWM signal with vmin = 0 V and vmax = 5 V. Here, the time step dt is set to 0.01 seconds and the PWM period T is set to 1 second.
DELTA PWM METHOD
In the delta PWM method, a running integral of the deviation between the analog input signal u(t) and the PWM output signal uPWM(t) is maintained. When the integral crosses a threshold (i.e., when the integral becomes too big in magnitude, either as a positive number or a negative number), then the output uPWM is switched.
ILLUSTRATION OF DELTA PWM METHOD
Example of delta PWM method. Approximation of an analog signal u(t) = 2.5 + 2 sin(t) using a PWM signal with vmin = 0 V and vmax = 5 V. Here, the time step dt is set to 0.01 seconds and the threshold ε is set to 0.1.
ILLUSTRATION OF PWM METHOD on M0+ • CCx registers control the duty cycle
• COUNT UP:
WO[x] output is set at a start or compare match between the COUNT value and the top value and cleared on the compare match between the COUNT value and CCx register value.
• COUNT DOWN:
WO[x] output is cleared at start or compare match between the COUNT value and the top value and set on the compare match between the COUNT value and CCx register value.
ILLUSTRATION OF PWM METHOD on M0+ - TOP Change
Registers For Timers (8-Bit, similar for 16 and 32)
Registers For Timers (8-Bit, similar for 16 and 32)
Registers For Timers (8-Bit, similar for 16 and 32)
Registers For Timers (as per the HAL)
Input Capture: Ultrasonic Ranger
HC-SR04 ultrasonic module:
3mm resolution in the range 20-500 mm
Ultrasonic Pulse triggered by delivering to it a 10 us pulse
Generates an 8 pulse 40 kHz signal driving the Ultrasonic transducer
Distance determined as a Pulse Width obtained from PWM module output.
distance = pulse width * 58x10-6 [cm]
Ultrasonic Sensor Protocol
Implementation of Measurement:
Use 2 Timers.
TIM4 to generate the trigger pulse. TIM1 to measure the echo pulse.
Outline of Algorithm
1. Configure TIM1 prescaler and period
2. Configure TIM1 channel 1 to latch the timer value on rising
3. Configure TIM1 channel 2 to latch timer value on a falling input
4. Configure TIM1 to reset on the capture of completed event
5. Enable Interrupt for complete TIM1 event
6. Write ISR to store the difference between falling and rising edges and signal data ready by setting a user defined flag.
A/D AND D/A CONVERSION
• An ADC is used to read an analog signal (a voltage or a current) into a digital computer, e.g., to read an analog sensor.
• A DAC is used to write out an analog signal (a voltage or a current) from a digital computer, e.g., outputting a voltage to control an electric motor.
A/D AND D/A CONVERSION
A/D CONVERSION
ADC CHARACTERISTICS
The input signal is an analog signal (voltage, current), and the output is a binary number.
Precision: number of distinguishable ADC inputs (e.g., 12 bits, 2n -1 = 4095 alternatives)
Range: maximum and minimum ADC input (e.g., 0 to +3.3V unipolar).
Resolution (Quantization Interval Q): smallest distinguishable change in input (e.g., 3.3V/4096, which is about 0.81 mV). The resolution is the change in input that causes the digital output to change by 1.
ADC EQUATIONS
ADC EQUATIONS
ADC EXAMPLE
ADC IMPLEMENTATION
Parallel vs. Ramp vs. Sample and Hold
QUANTIZATION ERROR
QUANTIZATION ERROR
We must measure an input signal that varies between 0mV and 600mV. The system ADC can accept input in the range of 0V to 2.5V.
If the signal is amplified with a gain of 4 before reaching ADC, how many bits of resolution do we need to know the value of the input with max error 0.1%?
How many bits of resolution would you need if you were not using the amplifier?
DAC EQUATIONS
DAC IMPLEMENTATION
Nyquist theorem: To capture signal that varies at frequency f, you must sample at a rate fs > 2f. See the demo.
SAMPLING EXAMPLES (NO ALIASING)
SAMPLING EXAMPLES (ALIASING)
SAMPLING EXAMPLES (ALIASING)
REDUCING EFFECTS OF ALIASING
HARDWARE CONSTRAINTS
READING A DATASHEET
• Part number
• Basic specifications
• Functional block diagram or schematic diagram
• Date/revision number
PINOUT OR CONNECTION DIAGRAM
Lists pins, their functions, and where they’re physically located
ABSOLUTE MAXIMUM RATINGS
Tells you what you can do without destroying the device, not the conditions under which it operates correctly.
RECOMMENDED OPERATING CONDITIONS
Recommended settings for various parts of the device.
TYPICAL PERFORMANCE CHARACTERISTICS
OTHER THINGS
• Truth tables
• Timing diagrams
• Applications
• Schematics
• Physical dimensions
程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com