[DemoCode下载] 003定时器的不同模式

[复制链接]
835|5
 楼主| antusheng 发表于 2019-6-30 23:07 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 Timer0/1 Mode0 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. //*****************  The Following is in define in Fucntion_define.h  ***************************
  21. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  22. //***********************************************************************************************
  23. #if 0
  24. //#define                TIMER1_MODE0_ENABLE                TMOD&=0x0F
  25. //#define                TIMER1_MODE1_ENABLE                TMOD&=0x0F;TMOD|=0x10
  26. //#define                TIMER1_MODE2_ENABLE                TMOD&=0x0F;TMOD|=0x20
  27. //#define                TIMER1_MODE3_ENABLE                TMOD&=0x0F;TMOD|=0x3F

  28. //#define                TIMER0_MODE0_ENABLE                TMOD&=0xF0
  29. //#define                TIMER0_MODE1_ENABLE                TMOD&=0xF0;TMOD|=0x01
  30. //#define                TIMER0_MODE2_ENABLE                TMOD&=0xF0;TMOD|=0x02
  31. //#define                TIMER0_MODE3_ENABLE                TMOD&=0xF0;TMOD|=0xF3
  32. #endif

  33. #define TH0_INIT        0xFC //5.0ms@XTAL=12MHz, Period = (10.85/2) ms@XTAL=22.1184MHz
  34. #define TL0_INIT        0x0F
  35. #define TH1_INIT        0xE0 //2.5ms@XTAL=12MHz, Period = (5.425/2) ms@XTAL=22.1184MHz
  36. #define TL1_INIT        0x00


  37. /************************************************************************************************************
  38. *    TIMER 0 interrupt subroutine
  39. ************************************************************************************************************/
  40. void Timer0_ISR (void) interrupt 1          //interrupt address is 0x000B
  41. {
  42.     TH0 = TH0_INIT;
  43.     TL0 = TL0_INIT;   
  44.     P12 = ~P12;                                                                                                                        // GPIO toggle when interrupt
  45. }
  46. /************************************************************************************************************
  47. *    TIMER 1 interrupt subroutine
  48. ************************************************************************************************************/
  49. void Timer1_ISR (void) interrupt 3          //interrupt address is 0x001B
  50. {
  51.     TH1 = TH1_INIT;
  52.     TL1 = TL1_INIT;   
  53.     P03 = ~P03;                                                                                                                        // GPIO toggle when interrupt
  54. }
  55. /************************************************************************************************************
  56. *    Main function
  57. ************************************************************************************************************/
  58. void main (void)
  59. {
  60.         TMOD = 0XFF;
  61.         Set_All_GPIO_Quasi_Mode;
  62.         TIMER0_MODE0_ENABLE;                        //Timer 0 and Timer 1 mode configuration
  63.         TIMER1_MODE0_ENABLE;
  64.    
  65.         clr_T0M;
  66.         clr_T1M;
  67.    
  68.         TH0 = TH0_INIT;
  69.         TL0 = TL0_INIT;
  70.         TH1 = TH1_INIT;
  71.         TL1 = TL1_INIT;
  72.    
  73. //        set_ET0;                                    //enable Timer0 interrupt
  74.         set_ET1;                                    //enable Timer1 interrupt
  75.         set_EA;                                     //enable interrupts
  76.        
  77.         set_TR0;                                    //Timer0 run
  78. //        set_TR1;                                    //Timer1 run

  79.   while(1)
  80.         {
  81.                 TH0 = TH0_INIT;
  82.                 TL0 = TL0_INIT;
  83.                 set_TR0;
  84.                 while(!TF0);
  85.                 clr_TR0;
  86.                 P12 = ~P12;       
  87.                 TF0 = 0 ;
  88. }
  89. }


 楼主| antusheng 发表于 2019-6-30 23:08 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 Timer0/1 Mode1 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. //*****************  The Following is in define in Fucntion_define.h  ***************************
  21. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  22. //***********************************************************************************************
  23. #if 0
  24. //#define                TIMER0_MODE0_ENABLE                TMOD&=0x0F
  25. //#define                TIMER0_MODE1_ENABLE                TMOD&=0x0F;TMOD|=0x10
  26. //#define                TIMER0_MODE2_ENABLE                TMOD&=0x0F;TMOD|=0x20
  27. //#define                TIMER0_MODE3_ENABLE                TMOD&=0x0F;TMOD|=0x3F

  28. //#define                TIMER1_MODE0_ENABLE                TMOD&=0xF0
  29. //#define                TIMER1_MODE1_ENABLE                TMOD&=0xF0;TMOD|=0x01
  30. //#define                TIMER1_MODE2_ENABLE                TMOD&=0xF0;TMOD|=0x02
  31. //#define                TIMER1_MODE3_ENABLE                TMOD&=0xF0;TMOD|=0xF3
  32. #endif

  33. #define TH0_INIT        50000
  34. #define TL0_INIT        50000
  35. #define TH1_INIT        25000
  36. #define TL1_INIT        25000

  37. UINT8 u8TH0_Tmp,u8TL0_Tmp,u8TH1_Tmp,u8TL1_Tmp;
  38. /************************************************************************************************************
  39. *    TIMER 0 interrupt subroutine
  40. ************************************************************************************************************/
  41. void Timer0_ISR (void) interrupt 1  //interrupt address is 0x000B
  42. {
  43.     TH0 = u8TH0_Tmp;
  44.     TL0 = u8TL0_Tmp;   
  45.    
  46.     P12 = ~P12;                     // GPIO1 toggle when interrupt
  47. }

  48. /************************************************************************************************************
  49. *    TIMER 1 interrupt subroutine
  50. ************************************************************************************************************/
  51. void Timer1_ISR (void) interrupt 3  //interrupt address is 0x001B
  52. {
  53.     TH1 = u8TH1_Tmp;
  54.     TL1 = u8TL1_Tmp;   

  55.     P03 = ~P03;                     //P0.3 toggle when interrupt
  56. }
  57. /************************************************************************************************************
  58. *    Main function
  59. ************************************************************************************************************/
  60. void main (void)
  61. {
  62.    
  63.     Set_All_GPIO_Quasi_Mode;
  64.                 TIMER0_MODE1_ENABLE;
  65.                 TIMER1_MODE1_ENABLE;

  66.     clr_T1M;
  67.     //set_T1M;

  68.     u8TH0_Tmp = (65536-TH0_INIT)/256;
  69.     u8TL0_Tmp = (65536-TL0_INIT)%256;   
  70.     u8TH1_Tmp = (65536-TH1_INIT)/256;
  71.     u8TL1_Tmp = (65536-TL1_INIT)%256;
  72.    
  73.     TH0 = u8TH0_Tmp;
  74.     TL0 = u8TL0_Tmp;
  75.     TH1 = u8TH1_Tmp;
  76.     TL1 = u8TL1_Tmp;
  77.    
  78.     set_ET0;                                    //enable Timer0 interrupt
  79.     set_ET1;                                    //enable Timer1 interrupt
  80.     set_EA;                                     //enable interrupts
  81.     set_TR0;                                    //Timer0 run
  82.     set_TR1;                                    //Timer1 run
  83.     while(1);
  84. }

 楼主| antusheng 发表于 2019-6-30 23:08 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 Timer0/1 Mode2 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. //*****************  The Following is in define in Fucntion_define.h  ***************************
  21. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  22. //***********************************************************************************************
  23. #if 0
  24. //#define                TIMER0_MODE0_ENABLE                TMOD&=0x0F
  25. //#define                TIMER0_MODE1_ENABLE                TMOD&=0x0F;TMOD|=0x10
  26. //#define                TIMER0_MODE2_ENABLE                TMOD&=0x0F;TMOD|=0x20
  27. //#define                TIMER0_MODE3_ENABLE                TMOD&=0x0F;TMOD|=0x3F

  28. //#define                TIMER1_MODE0_ENABLE                TMOD&=0xF0
  29. //#define                TIMER1_MODE1_ENABLE                TMOD&=0xF0;TMOD|=0x01
  30. //#define                TIMER1_MODE2_ENABLE                TMOD&=0xF0;TMOD|=0x02
  31. //#define                TIMER1_MODE3_ENABLE                TMOD&=0xF0;TMOD|=0xF3
  32. #endif

  33. #if 1
  34.     #define TIMER0_RELOAD_VALUE     50
  35.     #define TIMER1_RELOAD_VALUE     50
  36. #else
  37.     #define TIMER0_RELOAD_VALUE     250
  38.     #define TIMER1_RELOAD_VALUE     250
  39. #endif

  40. /************************************************************************************************************
  41. *    TIMER 0 interrupt subroutine
  42. ************************************************************************************************************/
  43. void Timer0_ISR (void) interrupt 1              //interrupt address is 0x000B
  44. {  
  45.     P12 = ~P12;                                 // GPIO1 toggle when interrupt
  46. }

  47. /************************************************************************************************************
  48. *    TIMER 1 interrupt subroutine
  49. ************************************************************************************************************/
  50. void Timer1_ISR (void) interrupt 3              //interrupt address is 0x001B
  51. {
  52.     P03 = ~P03;                                 //P0.3 toggle when interrupt
  53. }

  54. /************************************************************************************************************
  55. *    Main function
  56. ************************************************************************************************************/
  57. void main (void)
  58. {

  59.     Set_All_GPIO_Quasi_Mode;
  60.                 TIMER0_MODE2_ENABLE;
  61.                 TIMER1_MODE2_ENABLE;
  62.    
  63.     TH0 = (256-TIMER0_RELOAD_VALUE);            //initial counter values
  64.     TL0 = (256-TIMER0_RELOAD_VALUE);   
  65.     TH1 = (256-TIMER1_RELOAD_VALUE);
  66.     TL1 = (256-TIMER1_RELOAD_VALUE);   
  67.    
  68.     set_ET0;                                    //enable Timer0 interrupt
  69.     set_ET1;                                    //enable Timer1 interrupt
  70.     set_EA;                                     //enable interrupts
  71.     set_TR0;                                    //Timer0 run
  72.     set_TR1;                                    //Timer1 run
  73.     while(1);
  74. }

 楼主| antusheng 发表于 2019-6-30 23:08 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 Timer0/1 Mode3 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. //*****************  The Following is in define in Fucntion_define.h  ***************************
  21. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  22. //***********************************************************************************************
  23. #if 0
  24. //#define                TIMER0_MODE0_ENABLE                TMOD&=0x0F
  25. //#define                TIMER0_MODE1_ENABLE                TMOD&=0x0F;TMOD|=0x10
  26. //#define                TIMER0_MODE2_ENABLE                TMOD&=0x0F;TMOD|=0x20
  27. //#define                TIMER0_MODE3_ENABLE                TMOD&=0x0F;TMOD|=0x3F

  28. //#define                TIMER1_MODE0_ENABLE                TMOD&=0xF0
  29. //#define                TIMER1_MODE1_ENABLE                TMOD&=0xF0;TMOD|=0x01
  30. //#define                TIMER1_MODE2_ENABLE                TMOD&=0xF0;TMOD|=0x02
  31. //#define                TIMER1_MODE3_ENABLE                TMOD&=0xF0;TMOD|=0xF3
  32. #endif

  33. #define TH0_INIT        (256-100)
  34. #define TL0_INIT        (256-50)
  35. #define TH1_INIT        0x00
  36. #define TL1_INIT        0x00

  37. /************************************************************************************************************
  38. *    TIMER 0 interrupt subroutine
  39. ************************************************************************************************************/
  40. void Timer0_ISR (void) interrupt 1              // interrupt address is 0x000B
  41. {              
  42.     TL0 = TL0_INIT;                             // reload by software   
  43.     P12 = ~P12;                                 // GPIO1 toggle when interrupt
  44. }

  45. /************************************************************************************************************
  46. *    TIMER 1 interrupt subroutine
  47. ************************************************************************************************************/
  48. void Timer1_ISR (void) interrupt 3              //interrupt address is 0x001B
  49. {           
  50.     TH0 = TH0_INIT;
  51.                 Send_Data_To_UART0(0x54);                                                                                //print charater "T" means timer interrupt
  52.                 P12 = ~ P12;                                                                                                                                //Mark UART output to find the real timer interrupt timming.
  53. }

  54. /************************************************************************************************************
  55. *    Main function
  56. ************************************************************************************************************/
  57. void main (void)
  58. {
  59.     Set_All_GPIO_Quasi_Mode;
  60.                 InitialUART0_Timer3(115200);
  61.        
  62.                 TIMER0_MODE3_ENABLE;
  63.                 TIMER1_MODE3_ENABLE;
  64.    
  65.     TH0 = TH0_INIT;                             //initial counter values
  66.     TL0 = TL0_INIT;   
  67.     TH1 = TH1_INIT;
  68.     TL1 = TL1_INIT;   
  69.    
  70.     set_ET0;                                    //enable Timer0 counter interrupt
  71.     set_ET1;                                    //enable Timer1 counter interrupt
  72.     set_EA;                                     //enable interrupts
  73.     set_TR0;                                    //Timer0 run
  74.     set_TR1;                                    //Timer1 run
  75.    
  76.     while(1);
  77. }

 楼主| antusheng 发表于 2019-6-30 23:09 | 显示全部楼层
没想到新唐做的例子,做的这么全面。
zhuomuniao110 发表于 2019-7-22 13:35 | 显示全部楼层
不同模式适合不同的应用场景。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

86

主题

1521

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部