simple infrared softpwm send

4
C:\Users\Ilija\Desktop\Simple_Infrared_softPWM_Send\Simple_Infrared_softPWM_Send.ino 17. februar 2016 15:30 #define txPinIR 3 //IR carrier output unsigned char carrierFreq = 0; //default unsigned char period = 0; //calculated once for each signal sent in initSoftPWM unsigned char periodHigh = 0; //calculated once for each signal sent in initSoftPWM unsigned char periodLow = 0; //calculated once for each signal sent in initSoftPWM unsigned long sigTime = 0; //used in mark & space functions to keep track of time unsigned long sigStart = 0; //used to calculate correct length of existing signal, to handle some repeats //RAW Mitsubishi 88 bit signal - make sure buffer starts with a Mark unsigned int Mitsubishi88AC_RAW[] = {3172, 1586, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 394, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 1182, 394, 394, 394}; //AnalysIR Batch Export (IRremote) - RAW unsigned char Mitsubishi88AC_Hex[] = {0x4A, 0x75, 0xC3, 0x64, 0x9B, 0xFF, 0x00, 0xFD, 0x02, 0x7D, 0x82}; void setup() { Serial.begin(57600); pinMode(txPinIR, OUTPUT); } void loop() { //Next send the Mitsubishi AC RAW signal defined above Serial.println(F("Sending Mitsubishi88AC_RAW @ 40kHz")); sendRawBuf(Mitsubishi88AC_RAW, sizeof(Mitsubishi88AC_RAW)/ sizeof(Mitsubishi88AC_RAW[0]), 40); delay(5000); //wait 5 seconds between each signal (change to suit) //Next send the Mitsubishi88AC_Hex signal defined above Serial.println(F("Sending Mitsubishi88AC_Hex @ 36kHz")); sendHexMITSUBISHI88AC(Mitsubishi88AC_Hex, sizeof(Mitsubishi88AC_Hex)/ sizeof(Mitsubishi88AC_Hex[0]), 36); delay(5000); //wait 5 seconds between each signal (change to suit) //Next send the Mitsubishi88AC_Hex signal defined above Serial.println(F("Sending Mitsubishi88AC_Hex @ 33kHz")); sendHexNEC(NEC_HEX_VALUE, NEC_BIT_COUNT, 1, 33); delay(5000); //wait 5 seconds between each signal (change to suit) //Next send the Mitsubishi88AC_Hex signal defined above Serial.println(F("Sending Mitsubishi88AC_Hex @ 30kHz")); sendHexMITSUBISHI88AC(Mitsubishi88AC_Hex, sizeof(Mitsubishi88AC_Hex)/ sizeof(Mitsubishi88AC_Hex[0]), 30); delay(5000); //wait 5 seconds between each signal (change to suit) } -1-

Upload: apofview

Post on 16-Jul-2016

12 views

Category:

Documents


3 download

DESCRIPTION

Arduino code for soft pwm

TRANSCRIPT

C:\Users\Ilija\Desktop\Simple_Infrared_softPWM_Send\Simple_Infrared_softPWM_Send.ino 17. februar 2016 15:30

#define txPinIR 3 //IR carrier output

unsigned char carrierFreq = 0; //defaultunsigned char period = 0; //calculated once for each signal sent in initSoftPWMunsigned char periodHigh = 0; //calculated once for each signal sent in initSoftPWMunsigned char periodLow = 0; //calculated once for each signal sent in initSoftPWM

unsigned long sigTime = 0; //used in mark & space functions to keep track of timeunsigned long sigStart = 0; //used to calculate correct length of existing signal, to handle some repeats

//RAW Mitsubishi 88 bit signal - make sure buffer starts with a Markunsigned int Mitsubishi88AC_RAW[] = {3172, 1586, 394, 394, 394, 1182, 394, 394, 394, 394,394, 1182, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394,394, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 394, 394, 394,394, 394, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394, 394, 394, 394, 394,1182, 394, 394, 394, 394, 394, 1182, 394, 394, 394, 394, 394, 1182, 394, 1182, 394, 394,394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394,1182, 394, 1182, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394,394, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 1182,394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 1182, 394, 394, 394, 394,394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 1182, 394, 394, 394, 1182, 394, 1182, 394,394, 394, 394, 394, 394, 394, 394, 394, 394, 394, 1182, 394, 394, 394}; //AnalysIR Batch Export (IRremote) - RAW

