-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab1Ex4.c
135 lines (114 loc) · 4.7 KB
/
Lab1Ex4.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//0. INFORMATION & NOTES______________________________________________________________________________
/////////////////////////
// Lab assessment ME II /
// Name: Thomas Hollis /
// Student ID: 9563426 /
// Lab No. 1 /
// Exercise No. 4 /
/////////////////////////
//Port A: Analogue inputs from the rotary potentiometer (RA0), the LM335Z temperature sensor (RA1) and the Light Dependent Resistor (RA2). Digital output to control the LED transistor Q3 (RA4). 5V closed, 0V open.
//Port B: Push-button PB1 digital input (RB0).
//Port C: Toggle switch digital inputs (RC2, RC3, RC4 and RC5). Least significant.
//Port F: Digital outputs to control 8 LEDs and 7seg displays (RF0 ? RF7). Write 0V to illuminate.
//Port H: Digital outputs to control 7-segment transistors Q1 and Q2 (RH0 and RH1). 0V closed, 5V open. Toggle switch digital inputs (RH4, RH5, RH6 and RH7). Most significant.
//Port J: Push-button PB1 digital input (RJ5). Digital output for controlling the sounder (RJ6).
//7seg - 0:132, 1:245, 2:76, 3:100, 4:53, 5:38, 6:6, 7:244, 8:4, 9:36, A:20, B:7, C:142, D:132, E:14, F:30
//1. PREPROCESSOR DIRECTIVES__________________________________________________________________________
#include "xc_config_settings.h"
#include "delays.h"
//2. DEFINITIONS______________________________________________________________________________________
//#define PI 3.1415926
//#define E 2.71828182846
//#define E0 0.00000000000885
//TO CHANGE DURING LAB
#define HEX0 4 //8 - change to any symbol
#define HEX1 36 //9 - change to any symbol
#define HEX2 20 //A - change to any symbol
#define HEX3 7 //B - change to any symbol
//3. FUNCTIONS________________________________________________________________________________________
unsigned char read_S1(void) {
unsigned char x;
TRISC=0xFF;
x = PORTC >> 2;
return x;
}
unsigned char read_S2(void) {
unsigned char x;
TRISH=0xF0;
x = PORTH >> 4;
return x;
}
void display_on_U1 (unsigned int value){
LATH = 0b11111110; //swap this to start on different screen
LATF = value;
}
void display_on_U2 (unsigned int value){
LATH = 0b11111101;
LATF = value;
}
unsigned char PB1_pressed(void){
return (!PORTJbits.RJ5);
}
unsigned char PB2_pressed(void){
return (!PORTBbits.RB0);
}
//4. MAIN_____________________________________________________________________________________________
void main (void) {
//4.1. Configuration of I/O
ADCON1 = 0x0F; // enable digital I/O
TRISC = 0b00111100; // Port C bits all inputs - C2-C5 are switches 0-3
TRISH = 0b11110000; // Port H bits all inputs - H4-H7 are switches 4-7
TRISF = 0x00; // Port F output - LEDs/7seg display
TRISA = 0x00; //LED transistor enable
LATA = 0b00010000; //LED transistor enable
//4.2. Read & Display
int patterns[4] = {HEX0, HEX1, HEX2, HEX3};
unsigned char switch_bank, x, y, z; //variable declarations
x = 0;
while (1){
switch_bank = read_S1(); //change this to s2 if required
if (PB1_pressed() == 1){
x++;
Delay10KTCYx(65);
}
switch(switch_bank){
case 1:
if(x%2 == 0){
display_on_U1(patterns[0]);
}
else {
display_on_U2(patterns[0]);
}
break;
case 2:
if(x%2 == 0){
display_on_U1(patterns[1]);
}
else {
display_on_U2(patterns[1]);
}
break;
case 4:
if(x%2 == 0){
display_on_U1(patterns[2]);
}
else {
display_on_U2(patterns[2]);
}
break;
case 8:
if(x%2 == 0){
display_on_U1(patterns[3]);
}
else {
display_on_U2(patterns[3]);
}
break;
default:
LATH = 0b11111111;
LATF = 0b11111111;
}
}
}
//6. DEBUG STATUS_________________________________________________________________________________
//debugged