Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Problem with Keil C program

Status
Not open for further replies.

onde

Junior Member level 1
Joined
Nov 29, 2002
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
88
keil c-Problem

hello!

I have a problem with the following program:
// Timerinterrupt

#include <reg517a.h>

void INTTIM0 (void) interrupt 1
{ if (P4 < 0x7f) P4 <<= 1;
if (P4 > 0x02) P4 >>= 1;
TH0=-39; // 10ms
TL0=0;
}

main()
{
P4 = 0x01;
TMOD = 0x01; // Timer 0 Mode 1 16 Bit
TH0 = -39; // every 10ms Int.
TL0 = 0;
ET0 = 1; // Timer 0 Interrupt disabled
EAL = 1; // enable all

TR0 = 1; //

while(1)
{
}
}

The Problem is, P4 is shifted only 1 time (from 0x01 to 0x10), after this nothing is happing.
Any Idea ???
 

zohar

Junior Member level 1
Joined
Feb 20, 2002
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Israel
Activity points
71
not so clearly

what you mean that P4 swaps only once, for it to shift from 0x01 to 0x10 it MUST be shift FOUR times???
 

zohar

Junior Member level 1
Joined
Feb 20, 2002
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Israel
Activity points
71
forgoten issue

You place both statments as an if, instead of blocking the statments as [if]
[else]
blocks.
I mean that in the next shift, P4 shift one time to the LEFT: 0x02 -> 0x04,
then the next if statment take place: P4 shift back to RIGHT: 0x04 -> 0x02.
The sequence occur each interrupt cycle.

Change the statment to [If] [else] block!
 

onde

Junior Member level 1
Joined
Nov 29, 2002
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
88
Hi,

thx for your answere and for you hint :)
aua, I think it´s not my day.
bye
 

thomaz

Member level 2
Joined
May 4, 2001
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
atlantis
Activity points
329
an other thing:
even if my compiler accepts
while (1)
{
}

I write:
while (1)
{
;
}


also had bad experience with:
{
blablabla...

goto xyz_label;

blablabla...

xyz_label:
}

one of the compilers I'm using wanted to see the ";" between the label and the bracket


thomaz
 

Robby

Member level 2
Joined
Jan 24, 2002
Messages
50
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
423
C-problem

Hello onde,

Please describe your problem clear, so other people can help you.
Your problem isn't a compiler or even ke*l problem.
I guess you wish to realize a constant and repeatedly shift through P4.
If so, you have to register wether the two shift events reach there bounderies. If so, register and turn around the shift direction.

Please post for more details.

Robby
 

algilsan

Advanced Member level 4
Joined
Jan 20, 2002
Messages
116
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Location
chile
Activity points
1,078
Hi,

if you put th second condition in an ELSE statment, you will get the seceuence: 1, 2, 4, 8, 16, 32, 64, 128, 64, 128, 64, 128 ... and so on, because you have to decideif you are Up-shifting or Down-Shifting. Sometihg likethis wil go well,

unsigned char UP = 1;

void timer0 interrupt() using 1{

if (UP)
if ( (P4 <<= 1) < 0x80 ){
UP = 0;
}
else
 

algilsan

Advanced Member level 4
Joined
Jan 20, 2002
Messages
116
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,298
Location
chile
Activity points
1,078
Hi,

if you put th second condition in an ELSE statment, you will get the seceuence: 1, 2, 4, 8, 16, 32, 64, 128, 64, 128, 64, 128 ... and so on, because you have to decideif you are Up-shifting or Down-Shifting. Sometihg likethis wil go well,

unsigned char UP = 1;

void timer0 interrupt() using 1{

if (UP)
if ( P4 < 0x80 )
P4 <<= 1;
else{
UP = 0;
P4 >>= 1;
}

else
if ( P4 > 0x01)
P4 >>= 1;
else{
UP = 1;
P4 <<= 1;
}

............... Rest of routine
}
 

edward

Newbie level 1
Joined
Sep 27, 2001
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
10
the Optimized Code as follows:

#define true 1
#define false 0

typedef bit bool;

bool UP = 1;

void timer0 interrupt() using 1
{
if (UP && P4<0x80)
P4 <<= 1;
else if(UP)
{
UP = 0;
P4 >>= 1;
}
else if (P4 > 0x01)
P4 >>= 1;
else
{
UP = true;
P4 <<= 1;
}
}
 

cheolim

Full Member level 1
Joined
Dec 3, 2002
Messages
97
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
873
Hello onde,

In my opinion, your problem may comes some usage restriction.
P4 is declared in reg517a.h as of below.
sfr P4 = 0xE8;

In my case, whenever I use the special funcion register(sfr),
I use the replace variable of unsigned char type.

#include <reg517a.h>

void INTTIM0 (void) interrupt 1
{
unsigned char IO=0;

IO = P4;
if (IO < 0x7f) {
IO <<= 1;
P4 = IO;
}
if (IO > 0x02) {
IO >>= 1;
P4 = IO;
}
TH0=-39; // 10ms
TL0=0;
}

main()
{
P4 = 0x01;
TMOD = 0x01; // Timer 0 Mode 1 16 Bit
TH0 = -39; // every 10ms Int.
TL0 = 0;
ET0 = 1; // Timer 0 Interrupt disabled
EAL = 1; // enable all

TR0 = 1; //

while(1)
{
}
}
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top