unsigned char Mitsubishi88AC_Hex[] = {0x4A, 0x75, 0xC3, 0x64, 0x9B, 0xFF, 0x00, 0xFD, 0x02,0x7D, 0x82};

void setup() {Serial.begin(57600);pinMode(txPinIR, OUTPUT);

}

void loop() {//Next send the Mitsubishi AC RAW signal defined aboveSerial.println(F("Sending Mitsubishi88AC_RAW @ 40kHz"));sendRawBuf(Mitsubishi88AC_RAW, sizeof(Mitsubishi88AC_RAW) / sizeof(Mitsubishi88AC_RAW[0]),40);delay(5000); //wait 5 seconds between each signal (change to suit)

//Next send the Mitsubishi88AC_Hex signal defined aboveSerial.println(F("Sending Mitsubishi88AC_Hex @ 36kHz"));sendHexMITSUBISHI88AC(Mitsubishi88AC_Hex, sizeof(Mitsubishi88AC_Hex) /sizeof(Mitsubishi88AC_Hex[0]), 36);delay(5000); //wait 5 seconds between each signal (change to suit)

//Next send the Mitsubishi88AC_Hex signal defined aboveSerial.println(F("Sending Mitsubishi88AC_Hex @ 33kHz"));sendHexNEC(NEC_HEX_VALUE, NEC_BIT_COUNT, 1, 33);delay(5000); //wait 5 seconds between each signal (change to suit)

//Next send the Mitsubishi88AC_Hex signal defined aboveSerial.println(F("Sending Mitsubishi88AC_Hex @ 30kHz"));sendHexMITSUBISHI88AC(Mitsubishi88AC_Hex, sizeof(Mitsubishi88AC_Hex) /sizeof(Mitsubishi88AC_Hex[0]), 30);delay(5000); //wait 5 seconds between each signal (change to suit)

}

-1-

C:\Users\Ilija\Desktop\Simple_Infrared_softPWM_Send\Simple_Infrared_softPWM_Send.ino 17. februar 2016 15:30

//----------------------------declare functionss-----------------------------------void sendRawBuf(unsigned int *sigArray, unsigned int sizeArray, unsigned char kHz) {if (carrierFreq != kHz) initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sentsigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delaysfor (int i = 0; i < sizeArray; i++) {mark(sigArray[i++]); //also move pointer to next positionif (i < sizeArray) { //check we have a space remaining before sending itspace(sigArray[i]); //pointer will be moved by for loop

}}

}

void sendHexMITSUBISHI88AC(unsigned char *sigArray, unsigned int sizeArray, unsigned charkHz) { //Mitsubish 88 bit Ir protocol format/* A basic 88 bit NEC-'like' signal is made up of:

* 1 x 3172 uSec Header Mark, followed by * 1 x 1586 uSec Header Space, followed by * 32 x bits uSec ( 1- bit 394 uSec Mark followed by 1182 uSec space; 0 - bit 394 uSec

Mark follwed by 394 uSec Space) * 1 x 9000 uSec Trailer Mark * There can also be a generic repeat signal, which is usually not neccessary & can be

replaced by sending multiple signals */#define MITSUBISHI88AC_HEADER_MARK 3172#define MITSUBISHI88AC_HEADER_SPACE 1586#define MITSUBISHI88AC_ONE_MARK 394#define MITSUBISHI88AC_ZERO_MARK 394#define MITSUBISHI88AC_ONE_SPACE 1182#define MITSUBISHI88AC_ZERO_SPACE 394#define MITSUBISHI88AC_TRAILER_MARK 394

//if (carrierFreq != kHz) initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sentsigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delays

// First send header Mark & Spacemark(MITSUBISHI88AC_HEADER_MARK);space(MITSUBISHI88AC_HEADER_SPACE);

for (unsigned int i = 0; i < sizeArray; i++) { //iterate thru each byte in sigArrayregister unsigned char bitMask = 0x80; //starting value of bitmask fo each Hex bytewhile (bitMask) { //do 8 times for each bit of the 8 bit byteif (bitMask & sigArray[i]) { //its a One bitmark(MITSUBISHI88AC_ONE_MARK);space(MITSUBISHI88AC_ONE_SPACE);

}else { // its a Zero bitmark(MITSUBISHI88AC_ZERO_MARK);space(MITSUBISHI88AC_ZERO_SPACE);

}bitMask = (unsigned char) bitMask >> 1; // shift the mask bit along until it reaches zero & we exit the while loop

-2-

C:\Users\Ilija\Desktop\Simple_Infrared_softPWM_Send\Simple_Infrared_softPWM_Send.ino 17. februar 2016 15:30

}

}// Last send NEC Trailer MArkmark(MITSUBISHI88AC_TRAILER_MARK);

}

