编写用户定义的函数和'for loop' [英] Writing user-defined functions and 'for loop'

查看:58
本文介绍了编写用户定义的函数和'for loop'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我的学校作业遇到了麻烦,因为没有一个朋友知道怎么做。因此,此代码适用于要求1,即

1. PORTC引脚7连接到LED 7,引脚0连接到LED 0

2.延迟3秒间隔每个输出之间。

基本上他们要问的是:

LED7 LED0两个LED都关闭

0 0

LED7 LED0

1:1 0闪烁5次,比率为1:1

LED7 LED0

0 2:1闪烁10次比例为2:1

LED7 LED0

1:1 1:1闪烁10次,比例为1:1



现在问题是我需要重写代码以便它可以满足我的下一个要求:

1.无需输入

2. PORTD到LED(RD0到L0,RD1到L1等)

3.编写用户定义的延迟函数

4.为特定任务编写用户自定义函数

5.使用for循环重复任务。<<<<这是我不理解的部分。



所以现在它是

L7 -L0

所有LED熄灭,无需功能

L7-L0

所有LED指示灯闪烁,闪烁,比率为1:1
L7-L0

从L0到L7运行LED,引入逻辑左移命令(<<<<>)
L7-L0

从L7运行LED到L0,引入逻辑sh * t右命令(>>)



我几乎坚持要求2我会很感激给予任何帮助..谢谢



以下是要求1的代码。

< a href =http://pastebin.com/iNkjt5iN> [C]编写用户定义的函数和'for loop' - Pastebin.com [ ^ ]



我的尝试:



我几乎坚持这个,不知道该怎么做。我很抱歉,如果这是基础知识,但我几乎是新的





< pre lang =c ++> #include < < span class =code-leadattribute> p18f4520.h >
#include < delays.h < span class =code-keyword>>

// 2.0配置控制器
#pragma config OSC = HS //设置外部高速(HS)振荡
< span class =code-preprocessor> #pragma config WDT = OFF //禁用MCU的监视功能
#pragma config LVP = OFF //禁用低电压编程(LVP)
#pragma config PBADEN = OFF //端口B模拟到数字模块是OFF

// 3.0定义:使用面向位的指令定义每个LED连接到哪个引脚
#define L0 PORTCbits.RC0 // RC0分配为L0
#define L7 PORTCbits.RC7 // RC7分配为L7

// 4.0用户定义函数

