Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 54 → Rev 55

/IR-TX-BL/branches/LED-CTRL_V0.01/main.c
14,26 → 14,19
#include "uart.h"
#include "twislave.h"
 
volatile unsigned int ppm_signal = 1500;
volatile unsigned char ppm_new = 0;
volatile unsigned int PPM_SIGNAL = 1500;
volatile unsigned char PPM_NEW = 0;
volatile unsigned char TMR1OvF = 0;
volatile unsigned int TMR1MS;
volatile unsigned char ch0;
volatile unsigned char ch1;
volatile unsigned char ch2;
volatile unsigned char ch3;
volatile unsigned char ch4;
volatile unsigned char ch5;
volatile unsigned char ch0_tmp;
volatile unsigned char ch1_tmp;
volatile unsigned char ch2_tmp;
volatile unsigned char ch3_tmp;
volatile unsigned char ch4_tmp;
volatile unsigned char ch5_tmp;
volatile unsigned char CH0;
volatile unsigned char CH1;
volatile unsigned char CH2;
volatile unsigned char CH3;
volatile unsigned char CH4;
volatile unsigned char CH5;
 
 
unsigned int timer;
 
/*##############################################################################*/
SIGNAL(SIG_OVERFLOW1)
{
TMR1OvF++;
59,9 → 52,9
{
if (ppm > 2100) ppm = 2100;
if (ppm < 900) ppm = 900;
ppm = (ppm_signal * 3 + ppm) / 4;
ppm_signal = ppm;
if (ppm_new < 50) ppm_new++;
ppm = (PPM_SIGNAL * 3 + ppm) / 4;
PPM_SIGNAL = ppm;
if (PPM_NEW < 50) PPM_NEW++;
}
}
68,10 → 61,7
 
}
 
 
 
 
 
/*##############################################################################*/
SIGNAL(SIG_OVERFLOW0)
{
// this function is called every 32us,
80,6 → 70,12
 
static unsigned char counter = 254;
static unsigned char ms1 = 0;
static unsigned char ch0_tmp = 0;
static unsigned char ch1_tmp = 0;
static unsigned char ch2_tmp = 0;
static unsigned char ch3_tmp = 0;
static unsigned char ch4_tmp = 0;
static unsigned char ch5_tmp = 0;
unsigned char PORTB_BAK;
unsigned char PORTD_BAK;
 
88,27 → 84,26
 
if (counter++ == 254)
{
PORTB_BAK LEDON (CH0_B | CH1_B | CH2_B);
PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D);
ch0_tmp = ch0;
ch1_tmp = ch1;
ch2_tmp = ch2;
ch3_tmp = ch3;
ch4_tmp = ch4;
ch5_tmp = ch5;
PORTB_BAK LEDON (CH0_B | CH1_B | CH2_B); //new cycle, output on
PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D); //
ch0_tmp = CH0;
ch1_tmp = CH1;
ch2_tmp = CH2;
ch3_tmp = CH3;
ch4_tmp = CH4;
ch5_tmp = CH5;
counter = 0;
}
 
if (ch0_tmp == counter) PORTB_BAK LEDOFF CH0_B;
if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B;
if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B;
if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D;
if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D;
if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D;
if (ch0_tmp == counter) PORTB_BAK LEDOFF CH0_B; //channel value reached, output off
if (ch1_tmp == counter) PORTB_BAK LEDOFF CH1_B; //
if (ch2_tmp == counter) PORTB_BAK LEDOFF CH2_B; //
if (ch3_tmp == counter) PORTD_BAK LEDOFF CH3_D; //
if (ch4_tmp == counter) PORTD_BAK LEDOFF CH4_D; //
if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D; //
PORTB = PORTB_BAK;
PORTD = PORTD_BAK;
 
if (ms1++ == 32)
{
122,42 → 117,39
}
 
 
 
/*##############################################################################*/
unsigned int SetDelay (unsigned int t)
{
unsigned char temp_hi;
unsigned char temp_lo;
unsigned char hi_byte;
unsigned char lo_byte;
 
temp_hi = (TMR1MS >> 8);
temp_lo = (TMR1MS & 0xff);
if (temp_hi != (TMR1MS >> 8)) temp_hi = (TMR1MS >> 8);
hi_byte = (TMR1MS >> 8);
lo_byte = (TMR1MS & 0xff);
if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
 
return(((temp_hi << 8) | temp_lo) + t + 1);
 
return(((hi_byte << 8) | lo_byte) + t + 1);
}
 
 
 
/*##############################################################################*/
char CheckDelay(unsigned int t)
{
unsigned char temp_hi;
unsigned char temp_lo;
unsigned char hi_byte;
unsigned char lo_byte;
 
temp_hi = (TMR1MS >> 8);
temp_lo = (TMR1MS & 0xff);
if (temp_hi != (TMR1MS >> 8)) temp_hi = (TMR1MS >> 8);
hi_byte = (TMR1MS >> 8);
lo_byte = (TMR1MS & 0xff);
if (hi_byte != (TMR1MS >> 8)) hi_byte = (TMR1MS >> 8);
 
return(((t - ((temp_hi << 8) | temp_lo)) & 0x8000) >> 9);
 
return(((t - ((hi_byte << 8) | lo_byte)) & 0x8000) >> 9);
}
 
 
 
 
/*##############################################################################*/
void StartPWM(void)
{
//Timer 0 Config for getting right timing for IR Pattern
//Timer 0 Config
TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00); // (@8MHz) = 1/8us Clk = 256/8 = 32us overflow
TIMSK setbit (1<<TOIE0); // enable Int
 
164,12 → 156,11
}
 
 
 
