2338 views|8 replies

95

Posts

0

Resources
The OP
 

[RISC-V MCU CH32V103 Review] LED lights flash alternately [Copy link]

 This post was last edited by jennyzhaojie on 2021-1-26 00:20

The only peripheral resource on board is the LED light, and its circuit schematic is shown in Figure 1. Let's make good use of it.

Figure 1 LED circuit schematic

In fact, the main purpose of using LED lights is to learn how to use I/O. What is worth mentioning is that these two LED lights do not occupy fixed resources, which means they can be connected wherever they are needed.

So how to achieve the alternating flashing of LED lights?

The idea is:

1) Set the two I/O ports as output and connect them to the two LED lights. Here, PA0 controls LED1 and PA1 controls LED2.

2) Use variables i and j to control the output of high and low levels, and stagger the high and low level outputs of the two I/Os in time. When i=0, the statement for PA0 to output a high level is GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i==0) ? (i=Bit_SET):(i=Bit_RESET)), and when j=1, the statement for PA0 to output a low level is GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i==0) ? (i=Bit_SET):(i=Bit_RESET));

3) Use the delay function to switch the high and low level output states of the two LED lights on time. The statement for switching the high and low level output is GPIO_WriteBit(GPIOx, GPIO_Pin_y, (z==0) ? (z=Bit_SET):(z=Bit_RESET)), and the delay function is Delay_Ms();

The function to configure PA0 and PA1 as output ports is:

void GPIO_Toggle_INIT(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOA, &GPIO_InitStructure);
}

The main program is:

int main(void)
{
 u8 i=0,j=1;
 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
   Delay_Init();
 GPIO_Toggle_INIT();
   //GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_SET);
   //GPIO_WriteBit(GPIOA, GPIO_Pin_1,Bit_RESET);
 while(1)
   {
 Delay_Ms(250);
 GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i==0) ? (i=Bit_SET):(i=Bit_RESET));
 GPIO_WriteBit(GPIOA, GPIO_Pin_1, (j==0) ? (j=Bit_SET):(j=Bit_RESET));
 }
}

After compilation, the compilation result is shown in Figure 2.

After downloading, the running effect is shown in Figure 3.

Figure 3 Operation effect

This post is from Domestic Chip Exchange

Latest reply

I only understood part of the code, but it feels like the lights will flash alternately faster after running it.  Details Published on 2023-1-28 16:34

赞赏

1

查看全部赞赏

 
 

2

Posts

0

Resources
2
 
It is very useful for beginners. There is really not much information. You can learn
This post is from Domestic Chip Exchange

Comments

Thanks for your support  Details Published on 2021-1-25 13:55
 
 
 

95

Posts

0

Resources
3
 
kennyxuxu posted on 2021-1-25 10:38 This is very useful for beginners. There is really not much information on this. You can learn from it.

Thanks for your support

This post is from Domestic Chip Exchange
 
 
 

7422

Posts

2

Resources
4
 

Thanks for sharing, I can learn a lot from lighting a lamp

This post is from Domestic Chip Exchange

Comments

The starting point of learning!  Details Published on 2021-1-26 00:18
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

95

Posts

0

Resources
5
 
freebsder posted on 2021-1-25 22:27 Thank you for sharing, I can learn a lot from lighting a lamp

The starting point of learning!

This post is from Domestic Chip Exchange

Comments

The board is just too ugly, it's just a student's work, it doesn't have the high-end style of other development boards  Details Published on 2021-1-26 22:33
 
 
 

1942

Posts

2

Resources
6
 

Thanks for sharing, Qinheng did a good job!

This post is from Domestic Chip Exchange

Comments

It would be better if there were more peripherals, haha...  Details Published on 2021-1-27 00:32
 
 
 

7422

Posts

2

Resources
7
 
jennyzhaojie posted on 2021-1-26 00:18 The starting point of learning!

The board is just too ugly, it's just a student's work, it doesn't have the high-end style of other development boards

This post is from Domestic Chip Exchange
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

95

Posts

0

Resources
8
 
w494143467 posted on 2021-1-26 21:41 Thanks for sharing, Qinheng did a good job!

It would be better if there were more peripherals, haha...

This post is from Domestic Chip Exchange
 
 
 

33

Posts

0

Resources
9
 
I only understood part of the code, but it feels like the lights will flash alternately faster after running it.
This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list