void initSoftPWM(unsigned char carrierFreq) { // Assumes standard 8-bit Arduino, running at 16Mhz//supported values are 30, 33, 36, 38, 40, 56 kHz, any other value defaults to 38kHz//we will aim for a duty cycle of circa 33%

period = (1000 + carrierFreq / 2) / carrierFreq;periodHigh = (period + 1) / 3;periodLow = period - periodHigh;// Serial.println (period);// Serial.println (periodHigh);// Serial.println (periodLow);Serial.println (carrierFreq);

switch (carrierFreq) {case 30 : //delivers a carrier frequency of 29.8kHz & duty cycle of 34.52%periodHigh -= 6; //Trim it based on measurementt from OscilloscopeperiodLow -= 10; //Trim it based on measurementt from Oscilloscopebreak;

case 33 : //delivers a carrier frequency of 32.7kHz & duty cycle of 34.64%periodHigh -= 6; //Trim it based on measurementt from OscilloscopeperiodLow -= 10; //Trim it based on measurementt from Oscilloscopebreak;

case 36 : //delivers a carrier frequency of 36.2kHz & duty cycle of 35.14%periodHigh -= 6; //Trim it based on measurementt from OscilloscopeperiodLow -= 11; //Trim it based on measurementt from Oscilloscopebreak;

case 40 : //delivers a carrier frequency of 40.6kHz & duty cycle of 34.96%periodHigh -= 6; //Trim it based on measurementt from OscilloscopeperiodLow -= 11; //Trim it based on measurementt from Oscilloscopebreak;

case 56 : //delivers a carrier frequency of 53.8kHz & duty cycle of 40.86%periodHigh -= 6; //Trim it based on measurementt from OscilloscopeperiodLow -= 12; //Trim it based on measurementt from OscilloscopeSerial.println(periodHigh);Serial.println(periodLow);

break;

case 38 : //delivers a carrier frequency of 37.6kHz & duty cycle of 36.47%default :periodHigh -= 6; //Trim it based on measurementt from OscilloscopeperiodLow -= 11; //Trim it based on measurementt from Oscilloscopebreak;

}

-3-

C:\Users\Ilija\Desktop\Simple_Infrared_softPWM_Send\Simple_Infrared_softPWM_Send.ino 17. februar 2016 15:30

}

void mark(unsigned int mLen) { //uses sigTime as end parametersigTime += mLen; //mark ends at new sigTimeunsigned long now = micros();unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delaysif (dur == 0) return;while ((micros() - now) < dur) { //just wait here until time is up

digitalWrite(txPinIR, HIGH);if (periodHigh) delayMicroseconds(periodHigh);digitalWrite(txPinIR, LOW);if (periodLow) delayMicroseconds(periodLow);

}}

void space(unsigned int sLen) { //uses sigTime as end parametersigTime += sLen; //space ends at new sigTimeunsigned long now = micros();unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delaysif (dur == 0) return;while ((micros() - now) < dur) ; //just wait here until time is up

}

-4-