You are on page 1of 14

EMBEDDED SYSTEM COURSE REPORT

专业/Major: COMPUTER SCIENCE AND


TECNOLOGY
年级/Grade: 2020
学号/ID No.: 202053080033
姓名/Name:GOORE BI DJE JEAN ROMAIN
DESIRE

Experiment 1: Illuminate LED Light

Code:
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED PBout(5)
void LED_Init(void);
#endif
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
main.c
#include "sys.h"
#include "led.h"
int main(void)
{
LED_Init();
LED=0;
}

Explanation:
This code is used to control an LED on an embedded system. The code is divided into three
files: led.h, led.c, and main.c.
Here's a breakdown of what each file does:
1. led.h: This is the header file for the LED control code. It includes a guard (#ifndef
__LED_H) to prevent multiple inclusions of the same header file, which could lead to
redefinition errors. It includes another header file "sys.h" which likely contains
system-level definitions and functions. It defines a macro LED as PBout(5), which is
probably a macro for accessing the output register of a specific GPIO pin (in this case,
pin 5). It also declares a function LED_Init(void) which is defined in led.c.
2. led.c: This is the implementation file for the LED control code. It includes the header
file led.h and defines the function LED_Init(void). This function initializes the GPIO
pin connected to the LED. It sets up the GPIO pin as an output pin with a push-pull
configuration and a speed of 50MHz. It then sets the GPIO pin high, which would turn
the LED on if it's active high.
3. main.c: This is the main program file. It includes the header files "sys.h" and
"led.h". In the main function, it calls LED_Init() to initialize the LED. The code
seems to be incomplete as LED= is not a complete statement.
The exact behavior of this code may vary depending on the specific microcontroller being
used and the definitions in "sys.h". The PBout(5), GPIO_InitTypeDef,
RCC_APB2PeriphClockCmd, GPIO_Init, and GPIO_SetBits are likely specific to the
microcontroller and its peripheral library.

Experiment 2: Flowing Water Lamp

Code:
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PBout(5)
#define LED1 PEout(5)
void LED_init(void);
#endif
led.c
#include "led.h"
void LED_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|
RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
int main(void)
{
delay_init();
LED_init();while(1)
{
LED0=0;
LED1=1;
delay_ms(300);
LED0=1;
LED1=0;
delay_ms(300);
}
}

Explanation
This code is for an embedded system that controls two LEDs, creating a "flowing water lamp"
effect. The LEDs are connected to GPIO pin 5 of ports B and E, respectively. The LEDs
alternately turn on and off, creating a visual effect similar to flowing water.
Here's a breakdown of each file:
1. led.h: This is the header file for the LED control code. It includes a guard (#ifndef
__LED_H) to prevent multiple inclusions of the same header file. It includes another
header file "sys.h" which likely contains system-level definitions and functions. It
defines two macros LED0 and LED1 as PBout(5) and PEout(5), which are probably
macros for accessing the output register of specific GPIO pins. It also declares a
function LED_init(void) which is defined in led.c.
2. led.c: This is the implementation file for the LED control code. It includes the header
file led.h and defines the function LED_init(void). This function initializes the GPIO
pins connected to the LEDs. It sets up both GPIO pins as output pins with a push-pull
configuration and a speed of 50MHz. It then sets both GPIO pins high, which would
turn the LEDs on if they're active high.
3. main.c: This is the main program file. It includes the header files "sys.h", "delay.h",
"usart.h", and "led.h". In the main function, it calls delay_init() to initialize the
delay function and LED_init() to initialize the LEDs. Then, in an infinite loop, it
alternately turns on and off the LEDs with a delay of 300 milliseconds between each
state change. This creates the "flowing water lamp" effect.

Experiment 3: Buzzer
Code:
beep.h
#ifndef __BEEP_H
#define __BEEP_H
#include "sys.h"
#define BEEP PBout(8)
void BEEP_Init(void);
#endif
beep.c
#include "beep.h"
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
}
main.c
#include "sys.h"
#include "delay.h"
#include "beep.h"
int main(void)
{
delay_init();
BEEP_Init();
while(1)
{
BEEP=0;
delay_ms(300);
BEEP=1;
delay_ms(300);
}}
Explanation

This code is for an embedded system that controls a buzzer. The buzzer is connected to GPIO
pin 8 of port B. The buzzer alternately turns on and off, creating a beeping sound.
Here's a breakdown of each file:
1. beep.h: This is the header file for the buzzer control code. It includes a guard (#ifndef
__BEEP_H) to prevent multiple inclusions of the same header file. It includes another
header file "sys.h" which contains system-level definitions and functions. It defines a
macro BEEP as PBout(8), which is probably a macro for accessing the output register
of a specific GPIO pin. It also declares a function BEEP_Init(void) which is defined
in beep.c.
2. beep.c: This is the implementation file for the buzzer control code. It includes the
header file beep.h and defines the function BEEP_Init(void). This function initializes
the GPIO pin connected to the buzzer. It sets up the GPIO pin as an output pin with a
push-pull configuration and a speed of 50MHz. It then sets the GPIO pin low, which
would turn the buzzer off if it's active high.
3. main.c: This is the main program file. It includes the header files "sys.h", "delay.h",
and "beep.h". In the main function, it calls delay_init() to initialize the delay
function and BEEP_Init() to initialize the buzzer. Then, in an infinite loop, it
alternately turns on and off the buzzer with a delay of 300 milliseconds between each
state change. This creates a beeping sound.

