#include <stdio.h>
#include <stdint.h>
#define even 0
#define odd 1
#define enable 1
#define disable 0
typedef struct{
uint8_t parity_enable:1; // enable = 1, disable = 0
uint8_t parity_type:1; // even = 1, odd = 0
uint8_t reserved:6; // reserved bits
}UART_Parity;
uint8_t Create_UARTframe(uint8_t data,UART_Parity *ctrl){
uint8_t tmp = data;
uint8_t sum = 0;
uint8_t parity;
for(uint8_t i =0; i<8; i++){
sum += tmp & 0x1;
tmp >>= 1;
}
if(ctrl->parity_enable == disable){
return data;
}
else if(ctrl->parity_enable == enable){
if(ctrl->parity_type == even){
if(sum%2){
parity = 1;
}
else{
parity = 0;
}
}
else if(ctrl->parity_type == odd){
if(sum%2){
parity = 0;
}
else{
parity = 1;
}
}
}
data = data | (parity<<7);
return data;
}
int main(){
uint8_t data;
uint8_t parity_enable, parity_type;
UART_Parity uart_handle;
scanf("%hhu %hhu %hhu",&data , &parity_enable, &parity_type);
uart_handle.parity_enable = parity_enable;
uart_handle.parity_type = parity_type;
data = Create_UARTframe(data, &uart_handle);
printf("frame = 0x%02X",data);
return 0;
}
Input
85 1 0
Expected Output
frame = 0x55