Mu Ke of "ARM microcontroller and embedded system" -- experiment with a little LED light

Keywords: Fragment

Affected by the current epidemic situation, we can only work at home, and finally have time to play the BLAZAR - β learning board. Now, the experiment I is organized as follows for your reference

I. conditions required for the experiment

1) BLAZAR - β learning board

2) Jlink debugger

3) Codeworior10.5 software development environment

II. Experiment content

Task 1: two lights are on and off at the same time to flash

Analysis: 1) select two LED lights led4 and led7 on the board, and check the schematic diagram to see that the two LED control pins are connected to the PortC module

2) enable the clock of PortC module, configure the corresponding PortC as GPIO, and then configure the direction as output

3) according to the schematic diagram, if the LED control pin outputs high level, the LED will be on, otherwise the LED will be off

Implementation code:

void delay(void)
{
	int i,j;

	for(i=0;i<300;i++)
	    for(j=0;j<2000;j++)
	        ;
}

void led4_init(void)
{
	SIM_SCGC5 |= (1 << 11);  //enable portc clk gating
	
	PORTC_PCR3 |= (1 << 8); //config as GPIO
	PORTC_PCR4 |= (1 << 8); //config as GPIO
	PORTC_PCR5 |= (1 << 8); //config as GPIO
	
	GPIOC_PDDR |= ((1 << 3)|(1 << 4)|(1 << 5)); //direction as output
}

void led7_init(void)
{
	SIM_SCGC5 |= (1 << 11);  //enable portc clk gating
	SIM_SCGC5 |= (1 << 10);  //enable portb clk gating
	
	PORTC_PCR0 |= (1 << 8); //config as GPIO
	PORTC_PCR2 |= (1 << 8); //config as GPIO
	PORTB_PCR19 |= (1 << 8); //config as GPIO
	
	GPIOC_PDDR |= ((1 << 0)|(1 << 2)); //direction as output
	GPIOB_PDDR |= (1 << 19); //direction as output
}

void led4_and_led7_on(void)
{
	GPIOC_PDOR |= (1 << 0); //output high  Green
	GPIOC_PDOR |= (1 << 4); //output high
	delay();
	GPIOC_PDOR &= ~(1 << 0);//output low
	GPIOC_PDOR &= ~(1 << 4);//output low
	delay();
}

Task 2: two lights flash alternately

Analysis: on the basis of Experiment 1, modify the code so that you can turn on and off, and you can turn off and on

Implementation code fragment:

void led4_or_led7_on(void)
{
	GPIOC_PDOR |= (1 << 0); //output high  Green
	GPIOC_PDOR &= ~(1 << 4);//output low
	delay();
	GPIOC_PDOR |= (1 << 4); //output high
	GPIOC_PDOR &= ~(1 << 0);//output low
	delay();
}

Task 3: associate the status of two LED lights with two keys, i.e. when the key is pressed, one LED is on, and when the key is released, the LED is off

Analysis: 1) select S4 and S2 keys, and check the schematic diagram. It can be seen that S4 is connected to PortC9 pin and S2 is connected to PortC7 pin

2) configure the pins of PortC9 and PortC7 as the input direction, and then read the PDIR register to obtain the key status

3) in order to ensure the pin status of PortC9 and PortC7 is determined after the key is released, it is necessary to enable the internal pull-up resistance

Implementation code fragment:

void s4_init(void)
{
	SIM_SCGC5 |= (1 << 11);  //enable portc clk gating
	
	PORTC_PCR9 |= ((1 << 8)|(1 << 1)|(1 << 0)); //config GPIO and enable pull-up
	
	GPIOC_PDDR &= ~(1 << 9); //direction as input
}

void s2_init(void)
{
	SIM_SCGC5 |= (1 << 11);  //enable portc clk gating
	
	PORTC_PCR7 |= ((1 << 8)|(1 << 1)|(1 << 0)); //config GPIO and enable pull-up
	
	GPIOC_PDDR &= ~(1 << 7); //direction as input
}

void task3_implement(void)
{
	if((GPIOC_PDIR & (1 << 9)) == 0)
	{
		GPIOC_PDOR |= (1 << 4); //output high
	}
	else if((GPIOC_PDIR & (1 << 7)) == 0)
	{
		GPIOC_PDOR |= (1 << 0); //output high
	}
	else {
		GPIOC_PDOR &= ~(1 << 4);//output low
		GPIOC_PDOR &= ~(1 << 0);//output low
	}
}

Task 4: same as task 3, omitted

Task 5: change the flashing mode of two lights by pressing the key. For example, at the beginning, two lights flash at the same time. When pressing the key, the two lights flash alternately. Then press the key, the two lights flash at the same time

Analysis: 1) select S4 key

2) need a global variable to record key information

Implementation code fragment:

void task5_implement(void)
{
	if((GPIOC_PDIR & (1 << 9)) == 0)
		mode = !mode;
	switch(mode)
	{
	case 1:
		led4_or_led7_on();
		break;
	default:
		led4_and_led7_on();
		break;
	}
}

So far, the experiment is finished. Welcome to criticize and correct. Thank you^_^

Published 35 original articles, won praise 11, visited 40000+
Private letter follow

Posted by Jim02 on Tue, 11 Feb 2020 21:49:24 -0800