lots of fixes

This commit is contained in:
2025-11-24 16:57:26 +01:00
parent 55a1469b99
commit 9e771ba4fc
5 changed files with 167 additions and 71 deletions

2
.gitmodules vendored
View File

@@ -1,3 +1,3 @@
[submodule "libs/adafruitGFX"] [submodule "libs/adafruitGFX"]
path = libs/adafruitGFX path = libs/adafruitGFX
url = https://github.com/MarkWalter94/AdafruitGFX-RaspberryPICO.git url = https://git.gay/LailaTheElf/AdafruitGFX-RaspberryPICO.git

View File

@@ -1,3 +1,5 @@
#include <cstdint>
#include <pico/time.h>
#include <stdio.h> #include <stdio.h>
#include <pico/stdlib.h> #include <pico/stdlib.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
@@ -11,12 +13,12 @@
#define PIN_RGB_G 16 #define PIN_RGB_G 16
#define PIN_RGB_B 25 #define PIN_RGB_B 25
#define PIN_SPI_MISO 0
#define PIN_SPI_CLK 2 #define PIN_SPI_CLK 2
#define PIN_SPI_MISO 4
#define PIN_SPI_MOSI 3 #define PIN_SPI_MOSI 3
#define PIN_MAX31856_CS 1 #define PIN_MAX31856_CS 1
#define PIN_DISPLAY_CS 4 #define PIN_DISPLAY_CS 0
#define PIN_DISPLAY_DC 5 #define PIN_DISPLAY_DC 7
#define PIN_DISPLAY_RST 6 #define PIN_DISPLAY_RST 6
max31856_t temp[2]; max31856_t temp[2];
@@ -51,40 +53,58 @@ void rgb_rainbow()
int main() { int main() {
stdio_init_all(); stdio_init_all();
rgb_rainbow();
rgb_rainbow();
printf("Hello, world!\n");
init_rbg(); init_rbg();
rgb_rainbow();
rgb_rainbow();
display.begin(); display.begin();
// init temp sensors // init temp sensors
// temp[0].pin_cs = PIN_MAX31856_CS; temp[0].pin_cs = PIN_MAX31856_CS;
// max31856_init(&(temp[0])); max31856_init(&(temp[0]));
// // temp[1].pin_cs = 5; gpio_set_function(PIN_SPI_CLK, GPIO_FUNC_SPI);
// //max31856_init(&(temp[1])); gpio_set_function(PIN_SPI_MOSI, GPIO_FUNC_SPI);
// // config temp sensor 0 gpio_set_function(PIN_SPI_MISO, GPIO_FUNC_SPI);
// max31856_write8(&(temp[0]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); // temp[1].pin_cs = 5;
// max31856_write8(&(temp[0]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K); //max31856_init(&(temp[1]));
// // config temp sensor 1 // config temp sensor 0
// //max31856_write8(&(temp[1]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ); max31856_write8(&(temp[0]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ);
// //max31856_write8(&(temp[1]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K); max31856_write8(&(temp[0]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_4 | MAX31856_REG_CR1_TC_TYPE_K);
// config temp sensor 1
//max31856_write8(&(temp[1]), MAX31856_REG_CR0, MAX31856_REG_CR0_CMODE | /*MAX31856_REG_CR0_OCFAULT |*/ MAX31856_REG_CR0_50HZ);
//max31856_write8(&(temp[1]), MAX31856_REG_CR1, MAX31856_REG_CR1_AVGSEL_1 | MAX31856_REG_CR1_TC_TYPE_K);
display.fillScreen(SSD1306_WHITE); display.fillScreen(SSD1306_BLACK);
display.setTextColor(SSD1306_BLACK); display.setTextColor(SSD1306_WHITE);
display.print("Henlo doggo"); display.setTextSize(2);
display.display();
rgb_rainbow(); rgb_rainbow();
while (true) while (true)
{ {
display.fillScreen(SSD1306_WHITE); char cj_str[8];
rgb_rainbow(); char tc_str[8];
spi_set_format(SPI_MAX31856, SPI_MAX31856_BLOCK_SIZE, SPI_MAX31856_SPO, SPI_MAX31856_SPH, SPI_MAX31856_BIT_ORDER);
sleep_ms(5);
max31856_temp_t cj = max31856_get_cj_temp_c(&temp[0]);
snprintf(&cj_str[0], 8, "%i.%02uC", cj.i, cj.f);
max31856_temp_t tc = max31856_get_tc_temp_c(&temp[0]);
snprintf(&tc_str[0], 8, "%i.%02uC", tc.i, tc.f);
printf("CJ temp: %s\n", &cj_str[0]);
printf("TC temp: %s\n", &tc_str[0]);
spi_set_format(spi0, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
sleep_ms(5);
display.fillScreen(SSD1306_BLACK); display.fillScreen(SSD1306_BLACK);
rgb_rainbow(); display.setCursor(0, 0);
display.print("CJ ");
display.println(&cj_str[0]);
display.print("TC ");
display.println(&tc_str[0]);
display.display();
sleep_ms(200);
} }
return 0; return 0;
} }

View File

@@ -1,17 +1,10 @@
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "hardware/spi.h" #include "hardware/spi.h"
#include <cstdint>
#include <stdint.h> #include <stdint.h>
#include "max31856.h" #include "max31856.h"
// max31856 pins
#define SPI_MAX31856 spi0
#define SPI_MAX31856_BOUD 1E6 // 1 MHz (max31856 can go up to 5 MHz)
#define SPI_MAX31856_SPO SPI_CPOL_0
#define SPI_MAX31856_SPH SPI_CPHA_1
#define SPI_MAX31856_BIT_ORDER SPI_MSB_FIRST
#define SPI_MAX31856_BLOCK_SIZE 8
void max31856_init(max31856_t *max) void max31856_init(max31856_t *max)
{ {
// init GPIO for CS // init GPIO for CS
@@ -53,7 +46,7 @@ uint8_t max31856_read8(max31856_t *max, uint8_t addr)
{ {
return -1; return -1;
} }
uint16_t buff_tx = (addr & 0x7F)<<8; uint8_t buff_tx = addr & 0x7F;
uint8_t buff_rx[] = {0, 0}; uint8_t buff_rx[] = {0, 0};
gpio_put(max->pin_cs, 0); gpio_put(max->pin_cs, 0);
spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 2); spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 2);
@@ -68,7 +61,7 @@ uint16_t max31856_read16(max31856_t *max, uint8_t addr)
{ {
return -1; return -1;
} }
uint16_t buff_tx = (addr & 0x7F)<<8; uint8_t buff_tx = addr & 0x7F;
uint8_t buff_rx[] = {0, 0, 0}; uint8_t buff_rx[] = {0, 0, 0};
gpio_put(max->pin_cs, 0); gpio_put(max->pin_cs, 0);
spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 3); spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 3);
@@ -76,3 +69,63 @@ uint16_t max31856_read16(max31856_t *max, uint8_t addr)
return buff_rx[1]<<8 | buff_rx[2]; return buff_rx[1]<<8 | buff_rx[2];
} }
uint32_t max31856_read24(max31856_t *max, uint8_t addr)
{
if (!(max->initilzed))
{
return -1;
}
uint8_t buff_tx = addr & 0x7F;
uint8_t buff_rx[] = {0, 0, 0, 0};
gpio_put(max->pin_cs, 0);
spi_read_blocking(SPI_MAX31856, buff_tx, &buff_rx[0], 4);
gpio_put(max->pin_cs, 1);
return 0 | buff_rx[1]<<16 | buff_rx[2]<<8 | buff_rx[3];
}
max31856_temp_t max31856_convert_cj_temp_c(uint16_t row) {
bool negarive = ((row & 0x8000) == 0) ? false : true;
if (negarive) {
row = (~row) + 1;
}
max31856_temp_t temp = {
(int16_t)(row >> 8),
(uint8_t)(((row & 0xFF)*100)/256)
};
if (negarive) {
temp.i = -temp.i;
}
return temp;
}
max31856_temp_t max31856_convert_tc_temp_c(uint32_t row) {
bool negarive = ((row & 0x800000) == 0) ? false : true;
if (negarive) {
row = (row^0xFFFFFF) + 1;
}
row &= 0xFFFFFF;
max31856_temp_t temp = {
(int16_t)(row >> 12),
(uint8_t)(((row & 0xFFF)*100)/0x1000)
};
if (negarive) {
temp.i = -temp.i;
}
return temp;
}
max31856_temp_t max31856_get_cj_temp_c(max31856_t *max) {
return max31856_convert_cj_temp_c(max31856_read16(max, MAX31856_REG_CJTH));
}
max31856_temp_t max31856_get_tc_temp_c(max31856_t *max) {
return max31856_convert_tc_temp_c(max31856_read24(max, MAX31856_REG_LTCBH));
}

