使用:
1,HX711 100克称重模块
2,INA226电流传感器(也可以读取电压)
3,mega2560 模拟口直接读取电压
4,7寸触摸串口屏
本程序使用freerots作为框架取代定时器,与串口屏通讯并在串口屏上操作下发指令。
先放出部分代码和部分触摸屏截屏
/*
Name: uno_AD_I2C_UART.ino
Created: 2020/5/8 12:07:30
Author: 黄军杰
*/
#include <Arduino_FreeRTOS.h>
#include <INA226.h>
INA226 inaR1;
INA226 inaR2;
INA226 inaR3;
INA226 inaR4;
INA226 inaR5;
INA226 inaR6;
INA226 inaR7;
INA226 inaR8;
INA226 inaR9;
INA226 inaR10;
//马达控制pin
const int motor1 = 2;
const int motor2 = 3;
const int motor3 = 4;
const int motor4 = 5;
const int motor5 = 6;
const int motor6 = 7;
const int motor7 = 8;
const int motor8 = 9;
const int motor9 = 10;
const int motor10 = 11;
//马达状态
bool motor1state = true;
bool motor2state = true;
bool motor3state = true;
bool motor4state = true;
bool motor5state = true;
bool motor6state = true;
bool motor7state = true;
bool motor8state = true;
bool motor9state = true;
bool motor10state = true;
//ina226地址
const int inaAdd1 = 0x40;
const int inaAdd2 = 0x42;
const int inaAdd3 = 0x42;
const int inaAdd4 = 0x42;
const int inaAdd5 = 0x42;
const int inaAdd6 = 0x42;
const int inaAdd7 = 0x42;
const int inaAdd8 = 0x42;
const int inaAdd9 = 0x42;
const int inaAdd10 = 0x42;
char ina_read_Voltage[10] = {
0 }; //INA226读取电压数据
int ina_read_Current[10] = {
0 }; //INA226读取电流数据
int ina_read_busVoltage[10] = {
0 }; //INA226读取bus侧电压数据
//马达计数器
int motor1cont = 0;
int motor2cont = 0;
int motor3cont = 0;
int motor4cont = 0;
int motor5cont = 0;
int motor6cont = 0;
int motor7cont = 0;
int motor8cont = 0;
int motor9cont = 0;
int motor10cont = 0;
double hx_data[10] = {
0 };
const int adc_pin[10] = {
A0, A1, A2, A3, A4, A5, A6, A7, A8, A9 }; //马达侧电压探测PIN
float adc_data[10] = {
0 }; //马达电压储存
//rtos 任务句柄
static TaskHandle_t xHandleTaskmotor1run = NULL;
static TaskHandle_t xHandleTaskmotor2run = NULL;
static TaskHandle_t xHandleTaskmotor3run = NULL;
static TaskHandle_t xHandleTaskmotor4run = NULL;
static TaskHandle_t xHandleTaskmotor5run = NULL;
static TaskHandle_t xHandleTaskmotor6run = NULL;
static TaskHandle_t xHandleTaskmotor7run = NULL;
static TaskHandle_t xHandleTaskmotor8run = NULL;
static TaskHandle_t xHandleTaskmotor9run = NULL;
static TaskHandle_t xHandleTaskmotor10run = NULL;
static TaskHandle_t xHandleTaskinaread = NULL;
static TaskHandle_t xHandleTaskreadvoltage = NULL;
int endcont = 1000; //马达运行次数
int motorruntime = 80; //工作制 rtos 15MS一个cycle
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
//while (!Serial);
for (size_t i = 22; i < 41; i = i + 2)
{
pinMode(i, OUTPUT);
}
for (size_t i = 21; i < 43; i = i + 2)
{
pinMode(i, INPUT);
}
for (size_t i = 0; i < 10; i++)
{
pinMode(adc_pin[i], INPUT);
}
for (size_t i = 2; i < 12; i++)
{
pinMode(i, OUTPUT);
}
INA_SETUP();
tasksetup();
delay(1000);
Serial.println("all ready! ");
}
void loop()
{
SERIAL_READ();
}
//显示屏数据结尾3个OXFF
void HMI_OXFF()
{
Serial1.write("0XFF");
Serial1.write("0XFF");
Serial1.write("0XFF");
}
void SERIAL_READ()
{
char SerialInData = 'Z';
if (Serial.available() > 0)
{
SerialInData = Serial.read();
delay(2);
Serial.println(SerialInData);
}
switch (SerialInData)
{
case 'A':
motor1state = false;
digitalWrite(motor1, motor1state);
Serial.println("motor1 off");
vTaskSuspend(xHandleTaskmotor1run);
break;
case 'B':
motor1state = true;
digitalWrite(motor1, motor1state);
Serial.println("motor1 on");
vTaskResume(xHandleTaskmotor1run);
break;
case 'C':
digitalWrite(motor2, LOW);
Serial.println("motor2 off");
vTaskSuspend(xHandleTaskmotor2run);
break;
case 'D':
digitalWrite(motor2, HIGH);
Serial.println("motor2 on");
vTask