Stopwatch timer programming MPLAB [PIC18F452]

Thread Starter

Vlad Moglan

Joined Dec 31, 2014
25
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:
#include "iq.h"
#include "it.h"
#define byte unsigned char
/* variables globale */
byte *trisb = 0xf93 ;
byte *portb = 0xf81 ;
byte *trisd = 0xf95 ;
byte *portd = 0xf83 ;
byte *trisc = 0xf94;
byte *portc = 0xf82;
int i=0;
int nb=0;
int second = 0;
int minute = 0;

// function prototypes
void affiche_7seg(byte);

// function implementations
void affiche_7seg(byte val)
{
    byte
    tab_7seg[]={0b00111111,0b00000110,0b01011011,0b01001111,
    0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,
    0b01101111};
    *portb=tab_7seg[val];
}

void it_int0()
{}

void it_int1()
{}

/* interruption timer */
void it_tmr0()
{
    i++;
    affiche_7seg(second % 10);
    affiche_7seg(minute % 10);
    if ((i % 10) == 0) second ++;
    if ((second % 10) == minute++);
}

// main function   
void main(void)
{
    *trisc = 0xff;
    *trisd = 0 ;
    *trisb = 0;
    *portb = 0;
    *portd = 0xfe;     // if this is d, affiche_7seg will show the number on the next to last lcd panel
    init_it_tmr0();
    while(1)
    {
    }
}
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.
 

Attachments

Dodgydave

Joined Jun 22, 2012
9,938
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,
 

Thread Starter

Vlad Moglan

Joined Dec 31, 2014
25
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,
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.
 

Thread Starter

Vlad Moglan

Joined Dec 31, 2014
25
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
 

Dodgydave

Joined Jun 22, 2012
9,938
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.
 

Thread Starter

Vlad Moglan

Joined Dec 31, 2014
25
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:
#include "iq.h"
#include "it.h"
#define byte unsigned char
#define TRUE 1
#define FALSE 0


/* variables globale */
byte *trisb = 0xf93 ;
byte *portb = 0xf81 ;
byte *trisd = 0xf95 ;
byte *portd = 0xf83 ;
byte *trisc = 0xf94;
byte *portc = 0xf82;
int i=0;
int nb=0;
int second = 0;
int minute = 0;
int start = 0;



// function prototypes
void affiche_7seg(byte);
void it_tmr0();
void startStopwatch();
void stopStopwatch();
void resetStopwatch();

// function implementations

// function that shows input value on selected panel
void affiche_7seg(byte val)
{
    byte
    tab_7seg[]={0b00111111,0b00000110,0b01011011,0b01001111,
    0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,
    0b01101111};
    *portb=tab_7seg[val];
}

// function that starts the stopwatch
void startStopwatch()
{
    if ((*portc&0b00000001) == 1)
    {
        start = TRUE;
    }
}

// function that stops the stopwatch
void stopStopwatch()
{
    if ((*portc&0b00000010) == 2)
    {
        start = FALSE;
    }
}

// function that resets the stopwatch
void resetStopwatch()
{
    if ((*portc&0b00000100) == 4)
    {
        minute = 0;
        second = 0;
        *portc = 0x00;
        start = FALSE;
    }
}

void it_int0()
{}

void it_int1()
{}

/* interruption timer */
void it_tmr0()
{
    resetStopwatch();
    startStopwatch();
    stopStopwatch();

    if (start == TRUE){

        i++;
        // displaying the second digit of second on the last panel
        *portd = 0xfe;
        affiche_7seg(second % 10);
        delayms(1);
   
        // displaying the first digit of second on the 3rd panel
        *portd = 0xfd;
        affiche_7seg((second/10) % 10);
        delayms(1);
   
        // displaying the first digit of minutes on the second panel
        *portd = 0xfb;
        affiche_7seg(minute%10);
        delayms(1);
   
        // display the first digit of minutes on the first panel
        *portd = 0xf7;
        affiche_7seg((minute%10) % 10);
        delayms(1);
   
        if ((i % 10) == 0) second ++;    // incrementing second

        // incrementing minute
        if ((second % 60 == 0) && (second != 0))
        {
            second = 0;
            minute ++;
        }
       
    }
}

// main function   
void main(void)
{
    *trisc = 0xff;
    *trisd = 0 ;
    *trisb = 0;
    *portb = 0;
    *portd = 0xfe;

   
    it_tmr0();
    init_it_tmr0();
   

    while(1)
    {
   
    }
}
iq.h
Code:
#include<delays.h>

void delayms(int time)
{
    int i;
    for (i=0; i< time;i++) Delay10TCYx(98);
}
Any ideas on how to fix this?
 
Top