// 声明并定义1:2的长延迟
void delay_long( void
{
int x;
for (x; x <20000; x ++)
{
// 20000次不执行任何操作
}
}
// 声明并定义1:1的短延迟
void delay_short( void
{
int x;
for (x = 0 ; x< 10000; x ++)
{
// 10000次无效
}
}

// LED7(1:1)和LED0(0) $的功能b $ b void display_1( void
{
L0 = 0 ;
L7 = 1 ;
delay_short();
L7 = 0 ;
delay_short();
}

// LED7(0)和LED0的功能(2: 1)
void display_2( void
{
L7 = 0 ;
L0 = 1 ;
delay_long();
L0 = 0 ;
delay_short();
}

// 5.0主程序启动
void main( void
{
// 5.1声明变量
int i;

// 5.2配置I / O端口
TRISCbits。 TRISC0 = 0 ; // 仅配置RC0作为输出
TRISCbits.TRISC7 = 0 ; // 仅配置RC7作为输出

// 5.3初始化变量和输出端口
i = 0 ; // 将0分配给interger i
PORTCbits.RC0 = 0 ; // 仅使用按位指令清除RC0位
PORTCbits.RC7 = 0 ; // 仅使用按位指令清除RC7位

// 无限循环
while 1
{
L0 = 0 ; // 关闭两个LED
L7 = 0 ;

Delay10KTCYx( 3000 ); // 3秒延迟@ 4Mhz振荡器

for (i = 0 ; i< 5; ++) // 循环5次
{
display_1(); // 调用display_1()函数
}
Delay10KTCYx( 3000 ); // 3秒延迟@ 4Mhz振荡器

for (i = 10 ; i< 10; i ++) // 循环10次
{
display_2(); // 调用display_2()函数
}

Delay10KTCYx ( 3000 ); // 3秒延迟@ 4Mhz振荡器

for (i = 0 ; i< 10; i ++) // 循环10次
{
L0 = 1 ;两个LED上的 //
L7 = 1 ;
delay_short(); // 调用delay_short()函数
L0 = 0 ; // 关闭LEDS
L7 = 0 ;
delay_short(); // 调用delay_short()函数
}
} // while循环结束
} // 主程序结束

解决方案

  for (i =  0 ; i< 5; ++) //  循环5次 
{
display_1(); // 调用display_1()函数
}



这个循环不起作用,与其他循环比较发现错误。



正确的缩进有助于阅读代码,使用它。



对于用户定义的功能:

- 你没有语言文件?

- 你什么都没有当然?

- 你在Google上什么都没有?

- 你没有互联网教程?


我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。

亲自尝试,您可能会发现它并不像您想象的那么难! />


所以从头开始,看看你已经拥有的代码是什么。 (你说我需要重写代码以便它......所以你有现有的代码可以给你一个起点。从那里,编写代码使LED显示一个特定的状态:全部或许。然后改变它显示所有关闭。然后左开LED。右边。

这两个检查你知道该怎么做,一切正常。

从那里继续对于实际的任务,如果你阅读你的课程材料/笔记应该很明显。



如果你遇到一个具体问题,那么请问一下(告诉我们)你的代码到目前为止)我们会尽力帮助你。但我们不会为你做这一切!


Hello ! I'm having trouble with my school homework as none of my friends know how to do this . So this code is for requirement 1 which is
1. PORTC pin 7 is connected to LED 7 and pin0 is connected to LED 0
2. A delay of 3 second interval between each output.
Basically what they're asking is :
LED7 LED0 Both LEDS off
0 0
LED7 LED0
1:1 0 Blink 5 times with ratio of 1:1
LED7 LED0
0 2:1 Blink 10 times with ratio of 2:1
LED7 LED0
1:1 1:1 Blink 10 times with ratio of 1:1

Now the problem is i need to rewrite the code so that it will do this for my next requirement :
1. No input are required
2. PORTD to LEDs (RD0 to L0 , RD1 to L1 and so on )
3. Write user-define delay functions
4. write user-defind function for the specific tasks
5. Use "for' loop to repeat a task. <<<< this is the part I don't understand.

So now its
L7-L0
All LEDs off, No function require
L7-L0
All LEDs Blinking , Blink with ratio of 1:1
L7-L0
Running LED from L0 to L7 , Introduce logical shift left command (<<)
L7-L0
Running LED from L7 to L0 , Introduce logical sh*t right command (>>)

Im pretty much stuck at requirement 2 and i would appreciate any help given .. Thanks

Here is the code for requirement 1 .
[C] writing user-defined functions and 'for loop' - Pastebin.com[^]

What I have tried:

I'm pretty much stuck with this and don't know what to do . I'm sorry if this is the basics but im pretty much new to this

[EDIT - the code from the link]

#include <p18f4520.h>
#include <delays.h>

//2.0 Configure Controller
#pragma config OSC = HS //set external High-speed (HS) oscillation
#pragma config WDT = OFF //disable the Watchdog feature of the MCU
#pragma config LVP = OFF //disable low voltage programming (LVP)
#pragma config PBADEN = OFF //Port B Analogue to Digital module is OFF

//3.0 Definition: define which pin each LED is connected to using bit-oriented instruction
#define L0 PORTCbits.RC0 //RC0 assigned as L0
#define L7 PORTCbits.RC7 //RC7 assigned as L7

//4.0 user define functions

//declare and define a long delay for 1:2
void delay_long(void)
{
	int x;
	for(x; x<20000;x++)
	{
		// do nothing for 20000 times
	}
}
//declare and define a short delay for 1:1
void delay_short(void)
{
	int x;
	for(x=0; x<10000;x++)
	{
		//do nothing for 10000 times
	}
}

//function for LED7 (1:1) and LED0 (0)
void display_1(void)
{
	L0=0;
	L7=1;
	delay_short();
	L7=0;
	delay_short();
}

//function for LED7 (0) and LED0 (2:1)
void display_2(void)
{
	L7=0;
	L0=1;
	delay_long();
	L0=0;
	delay_short();
}

//5.0 start of main program
void main(void)
{
	//5.1 Declare variables
	int i;

	//5.2 configure I/O ports
	TRISCbits.TRISC0 =0; //configure only RC0 as output
	TRISCbits.TRISC7 =0; //configure only RC7 as output

	//5.3 initialize variables and output ports
	i=0; //assign 0 to interger i
	PORTCbits.RC0 = 0; // clear bit RC0 only using a bitwise instruction
	PORTCbits.RC7 = 0; // clear bit RC7 only using a bitwise instruction

	// infinite loop
	while(1)
	{
		L0=0; //off both LEDs
		L7=0;

		Delay10KTCYx(3000); // 3 sec delay @4Mhz oscillator

		for(i=0; i<5;++) // loop 5 times
		{
			display_1(); //call the display_1() function
		}
		Delay10KTCYx(3000); //3 sec delay @4Mhz oscillator

		for(i=10; i<10; i++) // loop 10 times
		{
			display_2(); //call the display_2() function
		}

		Delay10KTCYx(3000); // 3 sec delay @4Mhz oscillator

		for(i=0; i<10;i++) //loop 10 times
		{
			L0=1; //on both LEDS
			L7=1;
			delay_short(); //call delay_short() function
			L0=0; //off both LEDS
			L7=0;
			delay_short(); //call delay_short() function
		}
	} //end of while loop
} //end of main program

解决方案

for(i=0; i<5;++) // loop 5 times
{
    display_1(); //call the display_1() function
}


This loop will not work, compare with other loops to spot the error.

Proper indentation helps reading the code, use it.

For user define functions:
- You have no language documentation ?
- you have nothing in course?
- you have nothing on Google ?
- you have no tutorial on internet ?


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!

So start at the beginning, and look at what the code you already have does. (You say " i need to rewrite the code so that it..." so you have existing code which will give you a starting point. From there, write code to make the LED's show a particular state: All on perhaps. Then change it to show all off. Then left LED on, right off.
This both checks that you know what to do, and that everything works.
From there, move on to the actual task and it should be pretty obvious if you read your course material / notes.

If you meet a specific problem, then please ask about that (showing us your code so far) and we will do our best to help. But we aren't going to do it all for you!


这篇关于编写用户定义的函数和'for loop'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