ADGAN
Full Member level 5
- Joined
- Oct 9, 2013
- Messages
- 295
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 18
- Activity points
- 1,837
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 void ISR() { counter++; if(counter>=10) flag_10s=0x1; } void main() { while(1) { if(flag_10s) { //put your code here } } }
Hi! How can I run a loop for a certain period of time? suppose I want to run a for loop for 10 seconds. What is the calculation to determine the number of iterations?
Code C - [expand] 1 2 3 while(counter <= 20){ //code to loop }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 unsigned char tick = 0; //Timer1 //Prescaler 1:8; TMR1 Preload = 3035; Actual Interrupt Time : 500 ms //Place/Copy this part in declaration section void InitTimer1(){ T1CON = 0x31; TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDB; TMR1IE_bit = 1; INTCON = 0xC0; } void Interrupt(){ if (TMR1IF_bit){ if(++tick == 20){ tick = 0; TMR1IE_bit = 0; } //Enter your code here TMR1IF_bit = 0; TMR1H = 0x0B; TMR1L = 0xDB; } } //call InitTimer1() whenever you want to start 10 seconds timer if(tick > 0){ //code to run for 10 seconds } else if(tick == 0){ //code to run at other times }
I'm using PIC16F887. But the problem is I'm using timer1 to count pulses. What I want to do is count pulses for 10s.
#define TMR0_PRESET 133 //Precalculated; TMR0 interrupt freq = 1KHz
//------------------------------------------------------------------------------
/* Global Variables */
volatile unsigned int skptmr1; // A skip timer variable
int i;
void main()
{
OPTION_REG = 0x3A;
TMR0 = TMR0_PRESET;
INTCON.T0IF = 0; //Clear TMR0 interrupt flag bit.
INTCON.T0IE = 1; //Enable TMR0 as an interrupt source.
INTCON.GIE = 1; //Enable interrupt operation.
//Initial time delays
skptmr1 = 1000; //1000-ms delay
while(1)
{
if(skptmr1==0) //If 1000ms has passed,
{
for(i=0;i<10;i++){
skptmr1 = 1000; // restart the delay and
}
}
}
}
//------------------------------------------------------------------------------
/* Interrupt Service Routine */
void interrupt(void)
{
if(INTCON.T0IF)
{
TMR0 = TMR0_PRESET; //Load TMR0 with a preset value so that interrupt
// frequency is 1000x per second
if(skptmr1) //If skip timer variable is not yet 0,
--skptmr1; // decrement it
INTCON.T0IF = 0; //Clear TMR0 interrupt flag.
}
return;
}
//------------------------------------------------------------------------------
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #define TMR0_PRESET 133 //Precalculated; TMR0 interrupt freq = 1KHz //------------------------------------------------------------------------------ /* Global Variables */ volatile unsigned int skptmr0; // A skip timer variable int i; void main() { OPTION_REG = 0x3A; TMR0 = TMR0_PRESET; INTCON.T0IF = 0; //Clear TMR0 interrupt flag bit. INTCON.T0IE = 1; //Enable TMR0 as an interrupt source. INTCON.GIE = 1; //Enable interrupt operation. //Initial time delays skptmr0 = 10000; //1000-ms delay while(1) { if(skptmr0 == 0){ } else if(skptmr0){ } } } //------------------------------------------------------------------------------ /* Interrupt Service Routine */ void interrupt(void) { if(T0IE && T0IF) { TMR0 = TMR0_PRESET; //Load TMR0 with a preset value so that interrupt // frequency is 1000x per second if(skptmr1) //If skip timer variable is not yet 0, --skptmr1; // decrement it INTCON.T0IF = 0; //Clear TMR0 interrupt flag. } return; } //------------------------------------------------------------------------------
while(1)
{
if(skptmr1==0) //If 10000ms has passed,
{
skptmr1 = 10000; // restart the delay and...
//...run the task that you want here
}
}
#define TMR0_PRESET 133 //Precalculated; TMR0 interrupt freq = 1KHz
//------------------------------------------------------------------------------
/* Global Variables */
volatile unsigned int skptmr0; // A skip timer variable
int i;
void main()
{
OPTION_REG = 0x3A;
TMR0 = TMR0_PRESET;
TRISA.RA0 = 0;
INTCON.T0IF = 0; //Clear TMR0 interrupt flag bit.
INTCON.T0IE = 1; //Enable TMR0 as an interrupt source.
INTCON.GIE = 1; //Enable interrupt operation.
//Initial time delays
skptmr0 = 10000; //1000-ms delay
while(1)
{
if(skptmr0 == 0){
PORTA.RA0 = 1;
}
}
}
//------------------------------------------------------------------------------
/* Interrupt Service Routine */
void interrupt(void)
{
if(T0IE && T0IF)
{
TMR0 = TMR0_PRESET; //Load TMR0 with a preset value so that interrupt
// frequency is 1000x per second
if(skptmr0) //If skip timer variable is not yet 0,
--skptmr0; // decrement it
INTCON.T0IF = 0; //Clear TMR0 interrupt flag.
}
return;
}
//------------------------------------------------------------------------------
Code C - [expand] 1 TMR0IE_bit = 0;
while(1)
{
if(skptmr0 == 0){ //Blink the LED every 1000-ms
skptmr0 = 1000; //LED connected to RA0 pin
PORTA.RA0 ^= 1;
}
}
When skptimer0 becomes 0 you have to stop the timer.
#define TMR0_PRESET 133 //Precalculated; TMR0 interrupt freq = 1KHz
//------------------------------------------------------------------------------
/* Global Variables */
volatile unsigned int skptmr0; // A skip timer variable
int i;
void main()
{
OPTION_REG = 0x3A;
TMR0 = TMR0_PRESET;
TRISA.RA0 = 0;
INTCON.T0IF = 0; //Clear TMR0 interrupt flag bit.
INTCON.T0IE = 1; //Enable TMR0 as an interrupt source.
INTCON.GIE = 1; //Enable interrupt operation.
//Initial time delays
skptmr0 = 10000; //1000-ms delay
if(skptmr0 == 0){
PORTA.RA0 ^= 1;
INTCON.T0IE = 0;
}
}
//------------------------------------------------------------------------------
/* Interrupt Service Routine */
void interrupt(void)
{
if(T0IE && T0IF)
{
TMR0 = TMR0_PRESET; //Load TMR0 with a preset value so that interrupt
// frequency is 1000x per second
if(skptmr0) //If skip timer variable is not yet 0,
--skptmr0; // decrement it
INTCON.T0IF = 0; //Clear TMR0 interrupt flag.
}
return;
}
//------------------------------------------------------------------------------
#define TMR0_PRESET 133 //Precalculated; TMR0 interrupt freq = 1KHz
//------------------------------------------------------------------------------
/* Global Variables */
volatile unsigned int skptmr0; // A skip timer variable
int i;
void main()
{
OPTION_REG = 0x3A;
TMR0 = TMR0_PRESET;
TRISA.RA0 = 0;
INTCON.T0IF = 0; //Clear TMR0 interrupt flag bit.
INTCON.T0IE = 1; //Enable TMR0 as an interrupt source.
INTCON.GIE = 1; //Enable interrupt operation.
//Initial time delays
skptmr0 = 10000; //1000-ms delay
do
{
if(skptmr0 == 0)
{
PORTA.RA0 ^= 1;
skptmr0 = 10000;
}
while(1);
}
//------------------------------------------------------------------------------
/* Interrupt Service Routine */
void interrupt(void)
{
if(T0IE && T0IF)
{
TMR0 = TMR0_PRESET; //Load TMR0 with a preset value so that interrupt
// frequency is 1000x per second
if(skptmr0) //If skip timer variable is not yet 0,
--skptmr0; // decrement it
INTCON.T0IF = 0; //Clear TMR0 interrupt flag.
}
return;
}
#define TMR0_PRESET 133 //Precalculated; TMR0 interrupt freq = 1KHz
//------------------------------------------------------------------------------
/* Global Variables */
volatile unsigned int skptmr0; // A skip timer variable
int i;
void main()
{
OPTION_REG = 0x3A;
TMR0 = TMR0_PRESET;
TRISA.RA0 = 0;
INTCON.T0IF = 0; //Clear TMR0 interrupt flag bit.
INTCON.T0IE = 1; //Enable TMR0 as an interrupt source.
INTCON.GIE = 1; //Enable interrupt operation.
//Initial time delays
skptmr0 = 10000; //1000-ms delay
while(1)
{
// let's the interrupt do the job
}
}
//------------------------------------------------------------------------------
/* Interrupt Service Routine */
void interrupt(void)
{
if(T0IE && T0IF)
{
TMR0 = TMR0_PRESET; //Load TMR0 with a preset value so that interrupt
// frequency is 1000x per second
if(skptmr0) //If skip timer variable is not yet 0,
{
--skptmr0; // decrement it
}
else
{
PORTA.RA0 ^= 1;
skptmr0 = 10000;
}
INTCON.T0IF = 0; //Clear TMR0 interrupt flag.
}
return;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?