-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIC_Ultrasonic_PWM_Control.c
172 lines (137 loc) · 5.03 KB
/
PIC_Ultrasonic_PWM_Control.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
169
170
171
172
// Filename: Class Project
// Instructor: Prof. Eniko T. Enikov
// Group: 3
// Date: 3/23/2023
#include <xc.h>
#include <math.h>
#include <pic16f690.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
#pragma config FOSC = INTRCIO, WDTE = OFF, PWRTE = OFF, MCLRE = ON, CP = OFF, CPD = OFF, BOREN = OFF, IESO = OFF, FCMEN = OFF
// Global variable
volatile uint8_t timer0_overflow_count = 0;
// Function Declarations
int initialization();
float measure_duration();
void Send(unsigned char x);
unsigned char Receive(void);
void __interrupt() ISR(void);
int main(){
initialization();
PORTC = 0;
__delay_ms(100);
while (1) {
__delay_us(20);
float curr_duration = measure_duration();
char *bytes = (char*)(&curr_duration);
for(int i=0; i < 4;i++){
Send(bytes[i]);
}
//Toggle motor 1
unsigned int PWM = Receive();
if (PWM == 0){
CCPR1L = 0;
}
else if (PWM == 127){
CCPR1L = PWM;
STRA = 1;
STRB = 1;
__delay_ms(10);
}
else if (PWM < 127){
//Toggle motor 1
CCPR1L = PWM;
STRA = 1;
STRB = 0;
__delay_ms(10);
}
else{
//Toggle motor 2
CCPR1L = 255-PWM;
STRA = 0;
STRB = 1;
__delay_ms(10);
}
}
return 0;
}
float measure_duration(){
// Signal Initialization
RA0 = 1;
__delay_us(10);
RA0 = 0;
TMR1 = 0; // Reset Timer1
T1CONbits.TMR1ON = 1; // Enable Timer1
// Start counting
timer0_overflow_count = 0; // Reset the overflow count
TMR0 = 0; // Reset Timer0 value
while (PORTBbits.RB4 == 0 && timer0_overflow_count < 20) {} // Wait for the echo pulse to start
if (timer0_overflow_count >= 20) {
T1CONbits.TMR1ON = 0; // Disable Timer1
return 0; // If timed out, return 0
}
TMR1 = 0; // Reset the timer
while (PORTBbits.RB4 == 1 && timer0_overflow_count < 20) {} // Wait for the echo pulse to end
T1CONbits.TMR1ON = 0; // Disable Timer1
if (timer0_overflow_count >= 20) return 0; // If timed out, return 0
unsigned short duration = TMR1; // Read the timer value
// return duration * 0.034 / 2;
return duration;
}
void Send(unsigned char x){ // Send 1 Byte to MATLAB via RS232
TXREG = x; // Move Byte to Transmit Data Register
SPEN = 1; // Enable Continuous Send (RCSTA reg)
while (!TRMT); // Wait until TXREG is empty (TXSTA reg)
}
unsigned char Receive(void){ // Receive 1 Byte from MATLAB via RS232
CREN = 1; // Enable Asynchronous Receiver (RCSTA reg)
while (!RCIF); // Wait for RCREG to fill (PIR1 reg)
return RCREG;
}
void __interrupt() ISR(void){
if (INTCONbits.T0IF) // Check if Timer0 overflow interrupt occurred
{
timer0_overflow_count++;// Increment the overflow count
TMR0 = 0; // Reset Timer0 value
INTCONbits.T0IF = 0; // Clear the Timer0 overflow interrupt flag
}
}
int initialization(){
// BANK3
PSTRCON = 0b00010000; //steering update at beginning
// BANK2
ANSEL = 0b00000000; // All pins digital
ANSELH = 0b00000000; // All pins digital
// BANK1
OSCCON = 0b01110000; // Setting Oscillator to do 8MHz
TRISA = 0b00000100; // Input: pin AN0 pin A2 as digital input
TRISB = 0b00010000; // Input: RB4
TRISC = 0b00000000; // Output: all pins
//TXSTA bits
TRMT = 1; // Empty Transmit Shift register
BRGH = 1; // set Baud Rate high
SYNC = 0; // Asynchronous mode
TXEN = 1; // Enable transmission
BRG16 = 0; // Set Baud Rate Generator to 8bit. 1 for 16
SPBRG = 25; // Set baud rate timer period
// BANK0
//CCP1CON; P1A-D all active high, PWM cycle least sig bits, PWM half bridge
CCP1CON = 0b00001100;
PR2 = 255;
CCPR1L = 0b00000000;
T2CON = 0b01111110;
// Timer1 configuration (for measuring echo pulse duration)
T1CONbits.TMR1CS = 0; // Select internal clock (FOSC/4)
T1CONbits.TMR1ON = 0; // Disable Timer1
// Timer0 configuration (for handling timeout)
OPTION_REGbits.PSA = 0; // Assign prescaler to Timer0
OPTION_REGbits.PS = 0b010; // Set the prescaler to 1:8
OPTION_REGbits.T0CS = 0; // Select internal instruction cycle clock
OPTION_REGbits.T0SE = 0; // Increment on low-to-high transition on T0CKI pin
TMR0 = 0;
RA1 = 1; //Enable Direction for MTR 1
RA2 = 0; //Enable Direction for MTR 1
RA5 = 0; //Enable Direction for MTR 2
RA4 = 1; //Enable Direction for MTR 2
return 0;
}