/* * test.c * * Created: 06/08/2018 10:27:43 AM * Author : jitendra */ #include #include #define F_CPU 8000000UL #define DEVICES 1 #include #include #include #include #include #include #include #include #include #include #include "main.h" #include "SPI.h" #include "UART.h" #include "bmp280_fun.h" struct bmp280_dev bmp[DEVICES]; struct bmp280_config conf; //char data[400] ; static int uart_putchar(char c, FILE *stream); static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); /* for (uint8_t i=0;i cnt); //rslt = 1; cs_high(dev_id); //_delay_ms(10); return rslt; } int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) { /* * The parameter dev_id can be used as a variable to select which Chip Select pin has * to be set low to activate the relevant device on the SPI bus */ /* * Data on the bus should be like * |---------------------+--------------+-------------| * | MOSI | MISO | Chip Select | * |---------------------+--------------|-------------| * | (don't care) | (don't care) | HIGH | * | (reg_addr) | (don't care) | LOW | * | (reg_data[0]) | (don't care) | LOW | * | (....) | (....) | LOW | * | (reg_data[len - 1]) | (don't care) | LOW | * | (don't care) | (don't care) | HIGH | * |---------------------+--------------|-------------| */ int8_t rslt = 0; /* Return 0 for Success, non-zero for failure */ uint8_t cnt = 0; cs_low(dev_id); //_delay_ms(10); SPI_transmit(reg_addr); do { SPI_transmit(reg_data[cnt]); cnt++; }while (len > cnt); cs_high(dev_id); return rslt; } void BMP280_getMeasurement(struct bmp280_dev *dev) { int8_t rslt; int32_t temp32; //200628 uint32_t pres32; //200628 uint8_t meas_dur; struct bmp280_uncomp_data ucomp_data; struct bmp280_calib_param cal_param; for (uint8_t i = 0; i < DEVICES; i++){ meas_dur = bmp280_compute_meas_time(dev); dev[i].delay_ms(meas_dur); /* Measurement time */ rslt = bmp280_get_uncomp_data(&ucomp_data, dev); rslt = bmp280_get_comp_temp_32bit(&temp32, ucomp_data.uncomp_temp, dev); // 200628 // int32_t temp32 = bmp280_get_comp_temp_32bit(ucomp_data.uncomp_temp, dev); 200628 rslt = bmp280_get_comp_pres_32bit(&pres32, ucomp_data.uncomp_press, dev); // 200628 // uint32_t pres32 = bmp280_get_comp_pres_32bit(ucomp_data.uncomp_press, dev); 200628 // uint32_t pres64 = bmp280_get_comp_pres_64bit(ucomp_data.uncomp_press, dev); 200628 // double temp = bmp280_get_comp_temp_double(ucomp_data.uncomp_temp, dev); 200628 // double pres = bmp280_get_comp_pres_double(ucomp_data.uncomp_press, dev); 200628 char resString[300]; // sprintf(resString, "Sensor %d UT: %ld, UP: %ld, T32: %ld, P32: %ld\r\n", \ dev[i].dev_id, ucomp_data.uncomp_temp, ucomp_data.uncomp_press, temp32, pres32); sprintf(resString, "Sensor %d UT: %ld\r\n", dev[i].dev_id, ucomp_data.uncomp_temp); uart_puts(resString); dev[i].delay_ms(50); } /*Get the duration for 1 sample.*/ // meas_dur = bmp280_compute_meas_time(dev); /* Loop to read out 10 samples of data */ //for (uint8_t i = 0; (i < 10) && (rslt == BMP280_OK); i++) { //bmp.delay_ms(meas_dur); /* Measurement time */ // rslt = bmp280_get_uncomp_data(&ucomp_data, dev); //bmp.delay_ms(meas_dur); /* Measurement time */; /* Check if rslt == BMP280_OK, if not, then handle accordingly */ //printf(" code %d\r\n", rslt); /* uint8_t t1 = cal_param.dig_t1; printf(" t1: %u\r\n", t1); _delay_ms(100); uint8_t t2 = cal_param.dig_t2; printf(" t2: %u\r\n", t2); _delay_ms(100); uint8_t t3 = cal_param.dig_t3; printf(" t3: %u\r\n", t3); _delay_ms(100); uint8_t p1 = cal_param.dig_p1; printf(" p1: %u\r\n", p1); _delay_ms(100); uint8_t p2 = cal_param.dig_p2; printf(" p2: %u\r\n", p2); _delay_ms(100); uint8_t p3 = cal_param.dig_p3; printf(" p3: %u\r\n", p3); _delay_ms(100); */ uint32_t tfine = cal_param.t_fine; printf(" tfine: %u\r\n", tfine); } void port_init(void) { PORTA = 0x00; DDRA = 0x00; PORTB = 0x00; //pull-up on for pushbutton DDRB = 0x00; DDRE = 0b11111010; } void system_init(void) { stdout = &mystdout; //Required for printf init port_init(); spi_init(); USART_Init(BAUDRATE); uart_puts("BMP280 start..."); //inti UART with 9600 baud rate with XTAL 16Mhz init_bmp280(bmp); //while(1); } int main(void) { /* Replace with your application code */ for (uint8_t i= 0 ; i < 100; i++){ _delay_ms(100); } system_init(); uart_puts("BMP280 is ok..."); _delay_ms(500); while (1) { //for (uint8_t i = 0; i < DEVICES; i++) //{ //bmp.dev_id = i; BMP280_getMeasurement(bmp); _delay_ms(1000); //} } }