This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

serial port and timer

Hi,

I have a problem.I must send through the serial port the number 1 and the number 0 every second using a timer of the c167cs. Would someone know me to help?

Parents Reply Children
  • The have inserted to "if" in such way to make me stamp, alternatively, "0" and "1" to distance of to second. The tails is the following:

    #include "c167cs.h"
    #include "intrins.h"
    
    int a;
    a=0;
    
    void GPT1_viTmr3(void) interrupt 0x23 {
    
      if(a=0)
         {S0TBUF = '0';
          a=1;}
      else
         {S0TBUF = '1';
          a=0;}
    
      P2 ^= 0x0001;  /* toggle P2.0 */
    }
    
    void main(void) {
      /* setup the serial port */
      S0BG  = 0x0040; /* 9.600 kbaud @ 20 MHz clock */
      S0CON = 0x0011; /* format: 8-N-1 */
    
      /* P3.10 is used for TxD and P3.11 is used for RxD */
      _bfld_( P3,0x0C00,0x0400);    /**/
      _bfld_(DP3,0x0C00,0x0400);    /* set data direction */
    
      S0CON |= 0x8000; /* enable baud rate */
    
      /* use GPT1 for the 1 second time base */
      T3CON = 0x0087;  /* clock tick is 51.2 usec, count down */
      T3    = 0x4C4B;  /* 1 second load value */
    
      T2CON = 0x0027;  /* t2 reloads T3 on the interrupt */
      T2    = 0x4C4B;
    
      T3IC  = 0x004B;  /* set up the interrupt level and enable it */
      T3R   = 1;       /* start timer 3 running */
    
      DP2   = 1;       /* toggle P2.0 every second */
    
      IEN   = 1;       /* globally enable interrupts */
    
      for(;;){};
    }
    

    The problem is that it stamps only me "1" every second. would someone know me to explain because? Thanks so much.

  • if (a = 0) { ... }
    

    is an assignment, i.e. it doesn't matter what value a had before your test. You must use == to compare, i.e.

    if (a == 0) { ... }
    

    Or reverse the two parts of the if/else and drop the explicit comparison:

    if (a) {
        S0TBUF = '1';
        a = 0;
    } else {
        S0TBUF = '0';
        a = 1;
    }
    

    You can also remove the if statement totally:

    S0TBUF = '0' + (a++ & 1);
    


    This extracts the odd/even bit of a, and continuously increments a.

  • The problem is that I want to stamp "0" then after a second "1", after another second "0", after another second "1" .... ect.With the instruction:

    S0TBUF = '0' + (a++ & 1);
    

    does it increase the value progressively?

  • "does it increase the value progressively?"

    Per's post clearly described what it does - read it again, carefully...

  • As I already mentioned: "This extracts the odd/even bit of a". This means you will alternate between adding 0 and 1 to your printout, i.e. the printout will be 0101010101010101010101...

    By the way - why do you want such a printout? It isn't exactly meaningful. You already have a toggling pin. Why emit trace output whenever you toggle the pin? Doesn't your pin toggling work?

  • Another thing: Why do you declare a as int? You are programming an 8-bit processor and int is in this case a 16-bit data type. Using a data type larger than the native register size is not recommended unless you need the range. In this case, your code only makes use of the values 0 and 1. You could use a 1-byte variable, or you could even modify your code to use a 1-bit variable.

  • In effects I don't know so much well to use the microcontrollore and I wanted something that alternatively stamped me to video "0" and "1" every second.

  • Another thing: Why do you declare a as int? You are programming an 8-bit processor and int is in this case a 16-bit data type. Using a data type larger than the native register size is not recommended unless you need the range.

    The C167 is a native 16-bit microcontroller.

    PS. I added the toggle to P2.0 since the Phytec board has LEDs connected to P2.

  • "It isn't exactly meaningful."

    No - it's a homework assignment!

  • I was completely convinced that I saw C51 as architecture when I wrote the comment.

    Anyway, a number of embedded processors has limited RAM space and special instructions for bit variables, so all variables should get their type based on a consious decision.

  • Yes, I thought as much, just wanted to point this out for other users.

    I would just make a comment to the generated code for the C166 is usually (from a code standpoint) not as efficient when using a byte variable over a word variable (usually you end up with an extra instruction).

    Per as you stated the user must decide what memory allocation is important for their application. I also appreciate your suggestion of an add instruction instead of an if statement.