121 lines
3.0 KiB
C
121 lines
3.0 KiB
C
/*
|
|
* File: main.c
|
|
* Author: sfokin
|
|
*
|
|
* Created on March 17, 2024, 6:05 AM
|
|
*/
|
|
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
|
|
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
|
|
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
|
|
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
|
|
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
|
|
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
|
|
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
|
|
#define _XTAL_FREQ 4000000
|
|
#define INPUT_PINS 0b00111011 // Pins: 0,1,3,4,5
|
|
|
|
#include <xc.h>
|
|
#include "common.h"
|
|
#include "main.h"
|
|
char STATE;
|
|
char btnState;
|
|
|
|
void __interrupt() handleInterrupt(void)
|
|
{
|
|
clear_bit(INTCON, 7);
|
|
|
|
if(test_bit(INTCON, 0))
|
|
{
|
|
handleIOC();
|
|
}
|
|
|
|
if(test_bit(PIR1, 0))
|
|
{
|
|
handleTMR1();
|
|
}
|
|
|
|
set_bit(INTCON, 7);
|
|
return;
|
|
}
|
|
|
|
void main(void)
|
|
{
|
|
init();
|
|
|
|
while(1)
|
|
{
|
|
sleep();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
static inline void init()
|
|
{
|
|
//Configure GPIO
|
|
GPIO = 0x0; //Clear GPIO port
|
|
TRISIO = 0; //Clear TRISIO register
|
|
ANSEL = 0; //Set all pins to digital IO
|
|
TRISIO = INPUT_PINS; //Set 0,1,3,4,5 pins as input
|
|
INTCONbits.GIE = 1; //Allow Global interrupts;
|
|
INTCONbits.GPIE = 1; //Allow interrupt on pin change
|
|
clear_bit(OPTION_REG, 7); //Global weak pull-up enable
|
|
WPU = INPUT_PINS; //Weak pull-up for 0,1,3,4,5 pins
|
|
IOC = INPUT_PINS; //IOC enable for 0,1,3,4,5 pins
|
|
|
|
//Configure Timer 1
|
|
TMR1 = 0; //Clear timer 1 value
|
|
T1CONbits.TMR1ON = 0; //Turn timer 1 Off
|
|
T1CONbits.T1CKPS0 = 1; //Set prescale 1:8
|
|
T1CONbits.T1CKPS1 = 1;
|
|
PIE1bits.TMR1IE = 1; //Enable timer 1 overflow interrupt
|
|
INTCONbits.PEIE = 1; //Enable peripheral interrupt
|
|
|
|
//Set default state
|
|
STATE = 0;
|
|
}
|
|
|
|
static inline void handleTMR1()
|
|
{
|
|
flip_bit(GPIO, 2);
|
|
TMR1 = 0;
|
|
clear_bit(PIR1, 0);
|
|
}
|
|
|
|
static inline void handleIOC()
|
|
{
|
|
|
|
handleButtons(0);
|
|
handleButtons(1);
|
|
handleButtons(3);
|
|
handleButtons(4);
|
|
handleButtons(5);
|
|
if(test_bit(btnState, 5))
|
|
{
|
|
STATE = STATE == 0 ? 1 : 0;
|
|
TMR1 = 0;
|
|
clear_bit(PIR1, 0);
|
|
}
|
|
|
|
clear_bit(INTCON, 0);
|
|
}
|
|
|
|
static inline void sleep()
|
|
{
|
|
//prepare and go to sleep
|
|
clear_bit(GPIO, 2); //Turn off led
|
|
SLEEP();
|
|
}
|
|
|
|
static inline void handleButtons(char bit)
|
|
{
|
|
clear_bit(btnState, bit);
|
|
if(!test_bit(GPIO, bit))
|
|
{
|
|
__delay_ms(10);
|
|
}
|
|
if(!test_bit(GPIO, bit))
|
|
{
|
|
set_bit(btnState, bit);
|
|
}
|
|
} |