Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

using Timer withour interrupt. generating a square wave using timer.

Status
Not open for further replies.

shubham kumar

Member level 3
Member level 3
Joined
Sep 11, 2014
Messages
59
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Location
bangalore
Visit site
Activity points
511
Hi,
I am using PIC18F452. I am trying to generate to square waves of different frequencies depending on switch level. I am using timer but the not the timer flag (TMR1IF_bit) as interrupt.

Is this possible to generate a pulse without using interrupt.



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
void wave_0()
      {
        TMR1L=0x0F; TMR1H=0x0F ;
       TMR1IF_bit=0;
        T1CON= 0x01;
        while(TMR1IF_bit==0);
       TMR1IF_bit=0;
        }
      
        void wave_1()
         {
        TMR1L=0; TMR1H=0 ;
       TMR1IF_bit=0;
        T1CON= 0x01;
       whileTMR1IF_bit==0);
      TMR1IF_bit=0;
         }
  
 void main() {
 
  AN0_bit=0;   AN1_bit=0;   AN2_bit=0;   AN3_bit=0;    // Configure AN pins as Digital I/O and turn off comparators
  
 TRISD.B7=1;
  TRISD.B6=0;
 
   do{
 
     PORTD.B6 = ~ PORTD.B6;
 
    if( TRISD.B7==1)
    {
     wave_0();
    }
  //  Delay_ms(500);
    if( TRISD.B7==0)
    {
     wave_1();
     }
  //   Delay_ms(500);
 
   } while(1);
}


I am trying to implement the above way but not getting through. Tell me if there is any error in that
OR Tell me if any other method exists.
 
Last edited by a moderator:

I did not checked this on datasheet, but if the uC has a PWM module, you could do that with a minimal code.

Yes, the uC has a PWM module. but similar kind of functionality (as that of above code) I implemented on 8051. (using timer not as interrupt but just as a delay by observing the timer flag) but I am not able to do that in PIC18. Obviously it is generations ahead as compared to 8051.

Just as we give command Delay_ms(20), I want to implement using timer.
 

You have to use Timer interrupt to get precise value. PWM is better. What is the clock frequency you are using ?
 

I am using freq of 20MHz

I am trying to generate to square waves of different frequencies depending on switch level. so if I will be using timer interrupt then the program will keep moving from main to ISR whenever the flag is raised.

As till now, with the discussion i had, the answer is we cant use timer without interrupt. Means without interrupt timers are useless. But in 8051uC, i made a program in which the programs waits till the timer flag is raised and after clearing the flag bit it again waits till it is raised again.

As like:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
main(){
--set timer values
while(1)
{
PORTB= PORTB;
--start timer
while(TMR1IF_bit==0); // kind of wait statement
TMR1IF_bit=0;
}
}



with the use of timer interrupt I didn't find a way to send two different frequency waves to the same pin when input at another pin is 1 or 0.
 
Last edited by a moderator:

Personally, I would structure your program differently like this:

Create a single function to make a delay decided by a parameter passed to it. Use the same basic code as you already have but instead of two almost identical functions to make the different delays, use one function and pass the delay (or TMR1 value) to it. That simplifies the code and makes the delay depend upon a variable so it's much easier to adjust.

I can't test it here but something like this:


Code C - [expand]
1
2
3
4
5
6
7
8
9
void waves(unsigned int TMR1Value)
      {
        TMR1H=(TMR1Value & 0xFF00) >> 8;
        TMR1L=TMR1Value & 0x00FF ;
        TMR1IF_bit=0;
        T1CON= 0x01;
        while(TMR1IF_bit==0);
        TMR1IF_bit=0;
       }



You can then call the timer delay with 'waves(0x0f)' and 'waves(0x00)' or pass any other int value to set the time you want. Note that this is a blocking routine, it will sit in the 'while' loop until the timer rolls over and no other code will execute. If you use interrupts it can be made a background task.

It is more accurate to use interrupts. PWM will be just as easy to generate pulses but you may have difficulty with fine tuning the repetition rate.

Brian.
 
If you don't use Interrupts and use a delay or any other process in the while(1) loop then that piece of code will also have to be executed each time the outer while(1) is executed. If timer is used you get precise waves.

What else do you want to do other than generating two square waves ?
 

Hi,

i´m not familiar with PIC..
additionally i also think you should go for PWM.

but
just as a delay by observing the timer flag

usually the flag is SET by the timer, but you have to manually CLEAR it in main loop. the timer won´t clear it.

Klaus
 
try this;


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main(){
 
PORTB = 0;
 
//set timer0 module here with 64ms or desired Interval
 TMR0IF = 0; // clear TMR0 flag
 TMR0IE = 1; // enable TMR0
 
 // don't enable GIE bit to avoid to go to interrupt routine
   while(1){
    
    if(TMR0IF){ // if timer0 flag 
     
     RB0 = !RB0;
     TMR0IF = 0; 
     
    }
   } // end while
} // end main

 
Apart from possible alternatives, the OP is essentially asking how a polled timer delay can be implemented with PIC18.

The answer is, yes, it works exactly as in your code (or the similar code suggested betwixt). So if you don't get the expected result, there's apparently a problem with the timer setup. It's not shown in your code snippet, so we can't know if it's right.
 

Thanks a lot Brian.

I know that if I am not using the interrupt means the program execution will stop till ||||while(TMR1IF_bit==0);||||
I just wanted to know that whether this concept will work or not. You just provided the whole solution. Thanks

Just one more thing,
Since this is not an auto reload timer so I do need to turn off the timer at the end of each function call. and also no need to reset TMRIF_bit at the end as while calling the function it will be reset before timer begins.
 

This is quite great.
Instead for waiting with the while loop, one can use this.

but with this I need to give the value for TMR low and high byte.
But I will use this in some other application.

- - - Updated - - -

Apart from possible alternatives, the OP is essentially asking how a polled timer delay can be implemented with PIC18.

The answer is, yes, it works exactly as in your code (or the similar code suggested betwixt). So if you don't get the expected result, there's apparently a problem with the timer setup. It's not shown in your code snippet, so we can't know if it's right.

Sorry, I just forgot to write that while updating in the forum. Thanks.
 

Apart from possible alternatives, the OP is essentially asking how a polled timer delay can be implemented with PIC18.
The sample code posted here is for PIC16 family. He can update this code for his PIC18 family.

pmk
 

Thank you guys... This was my first post in the forum and you all quite amazed me with your answers.
Truely I didn't expected such a fast response.

P.S.
I'll be eating your heads up :D :D .. just joking
 

This is the square wave output received (MPLAB Simulator Logic Analyzer) using the code (PIC16LF1938) I have posted with timer0 overflow interval of 0.5mSec (1:2 prescaler).

picture attached.

pmk
 

Attachments

  • SQUARE_WAVE_TIMER0.gif
    SQUARE_WAVE_TIMER0.gif
    36.3 KB · Views: 168
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top