libpic170x
0.2
Ease of use library for PIC16(L)1705/1709 chips
examples
blink.X
main.c
Go to the documentation of this file.
1
/*
2
Copyright 2018 Paul Konstantin Gerke
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
23
// PIC16LF1705 Configuration Bit Settings
24
25
// 'C' source line config statements
26
27
// CONFIG1
28
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
29
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
30
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
31
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
32
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
33
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
34
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
35
#pragma config IESO = ON // Internal/External Switchover Mode (Internal/External Switchover Mode is enabled)
36
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
37
38
// CONFIG2
39
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
40
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
41
#pragma config ZCDDIS = ON // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR)
42
#pragma config PLLEN = ON // Phase Lock Loop enable (4x PLL is always enabled)
43
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
44
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
45
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
46
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
47
48
49
#include <stdbool.h>
50
51
#include <xc.h>
52
54
#define _XTAL_FREQ 8000000
55
56
// Include all relevant libpic170x libraries
57
#include <
libpic170x/freq.h
>
58
#include <
libpic170x/timer0.h
>
59
#include <
libpic170x/io_control.h
>
60
66
void
interrupt
int_handler
() {
67
timer0_ih
(NULL);
68
}
69
76
int
main
() {
77
// First check the library configuration... if the library configuration
78
// is faulty, exit into an infinite loop to before doing anything else.
79
if
(!
libpic170x_check_library_build_arguments
()) {
80
while
(
true
) {}
// The safe fail-state... NOP forever
81
}
82
83
// Set OSCCON bits from macro that is defined in freq.h
84
85
OSCCON =
OSCCON_BITS
;
86
87
// Intilaize timer0 and enable interrupts
88
timer0_init
(NULL);
89
GIE = 1;
90
91
// Configure RC0 as output
92
pin_set_pin_mode
(
PIN_RC0
,
true
);
93
pin_set_output
(
PIN_RC0
,
false
);
94
95
while
(1) {
96
// Check if 1 scond has expired. Disable intterupts during the
97
// check-and-modify operation.
98
GIE = 0;
99
if
(
pic170x_timer0
.
ms
> 1000) {
100
// If 1 second has expired, toggle the output signal on/off.
101
pin_set_output
(
PIN_RC0
, !
pin_get_input
(
PIN_RC0
));
102
pic170x_timer0
.
ms
-= 1000;
103
}
104
GIE = 1;
105
106
// .. here we can do some other things - timing will be done in
107
// the background
108
}
109
}
io_control.h
GPIO input/output library allowing to write to and read from IO pins.
main
int main()
Main entrypoint.
Definition:
main.c:76
PIN_RC0
const PinDef * PIN_RC0
PIN_RC0 defintion.
OSCCON_BITS
#define OSCCON_BITS
OSCCON bits that can derived from the set _XTAL_FREQ that the OSCCON register can be initailized with...
Definition:
freq.h:80
pin_set_output
void pin_set_output(const PinDef *def, bool on)
Definition:
io_control.c:129
pic170x_timer0
Timer0 pic170x_timer0
Definition:
timer0.c:22
timer0_ih
void timer0_ih(Timer0 *timer0)
Definition:
timer0.c:40
libpic170x_check_library_build_arguments
static bool libpic170x_check_library_build_arguments()
Verify matching configuration parameters between main project and static library. ...
Definition:
freq.h:167
timer0_init
void timer0_init(Timer0 *timer0)
Definition:
timer0.c:24
pin_set_pin_mode
void pin_set_pin_mode(const PinDef *def, bool output)
Definition:
io_control.c:106
pin_get_input
bool pin_get_input(const PinDef *def)
Definition:
io_control.c:118
int_handler
void interrupt int_handler()
Interrupt handler for updating timer0.
Definition:
main.c:66
timer0.h
Coarse timing library for measuring times in the background.
Timer0::ms
uint32_t ms
Definition:
timer0.h:52
freq.h
Global timing definitions used for timing critical library components.
Generated by
1.8.13