📄 main.c
字号:
if (bit_pos++) {
store_bit(last_bit, 0);
bit_pos = 0;
last_valid = 0;
}
}
}
}
// ==================================================================
void biphase_capture(void)
{
uint8_t y;
uint16_t capt;
uint16_t icr = ICR1;
TCCR1B ^= (1<<ICES1); // Switchover of sensitive ICP edge
capt = icr - last_capture;
last_capture = icr; // counter1 is now free-running
if (debug_mode == 1) {
if(capt>255) store_pulse(255); else store_pulse((uint8_t)capt);
}
y = 0;
capt += halfDataRate >> 2; // dynamic resolution (dependent on datarate)
capt -= halfDataRate; // always subtract datarate/2
if (capt > halfDataRate) { // when longer than that, try to subtract once more
y++;
capt -= halfDataRate;
}
if ((capt<<1) > halfDataRate) { // check overflow of counter and range
// this check is also dynamic, depends on current datarate
// the faster datarate the more precise must the signal be
if (last_valid == 0) {
store_bit(bit_pos^1, 0); // rtf feature
store_bit ( 0, 1); // store bad bit
last_valid = 1;
}
bit_pos = 0;
} else {
if (y) {
store_bit(1, 0);
bit_pos = 0;
last_valid = 0;
} else {
if (bit_pos++) {
store_bit(0, 0);
bit_pos = 0;
last_valid = 0;
}
}
}
}
// ==================================================================
void miller_capture(void)
{
uint8_t y;
uint16_t capt;
uint16_t icr = ICR1;
TCCR1B ^= (1<<ICES1); // Switchover of sensitive ICP edge
capt = icr - last_capture;
last_capture = icr; // counter1 is now free-running
if (debug_mode == 1) {
if(capt>255) store_pulse(255); else store_pulse((uint8_t)capt);
}
y = 0; //first operation must be 16 bits wide (counter can be up to 256 !)
capt -= halfDataRate << 1; // always subtract datarate
// from now on, 8bit operations are enough
capt += halfDataRate >> 2; // dynamic resolution (dependent on datarate)
if ((uint8_t)capt > halfDataRate) { // when longer than that, try to subtract once more
y++;
(uint8_t)capt -= halfDataRate;
}
if ((uint8_t)capt > halfDataRate) { // when longer than that, try to subtract once more
y++;
(uint8_t)capt -= halfDataRate;
}
if ((capt<<1) > halfDataRate) { // check overflow of counter and range
// this check is also dynamic, depends on current datarate
// the faster datarate the more precise must the signal be
last_bit = 0;
store_bit ( 0, 1); // store bad bit
} else {
if (y==2) { // only combination "101"
store_bit(0, 0);
store_bit(1, 0);
last_bit = 1;
} else if (!y) { // the same bit as previously
store_bit(last_bit, 0);
} else if (last_bit) { // y==1, last_bit=1
store_bit(0, 0);
last_bit = 0;
} else { // y==1, last_bit=0
store_bit(0, 0);
store_bit(1, 0);
last_bit = 1;
}
}
}
// ==================================================================
// Down Link setup function
// Requires: maxCaptureTimeLow and maxCaptureTimeHi
// (located here instead of level2 because of register variables)
void Capture(uint8_t style) {
ClearCaptureBuffers();
last_valid = 0;
TCCR1B = 0; //disable Counter1
captured_bit_count = 0;
capture_cnt = 0;
bit_pos = 0;
TCNT1 = ~maxCaptureTimeLow; //set timer with initial time
currentMaxTimeHi = ~maxCaptureTimeHi;
TIFR = TIFR | (1<<ICF1) | (1<<TOV1); //clear pending interrupts
TIMSK = TIMSK | (1<<TICIE1) | (1<<TOIE1); //enable capture, overflow
enable_capture = style; //set capture style
TCCR1B = counter1set; //run!
while ( TIMSK & (1<<TICIE1) ) //wait until done
{}
if(last_valid == 0) { // rtf feature
switch (decode_number) {
case 1 :
last_bit = ((~edge)>>ICES1)&1;
store_bit(last_bit, 0);
break;
case 2 :
store_bit(bit_pos ^ 1, 0);
break;
default :
break;
}
}
if (capture_cnt != CAPTURE_SIZE) //flush captured bits
while (captured_bit_count != 0) {
store_bit(0,1);
}
enable_capture = 0;
TCCR1B = counter1set; //counter T1 running
sbi (TIMSK, TICIE1); //enable dummy capture
}
// ==================================================================
// Wait
void Wait(uint16_t period) {
TCCR1B = 0; //disable Counter1
TCNT1 = ~period; //set timer with initial time
currentMaxTimeHi = 0xFF;
TIFR = TIFR | (1<<ICF1) | (1<<TOV1); //clear pending interrupts
sbi(TIMSK, TOIE1); //enable overflow
TCCR1B = counter1set; //run!
while ( TIMSK & (1<<TICIE1) ) //wait until done
{}
// TCCR1B = counter1set; //counter T1 running
// sbi (TIMSK, TICIE1); //enable dummy capture
}
// ==================================================================
// ==================================================================
// INTERRUPT ROUTINES
// ==================================================================
SIGNAL (SIG_OVERFLOW1)
{
if (currentMaxTimeHi != 0xFF) { //hi datarate
currentMaxTimeHi = 0xFF;
} else {
TIMSK = TIMSK & (~((1<<TICIE1)|(1<<TOIE1))); //finished, disable all
}
}
// ==================================================================
SIGNAL (SIG_INPUT_CAPTURE1)
{
(*decode)();
}
// ==================================================================
// ==================================================================
// ==================================================================
int main(void)
{
SetLEDOn();
watchdog_reset = bit_is_set(MCUCSR, WDRF); //capture watchdog resets
avr_ini(); // initialization
main_receiver();
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -