Advertisement
Guest User

Untitled

a guest
Nov 24th, 2013
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. //mode
  2. #define SINGLE_OUTPUT 0x4
  3.  
  4. //PWM speed steps
  5. #define PWM_FLT 0x0
  6. #define PWM_FWD7 0x7
  7.  
  8. //channel
  9. #define CH1 0x0
  10.  
  11. //output
  12. #define RED 0x0
  13.  
  14. int IRPin = 13;
  15. int toggle[4] = {0,0,0,0};
  16.  
  17. void setup()
  18. {
  19.   pinMode(IRPin, OUTPUT);
  20.   digitalWrite(IRPin, LOW);
  21.    
  22. }
  23.  
  24. void loop()
  25. {
  26.   SingleOutput(PWM_FWD7, RED, CH1);
  27.   delay(2000);
  28.   SingleOutput(PWM_FLT, RED, CH1);
  29.   delay(2000);
  30.  
  31. }
  32.  
  33. void pf_send(int code1, int code2)
  34. {
  35.   int x = 128;
  36.  
  37.   start_stop_bit();
  38.  
  39.   while (x)
  40.   {
  41.     oscillationWrite(IRPin, 156);
  42.    
  43.     if (code1 & x) //high bit
  44.       high_pause();
  45.     else //low bit
  46.       low_pause();
  47.    
  48.     x >>= 1;  //next bit
  49.   }
  50.  
  51.   x = 128;
  52.   while (x)
  53.   {
  54.     oscillationWrite(IRPin, 156);
  55.    
  56.     if (code2 & x) // high bit
  57.       high_pause();
  58.     else //low bit
  59.       low_pause();
  60.  
  61.     x >>= 1;  //next bit
  62.   }
  63.  
  64.   start_stop_bit();
  65.   delay(10);
  66. }
  67.  
  68. void SingleOutput(int pwm, int output, int channel)
  69. {
  70.    int nib1, nib2, nib3, nib4, i;
  71.  
  72.    //set nibs
  73.    nib1 = toggle[channel] | channel;
  74.    nib2 = SINGLE_OUTPUT | output;
  75.    nib3 = pwm;
  76.    nib4 = 0xf ^ nib1 ^ nib2 ^ nib3;
  77.    
  78.    for(i = 0; i < 6; i++)
  79.    {
  80.      message_pause(channel, i);
  81.      pf_send(nib1 << 4 | nib2, nib3 << 4 | nib4);    
  82.    }
  83.    
  84.    if(toggle[channel] == 0)
  85.      toggle[channel] = 8;
  86.    else
  87.      toggle[channel] = 0;
  88. }
  89.  
  90. void start_pause()
  91. {
  92.   delayMicroseconds(1014);
  93. }
  94.  
  95. void high_pause()
  96. {
  97.   delayMicroseconds(546);
  98. }
  99.  
  100. void low_pause()
  101. {
  102.   delayMicroseconds(260);
  103. }
  104.  
  105. void tx_pause()
  106. {
  107.   delayMicroseconds(156);
  108. }
  109.  
  110. void message_pause(int channel, int count)
  111. {
  112.   unsigned char a = 0;
  113.  
  114.   if(count == 0)
  115.     a = 4 - channel + 1;
  116.   else if(count == 1 || count == 2)
  117.     a = 5;
  118.   else if(count == 3 || count == 4)
  119.     a = 5 + (channel + 1) * 2;
  120.      
  121.   delayMicroseconds(a * 77);
  122. }
  123.  
  124.  
  125. void start_stop_bit()
  126. {
  127.   oscillationWrite(IRPin, 156);  
  128.   //digitalWrite(IRPin, HIGH);
  129.   //tx_pause();
  130.   //digitalWrite(IRPin, LOW);
  131.   start_pause();
  132. }
  133.  
  134. void oscillationWrite(int pin, int time) {
  135.   for(int i = 0; i <= time/26; i++) {
  136.     digitalWrite(pin, HIGH);
  137.     delayMicroseconds(13);
  138.     digitalWrite(pin, LOW);
  139.     delayMicroseconds(13);
  140.   }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement