Page 8 of 21
Chapter 10
Page 184
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
char buf[] = {0xE7};
bcm2835_i2c_begin();
bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_148);
bcm2835_i2c_setSlaveAddress(0x40);
bcm2835_i2c_write(buf, 1);
bcm2835_i2c_read(buf, 1);
printf("User Register = %X \r\n", buf[0]);
bcm2835_i2c_end();
return (EXIT_SUCCESS);
}
Page 186
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
void setTimeout(uint32_t timeout) {
volatile uint32_t* stimeout = bcm2835_bsc1 + BCM2835_BSC_CLKT / 4;
bcm2835_peri_write(stimeout, timeout);
}
int main(int argc, char** argv) {
if (!bcm2835_init())
return 1;
bcm2835_i2c_begin();
bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_150);
setTimeout(0);
char buf[4] = {0xE3};
uint8_t status = bcm2835_i2c_read_register_rs(buf, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
printf("msb %d \n\r lsb %d \n\r checksum %d \n\r", msb, lsb, check);
bcm2835_i2c_end();
return (EXIT_SUCCESS);
}
Page 188
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
void setTimeout(uint32_t timeout)
{
volatile uint32_t *stimeout = bcm2835_bsc1 + BCM2835_BSC_CLKT / 4;
bcm2835_peri_write(stimeout, timeout);
}
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_i2c_begin();
bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500);
setTimeout(20000);
char buf[3] = {0xE3};
uint8_t status = bcm2835_i2c_read_register_rs(buf, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
printf("msb %d \n\r lsb %d \n\r checksum %d \n\r", msb, lsb, check);
bcm2835_i2c_end();
return (EXIT_SUCCESS);
}
Page 192
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
void setTimeout(uint16_t timeout);
uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check);
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_i2c_begin();
bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500);
setTimeout(20000);
char buf[4] = {0xE3};
uint8_t status = bcm2835_i2c_read_register_rs(buf, buf, 3);
uint8_t msb = buf[0];
uint8_t lsb = buf[1];
uint8_t check = buf[2];
printf("crc = %d\n\r", crcCheck(msb, lsb, check));
unsigned int data16 = ((unsigned int)msb << 8) | (unsigned int)(lsb & 0xFC);
float temp = (float)(-46.85 + (175.72 * data16 / (float)65536));
printf("Temperature %f C \n\r", temp);
buf[0] = 0xF5;
bcm2835_i2c_write(buf, 1);
while (bcm2835_i2c_read(buf, 3) == BCM2835_I2C_REASON_ERROR_NACK)
{
bcm2835_delayMicroseconds(500);
};
msb = buf[0];
lsb = buf[1];
check = buf[2];
printf("crc = %d\n\r", crcCheck(msb, lsb, check));
data16 = ((unsigned int)msb << 8) | (unsigned int)(lsb & 0xFC);
float hum = -6 + (125.0 * (float)data16) / 65536;
printf("Humidity %f %% \n\r", hum);
bcm2835_i2c_end();
return (EXIT_SUCCESS);
}
void setTimeout(uint16_t timeout)
{
volatile uint32_t *stimeout = bcm2835_bsc1 + BCM2835_BSC_CLKT / 4;
bcm2835_peri_write(stimeout, timeout);
}
uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check)
{
uint32_t data32 = ((uint32_t)msb << 16) | ((uint32_t)lsb << 8) | (uint32_t)check;
uint32_t divisor = 0x988000;
for (int i = 0; i < 16; i++)
{
if (data32 & (uint32_t)1 << (23 - i))
data32 ^= divisor;
divisor >>= 1;
};
return (uint8_t)data32;
}