Ppt#07 GPIO C-Programming
Ppt#07 GPIO C-Programming
SUBJECT-EMBEDDED SYSTEMS
BATCH-22CS (6TH SEMESTER THIRD YEAR)
On-board LED
Toggling LED
Examples
2
C Compiler Data Types
3
Data Types Examples
4
Constant/Literal values
Decimal is the default number format
int m, n; //16-bit signed numbers
m = 453; n = -25;
Hexadecimal: preface value with 0x or 0X
m = 0xF312; n = -0x12E4;
Octal: preface value with zero (0)
m = 0453; n = -023;
Don’t use leading zeros on “decimal” values. They will be interpreted as octal.
Character: character in single quotes, or ASCII value following “slash”
m = ‘a’; //ASCII value 0x61
n = ‘\13’; //ASCII value 13 is the “return” character
String (array) of characters:
unsigned char k[7];
strcpy(m,“hello\n”); //k*0+=‘h’, k*1+=‘e’, k*2+=‘l’, k*3+=‘l’, k*4+=‘o’,
//k*5+=13 or ‘\n’ (ASCII new line character),
//k*6+=0 or ‘\0’ (null character – end of string)
5
C Statement Types
Simple variable assignments
Includes input/output data transfers
Arithmetic operations
Logical operators
shift operators
Relational operators
Boolean operators
Control structures
IF, WHEN, FOR, SELECT
Function calls
User-defined and/or library functions
6
Arithmetic Operations
C examples – with standard arithmetic operators
int i, j, k; // 32-bit signed integers
uint8_t m, n, p; // 8-bit unsigned numbers
i = j + k; // add 32-bit integers
m = n - 5; // subtract 8-bit numbers
j = i * k; // multiply 32-bit integers
m = n / p; // quotient of 8-bit divide
m = n % p; // remainder of 8-bit divide
i = (j + k) * (i – 2); //arithmetic expression
*, /, % are higher in precedence than +, - (higher precedence
applied 1st)
Example:
j * k + m / n = (j * k) + (m / n)
7
Bit-Parallel Logical Operations
Bit-parallel (bitwise) logical operators produce n-bit results of the
corresponding logical operation:
& (AND) | (OR) ^ (XOR) ~ (Complement)
8
Bit-Set/Reset/Complement/Test
Use a “mask” to select bit(s) to be altered.
9
Shift Operators
x >> y (right shift operand x by y bit positions)
x << y (left shift operand x by y bit positions)
Vacated bits are filled with 0’s.
Shift right/left fast way to multiply/divide by power of 2
10
Relational Operators
Test relationship between two variables/expressions
11
Boolean Operators
&& (AND) and || (OR)
Produce TRUE/FALSE results when testing multiple TRUE/FALSE
conditions.
if ((n > 1) && (n < 5)) //test for n between 1 and 5
if ((c = ‘q’) || (c = ‘Q’)) //test c = lower or upper case Q
Note the difference between Boolean operators &&, || and
bitwise logical operators &, |
if ( k && m) //test if k and m both TRUE (non-zero values)
if ( k & m) //compute bitwise AND between m and n,
//then test whether the result is non-zero (TRUE)
12
C Control Structures
Control order in which instructions are executed (program flow)
Conditional execution
Execute a set of statements if some condition is met
Select one set of statements to be executed from several
options, depending on one or more conditions
Iterative execution
Repeated execution of a set of statements
A specified number of times, or
Until some condition is met, or
While some condition is true
13
IF-THEN Structures
Execute a set of statements if and only if some
condition is met.
14
IF-THEN-ELSE Structures
Execute one set of statements if a condition is met and an
alternate set if the condition is not met.
15
WHILE Loop Structures
Repeat a set of statements (a “loop”) as long as some condition
is met.
16
WHILE Loop Structure Example
17
FOR Loop Structure
Repeat a set of statements (one “loop”) while some condition is
met.
often a given # of iterations
18
FOR Loop Structure Example
Read 100 16-bit values from GPIOB into array C
Bit 0 of GPIOA (PA0) is 1 if data is ready, and 0 otherwise
uint16_t c[100];
uint16_t k;
for (k = 0; k < 200; k++) {
while ((GPIOA->IDR & 0x01) == 0) //repeat until PA0 = 1
{} //do nothing if PA0 = 0
c[k]=GPIOB->IDR; //read data from PB[15:0]
}
19
C Functions
Functions partition large programs into a set of smaller tasks
Helps manage program complexity
Smaller tasks are easier to design and debug
Functions can often be reused instead of starting over
Can use of “libraries” of functions developed by 3rd parties,
instead of designing your own
A function is “called” by another program to perform a task
The function may return a result to the caller
One or more arguments may be passed to the
function/procedure
20
C Functions Definition
21
C Functions Example
22
Cortex-M0 ARM “Header File”
Keil MDK-ARM provides a derivative-specific “header file” for
each microcontroller, which defines memory addresses and
symbolic labels for CPU and peripheral function register
addresses.
CMSIS Cortex-M0 Device Peripheral Access Layer Header File is
#include “stm32f030x8.h”
This file contains all the peripheral register's definitions, bits
definitions and memory mapping for STM32F0xx devices.
This file contains:
Data structures and the address mapping for all peripherals
Peripheral's registers declarations and bits definition
Macros to access peripheral’s registers hardware
23
Example of General Purpose I/O Registers address definitions in stm32f030x8.h
typedef struct
{
__IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */
__IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */
__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */
__IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */
__IO uint32_t BSRR; /*!< GPIO port bit set/reset register, Address offset: 0x1A */
__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /*!< GPIO alternate function low register, Address offset: 0x20-0x24 */
__IO uint32_t BRR; /*!< GPIO bit reset register, Address offset: 0x28 */
} GPIO_TypeDef;
24
Example of Reset and Clock Control Registers address definitions
in stm32f030x8.h
typedef struct
{
__IO uint32_t CR; /*!< RCC clock control register, Address offset: 0x00 */
__IO uint32_t CFGR; /*!< RCC clock configuration register, Address offset: 0x04 */
__IO uint32_t CIR; /*!< RCC clock interrupt register, Address offset: 0x08 */
__IO uint32_t APB2RSTR; /*!< RCC APB2 peripheral reset register, Address offset: 0x0C */
__IO uint32_t APB1RSTR; /*!< RCC APB1 peripheral reset register, Address offset: 0x10 */
__IO uint32_t AHBENR; /*!< RCC AHB peripheral clock register, Address offset: 0x14 */
__IO uint32_t APB2ENR; /*!< RCC APB2 peripheral clock enable register, Address offset: 0x18 */
__IO uint32_t APB1ENR; /*!< RCC APB1 peripheral clock enable register, Address offset: 0x1C */
__IO uint32_t BDCR; /*!< RCC Backup domain control register, Address offset: 0x20 */
__IO uint32_t CSR; /*!< RCC clock control & status register, Address offset: 0x24 */
__IO uint32_t AHBRSTR; /*!< RCC AHB peripheral reset register, Address offset: 0x28 */
__IO uint32_t CFGR2; /*!< RCC clock configuration register 2, Address offset: 0x2C */
__IO uint32_t CFGR3; /*!< RCC clock configuration register 3, Address offset: 0x30 */
__IO uint32_t CR2; /*!< RCC clock control register 2, Address offset: 0x34 */
} RCC_TypeDef;
Home Work:
1. Write a C-program to write High logic on all bits of PORTB
2. Write a C-program to write High logic on bit8-bit11 of PORTA.
27
GPIO C-Programming
Example#02: Write a C program for stm32f030R8 cortex-M0 ARM to
toggle PC8 with a delay of 1sec.
Steps
1. Enable the clock for GPIOC using RCC_AHBENR register (19th bit of
the register).
2. Set the PC8 as output in MODER Register (GPIOC->MODER) of (01 for
output).
3. Write HIGH(1) to PC8 in Output Data Register (GPIOC->ODR).
2. Call a delay function,
3. write LOW to PC8 in ODR.
4. Call a delay function
5. Repeat step 3 to 4
28
GPIO C-Programming
#include "stm32f030x8.h" //header file
void delay1s()
{
unsigned int i;
for(i=0; i<1000000; i++);
}
int main()
{
RCC->AHBENR |= (1<<19); //enable the clock for GPIO ports (A, B & C)
GPIOC->MODER |=(1<<8*2); //Set the PC8 as a output, can be done by (1<<16)
while(1)
{
GPIOC->ODR |=(1<<8); //set PC8 (LED ON)
delay1s(); //Call delay function
GPIOC->ODR |= (0<<8); //reset PC8 (LED OFF)
delay1s(); //Call delay function
}}
29
GPIO C-Programming
Toggling LED PTC9 using XOR(^) operation
#include "stm32f030x8.h" //header file
void delay1s()
{
unsigned int i;
for(i=0; i<1000000; i++);
}
int main()
{
RCC->AHBENR=(1<<19); //enable the clock for GPIO port C
GPIOC->MODER=(1<<8*2); //Set the PC8 as a output
while(1)
{
GPIOC->ODR ^=(1<<8); //toggling PC8 (LED ON/OFF)
delay1s(); //Call delay function
}} 30
GPIO C-Programming
Home Work:
2. Write a C program to toggle all bits of PORTC.
31
GPIO C-Programming
Example#03: Write a C program for stm32f030R8 cortex-M0 ARM to
read the switch connected on PA0 and display it on the LED on PC9.
#include "stm32f030x8.h"
int main()
{
RCC->AHBENR=0x00E0000; //Enable clock for port A-C
GPIOC->MODER |=(1<<9*2); //output mode on PC9
GPIOA->MODER=0x28000000; //input mode on portA
while(1)
{
if((GPIOA->IDR & 0x0001)==0x0001) //read the switch
GPIOC->ODR =0x0200; //set PC9
else
GPIOC->ODR =0x0000; //clear PC9
}} 32
Bit Definition for GPIO Registers
MODER Register
Set the input, output, alternate and Analog mode of GPIO ports (A to
D and F)
Follow the following syntax for setting one of the above mode of
PORTS
1. Input mode of (a) bit (s)
GPIOx->MODER &= GPIO_MODER_MODERy
where: x is port name (A-D & F)
y is pin number (0-15)
33
Bit Definition for GPIO Registers
MODER Register
2. Output mode
GPIOx->MODER |= GPIO_MODER_MODERy_z
Example: To set the output mode on pin#5 & pin#9 of PORTC (PC5
&PC9)
GPIOC->MODER |= GPIO_MODER_MODER5_0|GPIO_MODER_MODER9_0
3. Alternate function mode
GPIOx->MODER |= GPIO_MODER_MODERy_z
Example: To set the alternate function mode on pin#4 of PORTB (PB4).
GPIOB->MODER |= GPIO_MODER_MODER4_1
4. Analog mode
GPIOx->MODER |= GPIO_MODER_MODERy
Example: To set the alternate analog mode on pin#2 of PORTB
(PB2).
GPIOB->MODER |= GPIO_MODER_MODER2 34
Bit Definition for GPIO Registers
Output Data Register (ODR)
GPIOx->ODR |= GPIO_ODR_pin#
Example: To write high on pin#8 & pin#9 of PORTC (PC8 & PC9)
GPIOC->ODR |= GPIO_ODR_8|GPIO_ODR_9
35
Bit definition for RCC register 36
RCC_AHBENR register
RCC->AHBENR |= RCC_AHBENR_GPIOxEN (where x is PORT name)
Example:
To enable the clock for GPIOA (PORTA)
RCC->AHBENR |= RCC_AHBENR_GPIOAEN
To enable the clock for GPIOA & GPIOC
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOCEN
RCC_APB2ENR register
RCC->APB2ENR |= RCC_APB2ENR_spyEN
Where: sp is any special purpose peripheral such as USART, SPI, I2C, TIM
y is number of peripherals
Example: To enable the clock for SPI1
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN
To enable the clock for USART1 : RCC->APB2ENR |= RCC_APB2ENR_USART1EN
GPIO C-Programming
Example#04
On-board LED (PC8) Example#01 using bit-wise Register operation
#include "stm32f030x8.h"
int main()
{
RCC->AHBENR|=RCC_AHBENR_GPIOCEN; //Enable clock for PORT C
GPIOC->MODER |=GPIO_MODER_MODER8_0; //output mode on PC8
37
GPIO C-Programming
Example#05
Toggling PC8 & PC9 (on-board LEDs) using bit-wise register operation.
#include "stm32f030x8.h"
void delay1s()
{ int i;
for(i=0;i<=1000000; i++);
}
int main()
{
RCC->AHBENR|=RCC_AHBENR_GPIOCEN;
GPIOC->MODER|=GPIO_MODER_MODER9_0|GPIO_MODER_MODER8_0;
while(1)
{
GPIOC->ODR ^=(GPIO_ODR_9 | GPIO_ODR_8) ; //Toggling PC8 & PC9 bits
delay1s();
38
}}
GPIO C-Programming
Example#06: Write a C program for stm32f030R8 cortex-M0 ARM for
common-anode 7-segment Display connected to GPIOA(PA7-PA0).
Segment patterns for decimal digits are given in following table.
Segment patterns for the 10 decimal digits for a common anode 7-seg LED Display
Decimal PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 Hex value
value . g f e d c b a
0 1 1 0 0 0 0 0 0 0xC0
1 1 1 1 1 1 0 0 1 0xF9
2 1 0 1 0 0 1 0 0 0xA4
3 1 0 1 1 0 0 0 0 0xB0
4 1 0 0 1 1 0 0 1 0x99
5 1 0 0 1 0 0 1 0 0x92
6 1 0 0 0 0 0 1 0 0x82
7 1 1 1 1 1 0 0 0 0xF8
8 1 0 0 0 0 0 0 0 0x80
9 1 0 0 1 0 0 0 0 0x90
39
GPIO C-Programming
#include "stm32f030x8.h“
void delay1s()
{ int i;
for(i=0;i<=1000000; i++);
}
int main()
{
RCC->AHBENR|=RCC_AHBENR_GPIOAEN; //Enable clock for PORT A
GPIOA->MODER |=0x00005555; //output mode on bits0 to bits7(PA0 to PA7)
while(1)
{ GPIOA->ODR=0xC0; //display zero(0)
delay1s();
GPIOA->ODR=0xF9; //display 1
delay1s();
GPIOA->ODR=0xA4; //display 2
delay1s();
GPIOA->ODR=0xB0; //display 3
40
delay1s();
GPIO C-Programming
GPIOA->ODR=0x99; //display 4
delay1s();
GPIOA->ODR=0x92; //display 5
delay1s();
GPIOA->ODR=0x82; //display 6
delay1s();
GPIOA->ODR=0xF8; //display 7
delay1s();
GPIOA->ODR=0x80; //display 8
delay1s();
GPIOA->ODR=0x90; //display 9
delay1s();
} }
HOME WORK:
3.Write a C program for stm32f030R8 cortex-M0 ARM for common-cathode 7-
segment Display. 41
GPIO C-Programming
2nd Method of 7-segment Display (Example#07) using Array of integer
#include "stm32f030x8.h“
void delay1s()
{ int i;
for(i=0;i<=1000000; i++);
}
int main()
{uint8_t display_pattern[10]= {0xC0,0xF9, 0xA4, 0xB0, 0x99, 0x92,
0x82,0xF8,0x80,0x90};
unsigned int k;
RCC->AHBENR|=RCC_AHBENR_GPIOAEN; //Enable clock for PORT A
GPIOA->MODER |=0x00005555; //output mode on bits0 to bits7(PTA0 to PTA7)
while(1)
{ for (k=0; k<=9; k++)
{ GPIOC->ODR=display_pattern[k]; //display zero(0)
delay1s(); }
42
} }
GPIO C-Programming
Example#07: A pushbutton is connected to PB5, and a LED is
connected to PC8. Implement a C-program for the STM32F030
Cortex-M0+ ARM processor to perform the following:
i. Continuously monitor the PB5 bit until it becomes HIGH.
ii. When PB5 becomes HIGH, write your Roll Number in
hexadecimal to PORTA, and send a HIGH-to-LOW pulse to PC8.
iii. When PB5 becomes LOW, write the value $15 to PORTA.
46
Thanks
47