mirror of
https://github.com/ravinrabbid/DonCon2040.git
synced 2024-11-20 11:47:07 +01:00
Add library for MCP3204 ADC
This commit is contained in:
parent
661aef9fa9
commit
a7095c4aeb
@ -30,7 +30,8 @@ target_link_libraries(
|
|||||||
pico_multicore
|
pico_multicore
|
||||||
pio_ws2812
|
pio_ws2812
|
||||||
pico_ssd1306
|
pico_ssd1306
|
||||||
mcp23017)
|
mcp23017
|
||||||
|
mcp3204)
|
||||||
|
|
||||||
pico_enable_stdio_usb(${PROJECT_NAME} 1)
|
pico_enable_stdio_usb(${PROJECT_NAME} 1)
|
||||||
pico_enable_stdio_uart(${PROJECT_NAME} 0)
|
pico_enable_stdio_uart(${PROJECT_NAME} 0)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
add_subdirectory(pio_ws2812)
|
add_subdirectory(pio_ws2812)
|
||||||
add_subdirectory(pico_ssd1306)
|
add_subdirectory(pico_ssd1306)
|
||||||
add_subdirectory(mcp23017)
|
add_subdirectory(mcp23017)
|
||||||
|
add_subdirectory(mcp3204)
|
||||||
|
10
libs/mcp3204/CMakeLists.txt
Normal file
10
libs/mcp3204/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
file(GLOB mcp3204_SOURCES src/*.cpp)
|
||||||
|
|
||||||
|
add_library(mcp3204 STATIC ${mcp3204_SOURCES})
|
||||||
|
|
||||||
|
target_include_directories(
|
||||||
|
mcp3204
|
||||||
|
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include
|
||||||
|
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/mcp3204)
|
||||||
|
|
||||||
|
target_link_libraries(mcp3204 PUBLIC pico_stdlib hardware_spi)
|
21
libs/mcp3204/LICENSE
Normal file
21
libs/mcp3204/LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 Frederik Walk
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the “Software”), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
16
libs/mcp3204/include/mcp3204/Mcp3204.h
Normal file
16
libs/mcp3204/include/mcp3204/Mcp3204.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#ifndef _MCP3204_MCP3204_H_
|
||||||
|
#define _MCP3204_MCP3204_H_
|
||||||
|
|
||||||
|
#include "hardware/spi.h"
|
||||||
|
|
||||||
|
class Mcp3204 {
|
||||||
|
private:
|
||||||
|
spi_inst *m_spi;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Mcp3204(spi_inst *spi);
|
||||||
|
|
||||||
|
uint16_t read(uint8_t channel);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _MCP3204_MCP3204_H_
|
25
libs/mcp3204/src/Mcp3204.cpp
Normal file
25
libs/mcp3204/src/Mcp3204.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include "Mcp3204.h"
|
||||||
|
|
||||||
|
Mcp3204::Mcp3204(spi_inst *spi) : m_spi(spi) {
|
||||||
|
// Put SPI in 1,1 mode. In this mode date will be clocked out on
|
||||||
|
// a falling edge and latched from the ADC on a rising edge.
|
||||||
|
// Also the CS will be held low in-between bytes as required
|
||||||
|
// by the mcp3204.
|
||||||
|
spi_set_format(m_spi, 8, SPI_CPOL_1, SPI_CPHA_1, SPI_MSB_FIRST);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t Mcp3204::read(uint8_t channel) {
|
||||||
|
// Byte 1: '00000' to align the ADC's output,
|
||||||
|
// '1' as start bit,
|
||||||
|
// '1' for single-ended read,
|
||||||
|
// '0' for D2 (which is 'don't care' on MCP3204)
|
||||||
|
// Byte 2: Channel bits D1 and D0, followed by '0's as clocks to receive result
|
||||||
|
// Byte 3: Further '0's to receive result
|
||||||
|
uint8_t data_out[3] = {0x06, static_cast<uint8_t>(channel << 6), 0x00};
|
||||||
|
uint8_t data_in[3] = {};
|
||||||
|
|
||||||
|
spi_write_read_blocking(m_spi, data_out, data_in, 3);
|
||||||
|
|
||||||
|
// The 12 result bits are at the end of the ADC's output.
|
||||||
|
return (static_cast<uint16_t>(data_in[1] & 0x0F) << 8) | data_in[2];
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user