|
|
View previous topic :: View next topic |
Author |
Message |
leevise
Joined: 05 Aug 2010 Posts: 89
|
Timer0 and timer1 function (flag bit how to deal ) |
Posted: Wed Dec 19, 2012 7:08 pm |
|
|
Hello guys
I used the timer0 and timer1 as timer clock, but I use the PICC code. Now I need make it to CCS code, there are some questions when I translate the code.
1) how to open or close the Timer1 using CCS ? (PICC can control T1ON bit of T1CON register come true this function).
2) how to set two flags bit to timer0 and timer1 in CCS, as PICC TIM0IF and TIM1IF ?
the part of PICC code : Code: |
*********timer0/timer init********/
void timer_init()
{
GIE=1;
PEIE=1;
T0CS=0;
PSA=0;
PS0=1;
PS1=1;
PS2=1; //1:256
T0IE=1;
TMR0=248; //2ms
T1CKPS0=1;T1CKPS1=1; //1:8
TMR1CS=0;
TMR1IE=1;
[B]TMR1ON=1; [/B]
TMR1H=0xfb; //10ms
TMR1L=0x1e; //
}
/********interrupt ********/
void interrupt ISR(void)
{
[B]if (TMR0IF==1)[/B]
{
TMR0IF=0;
TMR0=248; //TMR0 2ms
Display();
}
[B]if (TMR1IF==1)[/B]
{
TMR1IF=0; //
TMR1H=0xfb; //10ms
TMR1L=0x1e;
count_10ms++; //10ms+1
if(count_10ms >= 100)
{
count_10ms = 0; //
sec++; //sec+1
if(sec == 60)
{
sec = 0;
min++; //min+1
if(min ==60)
{
min = 0;
hour++; //hour+1
if(hour ==24)
{
hour = 0;min=0;sec=0;
}
}
}
}
}
}
/********Key********/
void KeyProcess()
{
TMR1ON=0; //press K1,T1 open
if(K2==0) //press K2
{
Delay_ms(10);
if(K2==0)
{
while(!K2); //wait K2 release
beep();
hour++; //adjust hour
if(hour==24)
{
hour = 0;
}
}
}
if(K3==0) //press K3
{
Delay_ms(10);
if(K3==0)
{
while(!K3); //K3 release
beep();
min++; //adjust min
if(min==60)
{
min = 0;
}
}
}
if(K4==0) //press K4
{
Delay_ms(10);
if(K4==0)
{
while(!K4); //waiting for K4 release
beep();
TMR1ON=1; //TI start
K1_FLAG=0; //flag=0
}
}
}
/********main********/
void main(void)
{
port_init();
timer_init();
while(1)
{
if(K1==0)
{
Delay_ms(10);
if(K1==0)
{
while(!K1);
K1_FLAG=1; }
}
if(K1_FLAG==1)KeyProcess();
conv(hour,min,sec);
}
}
|
I translate the PICC code to CCS code as follow:
Code: |
#int_timer0
void timer0_init()
{
static int flag;
if (flag==1)//(TMR0IF==1)
{
flag=0;//TMR0IF=0;
set_timer0(0xf8); //TMR0=248;
Display();
}
}
#int_timer1
void timer1_init()
{
static int flag;
if (flag==1)
{
[B] flag=0;// is this define process? [/B]
set_timer1(0xfb);
/*TMR1IF=0; //
TMR1H=0xfb; //
TMR1L=0x1e; //
*/
count_10ms=0;
count_10ms++;
if(count_10ms >= 100)
{
count_10ms = 0;
sec++;
if(sec == 60)
{
sec = 0;
min++;
if(min ==60)
{
min = 0;
hour++;
if(hour ==24)
{
hour = 0;min=0;sec=0;
}
}
}
}
}
}
/****************/
void KeyProcess()
{
setup_timeR_1(T1_DISABLED);//=0;
if(key1==0)
{
delay_ms(10);
if(key1==0)
{
while(!key1);
//beep();
hour++;
if(hour==24)
{
hour = 0;
}
}
}
if(key2==0)
{
delay_ms(10);
if(key2==0)
{
while(!key2);
//beep();
min++;
if(min==60)
{
min = 0;
}
}
}
if(key3==0)
{
delay_ms(10);
if(key3==0)
{
while(!key3);
//beep();
setup_timeR_1(T1_INTERNAL);
key0_FLAG=0;
}
}
}
/****************/
void main(void)
{
port_init();
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(0xf8); //TMR0 2ms
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
set_timer1(0xfb);
enable_interrupts(int_timer0);
enable_interrupts(int_timer1);
enable_interrupts(global);
while(1)
{
if(key0==0)
{
delay_ms(10);
if(key0==0)
{
while(!key0);
Key0_FLAG=1;
}
}
if(key0_FLAG==1)KeyProcess();
conv(hour,min,sec);
}
}
|
pls explain me about these questions!
Thank you
Last edited by leevise on Thu Dec 20, 2012 7:56 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Dec 20, 2012 5:45 am |
|
|
Code: |
#int_timer0
void timer0_int() //Um. It is an interrupt, not an initialisation
{
//Er. If the interrupt is called, then TMR0IF, will always be set
//The compiler automatically clears it.
set_timer0(0xf8); //TMR0=248;
Display();
}
//Now, you don't show us 'Display'.
//Same basic layout for timer1, with a couple of extra point
//When you declare 'count_10msec', use:
int8 count_10mSec=0;
//This does the initialisation to zero, which you have put into the ISR...
#int_timer1
void timer1_int() {
set_timer1(0xFB1E); //you need both bytes - 16bit value...
//why are you then clearing count_10ms?. This does not happen in the
//assembler ISR. It needs to be where it is initialised.
etc...
|
Comments:
The CCS msec delay function, is 'delay_ms', not 'Delay_ms'. This will cause problems if you switch to use case significant mode. You have similar case problems with 'setup_timeR_1' |
|
|
leevise
Joined: 05 Aug 2010 Posts: 89
|
|
Posted: Thu Dec 20, 2012 7:47 am |
|
|
I make a clock via segment display. The code was programed by PICC, but I want to translate to CCS code,there are some flag bit is used in PICC, but how to translating CCS code ? pls help me
about CCS code ,I found some one use the "sTatic int flag", is it not pre-define?there is a syntax "clear_interrupt(timer0)", maybe use this ?
oh my god ,I am very puzzle!
when used the TOMER0 and TIMER1, and we need 2 flag bit ,how to distinguish which is TOMER0' TOIF bit ,which one is the TIMER1' T1IF bit? , i am very puzzling
:
Another question: how to open or closed the TION by CCS code ? is it "setup_timer_1(T1_DISABLED);
and set_timer_1(xxxxxx)"?
Code: |
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(HS&WDTDIS&LVPDIS);
uchar hour=12,min=00,sec=00; //define hour, min ,second
uchar count_10ms; //define 10ms counter
#define K1 RB2 //define K1
#define K2 RB3 //define K2
#define K3 RB4 //defineK3
#define K4 RB5 //defineK4
#define BEEP RE0 //define beep
uchar K1_FLAG=0; //define press key flag,when pressed K1,the flag set 1,un-press K1,the bit is 0
uchar const bit_tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//select which one segment works
uchar const seg_data[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0xbf};
//
uchar disp_buf[8]; //define a buffer
/****************/
void port_init(void)
{
OPTION=0x00; //port b pull up
TRISC = 0x00; //set port c output
TRISD = 0x00; //portd data output
ADCON1=0x06; //
TRISE=0x00; //(RE0)
PORTE=0xff;
}
/****************/
void Delay_ms(uint xms)
{
int i,j;
for(i=0;i<xms;i++)
{ for(j=0;j<71;j++) ; }
}
/*********buzzer function********/
void beep()
{
BEEP=0; //buzzer ring
Delay_ms(100);
BEEP=1; //shut off buzzer
Delay_ms(100);
}
/********segment convert function********/
void conv(uchar in1,uchar in2,uchar in3) //set parameters in1、in2、in3can receive the hour、min、sec
{
disp_buf[0] =in1/10; // dicimal of hour
disp_buf[1] = in1%10; // figure of hour
disp_buf[3] = in2/10; // minute's dicimal
disp_buf[4] = in2%10; // minute's figure
disp_buf[6] = in3/10; //second's dicimal
disp_buf[7] = in3%10; // second's figure
//disp_buf[2] = 17; // display"-"
disp_buf[5] = 17; // display"-"
}
/****************/
void Display()
{
uchar tmp; //
static uchar disp_sel=0; //
tmp=bit_tab[disp_sel]; //
PORTC=tmp; //
tmp=disp_buf[disp_sel]; //
tmp=seg_data[tmp];
PORTD=tmp; //
disp_sel++; //
if(disp_sel==8)
disp_sel=0; //
}
/****************/
void timer_init()
{
GIE=1; //
PEIE=1;
T0CS=0;
PSA=0;
PS0=1;
PS1=1;
PS2=1; //IMER0 Prescaler 1:256
T0IE=1; //overflow enable
TMR0=248; //TMR0 assignment 2ms
T1CKPS0=1;T1CKPS1=1; //timer1 prescaler1:8
TMR1CS=0; //
TMR1IE=1; //
TMR1ON=1; //open the timer1 interrupt
TMR1H=0xfb; //10ms
TMR1L=0x1e; //
}
/****************/
void interrupt ISR(void)
{
if (TMR0IF==1)
{
TMR0IF=0; //clear TMR1 overflow flag
TMR0=248; //TMR0 assignment 2ms
Display(); //callback display function
}
if (TMR1IF==1)
{
TMR1IF=0; //clear TMR1 over flow flag
TMR1H=0xfb; //,timing time 10ms
TMR1L=0x1e; //
count_10ms++; //10ms+1
if(count_10ms >= 100)
{
count_10ms = 0; //judge if the counter get to100
sec++; //
if(sec == 60)
{
sec = 0;
min++; //if reach the 60second,then minute add 1
if(min ==60)
{
min = 0;
hour++; //if reach 60 minute,then hour will add 1
if(hour ==24)
{
hour = 0;min=0;sec=0; //
}
}
}
}
}
}
/****************/
void KeyProcess()
{
TMR1ON=0; //if press K1,then shut down T1,clock pause
if(K2==0) //if press K2
{
Delay_ms(10); //eliminating the signal-wobble
if(K2==0)
{
while(!K2); //waiting for K2 release
beep();
hour++; //adjust hour
if(hour==24)
{
hour = 0;
}
}
}
if(K3==0) //if press K3
{
Delay_ms(10);
if(K3==0)
{
while(!K3); //waiting for K3 release
beep();
min++; //adjust minute
if(min==60)
{
min = 0;
}
}
}
if(K4==0) //if press K4
{
Delay_ms(10);
if(K4==0)
{
while(!K4); //waiting for K4 release
beep();
TMR1ON=1; //after adjusting ,clock go on
K1_FLAG=0; //when press K1 ,the flag become 0
}
}
}
/****************/
void main(void)
{
port_init();
timer_init();
while(1)
{
if(K1==0) //
{
Delay_ms(10); //
if(K1==0)
{
while(!K1); //
beep(); //
K1_FLAG=1;
}
}
if(K1_FLAG==1)KeyProcess();
conv(hour,min,sec);
}
}
| |
|
|
leevise
Joined: 05 Aug 2010 Posts: 89
|
|
Posted: Fri Dec 21, 2012 1:07 am |
|
|
Ttelmah wrote: | Code: |
#int_timer0
void timer0_int() //Um. It is an interrupt, not an initialisation
{
//Er. If the interrupt is called, then TMR0IF, will always be set
//The compiler automatically clears it.
set_timer0(0xf8); //TMR0=248;
Display();
}
//Now, you don't show us 'Display'.
//Same basic layout for timer1, with a couple of extra point
//When you declare 'count_10msec', use:
int8 count_10mSec=0;
//This does the initialisation to zero, which you have put into the ISR...
#int_timer1
void timer1_int() {
set_timer1(0xFB1E); //you need both bytes - 16bit value...
//why are you then clearing count_10ms?. This does not happen in the
//assembler ISR. It needs to be where it is initialised.
etc...
|
Comments:
The CCS msec delay function, is 'delay_ms', not 'Delay_ms'. This will cause problems if you switch to use case significant mode. You have similar case problems with 'setup_timeR_1' |
Oh, thank you for your advice.
but the PICC code is as follow:
Code: | void interrupt ISR(void)
{
if (TMR0IF==1)
{
TMR0IF=0; //clear TMR1 overflow flag
TMR0=248; //TMR0 assignment 2ms
Display(); //callback display function
}
if (TMR1IF==1)
{
TMR1IF=0; //clear TMR1 over flow flag
TMR1H=0xfb; //,timing time 10ms
TMR1L=0x1e; //
count_10ms++; //10ms+1
if(count_10ms >= 100)
{
count_10ms = 0; //judge if the counter get to100
sec++; //
if(sec == 60)
{
sec = 0;
min++; //if reach the 60second,then minute add 1
if(min ==60)
{
min = 0;
hour++; //if reach 60 minute,then hour will add 1
if(hour ==24)
{
hour = 0;min=0;sec=0; //
}
}
}
}
}
} |
How to deal it with CCS code ?I don't know what to do.
pls guide me translate the PICC ISR code to CCS int code. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Dec 21, 2012 2:55 am |
|
|
Seriously, use your eyes, brain, manual and examples:
This:
Code: |
void interrupt ISR(void)
{
if (TMR0IF==1)
{
TMR0IF=0; //clear TMR1 overflow flag
TMR0=248; //TMR0 assignment 2ms
Display(); //callback display function
}
//does exactly the same as (in CCS C), this:
#int_timer0
void timer0_int() {
set_timer0(0xf8); //TMR0=248;
Display();
}
|
The compiler tests the interrupt flag, and only calls this routine, if TMROIF==1. It also clears the interrupt flag for you.
We are not here to write the code for you. I've shown what needs to change, do it. |
|
|
leevise
Joined: 05 Aug 2010 Posts: 89
|
|
Posted: Fri Dec 21, 2012 7:18 am |
|
|
Ttelmah wrote: | Seriously, use your eyes, brain, manual and examples:
This:
Code: |
void interrupt ISR(void)
{
if (TMR0IF==1)
{
TMR0IF=0; //clear TMR1 overflow flag
TMR0=248; //TMR0 assignment 2ms
Display(); //callback display function
}
//does exactly the same as (in CCS C), this:
#int_timer0
void timer0_int() {
set_timer0(0xf8); //TMR0=248;
Display();
}
|
The compiler tests the interrupt flag, and only calls this routine, if TMROIF==1. It also clears the interrupt flag for you.
We are not here to write the code for you. I've shown what needs to change, do it. |
Thank you. I understand your means. My main puzzle is the interrupt flag in the past. Now I am ok. |
|
|
|
|
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
|