-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
168 lines (114 loc) · 4.55 KB
/
main.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
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
/**
* Author: Ahmed Sobhy
* Date: 10/06/2017
* Project Name: Fire Alarm System
* MCU: PIC16F676
* UPWORK: https://www.upwork.com/ab/proposals/916168207056740352
*/
// PIC16F676 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include "stdint.h"
#include "stdbool.h"
#define RESET_TIMER1 64536
volatile uint16_t ui16TickMs = 0;
void main(void) {
uint16_t ui16SwitchTimer = 0;
uint16_t ui16BlinkTimer = 0;
bool bFirstEntry = true;
// Initialize PORTA to input and all pull ups enabled
CMCONbits.CM = 0x07;
ANSEL = 0x00;
TRISA= 0xff;
WPUA = 0xff;
OPTION_REGbits.nRAPU = 0;
// Initialize PORTC to output
TRISC= 0x00;
PORTC= 0x00;
// Prescalar set to 1:1 of FOSC/4
T1CONbits.T1CKPS = 0x00;
// Internal CLK selected
T1CONbits.TMR1CS = 0;
// Init timer1 start value to interrupt every 1ms
TMR1 = RESET_TIMER1;
// enable timer1 interrupt
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
PIE1bits.TMR1IE = 1;
// turn timer1 ON
T1CONbits.TMR1ON = 0x01;
while(1){
// input RA0 will be pulled low and output RC4 will be pushed High
if( PORTAbits.RA0 == 0){
PORTCbits.RC4 = 1;
}
// input RA4 will be pulled low and output RC4 and RC3 will be pushed High
if( PORTAbits.RA4 == 0){
PORTCbits.RC4 = 1;
PORTCbits.RC3 = 1;
}
// input RA2 will be pulled low and output RC4 and RC2 will be pushed High
if( PORTAbits.RA2 == 0){
PORTCbits.RC4 = 1;
PORTCbits.RC2 = 1;
}
// input RA1 will be pulled low and output RC4 and RC1 will be pushed High
if( PORTAbits.RA1 == 0){
PORTCbits.RC4 = 1;
PORTCbits.RC1 = 1;
}
// If switch connected to RA5 is pressed
if( PORTAbits.RA5 == 0){
// start counting seconds
if( bFirstEntry ){
// set timer to current time
ui16SwitchTimer = ui16TickMs;
bFirstEntry = false;
}
// If we Press S1 for 02 seconds , input RA5 will be pulled low and output RC4 will be pulled Low
if( (ui16TickMs - ui16SwitchTimer) >= 2000 && (ui16TickMs - ui16SwitchTimer) < 3000 ){
PORTCbits.RC4 = 0;
}
// If we Press S1 for 05 seconds , input RA5 will be pulled low and output RC1, RC2, RC3 will be pulled Low
if( (ui16TickMs - ui16SwitchTimer) >= 5000 ){
PORTCbits.RC1 = 0;
PORTCbits.RC2 = 0;
PORTCbits.RC3 = 0;
bFirstEntry = true;
}
}
// Output RC5 will start blinking at 1Hz to show CPU in operation
if( ui16TickMs - ui16BlinkTimer >= 1000 ){
// reset Blink timer
ui16BlinkTimer = ui16TickMs;
// toggle pin RC5
PORTCbits.RC5 = ~PORTCbits.RC5;
}
// reset watchdog timer
CLRWDT();
}
}
void interrupt ISR(void){
if(PIR1bits.T1IF == 1){
// turn off counter
T1CONbits.TMR1ON = 0;
// clear the interrupt flag
PIR1bits.T1IF = 0;
// increment millisecond counter
ui16TickMs++;
// RESET_TIMER1
TMR1 = RESET_TIMER1;
// turn on timer1
T1CONbits.TMR1ON = 1;
}
}