当前位置:网站首页>Development of stm8s103f single chip microcomputer (1) lighting of LED lamp
Development of stm8s103f single chip microcomputer (1) lighting of LED lamp
2022-06-10 15:08:00 【It's an egg】
Chapter one STM8S103f SCM development LED Lights up
Preface
Who would have thought that students majoring in optoelectronics would have to learn so many different courses , lately ( Actually, it's been a while ) We learned about embedded , Although the teacher said this every day is simple , Give some first and second grade pupils a few days , They are also doing well , But only when you really get started , There are still some difficulties at the beginning of learning .
One 、 Target task
Complete the SCM STM8S103f On LED The light is on , Here I am going to take 2 A way to ,(1) That is to specify the port to control the high and low levels to light up LED, And the most basic ,(2) Use key input ,LED Light up by means of lamp output LED
Two 、 Task steps
1. Check pin
This is the first step , It's also the most basic step , As long as you are not so careless, there is no problem , Just look at the circuit diagram .

Like this is LED Lamp pin , The pin of lamp 1 is PD2, Light two PC7, And so on . except LED Outside , We also need KEY Key pin , On mission 2 And tasks 3 You need to use .

2. Task a
It is necessary to include the header file , Did you learn C The language knows , Like the board we learned , Write the program with this command at the top .
#include<iostm8s103f3.h>We want the macro to define the lighting control of each lamp , for example
#define LED1_ON() PD_ODR_bit.ODR2 = 0
#define LED2_ON() PC_ODR_bit.ODR7 = 0
#define LED3_ON() PC_ODR_bit.ODR6 = 0
#define LED4_ON() PC_ODR_bit.ODR3 = 0
#define LED1_OFF() PD_ODR_bit.ODR2 = 1
#define LED2_OFF() PC_ODR_bit.ODR7 = 1
#define LED3_OFF() PC_ODR_bit.ODR6 = 1
#define LED4_OFF() PC_ODR_bit.ODR3 = 1LED1_ON() Just give LED1 The pin is at a low level , Lighten up LED1,PD And the last 2 The object of this command is PD2 Pin ,ODR Indicates that in output mode The value written into the register is added to the corresponding pin through the latch ( That is to say PD2),bit Number of digits ,ODR2 Indicates that this pin is at ODR The... Of the output register 3 position .
OFF Macro definition and ON almost , Give the target pin a high level , bring LED The light goes out .
What pins do we need , You need to initialize these pins , Write the corresponding initialization function .
void LED_Init()
{
PD_DDR_bit.DDR2 = 1;
PD_CR1_bit.C12 = 1;
PD_CR2_bit.C22 = 1;
PC_DDR_bit.DDR7 = 1;
PC_CR1_bit.C17 = 1;
PC_CR2_bit.C27 = 1;
PC_DDR_bit.DDR6 = 1;
PC_CR1_bit.C16 = 1;
PC_CR2_bit.C26 = 1;
PC_DDR_bit.DDR3 = 1;
PC_CR1_bit.C13 = 1;
PC_CR2_bit.C23 = 1;
}viod LED_Init() Is to define a name LED_Init Function of , The first sentence of the function and the macro definition OFF It seems a bit similar , But they mean very different things .
DDR Is the data direction register ,PD_DDR_bit.DDR2 Express PD2 The data direction register of this pin , Yes 2 Two directions can be selected , If you want this to be an output , On the spot 1, Become an input , On the spot 0, Obviously , We're here to put LED The lamp acts as an output port , All four of them LED Lamp pin ODR Register set 1.
CR2 Is the port control register 1,PD_CR1_bit.C12 Express PD2 Port control register of this pin 1, It's in different DDR There are different selection modes , stay DDR Set up 1 Under the circumstances ,CR1 Is the output mode selector ,CR1 Set up 0 Analog open drain output , Set up 1 Push pull output .
CR2 Is the port control register 2,PD_CR1_bit.C12 Express PD2 Port control register of this pin 2, and 1 The same has different selection modes , stay DDR Set one ,CR2 Is the output speed selector ,CR2 Set up 0, The maximum output speed is 2MHZ, Set up 1, The maximum output speed is 10MHZ.
therefore 3 Sentence together , Give Way LED The lamp pin acts as an output , And the output mode is max 10MHZ Push pull output of .
The initialization function has been written , We can start to write the main function .
int main(void)
{
CLK_CKDIVR = 0x00;
LED_Init();
while(1)
{
LED1_OFF();
delay(1000);
LED2_OFF();
delay(1000);
LED2_ON();
LED3_OFF();
delay(1000);
LED3_ON();
delay(1000);
LED4_OFF();
delay(1000);
LED2_OFF();
LED4_ON();
delay(1000);
}
}CLK_CKDIVR Is a clock initialization function , I won't go into details here , We just need to know that it selects the entire system as 1 frequency division .
In the main function LED_Init(), take LED Lights are initialized .
stay while Apply in the dead circle LED Switch command for , A little reminder ,LED The light was on from the beginning . After burning the program , The phenomenon seen is LED1 destroy -LED2 destroy -LED2 bright -LED3 destroy -LED3 bright -LED4 destroy -LED2 destroy /LED4 bright
There is also one in the main function delay function , Is a delay function , The purpose is to prevent LED The time interval between lights on and off is too fast , Can't see the phenomenon clearly .
void delay(unsigned int ms)
{
unsigned int x,y;
for(x = ms;x>0;x--)
for(y = 1000;y>0;y--);
}3. Task 2
Use key input to control LED The lamp , Just make some deletions based on task 1
First , Delete the macro definition in task 1 , We already have the key input in task 2 to change the state , What is needed here is the initialization function of the edit key .
void KEY_Init()
{
PA_DDR_bit.DDR3 = 0;
PA_CR1_bit.C13 = 1;
PA_CR2_bit.C23 = 0;
PD_DDR_bit.DDR3 = 0;
PD_CR1_bit.C13 = 1;
PD_CR2_bit.C23 = 0;
PC_DDR_bit.DDR5 = 0;
PC_CR1_bit.C15 = 1;
PC_CR2_bit.C25 = 0;
}The key pin here PDR Register set 0, Indicates that the data direction of this pin is input . stay DDR Set up 0, That is, in the case of pin input ,CR1 Set up 1 Indicates pull-up output , Set up 0 Indicates floating output , Observe the circuit diagram of the key , Obviously, the pull-up output should be selected here . In input mode CR2 Set up 0 Indicates that external interrupts are prohibited , Set up 1 Indicates that external interrupts are allowed , There are no external interrupts , Set up 0 that will do . therefore 3 The meaning of the sentence , Set the pin corresponding to the key as the input end of the pull-up input , And external interrupts are prohibited ( Not set up KEY The initialization function is also OK , However, it is more standardized ).
In the main function , We will use the change of key state as input data , And then change LED The state of the light .
int main(void)
{
CLK_CKDIVR = 0x00;
LED_Init();
KEY_Init();
while(1)
{
if(PA_IDR_bit.IDR3==0)
{
delay(100);
if(PA_IDR_bit.IDR3==1)
{
delay(100);
PD_ODR ^= 0x04;
PC_ODR ^= 0x40;
}
}
if(PD_IDR_bit.IDR3==0)
{
delay(100);
if(PD_IDR_bit.IDR3==1)
{
delay(100);
PC_ODR ^= 0x80;
PC_ODR ^= 0x08;
}
}
if(PC_IDR_bit.IDR5==0)
{
delay(100);
if(PD_IDR_bit.IDR5==1)
{
delay(100);
PD_ODR ^= 0x04;
PC_ODR ^= 0x40;
PC_ODR ^= 0x80;
PC_ODR ^= 0x08;
}
}
}
}stay while In circulation , What I edit is a if sentence ,if Inside PA_IDR_bit.IDR3==0,IDR Is a kind of output register , Whether the pin is in input or output mode , Can read the pin status value through this register , This register is read-only . The meaning of this sentence is , If PA3( Key 1) Pressed , The next one is , If PA3 Be released , In the middle there's a delay Function to delay , Press the key 1 After being pressed and bounced up , To the corresponding LED Lamp pin status reversed .
PD_ODR ^= 0x04,0x04 yes 16 Base number , To convert to binary is 100, see ODR Register table

100,1 The corresponding is ODR2, So the corresponding pin is PD2(LED The lamp 1), Reverse its state , In the cycle, it is always flashing at a fixed frequency .
summary
That's all LED A basic illumination of , There are other ways to light up LED The lamp , Like interrupt changing state and calling library functions ( The engineering configuration of library functions is different ), It may be mentioned later .
边栏推荐
- Insight Technology a été sélectionné dans le rapport panorama des fournisseurs d'analyse de l'amour et d'informatique de la vie privée et a été évalué comme représentant des fournisseurs de solutions
- My first go program
- Overview of cann interface calling process
- CVPR 2022 oral | SCI: fast, flexible and robust low light image enhancement
- Golang uses reflection to directly copy data from one structure to another (through the same fields)
- 2022第十五届南京国际数字化工业博览会
- what‘t the meaning of “de facto“
- Wechat applet date comparison, calculation days
- CG碰撞检测 Collision Testing
- [logodetection data set processing] (1) divide the data set into training set and verification set
猜你喜欢

Docker deploys a redis cluster

Consumption mode of Message Oriented Middleware

In what scenario can we not use the arrow function?

Several reasons and solutions of virtual machine Ping failure

2022 the 15th Nanjing International Digital Industry Expo

One-way hash function

100003字,带你解密 双11、618电商大促场景下的系统架构体系

这个牛逼的低代码生成器,现在开源了!

opencv#4 手写体识别:自建训练集完美
![[registration] to solve the core concerns of technology entrepreneurs, the online enrollment of](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[registration] to solve the core concerns of technology entrepreneurs, the online enrollment of "nebula plan open class" was opened
随机推荐
This awesome low code generator is now open source!
博主自白
2022南京国际智慧工地装备展览会
Information theory and coding 2 final review BCH code
[logodetection dataset processing] (4) extract the logo area of each picture
BigDecimal removes extra 0 at the end
My first go program
这个牛逼的低代码生成器,现在开源了!
【报名】解决科技创业者核心关切,「星云计划公开课」线上招生开启
【LogoDetection 数据集处理】(3)将训练集按照类别划分为多个文件夹
如何实现erp外网连接?
Google Earth engine (GEE) - real time global 10 meter land use / land cover (LULC) data set based on S2 images
Kubernetes 1.24: 防止未经授权的卷模式转换
2022 Nanjing International Smart site equipment exhibition
反“内卷”,消息称 360 企业安全云将上线“一键强制下班”功能,电脑自动关闭办公软件
Mitm (man in the middle attack)
Notes on the second test of C language
Collision detection unity experiment code
Jaeger introduces native support for opentelemetry
A complete multi-user wechat public platform development source code, with free sharing of documents