Use of PIC Timer

Publisher:LovingLife2023Latest update time:2016-11-04 Source: eefocusKeywords:PIC  Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
TIM0 query method makes the LED flash for one second without using pre-scaling

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

             

              if(T0IF==1) //Judge whether the interrupt overflow bit overflows. Whether TOIF overflows has nothing to do with whether the total interrupt is enabled.

              {

                     T0IF=0; //need software clear

                     intnum1++;

                     if(intnum1==3906)//One second has passed

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }

                    

                     }

                      

              }

 

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

       OPTION=0x08; //Use the internal clock signal, and assign the prescaler to the WDT module, which is equivalent to not setting the prescaler for TM0.

       //One clock cycle is one second. When the initial value is not loaded, it overflows after 256 microseconds because it is an 8-bit timer.

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

TIM0 query method makes the LED flash for one second, using pre-scaling

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

             

              if(T0IF==1) //Judge whether the interrupt overflow bit overflows. Whether TOIF overflows has nothing to do with whether the total interrupt is enabled.

              {

                     T0IF=0; //need software clear

                     TMR0=61; //Re-initialize the timer.

                     intnum1++;

                     if(intnum1==20)//One second has passed

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }

                    

                     }

                      

              }

 

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

       OPTION=0x07; //Use the internal clock signal, the prescaler is assigned to the TIM0 module, and the frequency is divided by 256.

       //One clock cycle is one second. When the initial value is not loaded, it overflows after 256 microseconds because it is an 8-bit timer.

       TMR0=61; //256*Y=50000, =>Y=195, 256-195=61, so overflow occurs once every 50ms, and overflow occurs 20 times, which equals 1s.

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

TIM0 interrupt method makes the LED flash for one second, using pre-scaling

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

      

                     if(intnum1==2)//One second has passed

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }                  

                     }           

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

       OPTION=0x07; //Use the internal clock signal, the prescaler is assigned to the TIM0 module, and the frequency is divided by 256.

       //One clock cycle is one second. When the initial value is not loaded, it overflows after 256 microseconds because it is an 8-bit timer.

       INTCON=0xa0; //GIE=1, turn on the general interrupt, T0IE=1, turn on the T0 interrupt, T0IE is the TMR0 overflow interrupt enable bit.

       TMR0=61; //256*Y=50000, =>Y=195, 256-195=61, so overflow occurs once every 50ms, and overflow occurs 20 times, which equals 1s.

}

void interrupt time0()

{

       T0IF=0; //Since only TMR0 interrupt is enabled, there is no need to check which interrupt it is. The interrupt that comes in must be TMR0 overflow interrupt, so clear the interrupt overflow flag directly.

       TMR0=61;

       intnum1++;

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

The TMR1 interrupt method and the TIM0 interrupt method make the LED flash for one second without setting the prescaler.

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

      

                     if(intnum1==20)//One second has passed

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }                  

                     }           

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

 

       INTCON=0xc0; //GIE=1, open the general interrupt, open the first peripheral interrupt

       PIE1=0x01; // Enable the interrupt of timer 1

       TMR1L=(65536-50000)%256;

       TMR1H=(65536-50000)/256; //Enter an interrupt, which is 50ms,

 

       T1CON=0x01; //Do not set pre-scaling, turn off the crystal enable control bit of timer 1, synchronize with the external clock, select the internal clock, enable timer 1,

 

}

void interrupt time1()

{

       TMR1IF=0; // Clear the interrupt overflow flag.

       TMR1L=(65536-50000)%256;

       TMR1H=(65536-50000)/256;

      

       intnum1++;

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

TMR1 interrupt method TIM0 interrupt method makes LED flash for 400ms and sets pre-scaling

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

      

              /* if(intnum1==20)//One second has passed

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }                  

                     }*/         

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

 

       INTCON=0xc0; //GIE=1, open the general interrupt, open the first peripheral interrupt

       PIE1=0x01; // Enable the interrupt of timer 1

       TMR1L=(65536-50000)%256;

       TMR1H=(65536-50000)/256; //If the pre-scaling frequency is not set, the interruption time is 50ms. Now set the pre-scaling frequency to 8 times, and the interruption time is 400ms.

 

       T1CON=0x31; //Set 8 times pre-scaling, turn off the crystal enable control bit of timer 1, synchronize with the external clock, select the internal clock, enable timer 1,

 

}

void interrupt time1()

{

       TMR1IF=0; // Clear the interrupt overflow flag.

       TMR1L=(65536-50000)%256;

       TMR1H=(65536-50000)/256;

      

       //intnum1++;

              intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }                  

 

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

TMR2 pre-scaling and post-scaling      

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

      

                     if(intnum1==1000) //Originally, when the pre-scaling frequency was 1:1, it was 200ms. Now the pre-scaling frequency is 4. So it is 200*4 ms. Since the post-scaling frequency is 1:2, it is 200*4*2 ms

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }                  

                     }    

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

 

       INTCON=0xc0; //GIE=1, open the general interrupt, open the first peripheral interrupt peripheral function module interrupt

       PIE1=0x02; // Enable the interrupt of timer 2

 

       TMR2=56;

       T2CON = 0x0d; // Pre-scaling 1:4, enable tmr2 count enable/disable control bit, pre-scaling 1:4 post-scaling 1:2,

 

}

void interrupt time1()

