Programming Exercise using C

Thread Starter

Digit0001

Joined Mar 28, 2010
100
Hi
Can someone tell me if my code is correct? if not where is my mistake.
1)Write a C program to count the number of times PORT T pin 4 goes high. It should stop counting when PORT T pin 2 goes high. Note that DDRT should be appropriately configured. No de-bouncing is necessary.

Rich (BB code):
#define GOHIGH4 (0x10)
#define GOHIGH2 (0x04)

void main(void){
     DDRT |= GOHIGH4;
     DDRT &= GOHIGH2;
     int counting; //use to count
     
    for (;;){
        if((PTT&GOHIGH) != 0){
                 counting();
        else if((PTT&GOHIGH2) == 1){
        }
   }
}
P.S
 

tom66

Joined May 9, 2009
2,595
You are calling the integer variable "counting" as a function. This will compile, but is very likely to result in a segmentation fault. Instead, replace "counting()" with "counting++". Also, for debugging you should instead a printf statement to let you know what the count value is; at the moment, the function might as well do nothing as it has no side effects.
 

DumboFixer

Joined Feb 10, 2009
217
you have unmatched "{"

counting, if it is a variable, is not initialised

The "else if" section does nothing so may as well be deleted (shouldn't rely on compiler to initialise it for you)

In your code you define GOHIGH4 but use GOHIGH

Assuming that PTT refers to the port then counting will be called/incremented whenever the bit is set (so if the bit is set for 1 second then counting will be invoked however many times the loop can be executed in that 1 second) - you should put a leading edge check in (only a few lines of code).
 

Thread Starter

Digit0001

Joined Mar 28, 2010
100
If i get rid of the else if then how am i going to check whenever PORT 2 is high it will stop counting.

Modified code.
Rich (BB code):
#define GOHIGH4 (0x10)
#define GOHIGH2 (0x04)

void main(void){
     DDRT |= GOHIGH4;
     DDRT &= GOHIGH2;
     int counting=0; //use to count
     
    for (;;){
        if((PTT&GOHIGH4) != 0)
                 counting++;
   }
}
I have another problem it gives me an error saying i am missing a '{' just before the line int counting =1;. I have looked at the code many times i don't understand why it is saying i am missing one.
 

Flow

Joined May 30, 2010
37
Hey Digit,

you should come up with some mechanisms on how to find out for yourself if your code is right or not. You could either use an in-circuit debugger, which lets you single step through your code and look at the registers/ram. Or you could use LEDs as outputs and conditional if's to check whether your program is doing what it's supposed to.
 

DumboFixer

Joined Feb 10, 2009
217
If i get rid of the else if then how am i going to check whenever PORT 2 is high it will stop counting.

Modified code.
Rich (BB code):
#define GOHIGH4 (0x10)
#define GOHIGH2 (0x04)

void main(void){
     DDRT |= GOHIGH4;
     DDRT &= GOHIGH2;
     int counting=0; //use to count
     
    for (;;){
        if((PTT&GOHIGH4) != 0)
                 counting++;
   }
}
I have another problem it gives me an error saying i am missing a '{' just before the line int counting =1;. I have looked at the code many times i don't understand why it is saying i am missing one.
As it stands, if bit4 of the port is high then the count is going to be continually incremented so if the loop executes 1000 times a second, for example, then count will be incremented 1000 times a second. What I think you should be doing is incrementing on the leading edge of bit4.

Try this following pseudo code (do not try and compile it as it won't compile)

Rich (BB code):
current_state = port // read the port
changed_bits = current_state xor old_state // change bit now contains bits which have changed state
leading_edge = changed_bits and current_state // leading edge now contains only those bits which have just gone to 1
old_state = current_state // for next iteration
If you code that in c, checking leading_edge for relevant bit being set to "1" and incrementing counting when it is 1 will mean that counting is only incremented when the bit changes from 0 to 1 and not at any time when bit4 is 1.

If you want to check for a trailing edge use
Rich (BB code):
trailing_edge = change_bit and old_state
Try moving the variable definition of counting to be just after "main", ie before any executable code
 
Top