/*##############################################################################*/
void StartPPM(void)
{
//global timer1 Config
//Timer1 Config
TCCR1A = (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|
(0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11);
TCCR1B = (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|
181,7 → 172,6
}
 
 
 
/*##############################################################################*/
unsigned int GetPPM(void)
{
192,9 → 182,9
unsigned char temp_hi;
unsigned char temp_lo;
 
temp_hi = (ppm_signal >> 8);
temp_lo = (ppm_signal & 0xff);
if (temp_hi != (ppm_signal >> 8)) temp_hi = (ppm_signal >> 8);
temp_hi = (PPM_SIGNAL >> 8);
temp_lo = (PPM_SIGNAL & 0xff);
if (temp_hi != (PPM_SIGNAL >> 8)) temp_hi = (PPM_SIGNAL >> 8);
return( (temp_hi << 8) | temp_lo);
 
}
206,27 → 196,30
int main (void)
{
 
#define step 128
#define mul 2
#define STEP 256
#define MUL 1
#define REDUCE 5
#define sigeepadr 0x00
#define mineepadr 0x20
#define maxeepadr 0x22
#define eepsig 0x55aa
#define EEPADRSIG 0x00
#define EEPADRMIN 0x20
#define EEPADRMAX 0x22
#define EEPSIGATURE 0x55aa
 
 
 
unsigned int ppm;
signed int color;
unsigned int setupdly;
unsigned int ppmtodly;
unsigned int flashdly;
unsigned char setup;
unsigned int lmax;
unsigned int lmin;
unsigned int max;
unsigned int min;
unsigned int sig;
unsigned char setup;
unsigned long temp1;
unsigned long temp2;
 
 
 
DDRB = (CH0_B|CH1_B|CH2_B);
PORTB = 0x00;
 
236,12 → 229,12
DDRD = (ledgreen|CH3_D|CH4_D|CH5_D);
PORTD = 0x00;
 
ch0 = 0;
ch1 = 0;
ch2 = 0;
ch3 = 0;
ch4 = 0;
ch5 = 0;
CH0 = 0;
CH1 = 0;
CH2 = 0;
CH3 = 0;
CH4 = 0;
CH5 = 0;
 
lmax = 0x0000;
lmin = 0xffff;
253,130 → 246,152
sei();
 
 
min = 1100;
max = 1900;
 
if (eeprom_read_word(sigeepadr) != eepsig)
if (eeprom_read_word(EEPADRSIG) != EEPSIGATURE) //check eep if signature is there
{
eeprom_write_word(mineepadr, min);
eeprom_write_word(maxeepadr, max);
eeprom_write_word(sigeepadr, eepsig);
min = 1100; //default min
max = 1900; //default max
eeprom_write_word(EEPADRMIN, min); //no, write initial min
eeprom_write_word(EEPADRMAX, max); //and max values
eeprom_write_word(EEPADRSIG, EEPSIGATURE); //along with eep signature
}
else
{
min = eeprom_read_word(mineepadr);
max = eeprom_read_word(maxeepadr);
min = eeprom_read_word(EEPADRMIN); //signature ok
max = eeprom_read_word(EEPADRMAX); //read min and max
}
 
printf("ppm: %d / min: %d / max: %d\n",ppm,min,max);
 
 
setup = 0;
setup = 0; //reset setup toggle counter
setupdly = SetDelay(3000);
ppmtodly = SetDelay(5000);
flashdly = SetDelay(100);
while (1)
{
 
if (ppm_new > 20)
if (PPM_NEW > 20) //ppm Signal ok
{
PORTC clrbit ledred;
ppm = GetPPM();
ppmtodly = SetDelay(500);
if (lmax < ppm) lmax=ppm;
if (lmin > ppm) lmin=ppm;
ppm = GetPPM();
ppmtodly = SetDelay(500); //reset timeout
if (lmax < ppm) lmax=ppm; //update impulse max
if (lmin > ppm) lmin=ppm; //and min boundarys
}
else
{
PORTC setbit ledred;
ppm = min;
PORTC setbit ledred; //ppm signal not ok
ppm = min; //set ppm to minimum
}
if (CheckDelay(ppmtodly))
 
if (CheckDelay(ppmtodly)) //timeout
{
ppmtodly = SetDelay(5000);
ppm_new = 0;
ppmtodly = SetDelay(5000);
PPM_NEW = 0; //set ppm signal not ok
}
 
 
if (setup < 6)
{
if ((ppm > 1600) && ((setup&1)==0)) setup++; //
if ((ppm < 1400) && ((setup&1)==1)) setup++; //
 
if (CheckDelay(flashdly)) //setup timeout reached
{
CH0 = CH0 ^ 0xff;
CH1 = CH1 ^ 0xff;
CH2 = CH2 ^ 0xff;
flashdly = SetDelay(250);
}
 
}
 
if ((ppm > 1600) && ((setup&1)==0)) setup++;
if ((ppm < 1400) && ((setup&1)==1)) setup++;
if (setup == 6)
{
PORTD setbit ledgreen;
eeprom_write_word(mineepadr, lmin);
eeprom_write_word(maxeepadr, lmax);
min = lmin;
if (setup == 6) //if stick is toggled 6 times
{ //within setup timeout
PORTD setbit ledgreen; //store ppm min and max
eeprom_write_word(EEPADRMIN, lmin); //in eeprom
eeprom_write_word(EEPADRMAX, lmax);
min = lmin;
max = lmax;
setupdly = SetDelay(2000);
setupdly = SetDelay(2000);
 
if (CheckDelay(flashdly)) //setup timeout reached
{
CH0 = CH0 ^ 0xff;
CH1 = CH1 ^ 0xff;
CH2 = CH2 ^ 0xff;
flashdly = SetDelay(25);
}
 
}
if (CheckDelay(setupdly))
 
if (CheckDelay(setupdly)) //setup timeout reached
{
setup = 0;
setup = 7; //lockdown setup
PORTD clrbit ledgreen;
}
 
//printf("ppm: %d / min: %d / max: %d\n",ppm,lmin,lmax);
/*
// Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
if ((ppm >= (step * 0)) && (ppm < (step * 1)))
if (setup = 7)
{
ch0 = mul * ((ppm - (step * 0))); //fade in red > red (red only)
ch1 = 0;
ch2 = 0;
}
if ((ppm >= (step * 1)) && (ppm < (step * 2)))
{
ch0 = ((step-1) * mul);
ch2 = mul * ((ppm - (step * 1))); //fade in blue > purple (red + blue)
ch1 = 0;
}
if ((ppm >= (step * 2)) && (ppm < (step * 3)))
{
ch0 = mul * ((step - 1) - (ppm - (step * 2))); //fade out red > blue (blue only)
ch2 = ((step-1) * mul);
ch1 = 0;
}
if ((ppm >= (step * 3)) && (ppm < (step * 4)))
{
ch0 = 0;
ch2 = ((step-1) * mul);
ch1 = mul * ((ppm - (step * 3))); //fade in green > tuerkis (blue + green)
}
if ((ppm >= (step * 4)) && (ppm < (step * 5)))
{
ch0 = 0;
ch2 = mul * ((step - 1) - (ppm - (step * 4))); //fade out blue > green (green only)
ch1 = ((step-1) * mul);
}
if ((ppm >= (step * 5)) && (ppm < (step * 6)))
{
ch0 = mul * ((ppm - (step * 5))); //fade in red > yellow (green + red)
ch2 = 0;
ch1 = ((step-1) * mul);
}
if ((ppm >= (step * 6)) && (ppm < (step * 7)))
{
ch0 = ((step-1) * mul);
ch2 = 0;
ch1 = mul * ((step - 1) - (ppm - (step * 6))); //fade out green > red (red only)
}
if ((ppm >= (step * 7)) )
{
ch0 = ((step-1) * mul);
ch2 = 0;
ch1 = 9;
}
*/
 
temp1 = (STEP * (long)40000 * (long)6) / ((max-REDUCE) - (min+REDUCE));
temp2 = ((ppm - (min+REDUCE)) * temp1);
color = temp2 / 40000;
if (color < 0) color = 0;
if (color > (STEP * 6)) color = (STEP * 6);
// Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb >
if ((color >= (STEP * 0)) && (color < (STEP * 1)))
{
CH0 = MUL * ((color - (STEP * 0))); //fade in red > red (red only)
CH1 = 0;
CH2 = 0;
}
if ((color >= (STEP * 1)) && (color < (STEP * 2)))
{
CH0 = ((STEP-1) * MUL);
CH1 = 0;
CH2 = MUL * ((color - (STEP * 1))); //fade in blue > purple (red + blue)
}
if ((color >= (STEP * 2)) && (color < (STEP * 3)))
{
CH0 = MUL * ((STEP - 1) - (color - (STEP * 2))); //fade out red > blue (blue only)
CH1 = 0;
CH2 = ((STEP-1) * MUL);
}
if ((color >= (STEP * 3)) && (color < (STEP * 4)))
{
CH0 = 0;
CH1 = MUL * ((color - (STEP * 3))); //fade in green > cyan (blue + green)
CH2 = ((STEP-1) * MUL);
}
if ((color >= (STEP * 4)) && (color < (STEP * 5)))
{
CH0 = 0;
CH1 = ((STEP-1) * MUL);
CH2 = MUL * ((STEP - 1) - (color - (STEP * 4))); //fade out blue > green (green only)
}
if ((color >= (STEP * 5)) && (color < (STEP * 6)))
{
CH0 = MUL * ((color - (STEP * 5))); //fade in red > yellow (green + red)
CH1 = ((STEP-1) * MUL);
CH2 = 0;
}
if (color >= (STEP * 6))
{
CH0 = ((STEP-1) * MUL); //fade in red > yellow (green + red)
CH1 = ((STEP-1) * MUL);
CH2 = 0;
}
}
 
 
 
}
 
 
}
 
/IR-TX-BL/branches/LED-CTRL_V0.01/main.h
16,7 → 16,7
#define setbit |=
#define clrbit &=~
#define invbit ^=
#define correction -60
//#define correction -60
 
#define LEDON setbit
#define LEDOFF clrbit