-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_PIC.c
executable file
·86 lines (61 loc) · 2.3 KB
/
init_PIC.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
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#include "init_PIC.h"
#define TMR1H_0 0xFA // Fine tuned Timer1 registers (Ton = 1.5 msec => set position 0°)
#define TMR1L_0 0x60 //
#define TMR0H_set 0xB2 // Fine tuned Timer0 registers (PWM period)
#define TMR0L_set 0x04 //
static uint8_t new_TMR1H = TMR1H_0; //new pulse duration (i.e. new position of the servo)
static uint8_t new_TMR1L = TMR1L_0;
void init_PORTS(void) //PORTS configurations
{
TRISD = 0x00;
TRISA = 0x00;
TRISB = 0X03;
PORTA = 0x00;
PORTD = 0x00;
// PORTC
LATC = 0; //All pins off
PORTC = 0;
TRISCbits.RC7 = 1; //All pins as input (default), RC7 as Input (Usart RX)
TRISCbits.RC6 = 0; //RC6 as output (Usart TX)
TRISCbits.RC2 = 0;
return;
}
//--------------------------------------------------
void init_Timers(void){
T0CONbits.T08BIT = 0; //16-bit
T0CONbits.T0CS = 0; //internal instruction cycle clock (timer)
T0CONbits.PSA = 1; //No prescaler
T0CONbits.TMR0ON = 0; //Timer 0 disabled
TMR0H = TMR0H_set; //set TMR0H
TMR0L = TMR0L_set; //set TMR0L
//Timer 1
T1CONbits.RD16 = 1; //16-bit
T1CONbits.T1CKPS1 = 0; //No prescaler
T1CONbits.T1CKPS0 = 0;
T1CONbits.T1OSCEN = 0; //Timer 1 ext oscillator disabled
T1CONbits.TMR1CS = 0; //Internal clock (Fosc/4)
T1CONbits.TMR1ON = 0; //Timer 1 disabled
//set intitial position = 0°
TMR1H = TMR1H_0;
TMR1L = TMR1L_0;
return;
}
void init_interrupts(void) // Interrupts configuration
{
PIR1bits.RCIF = 0; //Clear RCIF Interrupt Flag
PIE1bits.RCIE = 1; //Set RCIE Interrupt Enable (USART RX)
INTCONbits.PEIE = 1; //Enable peripheral interrupts
PIE1bits.TMR1IE = 1; // enable interrupt on Timer1 overflows
INTCONbits.TMR0IE = 1; // enable interrupt on Timer0 overflow
INTCON2=0x00; /* Set Interrupt on falling Edge*/
INTCON3=0x00;
INTCONbits.INT0IF=0; /* Clear INT0IF flag*/
INTCONbits.INT0IE=1;
INTCON3bits.INT1IF=0;
INTCON3bits.INT1IE=1;
return;
}
//--------------------------------------------------