Problem getting character from RS232

Discussion in 'Programmer's Corner' started by xixorro, Jun 22, 2012.

  1. xixorro

    Thread Starter New Member

    Jun 6, 2012
    3
    0
    HI all..


    I am having trouble getting a character from a terminal to a variable and then continuing to run the program.

    I am inexperienced so I must be doing something wrong



    Here's my code:

    Code ( (Unknown Language)):
    1. #include <16F874A.h>
    2. #include <math.h>
    3. #include <ds1306.c>
    4. #use delay(clock=20000000)
    5. #fuses HS, NOWDT, NOPROTECT, NOBROWNOUT
    6. #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,parity=N, bits=8 )
    7.  
    8.  
    9.    int8 hour, minute, second, day, date, month, year, control;
    10.    char str[10], c;
    11.  
    12.  void main()
    13.    {  
    14.     printf ("inicio\r\n");
    15.     int8 hour, minute, second, day, date, month, year, control;
    16.    char str[10], c;
    17.        
    18.    ds1306_init();
    19.    printf ("ds1306_init();\r\n");
    20.    ds1306_writebyte(0x8F, 0x00);
    21.    printf (" ds1306_writebyte(0x8F, 0x00);\r\n");
    22.    delay_us(10);
    23.    
    24.    ds1306_writebyte(0x8F, 0x02);
    25.     printf ("ds1306_writebyte(0x8F, 0x02);\r\n");
    26.    
    27.    /*printf("enter hour (0 - 23).\r\n");
    28.    hour = gets(str);
    29.    printf("enter minute (0 - 60).\r\n");
    30.    minute = gets(str);
    31.    printf("enter second (0 - 60).\r\n");
    32.    second = gets(str);
    33.    printf("enter day (1=monday, 2=tuesday, etc).\r\n");
    34.    day = gets(str);
    35.    printf("enter day of month (1 - 31).\r\n");
    36.    date = gets(str);
    37.    printf("enter month (1 - 12).\r\n");
    38.    month = gets(str);
    39.    printf("enter year 20XX (0 - 99).\r\n");
    40.    year = gets(str);*/
    41.    
    42.    ds1306_settime_24hr(15, 20, 15);
    43.    ds1306_setdate(4, 23, 2, 6);
    44.   [COLOR="Red"]printf("Press 't' to retrieve the time and 'd' to retrieve the date.\r\n"); [/COLOR]
    45.    
    46.      while(1)
    47.    {
    48.       c = getc();
    49.      
    50.          printf ("%c\r\n",c);
    51.       if ((c == 't') || (c == 'T'))
    52.       {
    53.          second = bcd2bin(ds1306_readbyte(0x00));
    54.          minute = bcd2bin(ds1306_readbyte(0x01));
    55.          hour = bcd2bin(ds1306_readbyte(0x02));
    56.          printf("Time = ");
    57.          if (hour < 10)
    58.             printf("0%d:", hour);
    59.          else
    60.             printf("%d:", hour);
    61.          if (minute < 10)
    62.             printf("0%d:", minute);
    63.          else
    64.             printf("%d:", minute);
    65.          if (second < 10)
    66.             printf("0%d", second);
    67.          else
    68.             printf("%d", second);
    69.          printf("\r\n");
    70.       }
    71.     c = getc();
    72.       if ((c == 'a') || (c == 'A'))
    73.       {
    74.          second = bcd2bin(ds1306_readbyte(0x0B));
    75.          minute = bcd2bin(ds1306_readbyte(0x0C));
    76.          hour = bcd2bin(ds1306_readbyte(0x0D));
    77.          printf("Alarm time = ");
    78.          if (hour < 10)
    79.             printf("0%d:", hour);
    80.          else
    81.             printf("%d:", hour);
    82.          if (minute < 10)
    83.             printf("0%d:", minute);
    84.          else
    85.             printf("%d:", minute);
    86.          if (second < 10)
    87.             printf("0%d", second);
    88.          else
    89.             printf("%d", second);
    90.          printf("\r\n");
    91.       }
    92.  
    93.       if ((c == 'd') || (c == 'D'))
    94.       {
    95.          day = bcd2bin(ds1306_readbyte(0x03));
    96.          date = bcd2bin(ds1306_readbyte(0x04));
    97.          month = bcd2bin(ds1306_readbyte(0x05));
    98.          year = bcd2bin(ds1306_readbyte(0x06));
    99.          printf("Date = ");
    100.          switch(day)
    101.          {
    102.             case 1:  printf("Monday");
    103.                      break;
    104.             case 2:  printf("Tuesday");
    105.                      break;
    106.             case 3:  printf("Wednesday");
    107.                      break;
    108.             case 4:  printf("Thursday");
    109.                      break;
    110.             case 5:  printf("Friday");
    111.                      break;
    112.             case 6:  printf("Saturday");
    113.                      break;
    114.             case 7:  printf("Sunday");
    115.                      break;
    116.          }
    117.          printf(", ");
    118.          if (date < 10)
    119.             printf("0%d/", date);
    120.          else
    121.             printf("%d/", date);
    122.          if (month < 10)
    123.             printf("0%d/", month);
    124.          else
    125.             printf("%d/", month);
    126.          if (year < 10)
    127.             printf("0%d", year);
    128.          else
    129.             printf("%d", year);
    130.          printf("\r\n");
    131.  
    132.       }
    133.    
    134.    delay_ms(10000);
    135.       printf ("end of while");
    136.    }
    137. }
    after the red print f i wont show nothing anymore.

    Really need some of your help
     
  2. BillO

    Well-Known Member

    Nov 24, 2008
    985
    136
    I think you may want to use getchar() rather than getc(). getc() requires a pointer to a file as a parameter whereas getchar() assumes stdin.
     
Loading...