ecettest1
C Operators
-Relational -Arithmetic -Bitwise -Logical -Assignment
Holding torque
.If the motor exceeds this torque it loses synchronicity with the incoming pulses and stalls. •Holding Torque -the amount of external torque which must be exerted on the shaft motor when the motor is at full rated current and is at rest (zero speed).This is the static torque and is not on the torque speed curve.It is generally about 20% higher then the low speed torque of the dynamic torque-speed curve.
ATmega2560 Data Memory map (8 bit)
0x0000 -0x001F: 32 ALU General purpose registers R0 -R31 •0x0020 -0x005F: 64 I/O registers •Special Function Registers (SFRs) used by PORTs, Timers, SPI, EEPROM, Interrupts, SREG •0x0060 -0x1FF: 416 External I/O Registers •More SFRs used by PORTs, ADC, Timers, UARTs, Interrupts •0x0200 -0x21FF: Internal Static Random Access Memory (SRAM) •0x2200 -0xFFFF: External SRAM
Half-Stepping
1 & 2 phases on •Alternates between two phases on and one phase on. •Doubles the steps.
Central Processing Unit (sometimes known as the "core")
17bit Program Counter points to nextinstruction to be fetched. Can address 217(128k) memory locations. Flash EEPROM stores 16 or 32 bit wide instructions, hence memory arranged as 128k x 16bit (= 256kbyte) 16bit register points to memory in an area of SRAM where data can be stored temporarily. This area is called the stack 8bit SRAM data memory space with address range 0x0000 to 0x21FF. Contains: •32 -8bit general purpose registers mapped into the SRAM data space. (see below) •64 -8 bit SFRs (special function registers used to set up peripherals I/O ports, timers etc. located at 0x0020 thru 0x005F. •416 -8bit extended I/O registers •8192 -8bit locations for general data storage and the stack space.
Relational Operators
> Greater than • >= greater than equal to • < less than • <= less than equal to • == equal to • != not equal to
arrays
An array occupies a contiguous block of memory -a buffer -with elements (a.k.a. cells) of the same data type •The elements of an array in C are identified using an index, an integer with range 0 ≤ index ≤ N‐1 where N is the length of an array
external linkage
An identifier with external linkage represents the same function or object throughout the multi C file program •In effect the compiler passes such an identifier to the linker which resolves the identifier with other instances of inclusion in other translation units (C modules) and libraries •Note: •If an identifier is already declared with internal linkage it cannot be changed to external linkage •An external identifier can be initialized only once and that must occur when the variable is defined •The external identifier is defined outside of all functions (it is a global variable) •An external identifier has static storage duration i.e. it will exist in memory throughout the execution of the program. Of course, the value of the variable can change during program execution but its place in memory will not be lost. •An external variable used in another translation unit (c module) but not initialized there MUST use extern when declaring the variable in which case the compiler assumes that the actual definition of the variable is in somewhere else.
Internal linkage
An identifier with internal linkage represents the same object throughout the C file where it is defined. The static variable in one .c file cannot be accessed from the code in another .c file that is part of a multi‐module program •In effect the compiler DOES NOT PASS such an identifier to the linker •An identifier with internal linkage is defined (and possibly initialized) outside of all functions (it is a static variable). Uninitialized static variables are initialized to 0 and placed in the .bsssection of SRAM. If they are initialized when first declared they are placed in the .data section of SRAM •An internal identifier is defined by including the storage class specifier "static" when first defined •An internal identifier (variable) has static storage duration i.e. it will exist in memory throughout the execution of the program. Of course, the value of the variable can change during program execution but its place in memory will not be lost
no linkage
An identifier with neither external nor internal linkage has no linkage •It is not global •Include: •Function parameters •Object identifiers that are declared within a function and without the storage class specifier extern
Memory types
Architecture of a microcontroller may require that variables and constants be stored in different types of memory •Data that will not change should be stored in one type of memory •Data that must be read from and written to repetitively in a program should be stored in another type of memory •Memory that must be retained even when power is removed from the system should be stored in another type of memory
ATMEGA2560 SRAM Layout
At the end of the build process the variables are stored in these areas .data .bss Area for dynamic memory allocation. Can grow or shrink during runtime Area for storing local variables, return addresses and program states during function(s) execution. Initial address loaded into the Stack Pointer (SP) by the startup code.
half step
Better resolution •Less torque •Pattern must be repeated to go through one revolution
stepper motor calculations
Center shaft: 32 steps for magnetic rotor inside the motor to complete full rotation. •one step angle = 360˚ / 32 = 11.32˚ •half step mode: step angle = 11.32˚ / 2 = 5.625˚ •Reduction gear of 64:1 ratio, turns 64 revolutions for outer shaft to turn one revaluation •How many steps does it take to turn outer shaft one revolution? •32 * 64 = 2048 steps •How many degree does one step turn? •360°/ 2048 = 0.18° •In half step mode, how many steps does it take to turn one revolution? •2048 steps * 2 = 4096 steps
Unipolar
Center tap added between the two leads •Unidirectional current flow in each ½ of the windingLess torque than bipolar •Increased power loss because unipolar windings are thinner than bipolar motor windings, thus needing more windings
microstepping
Continuously varying motor currents •Coiling windings are not fully on or fully off. •Winding current approximates a sinusoidal AC waveform
local storage duration
Defined within a block or function that have no storage class specifier. i.e. without static added •Function parameters also have automatic storage duration. •Lifetime is delimited by the braces {...} •Stored in temporary storage area called stack frame
coil windings
Determines by how leads from each phase winding is brought out of the motor
STACK
Divided into FRAMES (Stack Frame). One frame for each function call. This area grows and shrinks as more or less functions are called and local variables are stored
bipolar
Each lead taken separately, bi‐directional current flow through entire winding •Can produce current flow in two directions •Each stator pole magnetized to North or South Uses windings with no common connection. •Must be driven from a H bridge circuit since the current in the windings must be reversed periodically to operate the motor
stack frame
Each time a program calls a function, a new stack frame is created and new instances of the local variable are generated and initialized if a declaration includes initialization
Stepper Motor
Electric motor that rotates in discrete step increments •Moves one step at a time •Each step is defined by a Step Angle •4 distinct steps, step angle = 90 degrees
second type of functiion
Functions where data is passed to the function but no data is returned
SRAM:
Heap, Stack and stack frames
Storage classes
How long a variable remains in memory and is therefore accessible •Which parts of the program (there may be multiple .c files making up the project) can access or use or change the variable
Static inside a function
If a variable is declared as static inside a function it will be initialized on the FIRST entry into the function ONLY and its value preserved on all future exits from the function i.e. it has static duration (lifetime). The variable will NOT be stored on the stack but in another section (the static .bsssection) of the SRAM.
goto
Jump to a label
VARIABLE DECLARATION
Must be declared before they can be used • This tells the compiler what type of variable it is i.e. how many bits need to be reserved in the memory for this variable.
wave step mode
Poor resolution •Less torque since only one winding is energized at a time
struct
Short for structure •Allows several variables to be grouped together under a single name •typedef-defines a new data type
Stepper motor operation fundamentals
Step 1: Phase A is energized, lock rotor in position •Step 2: Phase A turned off, Phase B turned on. Rotor rotate 90˚ clockwise •Step 3: Phase A turned on, but in reversed polarity, rotor rotate 90˚ clockwise •Step 4: Phase B turned on, in reversed polarity, rotor rotate 90˚ clockwise •basic "one phase on" stepping •Wave Drive -one phase turned on at a time
two phase stepper motor operation fundamentals
Step 1: Phase A, Phase B are on, locking Rotor in at the location, between the poles •Step2: phase A reversed direction, Rotor rotate 90˚ clockwise •Step 3: Phase B reverse direction, Rotor rotate 90˚ clockwise •Step 4: Phase A reverse direction, Rotor rotate 90˚ clockwise •"two phases on" stepping. •Full Step Drive
calculatiing rpm
Step Angle = 90deg •Each step takes 2 sec •What is the motor's rpm? •How many steps in one revolution? • /4 𝑠𝑡𝑒𝑝𝑠 • How long does it takes for one revolution? •4 𝑠𝑡𝑒𝑝𝑠 2 8 𝑠𝑒𝑐 • Revolution per minute (rpm) • 7.5𝑟𝑝𝑚
specifications of stepper
Step Angle •Rated Voltage, Current •Torque •Holding torque •Pull‐in Torque •Pull‐out Torque
stepper motor
Step is precise and repeatable, used without any feedback mechanism. Save board space for expensive sensing circuitry •Relatively inexpensive (resolution determines the cost) •Torque •Holds its position firmly •Eliminate mechanical brakes •Can be used for positioning applications: printers, machining equipment •But... resonances can occur if not properly controlled and it is not easy to operate at extremely high speed
full step
Still poor resolution •More torque since two winding is energized at a time
linkage
The extent (range) of an identifier's identity in and amongst translation units is determined by the identifier's linkage. •A C variable must have one of the following kinds of linkage: •External linkage (variable can be used anywhere in a multi C file program) •Internal linkage (variable can be used anywhere in a single C file •No linkage (variables with block scope (e.g. automatic or local variables) or with prototype scope have no linkage.
STANDARD DATA TYPES
Unsigned integer - Range 0- (2n -1) Signed integer - Range -2 n-1 - (2n-1 -1) n = number of bits 1 byte = 8 bits Data type double is 64 bits long
VARIABLES
Used to store information • Allow the programmer to refer to information through meaningful names distance_km weight_kg
VARIABLE NAMING
Variable names are critical in code readability • C is Case sensitive • Lower case letters are generally preferred • Start with a letter • Can contain numbers & underscores (do not begin with an underscore as it is used by the C library. Also do not begin a name with a number.) • No spaces • Be descriptive
VARIABLE INITIALIZATION
Variables can be initialized when declared
•Stack Pointer (SP)
always contains the address of the next available memory location on the STACK
static duration
any objects defined outside all functions, including main(), and usually located up with the #include and #define statements and those declared with the storage class specifier static INSIDE a function have static storage duration. All objects with static storage duration are generated and initialized before execution begins and their lifetime spans the program's entire runtime. They are NEVER stored in stack memory.
plain function
data is neither passed to or returned from the function:
scope and linkage
define which parts of a program (may consists of multiple .c files) can use or access or change the value of the variable.
Storage duration
how long the variable and its numeric value stay in memory.
Increment
i++;
block scope
identifiers declared within a block of code { code block } have block scope meaning that the identifier can be accessed only from its point of declaration to the end of the block of code in which it was declared.
file scope
if an identifier is declared outside all blocks of code and parameter lists (usually located above main() along with the #includes, #defines, then it has file scope and can be used anywhere after the declaration and up to the end of the C file
function prototype scope
if parameter names are used in a function prototype they only have prototype scope. Since these names have no significance outside the prototype and are not required, they are essentially like comments and can be omitted
overhead
indicates space where the program counter's return address. •SP address corresponding to the first memory location in this stack frame •Various internal register states may be saved and restored after the return from the function call
function scope
is essentially the same as block scope. Except for certain exceptions (labels, tags, union etctypes) the scope of the identifier begins immediately after its appearance in the declaration and ends at the closing brace of the function code. Note that parameters listed in the function definition are also local identifiers (variables) with function scope.
Decrement
j--;
allocated storage duration
memory space taken from the HEAP and acquired by the program at runtime by calling malloc() or calloc(). The storage duration lasts as long as the allocated block of memory exists and ends when the block is released back to the heap using realloc() or free().
break
move the program flow outside if the loops
continue
moves program flow to the next iteration
scope
refers to the part(s) of a program in which the variable is meaningful. In other words the part(s) of a program that can see, use, access or change the variable.
void
signifies no return value from the function or arguments passed to the function
Functions
splitting code into smaller segments •used many times over •time penalty in calling the function •reduces code memory
duration
storage lifetime there is no way to access an object before or after its duration, only during its duration
Pull-In Torque
the amount of torque in which the motor may move the load without acceleration.Generally older catalogs showed the Pull-in torque-speed curve to show the speeds at which the motor may start, stop and reverse without losing synchronicity with the incoming pulses.
Pull out torque
the amount of torque the motor may dynamically produce at various speeds.Generally this is the torque-speed curve.If the motor exceeds this torque it loses synchronicity with the incoming pulses and stalls.
Detent Torque
the amount of torque the motor produces when not energized.No current is flowing through the winding and the motor is at rest
after function is executed
this section is released and may be overwritten with local variable values and return addresses by future function executions
Automatic (local) variables in function
those that are only visible in the function i.e. they have BLOCK scope and are created in a stack frame in SRAM during execution of the function. These local variables are effectively LOST on exit from the function since their space is overwritten by new local variables in the next function call saved in a new stack frame. Their values are initialized again on re‐entry into the function.
DATA memory contains
three separate areas of Read/write Memory. •32 general purpose registers •62 I/O registers •Internal SRAM
SCOPE OF VARIABLES
• Depends on where variables are declared • Local Variables are declared inside a function • Global variables are declared outside any function or block.
ENUMERATIONS AND DEFINITION
• Improves code readability. • Replaces numbers with names and other more meaningful phrases.
MULTIPLE DECLARATIONS
• Multiple variables of the same type can be declared on the same line
TYPECASTING
• Used to promote the size of a variable to float or double. • Helps to optimize memory usage in embedded systems. • C compiler does implicit conversion or explicit conversion.
SIGNED INTEGERS
• int8_t 8bit signed integer • Most significant bit is the sign bit. • 0 for positive and 1 for negative • Range -128 to 127 • Remember 2's complement.
Atmel AVR Overview
•8-Bit RISC Microcontroller •Optimized for 8bit registers, data buses, memory addresses •Non-Floating Point Processor means the Processor is optimized for integer operations.
Relational
•< less than •> greater than •<= less than equal to •>= greater than equal to
3 type of function
•A function where data is passed to and returned from the function
Logical Operators
•AND && •OR || •NOT !
Arithmetic Operators
•Allow basic mathematical operations -Addition (+) -Subtraction (-) -Multiplication (*) -Division (/) •Modulus(%) •Increment(++) •Decrement(--)
Bitwise operators
•Clear a bit e.g. bit 3 -val = 0b11001100 & 0b11110111 •Set a bit e.g bit 1 -val = 0b11001100 | 0b00000010 •Toggle a bit e.g bit 5 -val = 0b11001100^0b00100000
Declare arrays
•Declare the following array called One_LEDwith the following value •0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 •uint8_t One_LED[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; •Declare the following array called Two_LEDswith the following value •0x18, 0x24, 0x42, 0x81 •uint8_t Two_LED[] = {0x18, 0x24, 0x42, 0x81};
Isolating a bit
•Example isolate bit 4 from value = 0011 0011 •Do not care about the other bits •Use an AND operator •Mask bits M = 0001 0000 •value & M 0001 0000 •right shift the result by 4 places, >>4
Representing Signed Numbers
•The 2's complement system is used to represent signed numbers •Perform subtraction by actually performing addition •Use same hardware to both add and subtract •Sign bit is placed in front of the MSB -0 for positive, 1 for negative
Modulus
•To get the modulus, perform long division and take the remainder •%
House Rules for Variables
•Use uint8_t as default -unless size or function dictates otherwise -avoid floating point unless absolutely required