|
|
View previous topic :: View next topic |
Author |
Message |
thedarkotaku88
Joined: 11 Feb 2013 Posts: 1
|
question on pic16f877a 6digit clock, need it to count down |
Posted: Mon Feb 11, 2013 7:23 pm |
|
|
hello everyone. i've gotten this code to work as a 24 hour clock, but i was wondering if anyone could help me with making it count down instead of up? any insight would be very helpful. heres the code:
Code: |
#include <16F877A.h>
#fuses XT,NOWDT,PUT,NOBROWNOUT,NOLVP
#use delay(clock = 4000000)
#byte PORTA=0X05
#byte PORTC=0x07
#byte PORTB=0X06
#define A0 PIN_A0
#define A1 PIN_A1
#define A2 PIN_A2
#define A3 PIN_A3
#define A4 PIN_A4
int sec1,sec2,min1,min2,hour1,hour2;
int counter=125;
int flag=0;
const int table[10]={0b00111111,
0b00000110,
0b01011011,
0b01001111,
0b01100110,
0b01101101,
0b01111101,
0b00000111,
0b01111111,
0b01100111};
void main()
{
PORTB=0X00;
PORTC=0X00;
set_tris_a(0xff);
set_tris_c(0x00);
set_tris_b(0x00);
set_RTCC(6);
setup_counters(RTCC_INTERNAL,RTCC_DIV_32);
enable_interrupts (INT_RTCC);
enable_interrupts (GLOBAL);
for(;;)
{
PORTB=table[sec1];
PORTC=0X01;
delay_ms(2);
PORTB=table[sec2];
PORTC=0x02;
delay_ms(2);
PORTB=table[min1];
PORTC=0x04;
delay_ms(2);
PORTB=table[min2];
PORTC=0x08;
delay_ms(2);
PORTB=table[hour1];
PORTC=0x10;
delay_ms(2);
PORTB=table[hour2];
PORTC=0x20;
delay_ms(2);
if(flag==1){
flag=0;
}
if(input(A0)==0)
{
disable_interrupts(INT_RTCC);
if(input(A1)==0){
delay_ms(250);
if(++min1>9) {
min1=0;
if(++min2>5) {
min2=0;
}
}
}
if(input(A2)==0){
delay_ms(250);
if( ++hour1>9 ){
hour1=0;
++hour2;
}
if( (hour2==2) & (hour1>3) ) {
sec1=0;
sec2=0;
min1=0;
min2=0;
hour1=0;
hour2=0;
}
}
}
else if(input(A0)==1){
enable_interrupts(INT_RTCC);
}
}
}
#int_RTCC
void microII()
{ //void switch_time();
if(--counter>0)return;
counter=125;
flag=1;
if(++sec1>9){
sec1=0;
if(++sec2>5){
sec2=0;
if(++min1>9){
min1=0;
if(++min2>5){
min2=0;
if( ++hour1>9 )
{
hour1=0;
++hour2;
}
if( (hour2==2) & (hour1>3) ) {
sec1=0;
sec2=0;
min1=0;
min2=0;
hour1=0;
hour2=0;
}
}
}
}
}
}
|
thanks in advance:) |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Feb 12, 2013 3:07 am |
|
|
Depends on exactly what you want to do.
Do you want to:-
1) Count down permanently?
OR
2) Count in either direction depending on the state of a button?
Mike
EDIT
3) Is your clock real or Proteus? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Tue Feb 12, 2013 3:47 am |
|
|
Also, down to 1, or down to 0?.
It's possibly the most complex way to do this possible. Much simpler to just have a single number counting, and then split this to form the required output digits. Having separate units and tens counters for everything is really labour intensive.
Code: |
#include <stdlib.h>
int8 counter;
idiv digits;
//no timings involved, but just showing how to count down
for (count=12;count>0;count--) {
digits=div(count,10);
//digits.quot will be the tens of hours, digits.rem will be the unit hours
//display how you want.
}
|
Much easier to count with just a counter going from 12 to 0, than fiddle around trying to count the units and tens separately. The div function allows you to split the digits and tens when you want.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|