Reworked IR code so it's 32 bits.

Added comments.
Added start/stop bit logic.
This commit is contained in:
Dan
2022-09-12 21:36:37 -04:00
parent c01acd51e0
commit 4064a2b0d8

View File

@@ -1,11 +1,14 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include <util/delay.h>
// unsigned long CODE = 0b10011111111100100100100100100100100100111111001110010010010010011; //65 bits
int code = 111111111000000001111112111000011 ;
//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++)
{
@@ -17,20 +20,31 @@ void carrier(int BlastTime) {
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*3);
_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);
//Start 32 bit Code
for (int i=0; i<9;i++){
sendShort();
}
@@ -40,7 +54,6 @@ void sendBlast(void) {
for (int i=0;i<6;i++){
sendShort();
}
_delay_ms(BIT_LENGTH);
for (int i=0;i<3;i++){
sendShort();
}
@@ -50,8 +63,8 @@ void sendBlast(void) {
for(int i=0;i<2;i++){
sendShort();
}
//stop bit
sendShort();
}
int main(void) {
@@ -62,14 +75,12 @@ int main(void) {
while(true)
{
if ((PINB & (1 << 0x01)) == 0)
{
if ((PINB & (1 << 0x01)) == 0){ //if the button is pushed start sending code
sendBlast();
}
else {
PORTB &= ~(1 << 0x04);
PORTB &= ~(1 << 0x04); //if the button is not pushed make sure PB4 is off
}
}
}
}