![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
trento
Joined: 14 Oct 2005 Posts: 26
|
delay problem |
Posted: Sat Oct 29, 2005 12:14 am |
|
|
Hi dear all
I have problem with the following program.The problem is when I activate the un active row in which showing in the program the program give a wrong result.I do not know if the problem is because I use three port .I notice that when I activate especialy the row which has delay_us(366) the problem start ,furthermore when I combain as shown to one which is delay_us(732) the problem remove so please tell me what is the problem ![Crying or Very sad](images/smiles/icon_cry.gif) |
|
![](templates/subSilver/images/spacer.gif) |
trento
Joined: 14 Oct 2005 Posts: 26
|
|
Posted: Sat Oct 29, 2005 12:16 am |
|
|
Code: | /****************************************************
* *
* COPYRIGHT (c) safoan *
* *
* codes = Chasing signal *
* *
* KIT = emBox-877 Kit ( CPU = PIC16F877A ) *
* *
* STATUS = DONE 12/09/05 *
* *
****************************************************/
#include <16F877A.h>
#FUSES HS,NOWDT,NOPROTECT,NOPUT
#USE DELAY( CLOCK=20000000 )
/*#USE FAST_IO(A)*/
#USE FAST_IO(B)
#USE FAST_IO(D)
/*#byte port_a=5 /* define the Address of PORTA */
#byte port_b=6 /* define the Address of PORTB */
#byte port_d=8 /* define the address of PORTD */
static byte CONST UP_MAP[8] = { 0xe1,0xc3,0x87,0x0f,0x1e,0x3c,0x78,0xf0 };
/***************************************
* LED wiring *
* ********** *
* 8 signals = PB0 - PB7 *
* 8 signals = PD0 - PD7 *
***************************************
* NOTE: see emBox-877 schematic *
***************************************/
main(){
char cnt;
/* set_tris_a(0xfc); /* set port_a to be RA0 outputs */
set_tris_b(0); /* set port_b to be ALL outputs */
set_tris_d(0); /* set port_d to be ALL outputs */
/* tnt = 0 ; /* intialize counter tnt */
/* port_a= 0, /* initialize & turn off ALL PORTA outputs */
port_b = 0xf0, /* initialize & turn off ALL PORTB outputs */
port_d = 0xf0; /* initialize & turn on ALL PORTD outputs */
for(;;){ /* forever loop*/
/* if(tnt == 80)
tnt = 0 ;
if (tnt % 2 == 0) /* multiplixer condition selector */
/* port_a = 0x01;
else port_a = 0x02;*/
/* delay_us(366);*/
for (cnt=0 ; cnt < 4 ; cnt++) { /* delay loop for the first H-bridge leg (positive cycle) */
port_b = UP_MAP[cnt]; /* pick value to display from array */
switch (cnt) {
case 0:
delay_us(1146);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(951);
break;
case 3:
delay_us(3076);
break; }
} /* delay time loop for increment stage (positive cycle)*/
for( cnt=0 ; cnt < 4 ; cnt++ ){ /* delay time loop for decrement stage (positive cycle) */
port_d = UP_MAP[cnt];
switch (cnt) {
case 0:
delay_us(951);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(1146);
break;
case 3:
delay_us(732);
break;
}
/* if(tnt == 80)
tnt = 0 ;
if ( tnt % 2 == 0 )
port_a = 0x03 ;
else
port_a = 0 ;
tnt++ ;*/
/*DELAY_US( 366);*/
}
for (cnt=4 ; cnt < 8 ; cnt++) { /* delay time loop for increment stage (negative cycle) */
port_b = UP_MAP[cnt] ;
switch (cnt) {
case 4:
delay_us(1146);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(951);
break;
case 7:
delay_us(3076);
break;
}
}
for (cnt=4 ;cnt < 8; cnt++){ /* delay time loop for decrement stage(negative cycle) */
port_d = UP_MAP[cnt];
switch (cnt) {
case 4:
delay_us(951);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(1146);
break;
case 7:
delay_us(732);
break;
} } } }
|
|
|
![](templates/subSilver/images/spacer.gif) |
trento
Joined: 14 Oct 2005 Posts: 26
|
|
Posted: Sat Oct 29, 2005 12:16 am |
|
|
Code: | /****************************************************
* *
* COPYRIGHT (c) safoan *
* *
* codes = Chasing signal *
* *
* KIT = emBox-877 Kit ( CPU = PIC16F877A ) *
* *
* STATUS = DONE 12/09/05 *
* *
****************************************************/
#include <16F877A.h>
#FUSES HS,NOWDT,NOPROTECT,NOPUT
#USE DELAY( CLOCK=20000000 )
/*#USE FAST_IO(A)*/
#USE FAST_IO(B)
#USE FAST_IO(D)
/*#byte port_a=5 /* define the Address of PORTA */
#byte port_b=6 /* define the Address of PORTB */
#byte port_d=8 /* define the address of PORTD */
static byte CONST UP_MAP[8] = { 0xe1,0xc3,0x87,0x0f,0x1e,0x3c,0x78,0xf0 };
/***************************************
* LED wiring *
* ********** *
* 8 signals = PB0 - PB7 *
* 8 signals = PD0 - PD7 *
***************************************
* NOTE: see emBox-877 schematic *
***************************************/
main(){
char cnt;
/* set_tris_a(0xfc); /* set port_a to be RA0 outputs */
set_tris_b(0); /* set port_b to be ALL outputs */
set_tris_d(0); /* set port_d to be ALL outputs */
/* tnt = 0 ; /* intialize counter tnt */
/* port_a= 0, /* initialize & turn off ALL PORTA outputs */
port_b = 0xf0, /* initialize & turn off ALL PORTB outputs */
port_d = 0xf0; /* initialize & turn on ALL PORTD outputs */
for(;;){ /* forever loop*/
/* if(tnt == 80)
tnt = 0 ;
if (tnt % 2 == 0) /* multiplixer condition selector */
/* port_a = 0x01;
else port_a = 0x02;*/
/* delay_us(366);*/
for (cnt=0 ; cnt < 4 ; cnt++) { /* delay loop for the first H-bridge leg (positive cycle) */
port_b = UP_MAP[cnt]; /* pick value to display from array */
switch (cnt) {
case 0:
delay_us(1146);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(951);
break;
case 3:
delay_us(3076);
break; }
} /* delay time loop for increment stage (positive cycle)*/
for( cnt=0 ; cnt < 4 ; cnt++ ){ /* delay time loop for decrement stage (positive cycle) */
port_d = UP_MAP[cnt];
switch (cnt) {
case 0:
delay_us(951);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(1146);
break;
case 3:
delay_us(732);
break;
}
/* if(tnt == 80)
tnt = 0 ;
if ( tnt % 2 == 0 )
port_a = 0x03 ;
else
port_a = 0 ;
tnt++ ;*/
/*DELAY_US( 366);*/
}
for (cnt=4 ; cnt < 8 ; cnt++) { /* delay time loop for increment stage (negative cycle) */
port_b = UP_MAP[cnt] ;
switch (cnt) {
case 4:
delay_us(1146);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(951);
break;
case 7:
delay_us(3076);
break;
}
}
for (cnt=4 ;cnt < 8; cnt++){ /* delay time loop for decrement stage(negative cycle) */
port_d = UP_MAP[cnt];
switch (cnt) {
case 4:
delay_us(951);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(1146);
break;
case 7:
delay_us(732);
break;
} } } }
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Oct 29, 2005 5:03 am |
|
|
First. may I suggest you actually look at the code you have posted, and see if you can read it!. The double line breaks, and sections 'remmed' out, make the flow almost impossible to follow.
Second, delete the double post...
Now there are some bits that may well not behave as you expect. For instance:
Code: |
if (tnt % 2 == 0) /* multiplixer condition selector */
/* port_a = 0x01;
else port_a = 0x02;*/
/* delay_us(366);*/
for (cnt=0 ; cnt < 4 ; cnt++) { /* delay loop for the first H-bridge
leg (positive cycle) */
|
You have 'remmed' out the statements that would be executed by the 'if', but not the 'if' itself. You set 'tnt' to '0', so the next part gets permanently executed. Now this may be what you intend, but the layout is so poor, that it will be very easy for you (and even easier for us, given we don't know what the 'flow' is meant to be), to lose track completely of what is going on.
Use some of the standard formatting rules, and re-layout the code. Things that are executed by a control statement, move right, and when the statement closes, move back left. I suspect that if you retype the code like this, the flow error will suddenly 'leap out'at you.
Now you talk about the 'un-active row', but I can see about a dozen rows that are not active. Knowing which one you actually mean is therefore hard...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Oct 29, 2005 6:05 am |
|
|
Ttelmah wrote: | Now there are some bits that may well not behave as you expect. For instance:
Code: |
if (tnt % 2 == 0) /* multiplixer condition selector */
/* port_a = 0x01;
else port_a = 0x02;*/
/* delay_us(366);*/
for (cnt=0 ; cnt < 4 ; cnt++) { /* delay loop for the first H-bridge
leg (positive cycle) */
|
You have 'remmed' out the statements that would be executed by the 'if', but not the 'if' itself. You set 'tnt' to '0', so the next part gets permanently executed. | Two lines higher in his code is another '/*' so actually this code is all commented out.
His layout is definately a mess.........
I refuse to look into this any further until he deletes the double post and gives us a cleaned up version of his program. |
|
![](templates/subSilver/images/spacer.gif) |
trento
Joined: 14 Oct 2005 Posts: 26
|
|
Posted: Sat Oct 29, 2005 10:19 am |
|
|
I am sorry for display my code by unreadable way because this is the first time i deal with like this discussion forume the problem is when i use the statments from ==> Code: | if(tnt == 80)
tnt = 0 ;
if ( tnt % 2 == 0 )
port_a = 0x03 ;
else
port_a = 0 ;
tnt++ ;
DELAY_US( 366);
| the programe give a strange result but when I delete it the program work ok
this is the full code
Code: |
#include <16F877A.h>
#FUSES HS,NOWDT,NOPROTECT,NOPUT
#USE DELAY( CLOCK=20000000 )
#USE FAST_IO(A)
#USE FAST_IO(B)
#USE FAST_IO(D)
#byte port_a=5
#byte port_b=6
#byte port_d=8
static byte CONST UP_MAP[8] = { 0xe1,0xc3,0x87,0x0f,0x1e,0x3c,0x78,0xf0 };
main(){
char cnt;
set_tris_a(0xfc);
set_tris_b(0);
set_tris_d(0);
tnt = 0 ;
port_a= 0,
port_b = 0xf0,
port_d = 0xf0;
for(;;){
if(tnt == 80)
tnt = 0 ;
if (tnt % 2 == 0)
port_a = 0x01;
else port_a = 0x02;
delay_us(366);
for (cnt=0 ; cnt < 4 ; cnt++) {
port_b = UP_MAP[cnt];
switch (cnt) {
case 0:
delay_us(1146);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(951);
break;
case 3:
delay_us(3076);
break; }
}
for( cnt=0 ; cnt < 4 ; cnt++ ){
port_d = UP_MAP[cnt];
switch (cnt) {
case 0:
delay_us(951);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(1146);
break;
case 3:
delay_us(732);
break;
}
if(tnt == 80)
tnt = 0 ;
if ( tnt % 2 == 0 )
port_a = 0x03 ;
else
port_a = 0 ;
tnt++ ;
DELAY_US( 366);
}
for (cnt=4 ; cnt < 8 ; cnt++) {
port_b = UP_MAP[cnt] ;
switch (cnt) {
case 4:
delay_us(1146);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(951);
break;
case 7:
delay_us(3076);
break;
}
}
for (cnt=4 ;cnt < 8; cnt++){
port_d = UP_MAP[cnt];
switch (cnt) {
case 4:
delay_us(951);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(1146);
break;
case 7:
delay_us(732);
break;
} } } }
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sun Oct 30, 2005 4:13 am |
|
|
You have still not got rid of the double post, formatted the code in an even remotely readable way, or said _what_ the code is actually meant to do?. Clean up the mess.
Though I hate doing your job for you, I am attaching the code in a more readable form, and with some comments inside. Document what you actually want this to do, and clean the thread up, and you may get some replies. As a general comment, the times involved in the loops themselves, make delays like '361usec' pointless, since by the time the code has executed, you are going to have values more like 380uSec.
Code: |
#include <16F877A.h>
#FUSES HS,NOWDT,NOPROTECT,NOPUT
#USE DELAY( CLOCK=20000000 )
#USE FAST_IO(A)
#USE FAST_IO(B)
#USE FAST_IO(D)
#byte port_a=5
#byte port_b=6
#byte port_d=8
byte CONST UP_MAP[8] = { 0xe1,0xc3,0x87,0x0f,0x1e,0x3c,0x78,0xf0 };
main() {
char cnt;
set_tris_a(0xfc);
set_tris_b(0);
set_tris_d(0);
//Setup ports b, and d as outputs. Port a as outputs on bottom two pins
tnt = 0 ;
//What on earth is 'tnt'. You have no variable defined with this name...
port_a= 0,
port_b = 0xf0,
port_d = 0xf0;
//Initial starting values. Both port b, and d 'high' on top four pins
for(;;) {
//loop forever
//Why not just loop using 'tnt', inside a 'while' loop?...
if(tnt == 80)
tnt = 0 ;
if (tnt % 2 == 0)
port_a = 0x01;
else
port_a = 0x02;
//Notice the use of indenting. See how you can now tell which
//statement is executed by which condition....
delay_us(366);
for (cnt=0 ; cnt < 4 ; cnt++) {
//Now loop through four states
port_b = UP_MAP[cnt];
switch (cnt) {
case 0:
delay_us(1146);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(951);
break;
case 3:
delay_us(3076);
break;
}
}
//Note how you can now see where the 'for' loop, and the 'switch' end
for( cnt=0 ; cnt < 4 ; cnt++ ) {
//Again four states
port_d = UP_MAP[cnt];
switch (cnt) {
case 0:
delay_us(951);
break;
case 1:
delay_us(999);
break;
case 2:
delay_us(1146);
break;
case 3:
delay_us(732);
break;
}
//Why the double check?. If you are testing 'tnt' (whatever it is)
//here, why check it earlier?.
if(tnt == 80)
tnt = 0 ;
if ( tnt % 2 == 0 )
port_a = 0x03 ;
else
port_a = 0 ;
tnt++ ;
//'tnt', does not really seem to do anything, except 'toggle' the bits
//of port a, on alternate passes in this, and the outer loop. Why not
//do this in a much more 'obvious' way?. Counting it up to '80'
//(actually 81), seems pointless. There are only two possible
//values being used by it...
DELAY_US( 366);
}
for (cnt=4 ; cnt < 8 ; cnt++) {
//Now four 'high' states
port_b = UP_MAP[cnt] ;
switch (cnt) {
case 4:
delay_us(1146);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(951);
break;
case 7:
delay_us(3076);
break;
}
}
for (cnt=4 ;cnt < 8; cnt++) {
port_d = UP_MAP[cnt];
switch (cnt) {
case 4:
delay_us(951);
break;
case 5:
delay_us(999);
break;
case 6:
delay_us(1146);
break;
case 7:
delay_us(732);
break;
}
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|