Repository navigation
Expand file tree
/
Copy pathfinalCode
More file actions
129 lines (112 loc) · 4.53 KB
/
Copy pathfinalCode
File metadata and controls
129 lines (112 loc) · 4.53 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
#include <xc.h>
#include <pic16f886.h>
#include <stdio.h>
#include <stdlib.h>
#include "digits.h"
// Define the system clock frequency
#define _XTAL_FREQ 8000000
// CONFIG1
#pragma config FOSC = INTRC_NOCLKOUT // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#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 BOREN = ON // Brown Out Reset Selection bits (BOR enbled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3/PGM pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
// CONFIG, changed settingsd
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bit (Code protection off)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
// Define solenoid selection pins and LED pin
#define SOLENOID_0 PORTAbits.RA0
#define SOLENOID_1 PORTAbits.RA1
#define SOLENOID_2 PORTAbits.RA2
#define SOLENOID_3 PORTAbits.RA3
#define LED PORTAbits.RA4
// Delay function prototype
void delay_ms(unsigned int ms);
// Function to select a solenoid
void select_solenoid(unsigned char solenoid) {
switch (solenoid) {
case 0:
SOLENOID_0 = 1;
break;
case 1:
SOLENOID_1 = 1;
break;
case 2:
SOLENOID_2 = 1;
break;
case 3:
SOLENOID_3 = 1;
break;
}
delay_ms(500); // Delay for 500 ms
switch (solenoid) {
case 0:
SOLENOID_0 = 0;
break;
case 1:
SOLENOID_1 = 0;
break;
case 2:
SOLENOID_2 = 0;
break;
case 3:
SOLENOID_3 = 0;
break;
}
delay_ms(500); // Delay for 500 ms
}
void main(void) {
OSCCON = 0x70; // Set up internal oscillator for 8MHz operation
ANSEL = 0x00; // Configure all I/O pins as digital
ANSELH = 0x00;
PORTA = 0x00; // Clear PORTA latch
TRISA = 0x00; // Set all PORTA pins as outputs
// Configure Timer0
OPTION_REGbits.T0CS = 0; // Timer0 clock source is the internal instruction cycle clock (Fosc/4)
OPTION_REGbits.PSA = 0; // Prescaler is assigned to the Timer0 module
OPTION_REGbits.PS = 0b100; // Prescaler is 32
TMR0 = 0; // Clear Timer0 register
INTCONbits.TMR0IE = 1; // Enable Timer0 interrupt
INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag
INTCONbits.GIE = 1; // Enable global interrupts
while(1) {
for (unsigned char i = 0; i < 4; i++) {
select_solenoid(i);
delay_ms(5000); // Delay for 5 seconds
}
}
PORTAbits.RA5 ^=1;
}
// Timer0 interrupt service routine
void __interrupt() ISR(void) {
static unsigned int count = 0;
if (INTCONbits.TMR0IF) {
TMR0 = 0; // Reset Timer0
count++;
if (count >= 61) { // Adjust this value for desired blink rate
LED = !LED; // Toggle LED
count = 0;
}
INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag
}
}
// Delay function implementation
void delay_ms(unsigned int ms) {
for (unsigned int i = 0; i < ms; i++) {
__delay_ms(1); // Use built-in delay function
}
}