{

       TMR2IF=0; // Clear the interrupt overflow flag.

       TMR2=56;

      

       intnum1++;

                           

 

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

TMR2 Prescaler and Postscaler Period Register

#include

#define uint unsigned int

#define uchar unsigned char

#define input  RA3

#define clk  RA5

#define cs_led  RE0

 

__CONFIG(0x3B31);

 

void init();

void delay(uint);

void write_164(uchar);

uint intnum1,intnum2;

void main()

{

       heat();

       while(1)

       {

      

                     if(intnum1==1000) //Originally, when the pre-scaling frequency was 1:1, 100 ms had arrived. Now the pre-scaling frequency is 4, so 100*4 ms has arrived. Since the post-scaling frequency is 1:2, it is 100*4*2 ms

                     {

                            intnum1=0;

                            intnum2++;

                            cs_led=0;

                            if(intnum2==1)

                            write_164(0xfd);

                            if(intnum2==2)

                            {

                                   intnum2=0;

                                   write_164(0xff);

                           

                            }                  

                     }    

       }

}

void init()

{

       TRISA=0b11010111;

       TRISE=0b11111110;

 

       INTCON=0xc0; //GIE=1, open the general interrupt, open the first peripheral interrupt peripheral function module interrupt

       PIE1=0x02; // Enable the interrupt of timer 2

 

       TMR2=0;

       PR2=100; //Period register

       T2CON = 0x0d; // Pre-scaling 1:4, enable tmr2 count enable/disable control bit, pre-scaling 1:4 post-scaling 1:2,

 

}

void interrupt time1()

{

       TMR2IF=0; // Clear the interrupt overflow flag.

       //TMR2=56;

      

       intnum1++;

                           

 

}

void delay(uint x)

{

       uint a,b;

       for(a=x;a>0;a--)

              for(b=110;b>0;b--);

 

}

void write_164(uchar dt)

{

       flying i;  

       for(i=0;i<8;i++)

       {

              clk=0;    

             

              if(dt&0x80)

                     input=1;

              else

                     input=0;

              dt=dt<<1;      

                    

              clk=1;          

       }           

}

Keywords:PIC  Timer Reference address:Use of PIC Timer

Previous article:Application of PIC interrupt (Part 2)
Next article:PIC 18XXX PORT LAT

Recommended ReadingLatest update time:2024-11-16 15:57

PIC12F629 IO Configuration
I debugged the program for an afternoon today and kept looking for a problem. I used PIC12F629's GPIO0, GPIO1, and GPIO2 as outputs, but only GPIO2 could output normally, and the other two IOs could not output high levels. I searched for a long time, checked the input and output port configuration problems, and checked
[Microcontroller]
PIC12F629 IO Configuration
Design of Electric Power Steering System Based on PIC18F458 and OSEK/VDX
The principle and working process of electric power steering system (eps) are analyzed in detail, pic18f458 microcontroller is used as the controller, and the power motor is controlled through its enhanced PWM pulse width modulation module ECCP to realize the power control, return control and damping control of eps sy
[Automotive Electronics]
Design of Electric Power Steering System Based on PIC18F458 and OSEK/VDX
PIC microcontroller in ICD LCD display clock demonstration program
;************************************************* ;* CLKTEST.ASM * ;*-----------------------------------------------* ;*-----------------------------------------------* ;************************************************* ;* ICDDEMO teaching experiment board LCD display clock demonstration program * ;******************
[Microcontroller]
Design and implementation of wireless real-time monitoring and anti-theft system based on PIC microcontroller
1. Main text Preface The production of this product took more than three months. During these three months, our group has been immersed in the laboratory except for class time. Even during the Chinese New Year, our group did not stay at home for a few days and rushed to school without stopping. During this period, w
[Microcontroller]
Design and implementation of wireless real-time monitoring and anti-theft system based on PIC microcontroller
PIC microcontroller AD conversion assembly program
This assembly program implements the A/D conversion function and is applied to the PIC microcontroller. The A/D uses the interrupt mode. The program inputs a DC voltage through the RA2 analog channel of the microcontroller. When the DC voltage is greater than 2.5V, the 8 LEDs flash. When the DC voltage returns t
[Microcontroller]
PIC16C71 single-chip computer key to wake up the CPU source program
; p=pic16c71,xt=40000hz LIST P=16c71 ; Z EQU 2 RBPU EQU 7 TEMP EQU 10H OPTIONREG EQU 1H F EQU 1 PORT_B EQU 06H ;              INCLUDE P16c71.INC              LIST ;              ORG 0 ; reset address              GOTO START ;              ORG 4 ; interrupt vector              GOTO SERVICEINTERRUPT ; START            
[Microcontroller]
Telephone Amplifier Controlled by PIC16C54 Microcontroller
This controller uses PIC 16C54  single-chip microcomputer  as the controller. It is very easy to use: just connect a telephone line to the loudspeaker through the controller, and you can remotely control the opening and closing of the loudspeaker on any telephone to make announcements and propaganda. It is suitable fo
[Microcontroller]
Telephone Amplifier Controlled by PIC16C54 Microcontroller
PIC microcontroller A/D conversion data storage and serial port efficiency
The data after A/D conversion of PIC microcontroller usually needs to occupy two 8-bit wide RAM units, but the storage units of PIC microcontroller are limited, so the storage units of the microcontroller cannot be effectively utilized. At the same time, it takes a lot of time to transmit data to the host computer thro
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号