-
Notifications
You must be signed in to change notification settings - Fork 0
/
exerc_4_3.c
77 lines (64 loc) · 1.41 KB
/
exerc_4_3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ====================================
File name: exerc_4_3.c
Date: 2020-02-19
Group nr 19
Members that contribute to the solutions
Kent Edström
Joakim Deak
Member not present at the demonstration time
Demonstration code: 22346
====================================== */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
void f_delay(int);
unsigned char random_inport( void);
void printport( int);
int main(){
int nr;
unsigned char port;
srand(time(NULL));
for ( nr=0; nr < 10; nr++){
port = random_inport();
printport( port);
f_delay(5);
}
return(0);
}
void f_delay( int tenth_sec){
clock_t start_t, end_t;
long int i;
start_t = clock();
do{
for(i=0; i< 10000000; i++);
end_t = clock();
}while ((end_t - start_t) < (tenth_sec*CLOCKS_PER_SEC)/10);
return;
}
unsigned char random_inport( void){
unsigned char inport = 0;
inport= rand() % 256;
return (inport);
}
void printport( int portvalue){
int binchar[8], rest,j, i=0;
rest = portvalue;
while(rest!=0){
binchar[i++]= rest % 2;
rest = rest / 2;
}
// Fill to 8 bits
while( i<8){
binchar[i++]=0;
}
// Print bits and at the end corresponding decimal value
int pressed = binchar[7];
for(j =i-1 ;j > -1;j--){
if(pressed){
printf(" %d",binchar[j]);
}
}
if(pressed){
printf(" --------Porten value = %X \n", portvalue);
}
}