Programming Exercise using C

Discussion in 'Homework Help' started by Digit0001, Aug 23, 2010.

  1. Digit0001

    Thread Starter Member

    Mar 28, 2010
    89
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2. #define GOHIGH4 (0x10)
    3. #define GOHIGH2 (0x04)
    4.  
    5. void main(void){
    6.      DDRT |= GOHIGH4;
    7.      DDRT &= GOHIGH2;
    8.      int counting; //use to count
    9.      
    10.     for (;;){
    11.         if((PTT&GOHIGH) != 0){
    12.                  counting();
    13.         else if((PTT&GOHIGH2) == 1){
    14.         }
    15.    }
    16. }
    17.  
    18.  
    P.S
     
  2. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    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.
     
  3. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    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).
     
  4. Digit0001

    Thread Starter Member

    Mar 28, 2010
    89
    0
    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.
    Code ( (Unknown Language)):
    1.  
    2. #define GOHIGH4 (0x10)
    3. #define GOHIGH2 (0x04)
    4.  
    5. void main(void){
    6.      DDRT |= GOHIGH4;
    7.      DDRT &= GOHIGH2;
    8.      int counting=0; //use to count
    9.      
    10.     for (;;){
    11.         if((PTT&GOHIGH4) != 0)
    12.                  counting++;
    13.    }
    14. }
    15.  
    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.
     
  5. Flow

    Member

    May 30, 2010
    37
    1
    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.
     
  6. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    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)

    Code ( (Unknown Language)):
    1.  
    2. current_state = port // read the port
    3. changed_bits = current_state xor old_state // change bit now contains bits which have changed state
    4. leading_edge = changed_bits and current_state // leading edge now contains only those bits which have just gone to 1
    5. old_state = current_state // for next iteration
    6.  
    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
    Code ( (Unknown Language)):
    1. trailing_edge = change_bit and old_state
    Try moving the variable definition of counting to be just after "main", ie before any executable code
     
Loading...