📄 main.c
字号:
/*
*-------------------------------------------------------------------------------
*-- RCSId: $Id: main.c,v 0.17 2003-10-10 10:55:40+02 mjg Exp mjg $
*-- $Name: $
*-------------------------------------------------------------------------------
*-- main.c (level1)
*-------------------------------------------------------------------------------
*-- $Log: main.c,v $
*-- Revision 0.17 2003-10-10 10:55:40+02 mjg
*-- *** empty log message ***
*--
*-- Revision 0.16 2003-08-21 16:00:21+02 mjg
*-- RTF capture problem
*--
*-- Revision 0.15 2003-08-20 10:53:20+02 mjg
*-- to redesign SearchPattern
*--
*-- Revision 0.14 2003-08-20 10:00:46+02 mjg
*-- to add debug features
*--
*-- Revision 0.13 2003-08-07 08:01:30+02 mjg
*-- *** empty log message ***
*--
*-- Revision 0.11 2003-07-22 13:27:48+02 mjg
*-- cloned firmware
*--
*-------------------------------------------------------------------------------
*/
#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include <stdio.h>
#include <stdlib.h>
#include "level4.h"
#include "level3.h"
#include "level2.h"
// ==================================================================
// capture variables
uint8_t halfDataRate; //half period data rate modul
uint8_t counter1set; //counter1 settings
uint8_t lwr; //last word read
uint8_t delayed; //delayed value
uint8_t forward_link_type; //forward link modulation type
uint8_t decode_number; //decoder routine number
void (*decode)(void); //decoder routine variable
uint8_t enable_capture; //enable capture (0=disable, 1=capture enable, 2=debug pulse capture)
uint16_t last_capture; //counter1 is now reset at capture start, we make a difference
//between current and last value
uint8_t currentMaxTimeHi; //actual hi value of maximum capture time
uint8_t watchdog_reset; //watchdog activated
// ==================================================================
// level 1 local variables
register uint8_t captured_byte asm ("r6"); //store_bit current capture bits
register uint8_t captured_valid asm ("r7"); //store_bit current capture valid bits
register uint8_t capture_cnt asm ("r8"); //store_bit current capture byte index
register uint8_t captured_bit_count asm ("r9"); //store_bit current capture bit counter
uint8_t bit_pos; //bit position in demodulation
uint8_t last_bit; //for biphase and miller usage
uint8_t edge; //for manchester usage
uint8_t last_valid; //last valid value for invalid sequence compression
uint8_t treshold_0;
uint8_t treshold_1;
// ==================================================================
// function declarations
// ==================================================================
void avr_ini(void)
{
// ---------------------------------------------------------------
// UART initialization
// ---------------------------------------------------------------
UARTIni(); // level4 uart init routine
// ---------------------------------------------------------------
// 16-bit Timer/Counter1 initialization
// ---------------------------------------------------------------
TCCR1A = 0; // no compare otuput, no PWM mode ...
TCCR1B = (1<<ICNC1) | (1<<ICES1) | (1<<CS12) | (1<<CS11);
// noise removal, trigger on rising edge,
// input = external pin T1 falling edge
// output compare registers OCR1A, OCR1B are not used
TCNT1 = 0; // Clear T/C1 register
// ---------------------------------------------------------------
// I/O Ports initialization
// ---------------------------------------------------------------
DDRB = 0x0A; // PORTB.0,2,4-7 as input, PORTB.1 = OC1A = output, PORTB.3 = OC2 = output
DDRD = 0x00; // PORTD.0-7 as input
DDRC = 0x1F; // PORTC.(4-0) = (mod, shd, LED, dbg_valid, dbg_data) = output, rest = input
cbi( PORTC, SHD_PIN ); //set shutdown active == startup antenna on
cbi( PORTC, MOD_PIN ); //set notmodulation == startup antenna on
SetLEDOn();
// ---------------------------------------------------------------
// Watch Dog Setup
// ---------------------------------------------------------------
cbi( MCUCSR, WDRF); //clear watchdog reset flag
wdt_enable(0x7);
// ---------------------------------------------------------------
// General Interrupt Enable
// ---------------------------------------------------------------
MCUCR = ((1<<ISC11) | (1<<ISC10)); //INT1 triggered on rising edge
GICR = 0; //INT0 & INT1 disabled
sei(); // General Interrupt Enable
}
// ==================================================================
// R E C E I V E R C O D E
// ==================================================================
// ---------------------------------------------------------------
// store bit into capture buffer bytes
void store_bit(uint8_t b, uint8_t v)
{
b &= 1;
v &= 1;
#ifdef DEBUG
if (bit_is_set(PINB, DBG_BADDATA)) cbi(PORTB, DBG_BADDATA); else sbi(PORTB, DBG_BADDATA); // debugging
if (bit_is_set(PINB, DBG_BADDATA)) cbi(PORTB, DBG_BADDATA); else sbi(PORTB, DBG_BADDATA); // debugging
if (b == 0) cbi(PORTB, DBG_DATA); else sbi(PORTB, DBG_DATA); // debugging bitval
if (v == 0) cbi(PORTB, DBG_BADDATA); else sbi(PORTB, DBG_BADDATA); // debugging bitok
#endif
if (enable_capture == 1) {
captured_byte = (captured_byte << 1) | b;
captured_valid = (captured_valid << 1) | v;
if (captured_bit_count == 7) {
captured_bit_count = 0;
capture_data[capture_cnt] = captured_byte;
capture_valid[capture_cnt++] = captured_valid;
if (capture_cnt == CAPTURE_SIZE) {
TIMSK = TIMSK & (~((1<<TICIE1)|(1<<TOIE1))); //disable all (other modulations)
cbi (GICR, INT1); //disable INT1
enable_capture = 0;
}
} else {
captured_bit_count++;
}
}
}
// ---------------------------------------------------------------
// debug routine stores the captured pulse lengths
void store_pulse(uint8_t b)
{
if (enable_capture == 2) {
uart_out_buffer[capture_cnt] = b;
if (++capture_cnt == UART_OUT_BUFFER_SIZE) {
TIMSK = TIMSK & (0xFF ^ ((1<<TICIE1)|(1<<TOIE1))); //disable all (other modulations)
cbi (GICR, INT1); //disable INT1 (PSK)
}
}
}
// ==================================================================
void manchester_capture(void)
{
uint8_t y;
uint16_t capt;
uint16_t icr = ICR1;
edge = TCCR1B;
TCCR1B = edge ^ (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;
(uint8_t)capt += halfDataRate >> 2; // dynamic resolution (dependent on datarate)
(uint8_t)capt -= halfDataRate; // always subtract datarate/2
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
if(last_valid == 0) {
last_bit = ((~edge)>>ICES1)&1; // rtf feature
store_bit(last_bit, 0); // rtf feature
store_bit ( 0, 1); // store bad bit
last_valid = 1;
}
last_bit = 0;
} else {
if (y) {
last_bit = ((~edge)>>ICES1)&1;
store_bit(last_bit, 0);
last_valid = 0;
bit_pos = 0;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -