Repository navigation
Expand file tree
/
Copy pathpressure_rig_16.cpp
More file actions
131 lines (106 loc) · 3.46 KB
/
Copy pathpressure_rig_16.cpp
File metadata and controls
131 lines (106 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <iostream>
#include <bitset>
#include <math.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
using namespace std;
float VREF = 5.0;
float RES = pow(2,12);
float G = 1.0;
float B = 0.06;
float A = 8.15;
int send_pressure(float p, const uint cs_pin, spi_inst_t *spi, uint16_t number)
{
int bytes;
uint16_t settings = 0x7; // 0111
settings = settings << 12;
float vout = ((p - B) / A);
float dac_float = (vout / 5.0) * 4096.0;
uint16_t dac_val = static_cast<uint16_t>(dac_float);
printf("Sending data to DAC \n");
printf("Expected VOUT %f\n",vout);
printf("Expected message 0x%X\n", dac_val);
uint16_t new_message_16bit = settings | dac_val;
const uint16_t* src = &new_message_16bit;
gpio_put(cs_pin, 0);
bytes = spi_write16_blocking(spi, src, 1);
gpio_put(cs_pin, 1);
return 0;
}
int read_pressure(const uint cs_pin, spi_inst_t *spi)
{
uint8_t buffer[2];
//uint16_t buffers[2];
uint16_t value;
uint16_t *dst = &value;
int half_word;
uint16_t result;
const float conversion_factor = 3.3f / (1 << 12);
{
gpio_put(cs_pin, 0);
//sleep_ms (2 * 1000);
half_word = spi_read16_blocking(spi, 0, dst, 1);
printf("Half Word Returned: %d", half_word);
// sleep_ms (2 * 1000);
// printf("IS SPI Busy: %d \n", spi_is_busy(spi));
// printf("Buffer 1 = %d ", buffer[0]);
// printf("Buffer 1 = 0x%x \n", buffer[0]);
// printf("Buffer 2 = %d ", buffer[1]);
// printf("Buffer 2 = 0x%x \n", buffer[1]);
printf("Buffer 2 = %d ", value);
printf("Buffer 2 = 0x%x \n", value);
gpio_put(cs_pin, 1);
printf("Voltage = %f \n",(value * conversion_factor));
//need to drop the first bit from buffer[0]
// result = (buffer[0] >> 1); //buffer is now 4 bits
// result = (buffer[0] << 8) | (buffer[1]);
// //concatenate both buffers to get the full 12 bit result from the mcp3301
// printf("Result = 0x%x \n",result);
// printf("Result = %d \n",result);
printf("Voltage = %f \n",(value * conversion_factor));
// printf("\n");
}
return 0;
}
int main ()
{
// MOSI TX
// MISO RX
int val;
uint16_t value = 0x88;
const uint sck_pin = 2; //CLOCK PIN
const uint mosi_pin = 3; //MOSI TX
const uint miso_pin = 4; //MISO RX
uint cs_pin = 1; //GP5
const uint cs_pin_dac = 8; //GP8 can be made into a chip select
spi_inst_t *spi = spi0;
stdio_init_all(); //Initialize all pins
sleep_ms(8500);
printf("SPI protocal begin \n");
// Setup SPI on Pico
spi_init(spi, 90000);
sleep_ms(10000);
spi_set_format(spi0, // SPI instance
16, // Number of bits per transfer
SPI_CPOL_1, // Polarity (CPOL)
SPI_CPHA_1, // Phase (CPHA)
SPI_MSB_FIRST);
// Initalize Pins
gpio_set_function(miso_pin,GPIO_FUNC_SPI);
gpio_set_function(mosi_pin,GPIO_FUNC_SPI);
gpio_set_function(sck_pin,GPIO_FUNC_SPI);
gpio_init(cs_pin);
gpio_set_dir(cs_pin, GPIO_OUT);
gpio_put(cs_pin, 1);
gpio_init(cs_pin_dac);
gpio_set_dir(cs_pin_dac, GPIO_OUT);
gpio_put(cs_pin_dac,1);
// has to run in a loop
while(1)
{
send_pressure(5.8,cs_pin_dac,spi,value);
sleep_ms(1000);
}
return 0;
}