Stopwatch timer programming MPLAB [PIC18F452]

Discussion in 'Homework Help' started by Vlad Moglan, May 26, 2015.

  1. Vlad Moglan

    Thread Starter New Member

    Dec 31, 2014
    25
    0
    Hello,

    Once again I find myself in great difficulty in the System Architecture class. I am supposed to create a stopwatch timer as can be seen in the attached picture. Thing is, I do not understand how everything works. Here's the code I have so far (it's not mine but I have to complete it):

    Code (Text):
    1. #include "iq.h"
    2. #include "it.h"
    3. #define byte unsigned char
    4. /* variables globale */
    5. byte *trisb = 0xf93 ;
    6. byte *portb = 0xf81 ;
    7. byte *trisd = 0xf95 ;
    8. byte *portd = 0xf83 ;
    9. byte *trisc = 0xf94;
    10. byte *portc = 0xf82;
    11. int i=0;
    12. int nb=0;
    13. int second = 0;
    14. int minute = 0;
    15.  
    16. // function prototypes
    17. void affiche_7seg(byte);
    18.  
    19. // function implementations
    20. void affiche_7seg(byte val)
    21. {
    22.     byte
    23.     tab_7seg[]={0b00111111,0b00000110,0b01011011,0b01001111,
    24.     0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,
    25.     0b01101111};
    26.     *portb=tab_7seg[val];
    27. }
    28.  
    29. void it_int0()
    30. {}
    31.  
    32. void it_int1()
    33. {}
    34.  
    35. /* interruption timer */
    36. void it_tmr0()
    37. {
    38.     i++;
    39.     affiche_7seg(second % 10);
    40.     affiche_7seg(minute % 10);
    41.     if ((i % 10) == 0) second ++;
    42.     if ((second % 10) == minute++);
    43. }
    44.  
    45. // main function  
    46. void main(void)
    47. {
    48.     *trisc = 0xff;
    49.     *trisd = 0 ;
    50.     *trisb = 0;
    51.     *portb = 0;
    52.     *portd = 0xfe;     // if this is d, affiche_7seg will show the number on the next to last lcd panel
    53.     init_it_tmr0();
    54.     while(1)
    55.     {
    56.     }
    57. }
    Okay, so apparently *portd decides of the panel on which output will be displayed. I have tried giving it values such as 0xfe, 0xfd, 0xfc, 0xfb, 0xfa. 0xfc and 0xfc give me weird values (2 panels containing the second counter at the same time).

    Point is, I do not understand most of this and it is not very well explained. What does the code in hexadecimal signify? Do I need other "display" functions (like affiche_7seg) for displaying minutes for example? Thank you in advance for your help.

    PS: If you don't understand what I'm talking about, it's because I am very confused with this. It's my hardest class and it is not at all well explained.
     
  2. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,979
    744
    Looks like port b is the segments A-G dp, and port d is the Cathodes for the Leds 1 to 4.

    so to make the segments light up you need 1's on port b and 0 on port d,
     
  3. Vlad Moglan

    Thread Starter New Member

    Dec 31, 2014
    25
    0
    Thank you for your answer, I will see what I can do knowing that. Also sorry for the late answer, but I am rarely at home.
     
  4. Vlad Moglan

    Thread Starter New Member

    Dec 31, 2014
    25
    0
    Okay, you were totally right about that and it worked. Still a lot to figure out but at least now I can display stuff on all 4 screens. Thing is it has to be different stuff (it only displays seconds everywhere), but I'll try to figure that on my own.
    Sorry to be so bothersome, but how did you figure all of this out? What do these segments actually mean?

    Thanks a lot for the answer, though, really appreciate it.

    PS: This is magic to me
     
  5. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,979
    744
    The segments are led common cathode, each segment can be lit individually,on any digit,

    So you have 7 segments labelled A to G, and a decimal point dp, and four digits , you need to study "led seven segment multiplexing " youtube have plenty videos.
     
  6. Vlad Moglan

    Thread Starter New Member

    Dec 31, 2014
    25
    0
    Okay, got the stopwatch to work with start, stop and reset all in order. I have a problem though, there is too much flickering and I am not quite aware of the problem. Here is the video:

    I will post the code below.


    tp2-3.c

    Code (Text):
    1. #include "iq.h"
    2. #include "it.h"
    3. #define byte unsigned char
    4. #define TRUE 1
    5. #define FALSE 0
    6.  
    7.  
    8. /* variables globale */
    9. byte *trisb = 0xf93 ;
    10. byte *portb = 0xf81 ;
    11. byte *trisd = 0xf95 ;
    12. byte *portd = 0xf83 ;
    13. byte *trisc = 0xf94;
    14. byte *portc = 0xf82;
    15. int i=0;
    16. int nb=0;
    17. int second = 0;
    18. int minute = 0;
    19. int start = 0;
    20.  
    21.  
    22.  
    23. // function prototypes
    24. void affiche_7seg(byte);
    25. void it_tmr0();
    26. void startStopwatch();
    27. void stopStopwatch();
    28. void resetStopwatch();
    29.  
    30. // function implementations
    31.  
    32. // function that shows input value on selected panel
    33. void affiche_7seg(byte val)
    34. {
    35.     byte
    36.     tab_7seg[]={0b00111111,0b00000110,0b01011011,0b01001111,
    37.     0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,
    38.     0b01101111};
    39.     *portb=tab_7seg[val];
    40. }
    41.  
    42. // function that starts the stopwatch
    43. void startStopwatch()
    44. {
    45.     if ((*portc&0b00000001) == 1)
    46.     {
    47.         start = TRUE;
    48.     }
    49. }
    50.  
    51. // function that stops the stopwatch
    52. void stopStopwatch()
    53. {
    54.     if ((*portc&0b00000010) == 2)
    55.     {
    56.         start = FALSE;
    57.     }
    58. }
    59.  
    60. // function that resets the stopwatch
    61. void resetStopwatch()
    62. {
    63.     if ((*portc&0b00000100) == 4)
    64.     {
    65.         minute = 0;
    66.         second = 0;
    67.         *portc = 0x00;
    68.         start = FALSE;
    69.     }
    70. }
    71.  
    72. void it_int0()
    73. {}
    74.  
    75. void it_int1()
    76. {}
    77.  
    78. /* interruption timer */
    79. void it_tmr0()
    80. {
    81.     resetStopwatch();
    82.     startStopwatch();
    83.     stopStopwatch();
    84.  
    85.     if (start == TRUE){
    86.  
    87.         i++;
    88.         // displaying the second digit of second on the last panel
    89.         *portd = 0xfe;
    90.         affiche_7seg(second % 10);
    91.         delayms(1);
    92.    
    93.         // displaying the first digit of second on the 3rd panel
    94.         *portd = 0xfd;
    95.         affiche_7seg((second/10) % 10);
    96.         delayms(1);
    97.    
    98.         // displaying the first digit of minutes on the second panel
    99.         *portd = 0xfb;
    100.         affiche_7seg(minute%10);
    101.         delayms(1);
    102.    
    103.         // display the first digit of minutes on the first panel
    104.         *portd = 0xf7;
    105.         affiche_7seg((minute%10) % 10);
    106.         delayms(1);
    107.    
    108.         if ((i % 10) == 0) second ++;    // incrementing second
    109.  
    110.         // incrementing minute
    111.         if ((second % 60 == 0) && (second != 0))
    112.         {
    113.             second = 0;
    114.             minute ++;
    115.         }
    116.        
    117.     }
    118. }
    119.  
    120. // main function  
    121. void main(void)
    122. {
    123.     *trisc = 0xff;
    124.     *trisd = 0 ;
    125.     *trisb = 0;
    126.     *portb = 0;
    127.     *portd = 0xfe;
    128.  
    129.    
    130.     it_tmr0();
    131.     init_it_tmr0();
    132.    
    133.  
    134.     while(1)
    135.     {
    136.    
    137.     }
    138. }
    iq.h
    Code (Text):
    1. #include<delays.h>
    2.  
    3. void delayms(int time)
    4. {
    5.     int i;
    6.     for (i=0; i< time;i++) Delay10TCYx(98);
    7. }
    8.  
    Any ideas on how to fix this?
     
Loading...