Copy Code To Arduino IDE
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#define BAUDRATE 9600
#define STOPBITS 1
#define CYCLES_PER_BIT ( (F_CPU) / (BAUDRATE) )
#if (CYCLES_PER_BIT > 255)
#define DIVISOR 8
#define CLOCKSELECT 2
#else
#define DIVISOR 1
#define CLOCKSELECT 1
#endif
#define FULL_BIT_TICKS ( (CYCLES_PER_BIT) / (DIVISOR) )
enum USISERIAL_SEND_STATE { AVAILABLE, FIRST, SECOND };
static volatile enum USISERIAL_SEND_STATE usiserial_send_state = AVAILABLE;
static inline enum USISERIAL_SEND_STATE usiserial_send_get_state(void)
{
return usiserial_send_state;
}
static inline void usiserial_send_set_state(enum USISERIAL_SEND_STATE state)
{
usiserial_send_state = state;
}
bool usiserial_send_available()
{
return usiserial_send_get_state() == AVAILABLE;
}
static volatile uint8_t usiserial_tx_data;
static inline uint8_t usiserial_get_tx_data(void)
{
return usiserial_tx_data;
}
static inline void usiserial_set_tx_data(uint8_t tx_data)
{
usiserial_tx_data = tx_data;
}
static uint8_t reverse_byte (uint8_t x) {
x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
return x;
}
void usiserial_send_byte(uint8_t data)
{
while (usiserial_send_get_state() != AVAILABLE)
{
};
usiserial_send_set_state(FIRST);
usiserial_set_tx_data(reverse_byte(data));
TCCR1 = 0;
TCCR1 |= (1 << CTC1);
TCCR1 |= (0 << CS10);
TCCR1 |= (0 << CS11);
TCCR1 |= (1 << CS12);
TCCR1 |= (0 << CS13);
GTCCR |= (1 << PSR1);
TIMSK |= (1 << OCIE1A);
OCR1C = FULL_BIT_TICKS;
TCNT1 = 0;
USIDR = 0x00 |
usiserial_get_tx_data() >> 1;
USICR = (1 << USIOIE) |
(0 << USIWM1) | (1 << USIWM0) |
(0 << USICS1) | (0 << USICS0) | (0 << USICLK);
DDRB |= (1 << PB1);
USISR = 1 << USIOIF |
(16 - 8);
}
ISR (TIM1_COMPA_vect)
{
USICR |= (1 << USICLK);
}
ISR (USI_OVF_vect) {
if (usiserial_send_get_state() == FIRST)
{
usiserial_send_set_state(SECOND);
USIDR = usiserial_get_tx_data() << 7
| 0x7F;
USISR = 1 << USIOIF |
(16 - (1 + (STOPBITS)));
}
else
{
PORTB |= 1 << PB1;
DDRB |= (1 << PB1);
USICR = 0;
USISR |= 1 << USIOIF;
TCCR1 = 0;
usiserial_send_set_state(AVAILABLE);
}
}
void setup() {
TCCR1 = 0;
mySwitch.enableReceive(0);
pinMode(1, HIGH);
digitalWrite(1, HIGH);
}
String test = "";
long temp = 0;
void loop() {
if (mySwitch.available()) {
test = "";
test += String(mySwitch.getReceivedValue());
test += "\n";
char text [test.length()];
test.toCharArray(text, test.length()) ;
uint8_t len = sizeof(text) - 1;
for (uint8_t i = 0; i < 8; i++)
{
while (!usiserial_send_available())
{
}
usiserial_send_byte(text[i]);
}
mySwitch.resetAvailable();
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.