-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOs.c
More file actions
194 lines (166 loc) · 5.75 KB
/
IOs.c
File metadata and controls
194 lines (166 loc) · 5.75 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* File: IOs.c
* Author: Ahad Ali, Mushtaba Al Yasseen, and Parbir Lehal
*
* Created on October 12, 2021, 3:26 PM
*/
#include "IOs.h"
#include "TimeDelay.h"
#include "UART2.h"
// Global Variables which are being used for the Application Project #1
uint16_t minutes = 00; // represents minutes on the clock (countdown/timer)
uint16_t seconds = 00; // represents seconds on the clock (countdown/timer)
int countdown_on = 0;
/*
* IO initializing
* Inputs: The three pushbuttons TRISA4, TRISA2 and TRISB4
* Pull Up resistors CN1PUE, CN0PUE and CN30PUE
* Return: nothing
*
*/
void IOinit() {
AD1PCFG = 0xFFFF; // Turn all analog pins as digital (as per lab manual)
// Set all inputs and outputs
TRISAbits.TRISA4 = 1; // Set RA4 as input
TRISBbits.TRISB4 = 1; // Set RB4 as input
TRISAbits.TRISA2 = 1; // Set RA2 as input
TRISBbits.TRISB8 = 0; // Set RB8 as output
// Set up the Pull up resistors
CNPU1bits.CN1PUE = 1; // Pull CN1 - RB4 Up
CNPU1bits.CN0PUE = 1; // Pull CN1 - RA4 Up
CNPU2bits.CN30PUE = 1; // Pull CN1 - RA2 Up
//Initialize CN Interrupt
CNEN1bits.CN0IE = 1; //Enables CN interrupt for CN0 (Pushbutton PB1)
CNEN1bits.CN1IE = 1; //Enables CN interrupt for CN1 (Pushbutton PB2)
CNEN2bits.CN30IE = 1; //Enables CN interrupt for CN30 (Pushbutton PB3)
IPC4bits.CNIP = 7; //Set the CN interrupt priority to a 7
IFS1bits.CNIF = 0; //Clears the CN interrupt flag
IEC1bits.CNIE = 1; //Enable the CN interrupt (general)
// UARTDisplayTime(); // displays starting time of 00m : 00s
return; // return to main
}
/*
* PushButton Check
* Inputs: Nothing
* Return: The LED LATB8
*
*/
void IOcheck() { // PB1 = RA2, PB2 = RA4, PB3 = RB4
// //UARTDisplayTime(); // displays starting time of 00m : 00s
//
//
// if ((PORTAbits.RA2 == 0 && PORTAbits.RA4 == 0 && PORTBbits.RB4 == 0)) {
// } else if ((PORTAbits.RA2 == 0 && PORTBbits.RB4 == 0)) {
// } else if ((PORTAbits.RA2 == 0 && PORTAbits.RA4 == 0)) {
// } else if ((PORTAbits.RA4 == 0 && PORTBbits.RB4 == 0)) {
// } else if (PORTAbits.RA2 == 0) { // PB1/bottom
// if (minutes < 59 && countdown_on == 0) {
// delay_ms(1000);
// minutes++;
// }
// } else if (PORTAbits.RA4 == 0) { // PB2/middle
// if (seconds < 59 && countdown_on == 0) {
// delay_ms(1000);
// seconds++;
// }
// } else if (PORTBbits.RB4 == 0) { // PB3/top
// //delay_ms(2000);
// uint16_t timePressed = 0;
// while (PORTBbits.RB4 == 0) {
// timePressed += 1;
// delay_ms(1000);
//
// }
//
// Disp2String('\n');
// if (timePressed < 3) {
// if (countdown_on == 1) {
// delay_ms(100);
// countdown_on = 0;
// } else {
// delay_ms(100);
// countdown_on = 1;
// }
// } else {
// minutes = 0; // Resets minutes back to zero
// seconds = 0; // Resets seconds back to zero
// countdown_on = 0; // turns off countdown;
// //UARTDisplayTime(); // displays starting time of 00m : 00s
//
// }
// }
// // else {
// //
// // }
//
//
// if (countdown_on == 1) {
//
// if (minutes == 0 && (seconds >= 0 && seconds <= 0)) {
// countdown_on = 0;
// LATBbits.LATB8 = 1;
// } else if (seconds == 0) {
// minutes--;
// seconds = 59;
// } else {
// LATBbits.LATB8 = 1 ? LATBbits.LATB8 == 0 : 0;
// seconds--;
// delay_ms(1000);
// }
//
//
// }
}
// clkval = 8
// clkval = 500 500 kHz
// clkval = 32 32 kHz
void NewClk(unsigned int clkval) {
char COSCNOSC;
if (clkval == 8) //8MHZ
{
COSCNOSC = 0x00;
} else if (clkval == 500) //500kHZ
{
COSCNOSC = 0x66;
} else if (clkval == 32) {
COSCNOSC = 0x55;
} else //default 32 kHZ
{
COSCNOSC = 0x55;
}
SRbits.IPL = 7;
CLKDIVbits.RCDIV = 0;
__builtin_write_OSCCONH(COSCNOSC);
__builtin_write_OSCCONL(0x01);
OSCCONbits.OSWEN = 1;
while (OSCCONbits.OSWEN == 1) {
}
SRbits.IPL = 0; //enable interrupts
}
//void UARTDisplayTime() // Function which will output the time when called
//{
// NewClk(8);
// if (countdown_on == 1 && minutes == 0 && seconds == 1) {
// Disp2String("\r \r");
// Disp2Dec(minutes); // Displaying the minutes in decimal (function was edited to have 2 values)
// Disp2String("m:"); // Displaying the minutes unit
// Disp2Dec(00); // Displaying the seconds in decimal (function was edited to have 2 values)
// Disp2String("s"); // Displaying the seconds unit}
// Disp2String(" -- ALARM\r"); // Displaying the seconds unit}
// } else {
// Disp2String("\r \r");
// Disp2Dec(minutes); // Displaying the minutes in decimal (function was edited to have 2 values)
// Disp2String("m:"); // Displaying the minutes unit
// Disp2Dec(seconds); // Displaying the seconds in decimal (function was edited to have 2 values)
// Disp2String("s\r"); // Displaying the seconds unit}
//
// }
// NewClk(32);
//}
// Gets called when any button is pressed (exits out of idle) instead of using polling
void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void) {
IFS1bits.CNIF = 0; // clear IF flag
IOcheck(); // Call IOCheck to determine what to do next
Nop(); // debounce buffer
return; // return to main
}