Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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]
C++
#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
Posted
Updated 25-Apr-16 6:04am
v4
Comments
CHill60 23-Apr-16 8:23am    
I've placed your code into your question - it's the preferred method of sharing code on this site.
I've also retagged your question - it's C++ not C#
We don't do homework - it defeats the point. However if it's only the for-loop that you are stuck with then read this C++ for loop[^]
Patrice T 23-Apr-16 8:34am    
I re-indented the code for readability.
Mohibur Rashid 25-Apr-16 19:41pm    
And this is c not c++, and there is no c++ compiler for PIC series.

C#
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 ?
 
Share this answer
 
v2
Comments
Member 12479883 25-Apr-16 12:15pm    
i got it :) thank you !
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!
 
Share this answer
 
Comments
Member 12479883 25-Apr-16 12:07pm    
Hi could you check on my recent source code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900