Download presentation
Presentation is loading. Please wait.
1
Arm Programming in C Chapter 7
Sepehr Naimi
2
Topics Data Types Time Delays Logic Operation in C
3
Languages High Level Languages C Language Low Level Languages
Easy to develop and update C Language Acceptable performance Low Level Languages High performance Not portable High Level Languages (like VB) C Language Machine Language
4
A simple C program Write a program that calculate the sum of {1,3,…,13,15} #include <stm32f10x.h> int main () { unsigned int sum; for (int i = 1; i <= 15; i+=2) sum += i; while (1); return 0; }
5
Data Types Data type Size Range char 1 byte -128 to 127 unsigned char 0 to 255 short int 2 bytes -32,768 to 32,767 unsigned short int 0 to 65,535 long 4 bytes -2,147,483,648 to 2,147,483,647 unsigned long 0 to 4,294,967,295 long long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 unsigned long long 0 to 18,446,744,073,709,551,615 The int data type is 32-bit in Arm. It is 16-bit in 16-bit and 8-bit processors.
6
ISO C99 integer data types
Size Range int8_t 1 byte -128 to 127 uint8_t 0 to 255 int16_t 2 bytes -32,768 to 32,767 uint16_t 0 to 65,535 int32_t 4 bytes -2,147,483,648 to 2,147,483,647 uint32_t 0 to 4,294,967,295 int64_t 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 uint64_t 0 to 18,446,744,073,709,551,615
7
What is the difference between signed and unsigned integers?
Data types (cont.) c char c; int8_t c; short int a1; int16_t a1; long lng; int32_t lng; uint16_t a; unsigned short a; lng a1 a What is the difference between signed and unsigned integers?
8
Signed vs. unsigned Unsigned Signed Unsigned Multiplication:
uint32_t a1 = 5; uint32_t a2 = 3; uint32_t a3 = 2; b = (a1 * a2) / a3; int32_t a1 = 5; int32_t a2 = 3; int32_t a3 = 2; b = (a1 * a2) / a3; Unsigned Multiplication: Signed Multiplication: MUL Rd,Rn,Op2 UMULL RdLo,RdHi,Rn,Rm SMULL Rdlo,Rdhi,Rm,Rn Unsigned Division: Signed Division: UDIV Rd, Rm, Rn SDIV Rd,Rm,Rn
9
Time Delays in C You can use for to make time delay
void delay(void) { volatile unsigned int i; for(i = 0; i < 42150; i++) { } } If you use for loop The clock frequency can change your delay duration! The compiler has direct effect on delay duration!
10
Bit-wise logical operators
& | ~
11
Shift operations in C data >> number of bits to be shifted right
data << number of bits to be shifted left >> 3 <<2
12
Setting a bit in a Byte to 1
We can use | operator to set a bit of a byte to 1 xxxx xxxx | xxx1 xxxx xxxx xxxx | 1 << 4 xxx1 xxxx OR GPIOA->ODR |= (1<<4); //set bit 4 (5th bit) of GPIOA->ODR
13
Clearing a bit in a Byte to 0
We can use | operator to set a bit of a byte to 1 xxxx xxxx & xxx0 xxxx xxxx xxxx & ~(1 << 4) xxx0 xxxx OR GPIOB->ODR &= ~(1<<4); //clear bit 4 (5th bit) of GPIOB->ODR
14
if( ((GPIOA->IDR & (1<<5)) != 0) //check bit 5 (6th bit)
Checking a bit in a Byte We can use & operator to see if a bit in a byte is 1 or 0 xxxx xxxx & x 0000 xxxx xxxx & (1 << 4) x0 0000 OR if( ((GPIOA->IDR & (1<<5)) != 0) //check bit 5 (6th bit)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.