Exemple #1
0
int main(void) {
	char buf[40];
	uint8_t devices, i, j, count, alarm_count;
	uint8_t device[EXPECTING_SENSORS][8];
	uint8_t alarm_device[EXPECTING_SENSORS][8];
	float temps[EXPECTING_SENSORS];
	
	/* Initialize system */
	SystemInit();
	/* Initialize delay */
	TM_DELAY_Init();
	/* Initialize OneWire on pin PD0 */
	TM_OneWire_Init();
	/* Initialize USART, TX: PB6, RX: PB7 */
	TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200);
	
	/* Initialize Leds */
	TM_DISCO_LedInit();
	/* Turn leds on */
	TM_DISCO_LedOn(LED_RED | LED_GREEN);
	
	/* Checks for any device on 1-wire */
	devices = TM_OneWire_First();
	count = 0;
	while (devices) {
		count++;
		/* Get full ROM value, 8 bytes, give location of first byte where to save */
		TM_OneWire_GetFullROM(&device[count - 1][0]);
		/* Get next device */
		devices = TM_OneWire_Next();
	}
	/* If any devices on 1wire */
	if (count > 0) {
		sprintf(buf, "Devices found on 1-wire: %d\n\r", count);
		TM_USART_Puts(USART1, buf);
		/* Display 64bit rom code for each device */
		for (j = 0; j < count; j++) {
			for (i = 0; i < 8; i++) {
				sprintf(buf, "0x%02X ", device[j][i]);
				TM_USART_Puts(USART1, buf);
			}
			TM_USART_Puts(USART1, "\n\r");
		}
	} else {
		TM_USART_Puts(USART1, "No devices on OneWire.\n\r");
	}
	
	/* Go through all connected devices and set resolution to 12bits */
	for (i = 0; i < count; i++) {
		TM_DS18B20_SetResolution(&device[i][0], TM_DS18B20_Resolution_12bits);
	}
	/* Set high temperature alarm on device number 0, 25degrees celcius */
	TM_DS18B20_SetAlarmHighTemperature(&device[0][0], 25);
	/* Disable alarm temperatures on device number 1 */
	TM_DS18B20_DisableAlarmTemperature(&device[1][0]);
	
	while (1) {
		/* Start temperature conversion on all bits */
		TM_DS18B20_StartAll();
		/* Wait until all are done */
		while (!TM_DS18B20_AllDone());
		/* Read temperature from each device separatelly */
		for (i = 0; i < count; i++) {
			/* Read temperature from ROM address and store it to temps variable */
			TM_DS18B20_Read(&device[i][0], &temps[i]);
			/* Print temperature */
			sprintf(buf, "Temp %d: %3.5f; ", i, temps[i]);
			TM_USART_Puts(USART1, buf);
		}
		alarm_count = 0;
		/* Check if any device has alarm flag set */
		while (TM_DS18B20_AlarmSearch()) {
			/* Store ROM of device which has alarm flag set */
			TM_OneWire_GetFullROM(&alarm_device[alarm_count][0]);
			alarm_count++;
		}
		sprintf(buf, "alarm: %d\n\r", alarm_count);
		TM_USART_Puts(USART1, buf);
		/* Any device has alarm flag set? */
		if (alarm_count > 0) {
			TM_USART_Puts(USART1, "THIS DEVICES HAVE ALARM!\n\r    ");
			/* Show rom of this devices */
			for (j = 0; j < alarm_count; j++) {
				for (i = 0; i < 8; i++) {
					sprintf(buf, "0x%02X ", alarm_device[j][i]);
					TM_USART_Puts(USART1, buf);
				}
				TM_USART_Puts(USART1, "\n\r    ");
			}
			TM_USART_Puts(USART1, "ALARM devices recognized!\n\r");
		}
		/* Some delay */
		Delayms(1000);
	}
}
int tmp_get(void *data)
{
	static uint32_t init = 0;
	static uint8_t devices, i, j, alarm_count;
	static uint8_t count;
	static uint8_t device[EXPECTING_SENSORS][8];
	static uint8_t alarm_device[EXPECTING_SENSORS][8];
	static float temps[EXPECTING_SENSORS];
	/* OneWire working struct */
	static TM_OneWire_t OneWire1;

	if (init == 0) {
		init = 1;
		/* Initialize OneWire on pin PD0 */
		TM_OneWire_Init(&OneWire1, GPIOD, GPIO_Pin_0, RCC_APB2Periph_GPIOD);

		/* Checks for any device on 1-wire */
		count = 0;
		devices = TM_OneWire_First(&OneWire1);
		while (devices) {
			/* Increase counter */
			count++;
			
			/* Get full ROM value, 8 bytes, give location of first byte where to save */
			TM_OneWire_GetFullROM(&OneWire1, device[count - 1]);
			
			/* Get next device */
			devices = TM_OneWire_Next(&OneWire1);
		}

		/* If any devices on 1wire */
		if (count > 0) {
//			printf("Devices found on 1-wire: %d\n", count);
			/* Display 64bit rom code for each device */
			for (j = 0; j < count; j++) {
				for (i = 0; i < 8; i++) {
//					printf("0x%02X ", device[j][i]);
				}
//				printf("\n");
			}
		} else {
			printf("No devices on OneWire.\n");
		}
		
		/* Go through all connected devices and set resolution to 12bits */
		for (i = 0; i < count; i++) {
			/* Set resolution to 12bits */
			TM_DS18B20_SetResolution(&OneWire1, device[i], TM_DS18B20_Resolution_12bits);
		}

		/* Set high temperature alarm on device number 0, 25 degrees celcius */
		TM_DS18B20_SetAlarmHighTemperature(&OneWire1, device[0], 25);

		/* Disable alarm temperatures on device number 1 */
		TM_DS18B20_DisableAlarmTemperature(&OneWire1, device[1]);
		}
		/* Start temperature conversion on all devices on one bus */
		TM_DS18B20_StartAll(&OneWire1);
		
		/* Wait until all are done on one onewire port */
		while (!TM_DS18B20_AllDone(&OneWire1));
		
		/* Read temperature from each device separatelly */
		for (i = 0; i < count; i++) {
			/* Read temperature from ROM address and store it to temps variable */
			if (TM_DS18B20_Read(&OneWire1, device[i], &temps[i])) {
				/* Print temperature */
				printf("Temp %d: %d; \n", i, (int)temps[i]);
			} else {
				/* Reading error */
				printf("Reading error;\n");
			}
		}

		*(float *)(data) = temps[0];
		if (i == 0)
			return -1;
		
		/* Reset alarm count */
		alarm_count = 0;
		
		/* Check if any device has alarm flag set */
		while (TM_DS18B20_AlarmSearch(&OneWire1)) {
			/* Store ROM of device which has alarm flag set */
			TM_OneWire_GetFullROM(&OneWire1, alarm_device[alarm_count]);
			/* Increase count */
			alarm_count++;
		}
		
//		printf("Devices with alarm: %d\n", alarm_count);
		
		/* Any device has alarm flag set? */
		if (alarm_count > 0) {
			/* Show rom of this devices */
			for (j = 0; j < alarm_count; j++) {
//				printf("Device with alarm: ");
				for (i = 0; i < 8; i++) {
//					printf("0x%02X ", alarm_device[j][i]);
				}
//				printf("\n    ");
			}
//			printf("ALARM devices recognized!\n\r");
		}
		
		/* Print separator */
//		printf("----------\n");

		return 0;
}