-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterr_ADC_PWM.c
More file actions
184 lines (108 loc) · 3.65 KB
/
Interr_ADC_PWM.c
File metadata and controls
184 lines (108 loc) · 3.65 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
/*
* File: adc_pwm.c
* Author: at
*
* Created on April 8, 2018, 4:03 PM
*/
#include "config.h"
#include <xc.h>
void SetupClock(void);
void PWM1_Init(unsigned char period);
void PWM1_setDC(unsigned int dutycycle);
void Setup_ADC(void);
void interrupt CheckSwitchPress();
void main(void) {
SetupClock();
/*Port configurations */
/* INPUTS */
//Make port B pin 0 as INput
TRISBbits.RB0 = 1 ;
/*OUTPUTS */
//Make port B pin 1 as OUTput
TRISBbits.RB1 = 0 ;
//Make port B pin 2 as OUTput
TRISBbits.RB2 = 0 ;
/*END of Port configurations */
/*Direction Set-up */
/* Forward Direction Motion */
LATBbits.LATB1 = 1;
LATBbits.LATB2 = 0;
//END
/* PWM Initialization */
//create a period
PWM1_Init(0x7C); //1KHZ period
//set up initial Duty cycle
PWM1_setDC(512);
/*END PWM Initialization */
/* ADC Initialization*/
// ADC set-up
unsigned int pot_val ;
Setup_ADC() ;
/* END of ADC initialization */
/* External-INTERRUPT Initialization */
INTCONbits.INT0IE = 1 ; // ENABLE interrupt 0 ( RB0)
INTCON2bits.INTEDG0 = 0 ; // Falling EDGE interrupt
INTCONbits.INT0IF = 0 ; //clear flag
INTCONbits.GIE=1; /* Enable Global Interrupt*/
ei(); // Enable Global interrupts
/* End OF interrupt Initialization*/
while (1){
//Delay for acquisition time
__delay_us(5);
//start conversion
ADCON0bits.GO = 1; //set GO/DONE bit
//wait until conversion done
while(ADCON0bits.GO==1){}; // wait until GO/DONE bit is cleared
//Read from the register the value
pot_val = ADRES;
// change Duty Cycle
PWM1_setDC(pot_val);
NOP();
}
return;
}
void Setup_ADC(){
TRISA0 = 1 ; // set RA0 as input
//msb and msb-1 don't care
//Vss as Ground , VDD as voltage reference 5V , AN0 only analog channel
ADCON1 = 0b00001110;
//msb-1 don't care
//right justified, 4 Tad , 1/8 FOSC
ADCON2 = 0b10010001;
//Active only CHAN0 , Turn on A/D converter
ADCON0 = ADCON0 | 1 ;
}
void SetupClock()
{
OSCCONbits.IRCF0 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF2 = 1;
}
void PWM1_Init(unsigned char period){
//TRISC &=0xFD; // Set RC2 as output
TRISCbits.RC2=0; //make Port2 as an Output
/* CCP PWM mode */
CCP1CON &= 0xCF; // 5,4 bits zeroed (DC1B1:DC1B0 = 00)
CCP1CON |= 0x0C; // PWM mode ( CCP1M3:CCP1M0 = 1100)
/* Timer2 configuration */
//TMR2 = 0x0F //Timer 2 inital value , if needed
PR2 = period; // configure timer2 period
T2CON = 0x02; // Set prescalar 16
TMR2ON = 1; // timer2 on
}
void PWM1_setDC(unsigned int dutycycle){
CCPR1L = dutycycle>>2; // PWM duty cycle - first 8-bits (MSb)
CCP1CON &= 0xCF; // 5,4 bits zeroed (DC1B1:DC1B0 = 00)
CCP1CON |= ((dutycycle<<4)&0x30); // PWM duty cycle - last 2-bits (LSb) in CCP1CON 5,4 bits
}
void interrupt CheckSwitchPress(){
if ( INTCONbits.INT0IF == 1) {
//Service the interrupt
/* Change direction of Wheel */
LATBbits.LATB1 = ~LATBbits.LATB1 ;
LATBbits.LATB2 = ~LATBbits.LATB2 ;
/* Finished */
/* clear Flag */
INTCONbits.INT0IF = 0 ;// clear interrupt flag
}
}