From adef642b5b2d07e34fbabb1188d4779bbbc0758b Mon Sep 17 00:00:00 2001 From: sfokin Date: Mon, 18 Mar 2024 06:06:36 +0300 Subject: [PATCH] make function for ir command --- main.c | 287 +++++++++++++++----------- main.h | 9 +- nbproject/Makefile-genesis.properties | 4 +- nbproject/configurations.xml | 4 +- 4 files changed, 177 insertions(+), 127 deletions(-) diff --git a/main.c b/main.c index 911d13c..14a4629 100644 --- a/main.c +++ b/main.c @@ -1,121 +1,168 @@ -/* - * 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 -#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); - } +/* + * 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 +#include "common.h" +#include "main.h" +char STATE; +char btnState; +char addr[] = {0x20,0xDF}; +char vol_up[] = {0x40,0xBF}; +char vol_down[] = {0xC0,0x3F}; +char input_select[] = {0xD0,0x2F}; +char ok[] = {0x22,0xdd}; +char power[] = {0x10,0xEF}; +char ir_command[4]; + +void __interrupt() handle_interrupt(void) +{ + clear_bit(INTCON, 7); + + if(test_bit(INTCON, 0)) + { + handle_IOC(); + } + + if(test_bit(PIR1, 0)) + { + handle_TMR1(); + } + + 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 +} + +static inline void handle_TMR1() +{ + flip_bit(GPIO, 2); + TMR1 = 0; + clear_bit(PIR1, 0); +} + +static inline void handle_IOC() +{ + + handle_buttons(0); + handle_buttons(1); + handle_buttons(3); + handle_buttons(4); + handle_buttons(5); + if(test_bit(btnState, 5)) + { + ir_command[0] = addr[0]; + ir_command[1] = addr[1]; + ir_command[2] = vol_up[0]; + ir_command[3] = vol_up[1]; + + while(!GP5) + send_command(ir_command); + } + + clear_bit(INTCON, 0); +} + +static inline void sleep() +{ + //prepare and go to sleep + GP2 = 0; //Turn off led + SLEEP(); +} + +static inline void handle_buttons(char 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; + } } \ No newline at end of file diff --git a/main.h b/main.h index 56d272d..3a8ed22 100644 --- a/main.h +++ b/main.h @@ -1,5 +1,8 @@ static inline void sleep(); static inline void init(); -static inline void handleButtons(char bit); -static inline void handleIOC(); -static inline void handleTMR1(); \ No newline at end of file +static inline void handle_buttons(char bit); +static inline void handle_IOC(); +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); \ No newline at end of file diff --git a/nbproject/Makefile-genesis.properties b/nbproject/Makefile-genesis.properties index 30d3abd..f6ca65d 100644 --- a/nbproject/Makefile-genesis.properties +++ b/nbproject/Makefile-genesis.properties @@ -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.Pack.dfplocation=C\:\\Program Files\\Microchip\\MPLABX\\v6.15\\packs\\Microchip\\PIC10-12Fxxx_DFP\\1.6.174 conf.ids=default default.languagetoolchain.dir=C\:\\Program Files\\Microchip\\xc8\\v2.45\\bin host.id=1jna-tnj5-7f -configurations-xml=07975d7e7282e550ac8decaaea8a2cd7 +configurations-xml=215034e96a2f762c825b7c03119c12d4 default.com-microchip-mplab-mdbcore-PICKit3Tool-PICkit3DbgToolManager.md5=50072f33d27b72924000ca2dca4b7622 com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=e62346c0c0ecee2637e613b49cb7b7fa default.com-microchip-mplab-nbide-toolchain-xc8-XC8LanguageToolchain.md5=486b02ed36e5bdffb1f797e38fa058e5 diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index b09e598..d5da495 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -183,7 +183,7 @@ - + @@ -230,7 +230,7 @@ - +