View File

@@ -1,6 +1,7 @@
#ifndef MAX31856_H #ifndef MAX31856_H
#define MAX31856_H #define MAX31856_H
#include <cstdint>
#include <stdint.h> #include <stdint.h>
typedef struct max31856_s { typedef struct max31856_s {
@@ -9,46 +10,68 @@ typedef struct max31856_s {
bool autoDeinit; bool autoDeinit;
} max31856_t; } max31856_t;
typedef struct max31856_temp_s {
int16_t i;
uint8_t f;
} max31856_temp_t;
void max31856_init(max31856_t *max); void max31856_init(max31856_t *max);
void max31856_deinit(max31856_t *max); void max31856_deinit(max31856_t *max);
int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value); int max31856_write8(max31856_t *max, uint8_t addr, uint8_t value);
uint16_t max31856_read16(max31856_t *max, uint8_t addr); uint16_t max31856_read16(max31856_t *max, uint8_t addr);
uint32_t max31856_read24(max31856_t *max, uint8_t addr);
max31856_temp_t max31856_convert_cj_temp_c(uint16_t row);
max31856_temp_t max31856_convert_tc_temp_c(uint32_t row);
max31856_temp_t max31856_get_cj_temp_c(max31856_t *max);
max31856_temp_t max31856_get_tc_temp_c(max31856_t *max);
#define MAX31856_REG_CR0 0x00 #define MAX31856_REG_CR0 ((uint8_t)0x00)
#define MAX31856_REG_CR0_CMODE 0x80 #define MAX31856_REG_CR0_CMODE ((uint8_t)0x80) // convertion mode
#define MAX31856_REG_CR0_1SHOT 0x40 #define MAX31856_REG_CR0_1SHOT ((uint8_t)0x40) // one-shot mode
#define MAX31856_REG_CR0_OCFAULT 0x10 #define MAX31856_REG_CR0_OCFAULT ((uint8_t)0x30) // open-circuit fault detection
#define MAX31856_REG_CR0_JC 0x08 #define MAX31856_REG_CR0_JC ((uint8_t)0x08) // cold-junction sensor disable
#define MAX31856_REG_CR0_FAULT 0x04 #define MAX31856_REG_CR0_FAULT ((uint8_t)0x04) // fault mode
#define MAX31856_REG_CR0_FAULTCLR 0x02 #define MAX31856_REG_CR0_FAULTCLR ((uint8_t)0x02) // foult status clear
#define MAX31856_REG_CR0_50HZ 0x01 #define MAX31856_REG_CR0_50HZ ((uint8_t)0x01) // 50Hz/60Hz node regection filter selection
#define MAX31856_REG_CR1 0x01 #define MAX31856_REG_CR1 ((uint8_t)0x01)
#define MAX31856_REG_CR1_AVGSEL_1 0x00 #define MAX31856_REG_CR1_AVGSEL_1 ((uint8_t)0x70) // thermocouple voltage convertion 1 semple averaging
#define MAX31856_REG_CR1_AVGSEL_2 0x10 #define MAX31856_REG_CR1_AVGSEL_2 ((uint8_t)0x10) // thermocouple voltage convertion 2 semples averaging
#define MAX31856_REG_CR1_AVGSEL_4 0x20 #define MAX31856_REG_CR1_AVGSEL_4 ((uint8_t)0x20) // thermocouple voltage convertion 4 semples averaging
#define MAX31856_REG_CR1_AVGSEL_8 0x30 #define MAX31856_REG_CR1_AVGSEL_8 ((uint8_t)0x30) // thermocouple voltage convertion 8 semples averaging
#define MAX31856_REG_CR1_AVGSEL_16 0x40 #define MAX31856_REG_CR1_AVGSEL_16 ((uint8_t)0x40) // thermocouple voltage convertion 16 semples averaging
#define MAX31856_REG_CR1_TC_TYPE_B 0x00 #define MAX31856_REG_CR1_TC_TYPE_B ((uint8_t)0x00) // thermocouple type B
#define MAX31856_REG_CR1_TC_TYPE_E 0x01 #define MAX31856_REG_CR1_TC_TYPE_E ((uint8_t)0x01) // thermocouple type E
#define MAX31856_REG_CR1_TC_TYPE_J 0x02 #define MAX31856_REG_CR1_TC_TYPE_J ((uint8_t)0x02) // thermocouple type J
#define MAX31856_REG_CR1_TC_TYPE_K 0x03 #define MAX31856_REG_CR1_TC_TYPE_K ((uint8_t)0x03) // thermocouple type K
#define MAX31856_REG_CR1_TC_TYPE_N 0x04 #define MAX31856_REG_CR1_TC_TYPE_N ((uint8_t)0x04) // thermocouple type N
#define MAX31856_REG_CR1_TC_TYPE_R 0x05 #define MAX31856_REG_CR1_TC_TYPE_R ((uint8_t)0x05) // thermocouple type R
#define MAX31856_REG_CR1_TC_TYPE_S 0x06 #define MAX31856_REG_CR1_TC_TYPE_S ((uint8_t)0x06) // thermocouple type S
#define MAX31856_REG_CR1_TC_TYPE_T 0x07 #define MAX31856_REG_CR1_TC_TYPE_T ((uint8_t)0x07) // thermocouple type T
#define MAX31856_REG_CR1_VM_GAIN8 0x08 #define MAX31856_REG_CR1_VM_GAIN8 ((uint8_t)0x08) // voltage mode, gain 8
#define MAX31856_REG_CR1_VM_GAIN32 0x0C #define MAX31856_REG_CR1_VM_GAIN32 ((uint8_t)0x0C) // voltage mode, gain 32
#define MAX31856_REG_MASK 0x02 #define MAX31856_REG_MASK ((uint8_t)0x02) // fault mask
#define MAX31856_REG_CJHF 0x03 #define MAX31856_REG_CJHF ((uint8_t)0x03) // cold juntion hige fault theshold
#define MAX31856_REG_CJLF 0x04 #define MAX31856_REG_CJLF ((uint8_t)0x04) // cold juntion low fault theshold
#define MAX31856_REG_LTHFTH 0x05 #define MAX31856_REG_LTHFTH ((uint8_t)0x05)
#define MAX31856_REG_LTHFTL 0x06 #define MAX31856_REG_LTHFTL ((uint8_t)0x06)
#define MAX31856_REG_LTLFTH 0x07 #define MAX31856_REG_LTLFTH ((uint8_t)0x07)
#define MAX31856_REG_LTLFTL 0x08 #define MAX31856_REG_LTLFTL ((uint8_t)0x08)
#define MAX31856_REG_CJTO 0x09 #define MAX31856_REG_CJTO ((uint8_t)0x09) // cold juntion temp offset
#define MAX31856_REG_CJTH 0x0A #define MAX31856_REG_CJTH ((uint8_t)0x0A) // cold juntion temp hige byte
#define MAX31856_REG_CJTL 0x0B #define MAX31856_REG_CJTL ((uint8_t)0x0B) // cold juntion temp low byte
#define MAX31856_REG_LTCBH ((uint8_t)0x0C) // thermocouple temp byte 2
#define MAX31856_REG_LTCBM ((uint8_t)0x0D) // thermocouple temp byte 1
#define MAX31856_REG_LTCBL ((uint8_t)0x0E) // thermocouple temp byte 0
#define MAX31856_REG_SR ((uint8_t)0x0F) // fault status
// max31856 pins
#define SPI_MAX31856 spi0
#define SPI_MAX31856_BOUD 1000E3 // 1 MHz (max31856 can go up to 5 MHz)
#define SPI_MAX31856_SPO SPI_CPOL_0
#define SPI_MAX31856_SPH SPI_CPHA_1
#define SPI_MAX31856_BIT_ORDER SPI_MSB_FIRST
#define SPI_MAX31856_BLOCK_SIZE 8
#endif #endif