|
|
View previous topic :: View next topic |
Author |
Message |
GiG
Joined: 03 Sep 2021 Posts: 39
|
Irregular counting in the timer0,Timer1 PIC |
Posted: Fri Sep 03, 2021 11:50 pm |
|
|
Hi, I do not know whether to open the topic correctly or not.
I had a question about PIC and CCS, I'm a little familiar with it
I wanted to make a 1 second pause with the timer.
I wanted help with the code I wrote.
The problem is Timers that sometimes the timer works but it counts irregularly or it counts and stops somewhere.
Notes:
-The connections to the timer sockets are constantly going up and down.
-The frequency is 8 MHz.
-PIC16F1939
The rest of the functions that are related to the display on a specific segment worked well. I tried counting with simple functions and it had no problems in counting.
Code: | #include <16f1939.h>
#FUSES PUT
#FUSES NOBROWNOUT
#fuses HS,NOWDT,NOPROTECT,NOLVP ,PLL_SW,NOMCLR
#device ADC=10
#use delay(internal=8000000)
#INT_TIMER0
//#INT_TIMER2
#use FIXED_IO( A_outputs=PIN_A7,PIN_A6,PIN_A5,PIN_A4,PIN_A3,PIN_A2,PIN_A1,PIN_A0 )
#use FIXED_IO( B_outputs=PIN_B1,PIN_B0 )
#use FIXED_IO( C_outputs=PIN_C7,PIN_C6,PIN_C5,PIN_C4,PIN_C3,PIN_C2,PIN_C1,PIN_C0 )
#use FIXED_IO( D_outputs=PIN_D7,PIN_D6,PIN_D5,PIN_D4,PIN_D3,PIN_D2,PIN_D1,PIN_D0 )
#use FIXED_IO( E_outputs=PIN_E1,PIN_E0 )
int16 speedb,timex,time_hour ,time_mint,time_sec,dg_hour,yk_hour,dg_mint,yk_mint,h_timer,m_timer,on_timer,second,hour,minute,counter;
int16 hg_dama,sg_dama,dg_dama,yk_dama,seg_buf,chbuf1,chbuf2,chbuf3,h,t;
#define up_led PIN_A0
#define onoff_led PIN_A1
#define dwn_led PIN_A2
#define light_led PIN_A3
//-----------------------------7 segment
#define cm10 PIN_A4
#define cm9 PIN_A5
#define cm8 PIN_E0
#define cm7 PIN_E1
#define cm5 PIN_A6
#define cm6 PIN_A7
#define cm4 PIN_C0
#define cm3 PIN_C1
#define cm2 PIN_C2
#define cm1 PIN_C3
#define d_seg PIN_C4
#define c_seg PIN_C5
#define b_seg PIN_C6
#define a_seg PIN_C7
#define dot_seg PIN_D0
#define g_seg PIN_D1
#define f_seg PIN_D2
#define e_seg PIN_D3
//----------------------------------------- c phase
#define sens1 PIN_B1
#define sens2 PIN_B2
//-----------------------------------------
#define buzzer PIN_B1
#define ir_sensor PIN_B4
//------------------------------------------toch key
#define up_key PIN_B5
#define onoff_key PIN_B6
#define dwn_key PIN_B7
#define light_key PIN_E3
//-------------------------------------------output
#define speed4 PIN_B0
#define speed3 PIN_D7
#define speed2 PIN_D6
#define speed1 PIN_D5
#define light PIN_D4
void show_seg(void){
switch (seg_buf){
Case 0 :
output_high(a_seg);
output_high(b_seg);
output_high(c_seg);
output_high(d_seg);
output_high(e_seg);
output_high(f_seg);
output_low(g_seg);
break;
Case 1:
output_low(a_seg);
output_high(b_seg);
output_high(c_seg);
output_low(d_seg);
output_low(e_seg);
output_low(f_seg);
output_low(g_seg);
break;
Case 2:
output_high(a_seg);
output_high(b_seg);
output_low(c_seg);
output_high( d_seg);
output_high( e_seg);
output_low(f_seg);
output_high( g_seg);
break;
Case 3:
output_high( a_seg);
output_high( b_seg);
output_high(c_seg);
output_high( d_seg);
output_low(e_seg);
output_low(f_seg);
output_high( g_seg);
break;
Case 4:
output_low( a_seg);
output_high( b_seg);
output_high( c_seg);
output_low(d_seg);
output_low(e_seg);
output_high( f_seg);
output_high( g_seg);
break;
Case 5:
output_high( a_seg);
output_low(b_seg);
output_high( c_seg);
output_high( d_seg);
output_low(e_seg);
output_high( f_seg);
output_high( g_seg);
break;
Case 6:
output_high(a_seg);
output_low(b_seg);
output_high(c_seg);
output_high( d_seg);
output_high(e_seg);
output_high(f_seg);
output_high(g_seg);
break;
Case 7:
output_high(a_seg);
output_high(b_seg);
output_high(c_seg);
output_low(d_seg);
output_low(e_seg);
output_low(f_seg);
output_low(g_seg);
break;
Case 8:
output_high(a_seg);
output_high( b_seg );
output_high(c_seg );
output_high(d_seg);
output_high(e_seg);
output_high( f_seg );
output_high(g_seg );
break;
Case 9:
output_high( a_seg);
output_high( b_seg);
output_high(c_seg);
output_high(d_seg);
output_low( e_seg);
output_high( f_seg);
output_high( g_seg);
break;
}
}
void split_display(void)
{
chbuf1=chbuf3/10;
chbuf2=chbuf1*10;
chbuf2=chbuf3-chbuf2;
}
void main(){
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
set_timer0(131);
while (1)
{
if (get_timer0()==0)
{
set_timer0(250);
counter++;
}
if (counter>=20){
minute++;
counter=0;
}
if (minute>=59){
hour++;
minute=0;
}
if (hour>=59){
second=minute=hour=0;
}
output_high(cm1);
output_high(cm2);
output_high(cm3);
output_high(cm4);
output_high(cm5);
output_high(cm6);
output_high(cm7);
output_high(cm8);
output_high(cm9);
output_high(cm10);
chbuf3=hour;
split_display();
dg_hour=chbuf1;
yk_hour=chbuf2;
seg_buf=dg_hour;
show_seg();
output_low( cm5);
Delay_us (10);
output_high( cm5 );
output_low(a_seg);
output_low(b_seg);
output_low(c_seg);
output_low(d_seg);
output_low(e_seg);
output_low(f_seg);
output_low(g_seg);
seg_buf=yk_hour;
show_seg();
output_low( cm6);
Delay_us (10);
output_high( cm6 );
chbuf3=minute;
split_display();
dg_mint=chbuf1;
yk_mint=chbuf2;
seg_buf=dg_mint;
show_seg();
output_low( cm7);
Delay_us (10);//50
output_high( cm7 );
output_low(a_seg);
output_low(b_seg);
output_low(c_seg);
output_low(d_seg);
output_low(e_seg);
output_low(f_seg);
output_low(g_seg);
seg_buf= yk_mint;
show_seg();
output_low( cm8);
Delay_us (10);//50
output_high( cm8 );
output_low(a_seg);
output_low(b_seg);
output_low(c_seg);
output_low(d_seg);
output_low(e_seg);
output_low(f_seg);
output_low(g_seg);
clear_interrupt(INT_TIMER0);
}
}
|
Only the part related to the timer has a problem. Is there another way to write it? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Sep 04, 2021 12:50 am |
|
|
Quote: | while (1)
{
if (get_timer0()==0)
{ |
Why do you think you will see this specific count of 0 ?
Your timer is counting at a rate of 125 KHz, which means it's clocked
every 8 usec. Your while() loop takes much longer than 8 usec to execute.
During that time, Timer0 will pass through many, many counts. The odds
of it being at a count of 0 when you poll it are very low.
Quote: | #INT_TIMER0
//#INT_TIMER2 |
These two lines don't do anything. You have no interrupt routines.
You don't enable interrupts. Delete those two lines.
Quote: |
#fuses HS,NOWDT,NOPROTECT,NOLVP ,PLL_SW,NOMCLR
#device ADC=10
#use delay(internal=8000000) |
If you're specifying the internal oscillator in the #use delay() statement,
then you don't need the HS or PLL_SW fuses. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9284 Location: Greensville,Ontario
|
|
Posted: Sat Sep 04, 2021 5:14 am |
|
|
Had a quick look at your code....
Have a look/read of this thread...
http://www.ccsinfo.com/forum/viewtopic.php?t=26177
it's a SW based Real Time Clock in the Code Library.
You can probably modify and use it.
I did several times, before using the DS3231 RTC came out.
Jay |
|
|
GiG
Joined: 03 Sep 2021 Posts: 39
|
|
Posted: Sat Sep 04, 2021 11:29 pm |
|
|
PCM programmer wrote: | Quote: | while (1)
{
if (get_timer0()==0)
{ |
Why do you think you will see this specific count of 0 ?
Your timer is counting at a rate of 125 KHz, which means it's clocked
every 8 usec. Your while() loop takes much longer than 8 usec to execute.
During that time, Timer0 will pass through many, many counts. The odds
of it being at a count of 0 when you poll it are very low.
Quote: | #INT_TIMER0
//#INT_TIMER2 |
These two lines don't do anything. You have no interrupt routines.
You don't enable interrupts. Delete those two lines.
Quote: |
#fuses HS,NOWDT,NOPROTECT,NOLVP ,PLL_SW,NOMCLR
#device ADC=10
#use delay(internal=8000000) |
If you're specifying the internal oscillator in the #use delay() statement,
then you don't need the HS or PLL_SW fuses. |
Thanks for the tips.
So I used that interrupt instead and the problem is solved. |
|
|
|
|
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
|