From 7af0a8a5b88b6873286056cf43eeefc1162a4c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 2 Aug 2014 14:43:44 -0700 Subject: [PATCH] implemented save_code() --- pic/MDR/MDR.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 109 insertions(+), 9 deletions(-) diff --git a/pic/MDR/MDR.c b/pic/MDR/MDR.c index 49d50f3..3c04e30 100644 --- a/pic/MDR/MDR.c +++ b/pic/MDR/MDR.c @@ -49,7 +49,7 @@ /* variables */ static uint8_t switches; /* save the last switch state */ #define START 208 /* starting timer 0 value to wait up to 12ms in start_timer*/ -static uint8_t code[3]; /* the received code (24 bits) */ +static uint8_t code[3] = {0xf1, 0x11, 0x11}; /* the received code (24 bits) */ /* configuration bits */ uint16_t __at(_CONFIG1) __CONFIG1 = _FCMEN_ON & /* enable fail-safe clock monitor */ @@ -151,6 +151,111 @@ void start_pulse_timer() TMR2ON = 1; /* start timer 2 */ } +/* write code (global variable) in EEPROM at specifoed address */ +void write_code(uint16_t address) +{ + /* use I2C to select address */ + send_start(); + if (send_byte(0xa0)) { /* write eeprom at 0xA0/0x50 */ + send_stop(); + return; + } + if (send_byte((uint8_t)(address>>8))) { /* go to address */ + send_stop(); + return; + } + if (send_byte((uint8_t)(address&0xff))) { /* go to address */ + send_stop(); + return; + } + /* write bytes */ + if (send_byte(code[0])) { + send_stop(); + return; + } + if (send_byte(code[1])) { + send_stop(); + return; + } + if (send_byte(code[2])) { + send_stop(); + return; + } + /* finish transaction */ + send_stop(); +} + +/* look in the memory for the code (global variable) + * if not present, write it a the next free memory space */ +void save_code() +{ + uint16_t address; + uint8_t stored[3]; + /* start at begining of the memory */ + send_start(); + if (send_byte(0xa0)) { /* write address to eeprom at 0xA0/0x50 */ + send_stop(); + return; + } + if (send_byte(0x00)) { /* go to address 0x0000 */ + send_stop(); + return; + } + if (send_byte(0x00)) { /* go to address 0x0000 */ + send_stop(); + return; + } + /* start reading */ + send_start(); + if (send_byte(0xa1)) { /* read eeprom at 0xA0/0x50 */ + send_stop(); + return; + } + /* go through memory */ + for (address=0; address<0x7FFF-2; address+=3) { + /* read stored code */ + stored[0] = read_byte(1); + stored[1] = read_byte(1); + stored[2] = read_byte(1); + if ((stored[0]&0x80)==0) { /* code always have the MSb to 1 */ + read_byte(0); /* send a NACK to stop reading */ + send_stop(); /* finish transaction */ + write_code(address); /* write code at this space */ + return; + } else if (stored[0]==code[0] && stored[1]==code[1] && stored[2]==code[2]) { /* code already stored */ + read_byte(0); /* send a NACK to stop reading */ + send_stop(); /* finish transaction */ + return; + } + } +} + +void clear_memory(void) +{ + uint16_t address; + uint8_t stored[3]; + /* start at begining of the memory */ + send_start(); + if (send_byte(0xa0)) { /* write address to eeprom at 0xA0/0x50 */ + send_stop(); + return; + } + if (send_byte(0x00)) { /* go to address 0x0000 */ + send_stop(); + return; + } + if (send_byte(0x00)) { /* go to address 0x0000 */ + send_stop(); + return; + } + /* go through memory */ + for (address=0; address<0x7FFF; address++) { + send_byte(0x00); /* clear byte */ + } + send_stop(); /* finish transaction */ + +} + /* funcion called on interrupts */ /* interrupt 0 is only one on PIC16 */ static void interrupt(void) __interrupt 0 @@ -161,14 +266,7 @@ static void interrupt(void) __interrupt 0 if (switches&SWITCH1) { /* switch 1 changed */ if (PORTB&SWITCH1) { /* switch 1 released */ led_off(); /* reset LED */ - send_start(); - send_byte(0xa0); - send_byte(0x1c); - send_byte(0x17); - send_start(); - send_byte(0xa1); - read_byte(); - send_stop(); + clear_memory(); } else { /* switch 1 pressed */ led_on(); /* test LED */ } @@ -232,6 +330,8 @@ void main (void) } if (bit==24) { /* received all 24 bits */ led_on(); + save_code(); + led_off(); } } if (bit==0) { /* sync pulse (can be the current one it it is not a continuation */