近来想用Arduino做个工控版,必须用到多线程处理, 程序写完了 在Protues里面仿真都没有问题 下载到板子上就不能正常运行
- void setup()
- {
- Serial.begin(9600);
- Serial.print("setup");
- attachInterrupt(1, counter, LOW); //脉冲输入
- attachInterrupt(0, Stop, HIGH); //急停
- pinMode(4, OUTPUT); //音波测试
- pinMode(5, OUTPUT); //电磁阀
- pinMode(6, INPUT_PULLUP); //启动
- pinMode(7, OUTPUT); //复位
- pinMode(13, OUTPUT); //led
- digitalWrite(13,HIGH);
- digitalWrite(7,HIGH);
- Scheduler.startLoop(loop2);
- Scheduler.startLoop(loop3);
- Scheduler.startLoop(loop4);
- Scheduler.startLoop(loop5);
- Scheduler.start();
- Serial.print(2);
- }
复制代码
这个是SETUP的程序 仿真的时候从串口接收到 setup2 逻辑都是正常的
下载到板子上 用串口助手 只接收到 se
随后板子就像死机一样 其他线程任务都无法执行
实在头疼找不到问题 仿真都行 下载到板子上就出问题了
以下是全部代码
- #include <SCoop.h>
- #include <EEPROM.h>
- double analog;
- char buff[10];
- unsigned char a, addr;
- unsigned int delay_time, work_time, cold_time;
- volatile unsigned char count = 0, temp = 0, start_flag = 0, display_flag = 0, shake_flag = 0, text_flag = 0;
- volatile unsigned long frq, run_time = 0, time_now = 0, time_old = 0;
- static unsigned char Frq_buff[7] = {0X6E, 0X30, 0X2E, 0X76, 0X61, 0X6C, 0X3D}; //ASCLL码 n0.val=
- static unsigned char Analog_buff[7] = {0X78, 0X30, 0X2E, 0X76, 0X61, 0X6C, 0X3D}; //ASCLL码 x0.val=
- static unsigned char End_buff[3] = {0XFF, 0XFF, 0XFF}; //结束符
- /********************************主程序区*****************************************/
- void setup()
- {
- Serial.begin(9600);
- Serial.print("setup");
- attachInterrupt(1, counter, LOW); //脉冲输入
- attachInterrupt(0, Stop, HIGH); //急停
- pinMode(4, OUTPUT); //音波测试
- pinMode(5, OUTPUT); //电磁阀
- pinMode(6, INPUT_PULLUP); //启动
- pinMode(7, OUTPUT); //复位
- pinMode(13, OUTPUT); //led
- digitalWrite(13,HIGH);
- digitalWrite(7,HIGH);
- Scheduler.startLoop(loop2);
- Scheduler.startLoop(loop3);
- Scheduler.startLoop(loop4);
- Scheduler.startLoop(loop5);
- Scheduler.start();
- Serial.print(2);
- }
- void loop()
- {
- //yield();
- Serial.print(3);
- Scheduler.delay(1000);
- }
- /********************************任务1程序区*****************************************/
- // 显示屏数据更新
- //defineTaskLoop(Task1)
- void loop2()
- {
- if (display_flag)
- {
- Serial.write("task1");
- analog = (double)(analogRead(A0) / 1023.00) * 5; //计算模拟量
- frq = 2000 / (double)(run_time / 1000.00) * 1000; //频率计算
- PrintFrq();
- PrintAnalog();
- Scheduler.delay(100); //刷新间隔
- }
- }
- /********************************任务2程序区*****************************************/
- //控制程序
- //defineTaskLoop(Task2)
- void loop3()
- {
- if (!digitalRead(6))
- {
- Serial.write("task2");
- //LEDshake();
- digitalWrite(5, HIGH); //开电磁阀
- Scheduler.delay(delay_time*10); //延时启动
- //sleep(100);
- digitalWrite(4, HIGH); //开超声
- display_flag = 1; //显示更新允许
- Scheduler.delay(work_time*10); //焊接延时
- //sleep(200);
- display_flag = 0; //显示更新禁止
- digitalWrite(4, LOW); //关超声
- Scheduler.delay(cold_time*10); //冷却延时
- //sleep(100);
- if (shake_flag)
- {
- for (int i = 10; i > 0; i--)
- {
- digitalWrite(5, HIGH);
- Scheduler.delay(200); //振动
- digitalWrite(5, LOW);
- Scheduler.delay(200);
- }
- shake_flag = 0;
- }
- else
- {
- digitalWrite(5, LOW); //关电磁阀
- }
- start_flag = 0; //清除标志
- }
- }
- /********************************任务3程序区*****************************************/
- //defineTaskLoop(Task3) //通讯
- void loop4()
- {
- if (Serial.available())
- {
- Serial.write("tesk3");
- a = Serial.available();
- Serial.readBytes(buff, a);
- switch (buff[0])
- {
- case 0x66 : text_flag = 1; break; //音波测试
- case 0x99 : addr = buff[1]; break; //确定参数序号
- case 0xaa : Data_change(); break; //修改参数
- case 0xbb : Updata(); break; //参数上传更新
- case 0xcc : shake(); break; //振落开关
- }
- }
- Data_handle(); //数据转化
- //Scheduler.delay(100); //通讯数据处理间隔
- }
- /********************************任务4程序区*****************************************/
- //defineTaskLoop(Task4) //音波测试
- void loop5()
- {
- if (text_flag)
- {
- Serial.write("task4");
- digitalWrite(4, HIGH); //开超声
- display_flag = 1; //开频率更新
- Scheduler.delay(1000);
- display_flag = 0;
- digitalWrite(4, LOW);
- text_flag = 0;
- }
- }
- /********************************中断程序区*****************************************/
- //脉冲定时 下降沿
- void counter()
- {
- count++; //脉冲计数
- if (count == 20) //以20Khz为标准0.1S 20*100=2000个脉冲更新一次频率
- {
- temp++;
- time_now = micros(); //读时间
- count = 0; //计数清零
- run_time = time_now - time_old; //时间差
- time_old = time_now; //时间移位
- }
- }
- //急停动作 上升沿
- void Stop()
- {
- digitalWrite(4, LOW); //关超声
- digitalWrite(5, LOW); //关电磁阀
- digitalWrite(7, LOW); //复位
- delay(999999); //如果复位失败,则假死机
- }
- /********************************函数程序区*****************************************/
- void PrintFrq()
- {
- unsigned char data[5];
- data[4] = frq % 10 + 0x30;
- data[3] = frq / 10 % 10 + 0x30;
- data[2] = frq / 100 % 10 + 0x30;
- data[1] = frq / 1000 % 10 + 0x30;
- data[0] = frq / 10000 % 10 + 0x30;
- Serial.write(Frq_buff, 7);
- Serial.write(data, 5);
- Serial.write(End_buff, 3);
- }
- void PrintAnalog()
- {
- unsigned char data[3];
- data[2] = (unsigned int)(analog * 100) % 10 + 0x30;
- data[1] = (unsigned int)(analog * 100) / 10 % 10 + 0x30;
- data[0] = (unsigned int)(analog * 100) / 100 % 10 + 0x30;
- Serial.write(Analog_buff, 7);
- Serial.write(data, 3);
- Serial.write(End_buff, 3);
- }
- void Data_change()
- {
- while (1)
- {
- if (a == 6)
- {
- EEPROM.write(addr * 10 , buff[1]); //规定 延时数据三位HEX 范围 0.00s~9.99s
- EEPROM.write(addr * 10 + 1, buff[3]);
- EEPROM.write(addr * 10 + 2, buff[4]); break;
- }
- if (a == 5)
- {
- EEPROM.write(addr * 10 , buff[1]);
- EEPROM.write(addr * 10 + 1 , buff[3]);
- EEPROM.write(addr * 10 + 2 , 0); break;
- }
- if (a == 3)
- {
- EEPROM.write(addr * 10 , buff[1]);
- EEPROM.write(addr * 10 + 1, 0);
- EEPROM.write(addr * 10 + 2, 0); break;
- }
- }
- }
- void Updata()
- {
- int i, j;
- for (i = 1; i < 4; i++)
- {
- Serial.write(Frq_buff, 7);
- Frq_buff[1] += 1;
- for (j = 0; j < 3; j++)
- {
- Serial.write(EEPROM.read(i * 10 + j)); //数据地址 10 11 12 20 21 22 30 31 32
- }
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.write(0xff);
- }
- Frq_buff[1] = 0x30;
- }
- void Data_handle() //数据处理 规定delaytime 10 11 12 worktime 20 21 22 coldtime 30 31 32
- {
- delay_time = (unsigned int)(EEPROM.read(10) - 0x30) * 100 + (unsigned int)(EEPROM.read(11) - 0x30) * 10 + (unsigned int)(EEPROM.read(12) - 0x30);
- work_time = (unsigned int)(EEPROM.read(20) - 0x30) * 100 + (unsigned int)(EEPROM.read(21) - 0x30) * 10 + (unsigned int)(EEPROM.read(22) - 0x30);
- cold_time = (unsigned int)(EEPROM.read(30) - 0x30) * 100 + (unsigned int)(EEPROM.read(31) - 0x30) * 10 + (unsigned int)(EEPROM.read(32) - 0x30);
- }
- void shake()
- {
- if (buff[1] == 0x00)
- {
- shake_flag = 1;
- }
- else
- {
- shake_flag = 0;
- }
- }
- void EEPROMwrite()
- {
- EEPROM.write(10,0x32);
- EEPROM.write(11,0x30);
- EEPROM.write(12,0x30);
- EEPROM.write(20,0x34);
- EEPROM.write(21,0x30);
- EEPROM.write(22,0x30);
- EEPROM.write(30,0x32);
- EEPROM.write(31,0x30);
- EEPROM.write(32,0x30);
- }
复制代码
|