24 second shot clock using pic16f877a with pause, inc/dec and reset

Status
Not open for further replies.

arse

Newbie level 3
Joined
Aug 17, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,307
This is actually our midterm project.... The program I made is not actually working.... I
 

Can you upload your work so far? Schematic(s), code, etc? Then we can see where the problem lies and try to fix it.
 

#define DIGIT1 PORTB.F0
#define DIGIT2 PORTB.F1
unsigned char Cnt = 0;
unsigned char Flag = 0;

// This function finds the bit pattern to be sent to the port to display a number
// on the 7-segment LED. The number is passed in the argument list of the function.
//
unsigned char Display(unsigned char no)
{
unsigned char Pattern;
unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F};
Pattern = SEGMENT[no]; // Pattern to return
return (Pattern);
}
//
// TMR0 timer interrupt service routine. The program jumps to the ISR at
// every 5ms.
//
void interrupt ()
{
unsigned char Msd, Lsd;
TMR0L = 100; // Re-load TMR0
INTCON = 0x20; // Set T0IE and clear T0IF
Flag = ~ Flag; // Toggle Flag
if(Flag == 0) // Do digit 1
{
DIGIT2 = 0;
Msd = Cnt / 10; // MSD digit
PORTC = Display(Msd); // Send to PORTC
DIGIT1 = 1; // Enable digit 1
}
else
{ // Do digit 2
DIGIT1 = 0; // Disable digit 1
Lsd = Cnt % 10; // LSD digit
PORTC = Display(Lsd); // Send to PORTC
DIGIT2 = 1; // Enable digit 2
}
}
//
// Start of MAIN Program. configure PORTB and PORTC as outputs.
// In addition, configure TMR0 to interrupt at every 10ms
//
void main()
{
TRISC = 0; // PORTC are outputs
TRISB = 0; // RB0, RB1 are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
//
// Configure TMR0 timer interrupt
//
T0CON = 0xC4; // Prescaler = 32
TMR0L = 100; // Load TMR0L with 100
INTCON = 0xA0; // Enable TMR0 interrupt
Delay_ms(1000);
for(; // Endless loop
{
Cnt++; // Increment Cnt
if(Cnt == 100) Cnt = 0; // Count between 0 and 99
Delay_ms(1000); // Wait 1 second
}
 

Here, I fixed the code:
Code:
#define DIGIT1 PORTB.F0
#define DIGIT2 PORTB.F1
unsigned char Cnt = 0;
unsigned char Flag = 0;

// This function finds the bit pattern to be sent to the port to display a number
// on the 7-segment LED. The number is passed in the argument list of the function.
//
unsigned char Display(unsigned char no)
{
unsigned char Pattern;
unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
Pattern = SEGMENT[no]; // Pattern to return
return (Pattern);
}
//
// TMR0 timer interrupt service routine. The program jumps to the ISR at
// every 5ms.
//
void interrupt (){
     unsigned char Msd, Lsd;
     TMR0 = 100; // Re-load TMR0
     T0IF_bit = 0;
     Flag = ~ Flag; // Toggle Flag
     if(Flag == 0){ // Do digit 1
             DIGIT2 = 0;
             Msd = Cnt / 10; // MSD digit
             PORTC = Display(Msd); // Send to PORTC
             DIGIT1 = 1; // Enable digit 1
     }
     else{ // Do digit 2
           DIGIT1 = 0; // Disable digit 1
           Lsd = Cnt % 10; // LSD digit
           PORTC = Display(Lsd); // Send to PORTC
           DIGIT2 = 1; // Enable digit 2
     }
}
//
// Start of MAIN Program. configure PORTB and PORTC as outputs.
// In addition, configure TMR0 to interrupt at every 10ms
//

void main(){
     PORTC = 0; //Clear initial state
     PORTB = 0; //Clear initial state
     TRISC = 0; // PORTC are outputs
     TRISB = 0; // RB0, RB1 are outputs
     DIGIT1 = 0; // Disable digit 1
     DIGIT2 = 0; // Disable digit 2
     //
     // Configure TMR0 timer interrupt
     //
     OPTION_REG = 0x84; //Prescaler 32
     //T0CON = 0xC4; // Prescaler = 32
     TMR0 = 100; // Load TMR0L with 100
     INTCON = 0xA0; // Enable TMR0 interrupt
     Delay_ms(1000);
     for(;;){ // Endless loop
           Cnt++; // Increment Cnt
           if(Cnt == 100) Cnt = 0; // Count between 0 and 99
           Delay_ms(1000); // Wait 1 second
     }
}

It counts from 0 to 99, count increments each second. I'm guessing you copied the code made for a PIC18 and expected it to work here. PIC16F877A has no TMR0L or TMR0H. Since it's just an 8-bit timer, you have just TMR0. There is no T0CON. So, the prescaler is set using the OPTION_REG register. That's about it. Get your configuration settings right and make sure you connect the hardware right.

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…