Add attempt to implement RC-5 sending

This commit is contained in:
Thammi 2013-08-31 14:17:24 +02:00
parent a25638e945
commit 73132a39d4
5 changed files with 107 additions and 0 deletions

30
firmware/apps/rctest.c Normal file
View File

@ -0,0 +1,30 @@
#include <pentabug/app.h>
#include <pentabug/hal.h>
#include <pentabug/rc5.h>
static void run(void) {
if(button_clicked(LEFT)) {
led_on(RIGHT);
struct rc5_data data = { 0, 12 };
uint8_t i;
for(i = 0; i < 32; ++i) {
/*led_inv(LEFT);*/
data.addr = i;
uint8_t n;
for(n = 0; n < 128; ++n) {
data.code = n;
rc5_send(&data);
wait_ms(120);
}
}
led_off(RIGHT);
}
}
REG(run);

View File

@ -9,6 +9,8 @@
#define OFF 0 #define OFF 0
#define ON 1 #define ON 1
#define TICKS_PER_MS 38
// INITIALIZATION // INITIALIZATION
// initializes the hardware (timers, ...) // initializes the hardware (timers, ...)
@ -68,4 +70,7 @@ uint8_t ir_recv(void);
// WARNING: the time is actually measured in pentamilliseconds which are similar but not identical to regular milliseconds // WARNING: the time is actually measured in pentamilliseconds which are similar but not identical to regular milliseconds
void wait_ms(uint16_t ms); void wait_ms(uint16_t ms);
// wait the given amount of ticks, see TICKS_PER_MS
void wait_ticks(int16_t ticks);
#endif /* HAL_H */ #endif /* HAL_H */

View File

@ -0,0 +1,13 @@
#ifndef RC5_H
#define RC5_H
#include <stdint.h>
struct rc5_data {
uint8_t addr;
uint8_t code;
};
void rc5_send(struct rc5_data *data);
#endif /* RC5_H */

View File

@ -225,6 +225,14 @@ void wait_ms(uint16_t ms) {
} }
} }
void wait_ticks(int16_t ticks) {
wait_time = ticks;
while(wait_time > 0) {
test_stop_app();
}
}
void ir_on(void) { void ir_on(void) {
ir_active = 1; ir_active = 1;
} }

51
firmware/lib/rc5.c Normal file
View File

@ -0,0 +1,51 @@
#include <pentabug/rc5.h>
#include <pentabug/hal.h>
#define HALF_BIT_TICKS 34
static uint8_t toggle = 0;
static void send_bit(uint8_t bit) {
ir_set(!bit);
wait_ticks(HALF_BIT_TICKS);
ir_set(bit);
wait_ticks(HALF_BIT_TICKS);
}
static void send_byte_part(uint8_t byte, uint8_t length) {
int8_t i;
for(i = length - 1; i >= 0; --i) {
send_bit(byte & (1 << i));
}
}
void rc5_send(struct rc5_data *data) {
// start bit
send_bit(1);
// field bit
send_bit(!(data->code & (1 << 6)));
// control bit
send_bit(toggle);
toggle = !toggle;
// address
send_byte_part(data->addr, 5);
// command
send_byte_part(data->code, 6);
// done
ir_off();
}