Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
voltage float hex value
//gcc 7.4.0 #include <stdio.h> #include <stdbool.h> typedef struct { bool bit7:1; bool bit6:1; bool bit5:1; bool bit4:1; bool bit3:1; bool bit2:1; bool bit1:1; bool bit0:1; } bitmask; #define VOLTAGE2FLOAT(v) ((float)v / 1000) int main(void) { unsigned int voltage_word = 0; unsigned int voltage_word_shifted = 0; unsigned char high_byte = 0; unsigned char low_byte = 0; unsigned char reg13 = 0; unsigned char reg14 = 0; unsigned char new_high_byte = 0; unsigned char new_low_byte = 0; float v = 14.43; unsigned int voltage = 0; unsigned int chargerVoltageTmp = 0; unsigned int chargerVoltageTmpPlusOne = 0; voltage = 14235; if ( voltage > 15400) { printf("hex word will be greater than 16.5v. do not add 1 \r\n"); chargerVoltageTmp = (( VOLTAGE2FLOAT(voltage) ) - 2.96 ) / .02; } else { printf("hex word will be less than 16.5v so we need to add 1 \r\n"); chargerVoltageTmp = (( VOLTAGE2FLOAT(voltage) + 1 ) - 2.96 ) / .02; } // chargerVoltageTmp = (( VOLTAGE2FLOAT(voltage) ) - 2.96 ) / .02; // chargerVoltageTmpPlusOne = (( VOLTAGE2FLOAT(voltage) + 1 ) - 2.96 ) / .02; printf("voltage mv [%i], charger hex value [0x%02X] \r\n", voltage, chargerVoltageTmp); voltage_word = chargerVoltageTmp; // calculate voltage word // voltage_word = 0x02A5; printf("voltage_word: [0x%04X] \r\n", voltage_word); // shift left 1 bit voltage_word_shifted = (voltage_word << 1); printf("voltage_word_shifted: [0x%04X] \r\n", voltage_word_shifted); // break word into high byte and low byte high_byte = (voltage_word_shifted >> 8); low_byte = (voltage_word_shifted << 8) >> 8; printf("break up into high/low bytes: [0x%02X] [0x%02X]\r\n", high_byte, low_byte); // read current high byte & 0x07 to mask off correct bits then | calculated high byte reg13 = 0xA0; // current high byte new_high_byte = reg13 | high_byte; // read current low byte & 0x01 to mask off correct bits then | calulated low byte reg14 = 0x0D6; // current low byte new_low_byte = (reg14 & 0x01) | low_byte; printf("current register value: [0x%02X] [0x%02X]\r\n", reg13, reg14); printf("new register value: [0x%02X] [0x%02X]\r\n", new_high_byte, new_low_byte); return 0; }
run
|
edit
|
history
|
help
0
Vereinfachung
main.c
Project 5 v.05
MinMaxArray
TeleBook2
Project 3 part 2 Book v0.5
json formatter
2.1.3 Sum of squares of first hundred natural numbers with different cores
ytre
Assignment 8 pt 2