code to transmit is now stored in EEPROM, for faster customization
This commit is contained in:
parent
6f269babff
commit
280211a76d
|
@ -1,10 +1,24 @@
|
|||
/* micro-controller firmware fot the Monarch 318LIPW1K(-L) remote
|
||||
*/
|
||||
Copyright (C) 2014 Kévin Redon <kingkevin@cuvoodoo.info>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
/* libraries */
|
||||
#define __12f629
|
||||
#include <pic12f629.h>
|
||||
#include <stdint.h>
|
||||
#define sleep() __asm sleep __endasm
|
||||
|
||||
/* the peripherals connected to the pins */
|
||||
#define LED_ANODE _GP0
|
||||
|
@ -12,16 +26,18 @@
|
|||
#define SWITCH _GP4
|
||||
#define LED_CATHODE _GP5
|
||||
|
||||
#define CODE 0xc917c2 // the code to transmit. least significant bit will be sent first, and must be 1 (sync bit)
|
||||
volatile int8_t transmit = 0; // transmitting (0: do not transmit, 1: transmit, -1: finish transmiting)
|
||||
volatile uint8_t phase = 0; // the 4 phases of the bit (2ms pause, 1ms pulse, 2ms pause, 1ms pulse. only transmit during one of the two pulse periode depending on the bit value)
|
||||
|
||||
/* simple functions */
|
||||
// NOTE LED and TX can not be enabled at the same time
|
||||
// note: LED and TX can not be enabled at the same time
|
||||
#define led_on() GPIO |= LED_ANODE;
|
||||
#define led_off() GPIO &= ~LED_ANODE;
|
||||
#define tx_on() GPIO |= TX;
|
||||
#define tx_off() GPIO &= ~TX;
|
||||
#define sleep() __asm sleep __endasm
|
||||
|
||||
/* variables */
|
||||
//volatile uint32_t code = 0; // the code to transmit (read from EEPROM)
|
||||
volatile int8_t transmit = 0; // transmitting (0: do not transmit, 1: transmit, -1: finish transmiting)
|
||||
volatile uint8_t phase = 0; // the 4 phases of the bit (2ms pause, 1ms pulse, 2ms pause, 1ms pulse. only transmit during one of the two pulse periode depending on the bit value)
|
||||
|
||||
/* configuration bits
|
||||
* all set (to 1) per default
|
||||
|
@ -35,17 +51,15 @@ uint16_t __at(_CONFIG) __CONFIG = _CPD_OFF & // no data code protect
|
|||
_WDTE_OFF & // no watchdog
|
||||
_INTRC_OSC_NOCLKOUT; // use internal oscilator and both I/O pins
|
||||
|
||||
/* default code to transmit in EEPROM
|
||||
a megacode is 3 bytes long (MSB of byte 1 is 1)
|
||||
append 0x00 to indicate end
|
||||
*/
|
||||
// 0x2100 comes from the PIC12F629 Memory Programming document
|
||||
__code uint8_t __at(0x2100) EEPROM[] = {0xc9, 0x17, 0xc2, 0x00};
|
||||
|
||||
/* set timer in ms */
|
||||
#define TICKS_PER_MS 145UL // the number of timer 1 ticks to wait for 1ms, using the internal 4MHz clock and a prescaler of 8, and hand tuning
|
||||
// 145 is a good value
|
||||
/*
|
||||
void timer (uint8_t ms) {
|
||||
TMR1ON = 0; // disable timer 1 (to write value safely)
|
||||
TMR1L = (0xffff-ms*TICKS_PER_MS); // set time
|
||||
TMR1H = (0xffff-ms*TICKS_PER_MS)>>8; // set time
|
||||
TMR1ON = 1; // start timer 1
|
||||
}
|
||||
*/
|
||||
|
||||
void timer_1ms() {
|
||||
TMR1ON = 0; // disable timer 1 (to write value safely)
|
||||
|
@ -61,13 +75,24 @@ void timer_2ms() {
|
|||
TMR1ON = 1; // start timer 1
|
||||
}
|
||||
|
||||
/* read EEPROM data */
|
||||
uint8_t read_eeprom(uint8_t address) {
|
||||
EEADR = address;
|
||||
RD = 1;
|
||||
return EEDATA;
|
||||
}
|
||||
|
||||
/* transmit the megacode */
|
||||
void megacode (void) {
|
||||
static uint8_t byte;
|
||||
uint8_t bit = phase/4;
|
||||
if (transmit != 0) {
|
||||
if (bit%8==0) { // read byte to transmit
|
||||
byte = read_eeprom(bit/8);
|
||||
}
|
||||
if (bit<24) { // transmit bit
|
||||
if (phase%2) {
|
||||
uint8_t pulse = (CODE>>(23-bit))&0x01;
|
||||
uint8_t pulse = (byte>>((23-bit)%8))&0x01;
|
||||
if ((phase%4==1 && !pulse) || (phase%4==3 && pulse)) {
|
||||
led_off();
|
||||
tx_on();
|
||||
|
@ -112,10 +137,12 @@ static void interrupt(void) __interrupt 0
|
|||
if (transmit == 1) {
|
||||
transmit = -1; // stop transmitting after last transmition
|
||||
}
|
||||
} else if (transmit == 0) { // switch is pressed, start transmission
|
||||
transmit = 1;
|
||||
phase = 0;
|
||||
megacode(); // start sending megacode
|
||||
} else { // switch is pressed, start transmission
|
||||
if (transmit == 0) { // only transmit if code is loaded
|
||||
transmit = 1;
|
||||
phase = 0;
|
||||
megacode(); // start sending megacode
|
||||
}
|
||||
}
|
||||
GPIF = 0; // clear interrupt
|
||||
}
|
||||
|
@ -142,7 +169,11 @@ void main (void)
|
|||
PIE1 = 1; // enable timer 1 interrupt
|
||||
PEIE = 1; // enable timer interrupt
|
||||
|
||||
GIE = 1; // globally enable interrupts
|
||||
if (read_eeprom(0)&0x80) { // verify if the code is programm (the MSB bit is always 1 for megacode codes)
|
||||
GIE = 1; // globally enable interrupts
|
||||
} else { // show error and got to sleep forever
|
||||
led_on();
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (transmit == 0) {
|
||||
|
|
|
@ -1,22 +1,42 @@
|
|||
# variables
|
||||
TARGET = 318LPW1K-L
|
||||
EEPROM = eeprom
|
||||
PIC = 12f629
|
||||
# software verion used:
|
||||
# pk2cmd: 1.21
|
||||
# sdcc: 3.4.0
|
||||
# gputils: 1.3.0
|
||||
|
||||
all: off flash on
|
||||
|
||||
# flash program
|
||||
flash: $(TARGET).hex
|
||||
pk2cmd -PPIC$(PIC) -F$< -M
|
||||
|
||||
# power on
|
||||
# flash custom megacode in EEPROM
|
||||
eeprom: $(EEPROM).hex
|
||||
pk2cmd -PPIC$(PIC) -F$< -ME
|
||||
|
||||
# power on remote
|
||||
on:
|
||||
pk2cmd -PPIC$(PIC) -T
|
||||
|
||||
# power off
|
||||
# power off remote
|
||||
off:
|
||||
pk2cmd -PPIC$(PIC) -V0
|
||||
|
||||
# compile source code
|
||||
%.hex: %.c
|
||||
# compile program and EEPROm
|
||||
compile: $(TARGET).hex $(EEPROM).hex
|
||||
|
||||
# compile program C source code (includes a default code)
|
||||
$(TARGET).hex: $(TARGET).c
|
||||
sdcc --std-c99 --opt-code-size --use-non-free -mpic14 -p$(PIC) $<
|
||||
|
||||
# compile eeprom for custom megacode
|
||||
$(EEPROM).hex: $(EEPROM).asm
|
||||
gpasm -o $(EEPROM).o -c $<
|
||||
gplink -w -r -o $(EEPROM) $(EEPROM).o
|
||||
|
||||
# remove temporary files
|
||||
clean:
|
||||
rm -f $(TARGET).hex $(TARGET).lst $(TARGET).asm $(TARGET).adb $(TARGET).o $(TARGET).cod
|
||||
rm -f $(TARGET).hex $(TARGET).lst $(TARGET).asm $(TARGET).adb $(TARGET).o $(TARGET).cod $(EEPROM).hex $(EEPROM).cod $(EEPROM).lst $(EEPROM).o
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
; define microcontroller
|
||||
list p=12f629
|
||||
; set custom megacode in EEPROM
|
||||
; megacode is 3 bytes long
|
||||
; end with 0x00 to indicate the end
|
||||
org 2100h ; EEPROM memory address
|
||||
DE 0xc9, 0x17, 0xc2, 0x00 ; megacode
|
||||
end
|
Loading…
Reference in New Issue