Experiment 4: Keyboard
Code:
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PBout(5)
#define LED1 PEout(5)
void LED_Init(void);
#endif
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|
RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
key.c
#include "stm32f10x.h"
#include "sys.h"
#include "key.h"
#include "delay.h"
void KEY_Init(void)
{
GPIO_InitTypeDef
GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOE,&GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)
key_up=1;
if(key_up&&(KEY0==0||KEY1==0))
{
delay_ms(10);
key_up=0;
if(KEY0==0)
return KEY0_PRES;
else if(KEY1==0)
return KEY1_PRES;
}
else if(KEY0==1&&KEY1==1)
key_up=1;
return 0;
}m
ain.c
#include "sys.h"
#include "key.h"
#include "delay.h"
#include "led.h"
int main(void)
{
vu8 key=0;
delay_init();
LED_Init();
KEY_Init();
LED0=0;
LED1=0;
while(1)
{
key=KEY_Scan(0);
if(key){
switch(key)
{
case KEY1_PRES:
LED1=!LED1;
break;
case KEY0_PRES:
LED0=!LED0;
break;
}
}
else delay_ms(10);
}
}

Explanation
This code is for an embedded system that controls two LEDs and reads the state of two keys
(buttons). The LEDs are connected to GPIO pin 5 of ports B and E, respectively, and the keys
are connected to GPIO pins 3 and 4 of port E. When a key is pressed, the corresponding LED
toggles its state (on to off, or off to on).
Here's a breakdown of each file:
1. led.h and led.c: These files are the same as in your previous experiments. They define
the initialization and control of two LEDs connected to GPIO pin 5 of ports B and E.
2. key.c: This file contains the code for initializing and scanning the keys. The
KEY_Init(void) function initializes the GPIO pins connected to the keys as input pins
with pull-up resistors. The KEY_Scan(u8 mode) function scans the state of the keys.
If a key is pressed, it debounces the key press with a delay of 10 milliseconds and
returns a value indicating which key was pressed (KEY0_PRES or KEY1_PRES). If
no key is pressed, it returns 0.
3. main.c: This is the main program file. It includes the header files "sys.h", "key.h",
"delay.h", and "led.h". In the main function, it initializes the delay function, the
LEDs, and the keys. Then, in an infinite loop, it scans the state of the keys. If a key is
pressed, it toggles the state of the corresponding LED. If no key is pressed, it
introduces a delay of 10 milliseconds.

Experiment 5: Breathing Light


Code:
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM3_PWM_Init(u16 arr,u16 psc);
#endif
timer.c
#include "timer.h"
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|
RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);TIM_OC2PolarityConfig(TIM3,TIM_OCPrelo
ad_Enable);
TIM_Cmd(TIM3,ENABLE);
}
main.c
#include "delay.h"
#include "sys.h"
#include "timer.h"
int main()
{
u16 led0pwmval=0;
u8 dir=1;
delay_init();
TIM3_PWM_Init(899,0);
while(1)
{
delay_ms(10);
if(dir)
led0pwmval++;
else
led0pwmval--;
if(led0pwmval>300)
dir=0;
if(led0pwmval==0)
dir=1;
TIM_SetCompare2(TIM3,led0pwmval);
}
}

Explanation

This code is for an embedded system that controls an LED to create a "breathing light" effect.
The LED is connected to GPIO pin 5 of port B. The brightness of the LED is controlled by a
PWM (Pulse Width Modulation) signal generated by timer 3. The duty cycle of the PWM
signal is gradually increased and decreased, causing the LED to slowly brighten and dim,
creating the "breathing" effect.
Here's a breakdown of each file:
1. timer.h: This is the header file for the timer control code. It includes a guard (#ifndef
__TIMER_H) to prevent multiple inclusions of the same header file. It includes
another header file "sys.h" which likely contains system-level definitions and
functions. It declares two functions TIM3_Int_Init(u16 arr,u16 psc) and
TIM3_PWM_Init(u16 arr,u16 psc) which are defined in timer.c.
2. timer.c: This is the implementation file for the timer control code. It includes the
header file timer.h and defines the function TIM3_PWM_Init(u16 arr,u16 psc).
This function initializes timer 3 to generate a PWM signal on GPIO pin 5 of port B.
The frequency and duty cycle of the PWM signal are determined by the parameters
arr and psc.
3. main.c: This is the main program file. It includes the header files "delay.h", "sys.h",
and "timer.h". In the main function, it initializes the delay function and the PWM
signal. Then, in an infinite loop, it gradually increases and decreases the duty cycle of
the PWM signal with a delay of 10 milliseconds between each change. This changes
the brightness of the LED, creating the "breathing light" effect.

You might also like