Good morning,
I am trying to convert this Github C code: main.c, rtd.c, rtd.h, with functions referenced in the mikroSDK Reference Manual. Here is the datasheet for the RTD Click board. I want to run the converted Python script from a raspberry pi. For the time being you will have to take it on faith that I have wired everything correctly because I truly believe my root problem lies in the code conversion.
The problem is I don't know what message to send to the RTD Click board and how to read the response given. In particular, the rdt.c code has this code below.
I have created the following python code that [un]successfully runs on the hardware. That is, it runs but doesn't give me the answer I want. I referenced the python code from Sparkfun tutorial.
I think I could solve my problem if I knew how to convert this line into Python.
ctx is a typedef struct referencing the connection to the Click Board while 0x01 is a register. That line calls this...
Which writes ®_address of length 1 to the RTD Click board who then responds with rx_data of length 1. However, rx_data is of length 2 which is why the call rtd_read_register 2x but shift the data 1x.
How do I convert that last line into something python function xfer2(msg) will understand?
Thank you for any advice.
I am trying to convert this Github C code: main.c, rtd.c, rtd.h, with functions referenced in the mikroSDK Reference Manual. Here is the datasheet for the RTD Click board. I want to run the converted Python script from a raspberry pi. For the time being you will have to take it on faith that I have wired everything correctly because I truly believe my root problem lies in the code conversion.
The problem is I don't know what message to send to the RTD Click board and how to read the response given. In particular, the rdt.c code has this code below.
Code:
void rtd_generic_transfer
(
rtd_t *ctx,
uint8_t *wr_buf,
uint16_t wr_len,
uint8_t *rd_buf,
uint16_t rd_len
)
{
spi_master_select_device( ctx->chip_select );
spi_master_write_then_read( &ctx->spi, wr_buf, wr_len, rd_buf, rd_len );
spi_master_deselect_device( ctx->chip_select );
}
void rtd_write_register ( rtd_t *ctx, uint8_t reg_address, uint8_t write_data )
{
uint8_t tmp[ 2 ];
reg_address |= 0x80;
tmp[ 0 ] = reg_address;
tmp[ 1 ] = write_data;
spi_master_select_device( ctx->chip_select );
spi_master_write( &ctx->spi, tmp, 2 );
spi_master_deselect_device( ctx->chip_select );
}
uint8_t rtd_read_register ( rtd_t *ctx, uint8_t reg_address )
{
uint8_t tmp;
uint8_t rx_data[ 2 ];
rtd_generic_transfer( ctx, ®_address, 1, rx_data, 1 );
tmp = rx_data[ 0 ];
return tmp;
}
uint16_t rtd_read_temperature ( rtd_t *ctx )
{
uint16_t return_value;
return_value = rtd_read_register( ctx, 0x01 );
return_value <<= 8;
return_value |= rtd_read_register( ctx, 0x02 );
return return_value;
}
Code:
import RPi.GPIO as GPIO
import time
import spidev
spi_ch = 0
#enable SPI
spi = spidev.SpiDev(0, spi_ch)
spi.max_speed_hz = 100000
def read_adc(adc_ch):
msg = 0xb00
msg = [msg, 0xb00000000]
reply = spi.xfer2(msg)
adc = 0
for n in reply:
adc = (adc << 8) + n
adc = adc >> 1
voltage = adc * 1.175
return voltage
adc = read_adc(0)
print('ADC read: ' + str(adc))
spi.close()
GPIO.cleanup()
Code:
return_vale = rtd_read_register(ctx, 0x01);
Code:
rtd_generic_transfer(ctx, ®_address, 1, rx_data, 1);
How do I convert that last line into something python function xfer2(msg) will understand?
Thank you for any advice.
Last edited: