极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12219|回复: 2

求助! Arduino步进和热电偶多任务运行的问题?

[复制链接]
发表于 2016-4-20 11:18:55 | 显示全部楼层 |阅读模式
前几天想做个电动山东煎饼机, 用arduino控制一个步进来旋转饼铛然后热电偶控制电热丝加热. 步进用的是TB6560, 热电偶用的是max6675. 热电偶用的是定时interrupt, 每100ms进行一次interrupt. 现在的问题是每次打断之后热电偶读出来的温度值都是第一次的值........然后打断会打断步进的脉冲信号 (示波器看的). 打断目前可以接受, 可以慢慢搞, 但是热电偶涉及到防火安全, 所以现在要弄好才能放心测试. 希望大家帮着看看为什么.
示波器显示每次打断的热电偶的clock 和cs的信号正常, 但是热电偶没办法拆下来测所以没法说. 热电偶和步进的程序单着都挺好, 和一起就不成了.
谢谢大伙儿了.


  1. #include <TimerOne.h>
  2. #include <AccelStepper.h>    //用的是accelstepper library
  3. #include <max6675.h>

  4. #define stepdir 6
  5. #define stepclk 5
  6. #define TIMER_US 100000                         // 100mS set timer duration in microseconds

  7. volatile bool in_long_isr = false;              // True if in long interrupt

  8. int thermoDO = 9;
  9. int thermoCS = 10;
  10. int thermoCLK = 11;
  11. int coilpin = 13;
  12. const int t_want = 90;

  13. AccelStepper stepper(stepdir,stepclk);
  14. MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
  15. void setup()
  16. {  
  17.    pinMode(stepclk, OUTPUT);
  18.    pinMode(stepdir, OUTPUT);
  19.    pinMode(coilpin, OUTPUT);
  20.    stepper.setMaxSpeed(300);
  21.    stepper.setSpeed(300);       
  22.   Timer1.initialize(TIMER_US);                  //
  23.   Timer1.attachInterrupt( timerIsr );           // attach the ISR routine here
  24.   Serial.begin(9600);// use Arduino pins
  25.   Serial.println("MAX6675 test");
  26.   delay(500);
  27. }

  28. void loop()
  29. {  
  30.    stepper.runSpeed();
  31. }

  32. void timerIsr()        //timer1 做的interrupt
  33. {
  34.   //  digitalWrite( LED0, digitalRead( LED0 ) ^ 1 );  // Toggle LED 0
  35.     volatile long i=0;
  36.     interrupts();                                   // Enable interrupts
  37.     //checkcoil();

  38.   int temp = thermocouple.readCelsius();

  39.   if (temp >= t_want)
  40.     digitalWrite(coilpin, LOW);

  41.   else if (temp <= t_want)
  42.     digitalWrite(coilpin, HIGH);
  43.    
  44.   Serial.print("C = ");
  45.   Serial.println(temp);
  46.     noInterrupts();
  47. }
  48. //void checkcoil()       //热电偶检查温度调整电热丝的程序, 赚到了isr里面
  49. //{
  50. //  int temp = thermocouple.readCelsius();
  51. //
  52. //  if (temp >= t_want)
  53. //    digitalWrite(coilpin, LOW);
  54. //
  55. //  else if (temp <= t_want)
  56. //    digitalWrite(coilpin, HIGH);
  57. //   
  58. //  Serial.print("C = ");
  59. //  Serial.println(temp);
  60. //  return;
  61. //}
复制代码
回复

使用道具 举报

发表于 2016-4-20 23:19:10 | 显示全部楼层
#define TIMER_US 100000 把这个弄大点试试,以前51采集热电偶也是不能采集太快,记得有个转换时间
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-21 13:14:26 | 显示全部楼层
275891381 发表于 2016-4-20 23:19
#define TIMER_US 100000 把这个弄大点试试,以前51采集热电偶也是不能采集太快,记得有个转换时间

药到病除, 谢谢, 但是现在打断程序还是会打断步进脉冲信号
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 02:14 , Processed in 0.034166 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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