View previous topic :: View next topic |
Author |
Message |
Mark A Carter
Joined: 28 Aug 2011 Posts: 5 Location: Vail Arizona USA
|
My exercise B under the USB Master Exerise Book not working. |
Posted: Wed Nov 06, 2013 2:59 pm |
|
|
Dear programmers.
I am trying to get the exercise B under the "USB Master Exercise Book" to function. Under the FURTHER STUDY section of the book to write a MACRO called "delay_seconds".
I am a novice C programmer just starting out.
It should turn on the Green LED D5 for five seconds then turn it off for five seconds then turn on the Yellow D1 LED for one second then turn it off for one second then finally turn the RED LED D0 on for five seconds then turn it off for five seconds then this program repeats this sequence. Please see my code below. I have attempted this exercise using an example code clip from a CCS compiler help topic. This work is done on the CCS Company PIC18F67J10 trainer PCB. My IDE version is 5.013 .
This is my first request for help under the CCS Forum Index -> General CCS C Discussion.
Thank you for your help.
Mark A Carter, KD7PHW, [email protected].
_____________________________________________________________
Code: |
#include <18F67J10.h>
#device ICD=TRUE
#fuses HS,NOWDT
#use delay (clock=10000000)
#define GREEN_LED PIN_D5
#define YELLOW_LED PIN_D1
#define RED_LED PIN_D0
void delay_seconds(int n) {
for (;n!=0;n--) {
delay_ms (1000);
}
}
#define delay_seconds(n)
void main () {
while (TRUE) {
output_low (GREEN_LED);
delay_seconds (5);
output_high (GREEN_LED);
delay_seconds (5);
output_low (YELLOW_LED);
delay_seconds (1);
output_high (YELLOW_LED);
delay_seconds (1);
output_low (RED_LED);
delay_seconds (5);
output_high (RED_LED);
delay_seconds (5);
}
}
| ANY _________________ Mark A Carter
KD7PHW |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 06, 2013 3:30 pm |
|
|
Quote: | void delay_seconds(int n) {
for (;n!=0;n--) {
delay_ms (1000);
}
}
#define delay_seconds(n)
void main () {
while (TRUE) {
output_low (GREEN_LED);
|
The reason it doesn't work is because you have defined your delay routine
out of existence. First you have the function. That's good. But then you
follow it with an empty macro declaration. Why ? The net effect is that
the compiler substitutes the empty macro for your delay function and
you can see this in the .LST file:
Code: |
.................... while (TRUE) {
.................... output_low (GREEN_LED);
00012: BCF TRISD.5
00014: BCF LATD.5
.................... delay_seconds (5);
.................... output_high (GREEN_LED);
00016: BCF TRISD.5
00018: BSF LATD.5
.................... delay_seconds (5);
.................... output_low (YELLOW_LED);
0001A: BCF TRISD.1
0001C: BCF LATD.1
.................... delay_seconds (1);
.................... output_high (YELLOW_LED);
0001E: BCF TRISD.1
00020: BSF LATD.1
.................... delay_seconds (1);
.................... output_low (RED_LED);
00022: BCF TRISD.0
00024: BCF LATD.0
.................... delay_seconds (5);
.................... output_high (RED_LED);
00026: BCF TRISD.0
00028: BSF LATD.0
.................... delay_seconds (5);
0002A: BRA 0012
.................... }
.................... }
....................
0002C: BRA 002C
....................
.................... |
There's no delay routine. There's nothing. The only thing left is your
code to toggle the LEDs.
Comment the following line out as shown below, re-compile and see
what happens:
Code: | // #define delay_seconds(n) |
|
|
|
Mark A Carter
Joined: 28 Aug 2011 Posts: 5 Location: Vail Arizona USA
|
My exercise B under the USB Master Exerise Book not working. |
Posted: Wed Nov 06, 2013 4:12 pm |
|
|
Dear Mr. PCM programmer.
The commenting out of that line did fix the problem.
The three LEDs not sequence correctly.
I do have one addition question through.
How do I make my void delay_seconds(int n) {
for (;n!=0;n--) {
delay_ms (1000);
}
}
code section into a FUNCTION as per the books exercise part B request?
Thank you.
Mark A Carter, [email protected] . _________________ Mark A Carter
KD7PHW |
|
|
Mark A Carter
Joined: 28 Aug 2011 Posts: 5 Location: Vail Arizona USA
|
My exercise B under the USB Master Exerise Book now working! |
Posted: Wed Nov 06, 2013 4:37 pm |
|
|
Dear Mr. PCM programmer
Thank you for your help.
It now works.
I also have gotten the #DEFINE function to work in the CCS book exercise.
Please see my corrected functioning code below.
Code: |
#device ICD=TRUE
#fuses HS,NOWDT
#use delay (clock=10000000)
#define GREEN_LED PIN_D5
#define YELLOW_LED PIN_D1
#define RED_LED PIN_D0
#define delay_in_seconds delay_seconds
void delay_seconds(int n) {
for (;n!=0;n--) {
delay_ms (1000);
}
}
void main () {
while (TRUE) {
output_low (GREEN_LED);
delay_in_seconds (5);
output_high (GREEN_LED);
delay_in_seconds (5);
output_low (YELLOW_LED);
delay_in_seconds (1);
output_high (YELLOW_LED);
delay_in_seconds (1);
output_low (RED_LED);
delay_in_seconds (5);
output_high (RED_LED);
delay_in_seconds (5);
}
} |
_________________ Mark A Carter
KD7PHW |
|
|
|