?? mc68hc908rf2.txt
字號:
MC68HC908RF2帶曼徹斯特碼的發射程序
作者: 文章來源: 時間:2007-10-21 16:09:25 閱讀次數:836
--------------------------------------------------------------------------------
波特率:9600
#include <hidef.h> /* for EnableInterrupts macro */
#include <MC68HC908RK2.h> /* include peripheral declarations */
/*****************************************************************************/
/* PROJECT : RF key demonstrator (ASK or FSK) */
/* constant file */
/*****************************************************************************/
/* Protocol */
#define nb_wake_up_bit 0 // not using the variable to delay for some time.
#define Preamble 0xFB86 // End of Tone + ID (B8) + Header (for using Romeo2 SPI)
#define Device_ID 0x7800 // length = 4 ; from $7800 to $7803
// NOTE: <IMPORTANT> Each of these 4 bytes must be programmed
// individually for each RK/RF2 upon programming the flash,
// and represent the tire identification.
#define wait_0_2ms 400 // for temperature sense
#define wait_0_5ms 1000 // for L22B pressure sense
#define wait_0_8ms 1600 // for long L90J pressure sense
#define wait_1ms 2000 // = 1 ms
#define wait_1_8ms 3810 // = 1.8 ms for Tango3
#define wait_2ms 4000 // = 2 ms for Remeo2
/* Transmission Timer Clock Generator */
/* This is the section that must be modified when the crystal is changed. */
/* You will also have to clear TSC[2:0] during transmission (see emis1.asm) */
/* ~9600 bauds using MCU clock for timer (not as accurate over temperature) */
// #define UHF_period 200 // =2MHz/10000bauds
// #define UHF_Duty_Cycle 100 // =0.5*UHF_period
/* use of DATACLK at 9600 bauds, 434 MHz*/
#define UHF_Period 22 // = 212kHz/9600 bauds
#define UHF_Duty_Cycle 11 // = 50%
/* use of DATACLK at 4800 bauds, 434 MHz*/
//#define UHF_period 44 // = 212kHz/4800 bauds
//#define UHF_Duty_Cycle 22 // = 50%
/* use of DATACLK at 9600 bauds, 315 MHz*/
// #define UHF_period 16 // = 154kHz/9600 bauds
// #define UHF_Duty_Cycle 8 // = 50%
/* use of DATACLK at 1200 bauds, 434 MHz:*/
// #define UHF_period_park 176 // = 212kHz/1200 bauds
// #define UHF_Duty_Cycle_park 88 // = 50%
/* use of DATACLK at 1200 bauds, 315 MHz:*/
// #define UHF_period_park 128 // = 154kHz/1200 bauds
// #define UHF_Duty_Cycle_park 64 // = 50%
//** hardef.h
//** Tango 3 I/O assignments
//#define tango_Band PTB_PTB0
#define tango_Band PTA_PTA7
#define tango_Enable PTB_PTB1
#define tango_DATA_RF PTB_PTB2
#define tango_UP_CLOCK PTB_PTB3
//#define tango_Mode PTA_PTA7 // PA7 **Important: porta!!! NOT portb!!!
#define tango_Mode PTB_PTB0
//* Mode Control pins *
//#define S1 PTA_PTA1
//#define S0 PTA_PTA2
#define S1 PTA_PTA2
#define S0 PTA_PTA3
//* Digital Data Output Pin *
//#define DOUT_BAR PTA_PTA3
//#define DOUTb PTA_PTA3
#define DOUT_BAR PTA_PTA1
#define DOUTb PTA_PTA1
//* Data Clock Pin *
#define DCLK PTA_PTA4
//* Serial Data In Pin *
#define SDATA PTA_PTA5
#define DDATA PTA_PTA5
#define LOWV 0x20
#define LVIOUT 0x80
#define nb_tx_byte 10
/*****************************************************************************/
/* PROJECT : RF key demonstrator (ASK or FSK) */
/* Variable file */
/* Modified by Jeff Burgess */
/* - added work-around for bug in Romeo2 receiver, where its SPI corrupts */
/* the last bit of data in the frame. Simply transmit one more useless */
/* byte, that will be corrupted instead of the useful data. */
/*****************************************************************************/
//*** define tyre's ID @ 0x7800
//const uchar ID[]@Device_ID = {0x55,0x10,0x77,0x77};
const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x01};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x02};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x03};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x04};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x05};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x06};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x07};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x08};
//const uchar ID[]@Device_ID = {0x55,0x10,0x02,0x09};
// transmit variables
// Preamble (2 bytes) - Device ID (4 bytes) - daytona data (2 bytes) -
// Status (1 byte) - CRC (1 byte) - Junk (1 byte)
uchar Tx_Byte[nb_tx_byte];
uchar Current_Tx_Bit;
uchar Next_Tx_Bit;
uchar current_tx_byte;
/* hardware variables for Daytona */
// variables for Daytona Serial and SAR routines
uchar T2; // temporary variables
uchar T3; //
uchar T4; //
uchar T5; //
uchar SAR; //
uchar STATUS; //
uchar Tx_count; // Measure pressure every 3 sec, tx every 54 sec.
uchar Max_Pres; // Holds the maximum pressure
uchar Min_Pres; // Holds the minimum pressure
uchar AVGCOUNT; // Averaging counter (only used with noisy L22B silicon)
uchar TEMPERATURE; // converted temperature
uchar PRESSURE; // converted pressure
uchar COUNTER; // counter to count every 3 or 6 secs
uchar Random_Number; // Pseudo-random number (from timer counter low register)
void init_ports(void);
void init_icg(void);
void init_timer(void);
void init_kbi(void);
void init_sys(void);
void tango_read_temperature_mode(void);
void tango_read_pressure_mode(void);
void tango_read_data_out(void);
void tango_idle(void);
void asleep_tx(void);
void Daytona_NoTx_Read(void);
void Daytona_Tx_Read(void); // Read temperature and pressure data.
void Check_battery(void);
void Fast_Data_Mode(void);
void Normal_Data_Mode(void);
void Transmit_Prepare(void);
void Transmit_Frame(void); // Process and transmit the data.
void Transmit_Message(void);
void Random_delay(void); // Add a random delay (~0 - 100 ms).
void tempo(uint ch);
void delay_200_ms(void);
void delay_800_ms(void); // Wait for about 0.8 seconds.
void make_frame(void);
uchar ad_converted(void);
void send_byte(uchar ch);
void toggle_clk(void);
/*********************************************************/
/* 函數:interrupt void Tx_UHF_ISR(void); */
/* 功能:中斷程序,將數據按曼徹斯特碼發射出去 */
/*********************************************************/
/* Tx_UHF_ISR */
/* Description : it decides what to do on timer overflow */
/* if current = next, then toggle on timer overflow,*/
/* else no toggle */
/* In : Next_Tx_bit, Current_Tx_Bit */
/* Out : TSC0, nb_1 */
/* local variables : */
/*********************************************************/
interrupt void Tx_UHF_ISR(void){
asm LDA TSC0 // reenable next input capture isr by reading tsc0
TSC0_CH0F = 0; // clearing input capture flag
if(!(Next_Tx_Bit==Current_Tx_Bit)){
TSC0_TOV0 = 0; //No Tovf
}
else{
TSC0_TOV0 = 1; // Tovf
}
}
/*********************************************************/
/* 函數:__interrupt void Cpu_Interrupt(void) */
/* 功能:中斷程序,執行空操作 */
/*********************************************************/
interrupt void KB_entry(void){
tango_Enable = 1; // replace the contents in <emis1.asm>
INTKBSCR_IMASKK = 1; // Disable keyboard Interrupts.
Daytona_NoTx_Read(); // Read pressure data (not include temperature)
Tx_count++;
if(Tx_count==1){
Transmit_Prepare();
}
init_kbi();
init_ports();
tango_idle();
asm stop
}
/*********************************************************/
/* 函數:void main(void); */
/*********************************************************/
void main(void) {
DisableInterrupts; /* disable interrupts */
/* include your code here */
//CONFIG = 0x32; // LVISTOP disabled!!!
// CONFIG[0]=COPD=0:ENABLE WATCHDOG!
// timeout period = 2**18-2**4 CGMXCLK cycles
CONFIG = 0x33; // WATCHDOG disabled!!!
init_sys(); // Initialize Module, clear counters/variables.
tango_idle(); // Place Daytona in IDLE mode and
// asm stop // wait here for falling edge at DOUT.
// when using sensor.
//*** for test program
for(;;) {
PTA_PTA6 = ~PTA_PTA6; //FOR TEST ok!
Transmit_Frame();
} // loop forever /
//please make sure that you never leave this function /
}
/**************************************************************/
/* LVI status check routine */
/* V0.1 17 July, 2002 Jeff Burgess */
/* Called with: jsr SEND_BYTE */
/* Returns: STATUS[7,6] containing the data */
/* from LVISR[7,5] respectively. */
/* (c) 2002 Motorola */
/**************************************************************/
void Check_battery(void){
asm LDA LVISR
T3 = LVISR;
if(0!=(T3&LOWV)){ //LOWV is bit 5 0010-0000
//STATUS.S_LOWV = 1;
}
if(0!=(T3&LVIOUT)){
//STATUS.S_LVIOUT = 1; //LVIOUT is bit 7 1000-0000
}
}
/**************************************************************/
/* 函數:void Daytona_NoTx_Read(void); */
/* 功能:沒到發射時間只采樣壓力值 */
/* 須更新最大最小壓力值:Max_Pres 和 Min_Pres */
/**************************************************************/
void Daytona_NoTx_Read(void){
tango_read_pressure_mode(); //Sample pressure
tempo(wait_0_5ms);
tango_read_data_out();
PRESSURE = ad_converted();
tango_idle();
if(PRESSURE>Max_Pres){
Max_Pres = PRESSURE;
}
else if(PRESSURE<Min_Pres){
Min_Pres = PRESSURE;
}
else {
}
}
/**************************************************************/
/* 函數:void Daytona_Tx_Read(void); */
/* 功能:已到發射時間,采樣壓力和溫度值 */
/**************************************************************/
void Daytona_Tx_Read(void){
tango_read_temperature_mode();
tempo(wait_0_2ms);
tango_read_data_out();
TEMPERATURE = ad_converted();
tango_read_pressure_mode();
tempo(wait_0_5ms);
PRESSURE = ad_converted();
tango_idle();
}
/**************************************************************/
/* 函數:uchar ad_converted(void); */
/* 功能:A/D轉換程序,用以采集MPXY8020傳感器的溫度、壓力數據 */
/**************************************************************/
uchar ad_converted(void){
uchar i;
uchar data=0;
uchar ad_data;
uchar data_buff;
SAR = 0;
T2 = 0x80;
for(i=1;i<8;i++){
data_buff = T2|SAR;
send_byte(data_buff);
if(1==DOUT_BAR) { //TOO LOW
SAR |=T2;
}
else{
T2 = T2>>1;
}
}
ad_data = SAR;
return ad_data;
}
void send_byte(uchar ch){
uchar data;
uchar mask = 0x80;
uchar i; //bit count
data = ch;
DCLK = 0;
for(i=8;i>0;i--){
if(1==data&mask){
SDATA = 1;
toggle_clk();
}
else{
SDATA = 0;
toggle_clk();
}
}
}
void toggle_clk(void){
DCLK = 1;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -