78 lines
1.4 KiB
C++
78 lines
1.4 KiB
C++
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <util/delay.h>
|
|
|
|
//32 bit code
|
|
int code = 0b11111111100000000111111011100001;
|
|
|
|
//length of each bit
|
|
#define BIT_LENGTH 10
|
|
|
|
//Generates the carrier requency for the supplied timeframe
|
|
void carrier(int BlastTime) {
|
|
for(int i=0; i<(BlastTime / 2); i++)
|
|
{
|
|
PORTB |= (1 << 0x04);
|
|
_delay_ms(BIT_LENGTH);
|
|
PORTB &= ~(1 << 0x04);
|
|
_delay_ms(BIT_LENGTH);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// send '1' bit
|
|
int sendShort(void){
|
|
carrier(BIT_LENGTH);
|
|
_delay_ms(BIT_LENGTH);
|
|
return 0;
|
|
}
|
|
|
|
// send '0/ bit
|
|
int sendLong(void){
|
|
_delay_ms(BIT_LENGTH);
|
|
carrier(BIT_LENGTH);
|
|
_delay_ms(BIT_LENGTH);
|
|
return 0;
|
|
}
|
|
|
|
//Send the entire code including init, start, and end bits
|
|
void sendBlast(void) {
|
|
carrier(20);
|
|
_delay_ms(20);
|
|
|
|
//start bit
|
|
sendShort();
|
|
_delay_ms(BIT_LENGTH);
|
|
|
|
for(int i=0; i<32; i++){
|
|
if (code &0x80000000){
|
|
sendShort();
|
|
}
|
|
else{
|
|
sendLong();
|
|
code<<=1;
|
|
}
|
|
}
|
|
|
|
//stop bit
|
|
sendShort();
|
|
}
|
|
|
|
int main(void) {
|
|
//PB1 Button (pin 6); PB4 LED (pin 3)
|
|
|
|
DDRB |= (1 << 0x04); // Set PB4 Output
|
|
PORTB |= (1 << 0x01); //Set PB1 high
|
|
|
|
while(true)
|
|
{
|
|
if ((PINB & (1 << 0x01)) == 0){ //if the button is pushed start sending code
|
|
sendBlast();
|
|
}
|
|
else {
|
|
PORTB &= ~(1 << 0x04); //if the button is not pushed make sure PB4 is off
|
|
}
|
|
}
|
|
}
|
|
|