搜索
bottom↓
回复: 11

TINY85A一开全局中断就响应INT0请有时间看的朋友分析一下

[复制链接]

出0入0汤圆

发表于 2014-5-8 00:02:40 | 显示全部楼层 |阅读模式
编译器是CodeWizardAVR,搭建了一个简单的最小化INT0实验,那些端口移位都是用来观察效果的。软件仿真我发现只要一开全局中断就进入INT0中断服务程序了,我的INTO是设置成低电平触发呢!怎么还没等我触发就进入INT0中断服务程序了呢?实际硬件测试也是这样的效果,请有时间看的朋友帮忙分析一下吧,谢谢!这是完整文件

  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V2.05.1b Evaluation
  4. Automatic Program Generator
  5. ?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
  6. http://www.hpinfotech.com

  7. Project :
  8. Version :
  9. Date    : 2014-5-7
  10. Author  : Freeware, for evaluation and non-commercial use only
  11. Company :
  12. Comments:


  13. Chip type               : ATtiny85
  14. AVR Core Clock frequency: 8.000000 MHz
  15. Memory model            : Small
  16. External RAM size       : 0
  17. Data Stack size         : 128
  18. *****************************************************/

  19. #include <tiny85.h>
  20. #include<delay.h>
  21. // External Interrupt 0 service routine
  22. interrupt [EXT_INT0] void ext_int0_isr(void)
  23. {
  24. // Place your code here
  25. PORTB&=~((1<<3)|(1<<4));
  26. //delay_ms(1000);
  27. PORTB|=(1<<3)|(1<<4);
  28. //delay_ms(1000);
  29. PORTB&=~((1<<3)|(1<<4));
  30. //delay_ms(1000);
  31. PORTB|=(1<<3)|(1<<4);
  32. //delay_ms(1000);
  33. GIMSK=0x00; //禁用INT0中断,等合适的时候再打开
  34. }



  35. // Declare your global variables here

  36. void main(void)
  37. {
  38. // Declare your local variables here

  39. // Crystal Oscillator division factor: 1
  40. #pragma optsize-
  41. CLKPR=0x80;
  42. CLKPR=0x00;
  43. #ifdef _OPTIMIZE_SIZE_
  44. #pragma optsize+
  45. #endif

  46. // Input/Output Ports initialization
  47. // Port B initialization
  48. // Func5=In Func4=Out Func3=Out Func2=Out Func1=In Func0=Out
  49. // State5=T State4=1 State3=1 State2=0 State1=P State0=0
  50. PORTB=0x1A;
  51. DDRB=0x1D;


  52. // Timer/Counter 0 initialization
  53. // Clock source: System Clock
  54. // Clock value: 125.000 kHz
  55. // Mode: CTC top=OCR0A
  56. // OC0A output: Disconnected
  57. // OC0B output: Disconnected
  58. TCCR0A=0x00;
  59. TCCR0B=0x00;
  60. TCNT0=0x00;
  61. OCR0A=0x00;
  62. OCR0B=0x00;

  63. // Timer/Counter 1 initialization
  64. // Clock source: System Clock
  65. // Clock value: Timer1 Stopped
  66. // Mode: Normal top=0xFF
  67. // OC1A output: Disconnected
  68. // OC1B output: Disconnected
  69. // Timer1 Overflow Interrupt: Off
  70. // Compare A Match Interrupt: Off
  71. // Compare B Match Interrupt: Off
  72. PLLCSR=0x00;

  73. TCCR1=0x00;
  74. GTCCR=0x00;
  75. TCNT1=0x00;
  76. OCR1A=0x00;
  77. OCR1B=0x00;
  78. OCR1C=0x00;

  79. // External Interrupt(s) initialization
  80. // INT0: On
  81. // INT0 Mode: Low level
  82. // Interrupt on any change on pins PCINT0-5: Off
  83. //GIMSK=0x40;
  84. //MCUCR=0x00;
  85. //GIFR=0x40;

  86. // Timer(s)/Counter(s) Interrupt(s) initialization
  87. TIMSK=0x00;

  88. // Universal Serial Interface initialization
  89. // Mode: Disabled
  90. // Clock source: Register & Counter=no clk.
  91. // USI Counter Overflow Interrupt: Off
  92. USICR=0x00;

  93. // Analog Comparator initialization
  94. // Analog Comparator: Off
  95. ACSR=0x80;
  96. ADCSRB=0x00;
  97. DIDR0=0x00;

  98. // ADC initialization
  99. // ADC disabled
  100. ADCSRA=0x00;
  101. while (1)
  102.       {
  103.       PORTB&=~((1<<3)|(1<<4));
  104.       //delay_ms(200);
  105.           PORTB|=(1<<3)|(1<<4);
  106.           //delay_ms(200);
  107.       PORTB&=~((1<<3)|(1<<4));
  108.       //delay_ms(200);
  109.           PORTB|=(1<<3)|(1<<4);
  110.           //delay_ms(200);      
  111.           /*使能INT0中断*/
  112.           GIMSK=0x40;
  113.           MCUCR=0x00;
  114.           GIFR=0x40;                     
  115.           MCUCR |= (1<<SM1) | (0<<SM0);  // 掉电模式
  116.           MCUCR |= (1<<SE);   //睡眠使能
  117.           #asm("sei")  // 开放全局中断
  118.            
  119.       //delay_ms(200);
  120.           PORTB|=(1<<2);
  121.           //delay_ms(200);
  122.           PORTB|=(1<<2);
  123.       //delay_ms(200);
  124.           PORTB|=(1<<2);
  125.           #asm("sleep")      //休眠
  126.           PORTB ^= 1<<2;     //唤醒后监测
  127.           //delay_ms(500);
  128.       }
  129. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2014-5-8 02:42:23 | 显示全部楼层
没看你的代码,我瞎猜,可能是启用前没有清除中断标识

出0入0汤圆

 楼主| 发表于 2014-5-8 03:04:59 | 显示全部楼层
本帖最后由 biying 于 2014-5-8 03:33 编辑
tohell 发表于 2014-5-8 02:42
没看你的代码,我瞎猜,可能是启用前没有清除中断标识


猜错!我已经清除了。最新测试,重新从别一段程序里取来的代码(是用PCINT1作为外部中断输入),新建了一个工程后外部中断正常了,可是相同的代码复制到原来出错的工程里,依然不能用,这是为何?难道编译器会“记仇”?这是正常的代码包,这是相同代码不正常的代码包。但是如果把PCINT1改为INT0的话,也就是只改这里
  1. // External Interrupt(s) initialization
  2. // INT0: Off
  3. // Interrupt on any change on pins PCINT0-5: On
  4. //GIMSK=0x20; //启用PCINT1
  5. //MCUCR=0x00;
  6. //PCMSK=0x02;
  7. //GIFR=0x20;
  8. GIMSK=0x40;  //记用INT0
  9. MCUCR=0x00;
  10. GIFR=0x40;
复制代码


但是两个包都是一样的不正常。INT0,你到底要怎么样才能才能让我用?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2014-5-8 03:12:17 | 显示全部楼层
好几次了,遇到过相同的代码,新建工程后粘贴过去编译下载测试就正常,现在又碰到,真搞不懂是CodeWizardAVR的错,还是我这个版本有BUG。连avrstudio也一起软件仿真出错,应该是CodeWizardAVR编译出来的文件有问题。

出0入0汤圆

 楼主| 发表于 2014-5-8 09:07:42 | 显示全部楼层
同样的代码,ICCV7 for AVR里改为INT0后居然是正常的!难道真是CodeWizardAVR的错?这是ICC的代码包

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2014-5-8 09:19:44 | 显示全部楼层
中断服务程序改好名字也不行

  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V2.05.1b Evaluation
  4. Automatic Program Generator
  5. ?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
  6. http://www.hpinfotech.com

  7. Project :
  8. Version :
  9. Date    : 2014-5-8
  10. Author  : Freeware, for evaluation and non-commercial use only
  11. Company :
  12. Comments:


  13. Chip type               : ATtiny85
  14. AVR Core Clock frequency: 8.000000 MHz
  15. Memory model            : Small
  16. External RAM size       : 0
  17. Data Stack size         : 128
  18. *****************************************************/

  19. #include <tiny85.h>
  20. #include<delay.h>
  21. // Pin change interrupt service routine
  22. //interrupt [PC_INT0] void pin_change_isr(void)
  23. interrupt [EXT_INT0] void ext_int0_isr(void)
  24. {
  25. // Place your code here
  26. PORTB&=0xfe;
  27. }

  28. // Declare your global variables here

  29. void main(void)
  30. {
  31. // Declare your local variables here

  32. // Crystal Oscillator division factor: 1
  33. #pragma optsize-
  34. CLKPR=0x80;
  35. CLKPR=0x00;
  36. #ifdef _OPTIMIZE_SIZE_
  37. #pragma optsize+
  38. #endif

  39. // Input/Output Ports initialization
  40. // Port B initialization
  41. // Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
  42. // State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
  43. //PORTB=0x3F;
  44. //DDRB=0x3F;
  45. DDRB = 0x3F;             //将所有I/O口均初始化为输出状态
  46.    PORTB = 0x3F;
  47.    DDRB&=0XFD;       //PCINT1设为输入
  48.    PORTB=0X3F;

  49. // Timer/Counter 0 initialization
  50. // Clock source: System Clock
  51. // Clock value: Timer 0 Stopped
  52. // Mode: Normal top=0xFF
  53. // OC0A output: Disconnected
  54. // OC0B output: Disconnected
  55. TCCR0A=0x00;
  56. TCCR0B=0x00;
  57. TCNT0=0x00;
  58. OCR0A=0x00;
  59. OCR0B=0x00;

  60. // Timer/Counter 1 initialization
  61. // Clock source: System Clock
  62. // Clock value: Timer1 Stopped
  63. // Mode: Normal top=0xFF
  64. // OC1A output: Disconnected
  65. // OC1B output: Disconnected
  66. // Timer1 Overflow Interrupt: Off
  67. // Compare A Match Interrupt: Off
  68. // Compare B Match Interrupt: Off
  69. PLLCSR=0x00;

  70. TCCR1=0x00;
  71. GTCCR=0x00;
  72. TCNT1=0x00;
  73. OCR1A=0x00;
  74. OCR1B=0x00;
  75. OCR1C=0x00;

  76. // External Interrupt(s) initialization
  77. // INT0: Off
  78. // Interrupt on any change on pins PCINT0-5: On
  79. //GIMSK=0x20;
  80. //MCUCR=0x00;
  81. //PCMSK=0x02;
  82. //GIFR=0x20;
  83. GIMSK=0x40;
  84. MCUCR=0x00;
  85. GIFR=0x40;

  86. // Timer(s)/Counter(s) Interrupt(s) initialization
  87. TIMSK=0x00;

  88. // Universal Serial Interface initialization
  89. // Mode: Disabled
  90. // Clock source: Register & Counter=no clk.
  91. // USI Counter Overflow Interrupt: Off
  92. USICR=0x00;

  93. // Analog Comparator initialization
  94. // Analog Comparator: Off
  95. ACSR=0x80;
  96. ADCSRB=0x00;
  97. DIDR0=0x00;

  98. // ADC initialization
  99. // ADC disabled
  100. ADCSRA=0x00;

  101. // Global enable interrupts
  102. #asm("sei")

  103. while (1)
  104.       {
  105.       // Place your code here
  106.        #asm("sei")            // 开放全局中断       while(1);
  107.       }
  108. }
复制代码

出0入0汤圆

 楼主| 发表于 2014-5-8 09:25:50 | 显示全部楼层
当要设为PCINT1中断时,第53到56行的端口处理为什么要这么麻烦?为何不直接设置为
PORTB=0x3F;
DDRB=0x3D;

软件仿真时倒是看到以上两句处理的话,PINB2为0,相当于触发外部中断;但如果像第53到56行那样处理后,PINB2为1,也就是没有触发外部中断。我觉得用M16时没这么麻烦呢

出0入0汤圆

 楼主| 发表于 2014-5-8 09:56:21 | 显示全部楼层
重新安装cvavre_2.05.1_b后重新编译下载居然INT0正常了,可是多编译多次后居然出现提示:Linker error: the program has no 'main' function,关掉软件重新打开再编译双没有提示了,此时再下载INT0又不正常了。

出0入0汤圆

 楼主| 发表于 2014-5-8 10:18:01 | 显示全部楼层
现在依然不能用INT0,用PCINT1正常

  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V2.05.1b Evaluation
  4. Automatic Program Generator
  5. ?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
  6. http://www.hpinfotech.com

  7. Project :
  8. Version :
  9. Date    : 2014-5-8
  10. Author  : Freeware, for evaluation and non-commercial use only
  11. Company :
  12. Comments:


  13. Chip type               : ATtiny85
  14. AVR Core Clock frequency: 8.000000 MHz
  15. Memory model            : Small
  16. External RAM size       : 0
  17. Data Stack size         : 128
  18. *****************************************************/

  19. /*****************************************************
  20. This program was produced by the
  21. CodeWizardAVR V2.05.1b Evaluation
  22. Automatic Program Generator
  23. ?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
  24. http://www.hpinfotech.com

  25. Project :
  26. Version :
  27. Date    : 2014-5-8
  28. Author  : Freeware, for evaluation and non-commercial use only
  29. Company :
  30. Comments:


  31. Chip type               : ATtiny85
  32. AVR Core Clock frequency: 8.000000 MHz
  33. Memory model            : Small
  34. External RAM size       : 0
  35. Data Stack size         : 128
  36. *****************************************************/   
  37. #include <tiny85.h>
  38. #include<delay.h>
  39. // Pin change interrupt service routine
  40. interrupt [PC_INT0] void pin_change_isr(void)
  41. //interrupt [EXT_INT0] void ext_int0_isr(void)
  42. {
  43. // Place your code here
  44. PORTB&=0xfe;
  45. }

  46. // Declare your global variables here

  47. void main(void)
  48. {
  49. // Declare your local variables here

  50. // Crystal Oscillator division factor: 1
  51. #pragma optsize-
  52. CLKPR=0x80;
  53. CLKPR=0x00;
  54. #ifdef _OPTIMIZE_SIZE_
  55. #pragma optsize+
  56. #endif

  57. // Input/Output Ports initialization
  58. // Port B initialization
  59. // Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
  60. // State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
  61. PORTB=0x3F;DDRB=0x3D;
  62. //DDRB = 0x3F;PORTB = 0x3F;DDRB&=0XFD;PORTB=0X3F;

  63. // Timer/Counter 0 initialization
  64. // Clock source: System Clock
  65. // Clock value: Timer 0 Stopped
  66. // Mode: Normal top=0xFF
  67. // OC0A output: Disconnected
  68. // OC0B output: Disconnected
  69. TCCR0A=0x00;
  70. TCCR0B=0x00;
  71. TCNT0=0x00;
  72. OCR0A=0x00;
  73. OCR0B=0x00;

  74. // Timer/Counter 1 initialization
  75. // Clock source: System Clock
  76. // Clock value: Timer1 Stopped
  77. // Mode: Normal top=0xFF
  78. // OC1A output: Disconnected
  79. // OC1B output: Disconnected
  80. // Timer1 Overflow Interrupt: Off
  81. // Compare A Match Interrupt: Off
  82. // Compare B Match Interrupt: Off
  83. PLLCSR=0x00;
  84. TCCR1=0x00;
  85. GTCCR=0x00;
  86. TCNT1=0x00;
  87. OCR1A=0x00;
  88. OCR1B=0x00;
  89. OCR1C=0x00;

  90. // External Interrupt(s) initialization
  91. // INT0: Off
  92. // Interrupt on any change on pins PCINT0-5: On
  93. GIMSK=0x20;MCUCR=0x00;PCMSK=0x02;GIFR=0x20;
  94. //GIMSK=0x40;MCUCR=0x00;GIFR=0x40;

  95. // Timer(s)/Counter(s) Interrupt(s) initialization
  96. TIMSK=0x00;

  97. // Universal Serial Interface initialization
  98. // Mode: Disabled
  99. // Clock source: Register & Counter=no clk.
  100. // USI Counter Overflow Interrupt: Off
  101. USICR=0x00;

  102. // Analog Comparator initialization
  103. // Analog Comparator: Off
  104. ACSR=0x80;
  105. ADCSRB=0x00;
  106. DIDR0=0x00;

  107. // ADC initialization
  108. // ADC disabled
  109. ADCSRA=0x00;

  110. // Global enable interrupts
  111. #asm("sei")  
  112. while (1);
  113. }
复制代码

出0入0汤圆

 楼主| 发表于 2014-5-8 10:39:49 | 显示全部楼层
换笔记本电脑安装同一个文件的软件,手动输入代码编译下载,INT0依然不能用

出0入0汤圆

发表于 2014-5-8 14:03:36 | 显示全部楼层
biying 发表于 2014-5-8 09:07
同样的代码,ICCV7 for AVR里改为INT0后居然是正常的!难道真是CodeWizardAVR的错?这是ICC的代码包 ...

比较两个工具生成的汇编文件看看

出0入0汤圆

 楼主| 发表于 2014-5-8 18:40:07 | 显示全部楼层
惭愧啊,汇编还没忙得学呢
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 20:24

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表