make function for ir command
This commit is contained in:
parent
f52cb857ab
commit
adef642b5b
287
main.c
287
main.c
|
|
@ -1,121 +1,168 @@
|
||||||
/*
|
/*
|
||||||
* File: main.c
|
* File: main.c
|
||||||
* Author: sfokin
|
* Author: sfokin
|
||||||
*
|
*
|
||||||
* Created on March 17, 2024, 6:05 AM
|
* 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 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 WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
|
||||||
#pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT 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 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 BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
|
||||||
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is 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)
|
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
|
||||||
#define _XTAL_FREQ 4000000
|
#define _XTAL_FREQ 4000000
|
||||||
#define INPUT_PINS 0b00111011 // Pins: 0,1,3,4,5
|
#define INPUT_PINS 0b00111011 // Pins: 0,1,3,4,5
|
||||||
|
|
||||||
#include <xc.h>
|
#include <xc.h>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
char STATE;
|
char STATE;
|
||||||
char btnState;
|
char btnState;
|
||||||
|
char addr[] = {0x20,0xDF};
|
||||||
void __interrupt() handleInterrupt(void)
|
char vol_up[] = {0x40,0xBF};
|
||||||
{
|
char vol_down[] = {0xC0,0x3F};
|
||||||
clear_bit(INTCON, 7);
|
char input_select[] = {0xD0,0x2F};
|
||||||
|
char ok[] = {0x22,0xdd};
|
||||||
if(test_bit(INTCON, 0))
|
char power[] = {0x10,0xEF};
|
||||||
{
|
char ir_command[4];
|
||||||
handleIOC();
|
|
||||||
}
|
void __interrupt() handle_interrupt(void)
|
||||||
|
{
|
||||||
if(test_bit(PIR1, 0))
|
clear_bit(INTCON, 7);
|
||||||
{
|
|
||||||
handleTMR1();
|
if(test_bit(INTCON, 0))
|
||||||
}
|
{
|
||||||
|
handle_IOC();
|
||||||
set_bit(INTCON, 7);
|
}
|
||||||
return;
|
|
||||||
}
|
if(test_bit(PIR1, 0))
|
||||||
|
{
|
||||||
void main(void)
|
handle_TMR1();
|
||||||
{
|
}
|
||||||
init();
|
|
||||||
|
set_bit(INTCON, 7);
|
||||||
while(1)
|
return;
|
||||||
{
|
}
|
||||||
sleep();
|
|
||||||
}
|
void main(void)
|
||||||
|
{
|
||||||
return;
|
init();
|
||||||
}
|
|
||||||
|
while(1)
|
||||||
static inline void init()
|
{
|
||||||
{
|
sleep();
|
||||||
//Configure GPIO
|
}
|
||||||
GPIO = 0x0; //Clear GPIO port
|
|
||||||
TRISIO = 0; //Clear TRISIO register
|
return;
|
||||||
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;
|
static inline void init()
|
||||||
INTCONbits.GPIE = 1; //Allow interrupt on pin change
|
{
|
||||||
clear_bit(OPTION_REG, 7); //Global weak pull-up enable
|
//Configure GPIO
|
||||||
WPU = INPUT_PINS; //Weak pull-up for 0,1,3,4,5 pins
|
GPIO = 0x0; //Clear GPIO port
|
||||||
IOC = INPUT_PINS; //IOC enable for 0,1,3,4,5 pins
|
TRISIO = 0; //Clear TRISIO register
|
||||||
|
ANSEL = 0; //Set all pins to digital IO
|
||||||
//Configure Timer 1
|
TRISIO = INPUT_PINS; //Set 0,1,3,4,5 pins as input
|
||||||
TMR1 = 0; //Clear timer 1 value
|
INTCONbits.GIE = 1; //Allow Global interrupts;
|
||||||
T1CONbits.TMR1ON = 0; //Turn timer 1 Off
|
INTCONbits.GPIE = 1; //Allow interrupt on pin change
|
||||||
T1CONbits.T1CKPS0 = 1; //Set prescale 1:8
|
clear_bit(OPTION_REG, 7); //Global weak pull-up enable
|
||||||
T1CONbits.T1CKPS1 = 1;
|
WPU = INPUT_PINS; //Weak pull-up for 0,1,3,4,5 pins
|
||||||
PIE1bits.TMR1IE = 1; //Enable timer 1 overflow interrupt
|
IOC = INPUT_PINS; //IOC enable for 0,1,3,4,5 pins
|
||||||
INTCONbits.PEIE = 1; //Enable peripheral interrupt
|
|
||||||
|
//Configure Timer 1
|
||||||
//Set default state
|
//TMR1 = 0; //Clear timer 1 value
|
||||||
STATE = 0;
|
//T1CONbits.TMR1ON = 0; //Turn timer 1 Off
|
||||||
}
|
//T1CONbits.T1CKPS0 = 1; //Set prescale 1:8
|
||||||
|
//T1CONbits.T1CKPS1 = 1;
|
||||||
static inline void handleTMR1()
|
//PIE1bits.TMR1IE = 1; //Enable timer 1 overflow interrupt
|
||||||
{
|
//INTCONbits.PEIE = 1; //Enable peripheral interrupt
|
||||||
flip_bit(GPIO, 2);
|
}
|
||||||
TMR1 = 0;
|
|
||||||
clear_bit(PIR1, 0);
|
static inline void handle_TMR1()
|
||||||
}
|
{
|
||||||
|
flip_bit(GPIO, 2);
|
||||||
static inline void handleIOC()
|
TMR1 = 0;
|
||||||
{
|
clear_bit(PIR1, 0);
|
||||||
|
}
|
||||||
handleButtons(0);
|
|
||||||
handleButtons(1);
|
static inline void handle_IOC()
|
||||||
handleButtons(3);
|
{
|
||||||
handleButtons(4);
|
|
||||||
handleButtons(5);
|
handle_buttons(0);
|
||||||
if(test_bit(btnState, 5))
|
handle_buttons(1);
|
||||||
{
|
handle_buttons(3);
|
||||||
STATE = STATE == 0 ? 1 : 0;
|
handle_buttons(4);
|
||||||
TMR1 = 0;
|
handle_buttons(5);
|
||||||
clear_bit(PIR1, 0);
|
if(test_bit(btnState, 5))
|
||||||
}
|
{
|
||||||
|
ir_command[0] = addr[0];
|
||||||
clear_bit(INTCON, 0);
|
ir_command[1] = addr[1];
|
||||||
}
|
ir_command[2] = vol_up[0];
|
||||||
|
ir_command[3] = vol_up[1];
|
||||||
static inline void sleep()
|
|
||||||
{
|
while(!GP5)
|
||||||
//prepare and go to sleep
|
send_command(ir_command);
|
||||||
clear_bit(GPIO, 2); //Turn off led
|
}
|
||||||
SLEEP();
|
|
||||||
}
|
clear_bit(INTCON, 0);
|
||||||
|
}
|
||||||
static inline void handleButtons(char bit)
|
|
||||||
{
|
static inline void sleep()
|
||||||
clear_bit(btnState, bit);
|
{
|
||||||
if(!test_bit(GPIO, bit))
|
//prepare and go to sleep
|
||||||
{
|
GP2 = 0; //Turn off led
|
||||||
__delay_ms(10);
|
SLEEP();
|
||||||
}
|
}
|
||||||
if(!test_bit(GPIO, bit))
|
|
||||||
{
|
static inline void handle_buttons(char bit)
|
||||||
set_bit(btnState, bit);
|
{
|
||||||
}
|
clear_bit(btnState, bit);
|
||||||
|
if(!test_bit(GPIO, bit))
|
||||||
|
{
|
||||||
|
__delay_ms(10);
|
||||||
|
}
|
||||||
|
if(!test_bit(GPIO, bit))
|
||||||
|
{
|
||||||
|
set_bit(btnState, bit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static inline void send_command(char *command)
|
||||||
|
{
|
||||||
|
send(183);
|
||||||
|
__delay_us(4400);
|
||||||
|
GP2 = 0;
|
||||||
|
send_byte(command[0]);
|
||||||
|
send_byte(command[1]);
|
||||||
|
send_byte(command[2]);
|
||||||
|
send_byte(command[3]);
|
||||||
|
send(9);
|
||||||
|
__delay_ms(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void send_byte(char byte)
|
||||||
|
{
|
||||||
|
char mask = 0b10000000;
|
||||||
|
for(char k=0; k<8; k++)
|
||||||
|
{
|
||||||
|
send(13);
|
||||||
|
if((byte & (mask >> k)) == 0)
|
||||||
|
{
|
||||||
|
__delay_us(400);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
__delay_us(1500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void send(int x)
|
||||||
|
{
|
||||||
|
for(int i = 0; i < x; i++)
|
||||||
|
{
|
||||||
|
GP2=1;
|
||||||
|
__delay_us(26);
|
||||||
|
GP2=0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
9
main.h
9
main.h
|
|
@ -1,5 +1,8 @@
|
||||||
static inline void sleep();
|
static inline void sleep();
|
||||||
static inline void init();
|
static inline void init();
|
||||||
static inline void handleButtons(char bit);
|
static inline void handle_buttons(char bit);
|
||||||
static inline void handleIOC();
|
static inline void handle_IOC();
|
||||||
static inline void handleTMR1();
|
static inline void handle_TMR1();
|
||||||
|
static inline void send_command(char *command);
|
||||||
|
static inline void send_byte(char byte);
|
||||||
|
static inline void send(int x);
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
#
|
#
|
||||||
#Sun Mar 17 17:44:34 MSK 2024
|
#Mon Mar 18 06:01:25 MSK 2024
|
||||||
default.languagetoolchain.version=2.45
|
default.languagetoolchain.version=2.45
|
||||||
default.Pack.dfplocation=C\:\\Program Files\\Microchip\\MPLABX\\v6.15\\packs\\Microchip\\PIC10-12Fxxx_DFP\\1.6.174
|
default.Pack.dfplocation=C\:\\Program Files\\Microchip\\MPLABX\\v6.15\\packs\\Microchip\\PIC10-12Fxxx_DFP\\1.6.174
|
||||||
conf.ids=default
|
conf.ids=default
|
||||||
default.languagetoolchain.dir=C\:\\Program Files\\Microchip\\xc8\\v2.45\\bin
|
default.languagetoolchain.dir=C\:\\Program Files\\Microchip\\xc8\\v2.45\\bin
|
||||||
host.id=1jna-tnj5-7f
|
host.id=1jna-tnj5-7f
|
||||||
configurations-xml=07975d7e7282e550ac8decaaea8a2cd7
|
configurations-xml=215034e96a2f762c825b7c03119c12d4
|
||||||
default.com-microchip-mplab-mdbcore-PICKit3Tool-PICkit3DbgToolManager.md5=50072f33d27b72924000ca2dca4b7622
|
default.com-microchip-mplab-mdbcore-PICKit3Tool-PICkit3DbgToolManager.md5=50072f33d27b72924000ca2dca4b7622
|
||||||
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=e62346c0c0ecee2637e613b49cb7b7fa
|
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=e62346c0c0ecee2637e613b49cb7b7fa
|
||||||
default.com-microchip-mplab-nbide-toolchain-xc8-XC8LanguageToolchain.md5=486b02ed36e5bdffb1f797e38fa058e5
|
default.com-microchip-mplab-nbide-toolchain-xc8-XC8LanguageToolchain.md5=486b02ed36e5bdffb1f797e38fa058e5
|
||||||
|
|
|
||||||
|
|
@ -183,7 +183,7 @@
|
||||||
<property key="programoptions.preservedataflash" value="false"/>
|
<property key="programoptions.preservedataflash" value="false"/>
|
||||||
<property key="programoptions.preservedataflash.ranges"
|
<property key="programoptions.preservedataflash.ranges"
|
||||||
value="${programoptions.preservedataflash.ranges}"/>
|
value="${programoptions.preservedataflash.ranges}"/>
|
||||||
<property key="programoptions.preserveeeprom" value="true"/>
|
<property key="programoptions.preserveeeprom" value="false"/>
|
||||||
<property key="programoptions.preserveeeprom.ranges" value="0-7f"/>
|
<property key="programoptions.preserveeeprom.ranges" value="0-7f"/>
|
||||||
<property key="programoptions.preserveprogram.ranges" value=""/>
|
<property key="programoptions.preserveprogram.ranges" value=""/>
|
||||||
<property key="programoptions.preserveprogramrange" value="false"/>
|
<property key="programoptions.preserveprogramrange" value="false"/>
|
||||||
|
|
@ -230,7 +230,7 @@
|
||||||
<property key="programoptions.preservedataflash" value="false"/>
|
<property key="programoptions.preservedataflash" value="false"/>
|
||||||
<property key="programoptions.preservedataflash.ranges"
|
<property key="programoptions.preservedataflash.ranges"
|
||||||
value="${programoptions.preservedataflash.ranges}"/>
|
value="${programoptions.preservedataflash.ranges}"/>
|
||||||
<property key="programoptions.preserveeeprom" value="true"/>
|
<property key="programoptions.preserveeeprom" value="false"/>
|
||||||
<property key="programoptions.preserveeeprom.ranges" value="0-7f"/>
|
<property key="programoptions.preserveeeprom.ranges" value="0-7f"/>
|
||||||
<property key="programoptions.preserveprogram.ranges" value=""/>
|
<property key="programoptions.preserveprogram.ranges" value=""/>
|
||||||
<property key="programoptions.preserveprogramrange" value="false"/>
|
<property key="programoptions.preserveprogramrange" value="false"/>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user