| 329 |
Kevin |
1 |
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
|
|
|
2 |
// PIC16F1825 Configuration Bit Settings
|
|
|
3 |
|
|
|
4 |
// CONFIG1
|
|
|
5 |
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
|
|
|
6 |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled)
|
|
|
7 |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
|
|
|
8 |
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
|
|
|
9 |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
|
|
10 |
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
|
|
11 |
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
|
|
12 |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
|
|
13 |
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
|
|
|
14 |
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
|
|
|
15 |
|
|
|
16 |
// CONFIG2
|
|
|
17 |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
|
|
18 |
#pragma config PLLEN = OFF // PLL Disabled (4x PLL disabled)
|
|
|
19 |
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
|
|
|
20 |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
|
|
|
21 |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
|
|
|
22 |
// </editor-fold>
|
|
|
23 |
|
|
|
24 |
#include "defines.h"
|
|
|
25 |
#include "INTERRUPTS.h"
|
|
|
26 |
#include "I2C1.h"
|
|
|
27 |
#include "HT16K33.h"
|
|
|
28 |
#include "SMT6500.h"
|
|
|
29 |
#include "IOC.h"
|
|
|
30 |
//#include "UART.h"
|
|
|
31 |
|
|
|
32 |
void Pins_Init(void) {
|
|
|
33 |
// Set all pins to digital I/O
|
|
|
34 |
ANSELA = 0x0;
|
|
|
35 |
ANSELC = 0x0;
|
|
|
36 |
|
|
|
37 |
// // Enable weak pull-up if WPU bit is set
|
|
|
38 |
// OPTION_REGbits.nWPUEN = 0;
|
|
|
39 |
|
|
|
40 |
BLNK_LAT = 0;
|
|
|
41 |
BLNK_TRIS = 0;
|
|
|
42 |
|
|
|
43 |
INIT_LAT = 0;
|
|
|
44 |
INIT_TRIS = 0;
|
|
|
45 |
|
|
|
46 |
BINH_LAT = 0;
|
|
|
47 |
BINH_TRIS = 0;
|
|
|
48 |
|
|
|
49 |
OSC_TRIS = 1;
|
|
|
50 |
ECHO_TRIS = 1;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
int main(void) {
|
|
|
54 |
// Set internal oscillator speed to 16MHz
|
|
|
55 |
OSCCONbits.SPLLEN = 0; // 4x PLL disabled (overwritten by config bits)
|
|
|
56 |
OSCCONbits.IRCF = 0b1111; // Base frequency @ 4MHz
|
|
|
57 |
OSCCONbits.SCS = 0b00; // System clock determined by config bits
|
|
|
58 |
|
|
|
59 |
// Initialize I/O
|
|
|
60 |
Pins_Init();
|
|
|
61 |
SMT6500_Init();
|
|
|
62 |
|
|
|
63 |
// Initialize I2C
|
|
|
64 |
I2C1_DATA i2c_data;
|
|
|
65 |
I2C1_Init(&i2c_data);
|
|
|
66 |
I2C1_Configure_Master(I2C_400KHZ);
|
|
|
67 |
|
|
|
68 |
// UART_DATA uart_data;
|
|
|
69 |
// UART_Init(&uart_data);
|
|
|
70 |
|
|
|
71 |
IOC_DATA ioc_data;
|
|
|
72 |
IOC_Init(&ioc_data, SMT6500_Callback);
|
|
|
73 |
|
|
|
74 |
LED_DATA led_data;
|
|
|
75 |
LED_Init(&led_data);
|
|
|
76 |
|
|
|
77 |
// Initialize interrupts
|
|
|
78 |
Interrupt_Init();
|
|
|
79 |
Interrupt_Enable();
|
|
|
80 |
|
|
|
81 |
__delay_ms(100);
|
|
|
82 |
|
|
|
83 |
LED_Start();
|
|
|
84 |
|
|
|
85 |
while (1) {
|
|
|
86 |
uint16_t distance = SMT6500_Read() / 7;
|
|
|
87 |
LED_Write_Num(distance);
|
|
|
88 |
// __delay_ms(100);
|
|
|
89 |
|
|
|
90 |
}
|
|
|
91 |
}
|