libpic170x  0.2
Ease of use library for PIC16(L)1705/1709 chips
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.
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
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.
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 }
GPIO input/output library allowing to write to and read from IO pins.
int main()
Main entrypoint.
Definition: main.c:76
const PinDef * PIN_RC0
PIN_RC0 defintion.
#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
void pin_set_output(const PinDef *def, bool on)
Definition: io_control.c:129
Timer0 pic170x_timer0
Definition: timer0.c:22
void timer0_ih(Timer0 *timer0)
Definition: timer0.c:40
static bool libpic170x_check_library_build_arguments()
Verify matching configuration parameters between main project and static library. ...
Definition: freq.h:167
void timer0_init(Timer0 *timer0)
Definition: timer0.c:24
void pin_set_pin_mode(const PinDef *def, bool output)
Definition: io_control.c:106
bool pin_get_input(const PinDef *def)
Definition: io_control.c:118
void interrupt int_handler()
Interrupt handler for updating timer0.
Definition: main.c:66
Coarse timing library for measuring times in the background.
uint32_t ms
Definition: timer0.h:52
Global timing definitions used for timing critical library components.