📄 beacon.c
字号:
plc_frame[4] = 0xC4;
plc_frame[8] = 0xC4;
plc_frame[12] = 0xC4;
// delay for several ms before send reply
for(i=0;i<250;i++)
for(j=0;j<250;j++);
sync_sendchar(BUFFER_WR_TXBUF);
send_block(plc_frame, 18);
send();
while (TX_BUSY == 0); // wait tx finish
}
uart_sendblock(plc_frame,18);
}
RXOK = 1;
}
}
// ********* once beacon rx finish, write to flash
if (beacon_rx_frame_count == BEACON_RX_FRAME_COUNT-1)
{
disable_beacon_rx_timer();
// intialize min_ea to search
min_ea = 200;
// search for min_ea in the table, if meet the first 0, point to it
for(i=0;i<BEACON_TABLE_NM;i++)
{
if(min_ea > link_status_1[i].average_ea)
{
min_ea = link_status_1[i].average_ea;
wr_ea_index = i;
}
if(min_ea==0) break;
}
uart_sendchar(min_ea);
uart_sendchar(wr_ea_index);
// if new ea > min_ea, prepare to write into table
if(average_ea > min_ea)
{
// check whether this frame from a new address?
// if no, replace old one in table
for(i=0;i<BEACON_TABLE_NM;i++)
{
if((plc_frame[0]==link_status_1[i].address_R)&&(plc_frame[1]==link_status_1[i].address_L))
{
// set wr_ea_in
wr_ea_index = i;
}
}
// write the new frame info into table, at the position of wr_ea_index
EA = 0;
write_table(wr_ea_index, plc_frame[0], plc_frame[1], average_ea, average_eb,success_received_frame);
EA = 1;
}
// reset parameters for a new batch of
average_ea = 0;
average_eb = 0;
beacon_rx_frame_count = 0;
success_received_frame = 0;
flag_first_frame = 0; // set at 0 to indicate the first frame for next node.
}
}
}
// Beacon rx timer setup
void beacon_rx_time_out()
{
disable_beacon_rx_timer();
beacon_rx_frame_count++;
/* set up timer seconds again. */
enable_beacon_rx_timer();
}
void enable_beacon_rx_timer(void)
{
beacon_rx_timer_count = 0;
beacon_rx_timer_enable = 1;
}
void disable_beacon_rx_timer(void)
{
beacon_rx_timer_count = 0;
beacon_rx_timer_enable = 0;
}
void write_table(unsigned char i, unsigned char add_R, unsigned char add_L, unsigned char ave_ea, unsigned char ave_eb,unsigned char succ_rate)
{
// RXOK=0;
EA = 0; // disable all interrupt when writing flash
FLASH_WriteByte((int code *)&link_status_1[i].address_R, add_R);
FLASH_WriteByte((int code *)&link_status_1[i].address_L, add_L);
FLASH_WriteByte((int code *)&link_status_1[i].average_ea, ave_ea);
FLASH_WriteByte((int code *)&link_status_1[i].average_eb, ave_eb);
FLASH_WriteByte((int code *)&link_status_1[i].success_rate, succ_rate);
EA = 1; // enable all interrupt after finishing writing flash
// for debug
uart_sendchar(0xEE);
uart_sendchar(0xEE);
uart_sendchar(0xEE);
uart_sendchar(0xEE);
uart_sendchar(link_status_1[i].address_R);
uart_sendchar(link_status_1[i].address_L);
uart_sendchar(link_status_1[i].average_ea);
uart_sendchar(link_status_1[i].average_eb);
uart_sendchar(link_status_1[i].success_rate);
}
/*=============================================================================
******** Using timer1 as beacon timer *******
=============================================================================*/
void timer1_init(unsigned char interval, unsigned char count)
{
TMOD = (TMOD & 0x0F) | 0x10; /* Set Mode 2 (8 bit reload) 使用T0与串口*/
TAMOD = 0x00;
TH1 = interval; // count 185 times from 71 to 256
// = 50 us, Reload TL0 to count count_val clocks TH0 = 0x47 */
TL1 = 0; // In LPC922
inter1=TH1;
EX1 = 1; //<----------------开外部中断1
IT1 = 1; //<---------------- 外部中断下沿触发
ET1 = 1; /* Enable Timer 0 Interrupts */
TR1 = 1; /* Start Timer 0 Running */
EA = 1; /* Global Interrupt Enable */
count1_reload = count;
tm1_count = count;
// Agilent beacon timer
beacon_timer_enable = 0;
beacon_rx_timer_enable = 0;
}
/*=============================================================================*/
// Timer 1 Interrupt Service Routine.
// Interrupt is generated everytime timer1 overflows.
/*=============================================================================*/
void timer1_ISR (void) interrupt 3
{
TH1 = inter1; // reload timer1 1ms
TL1 = 0;
if (tm1_count-- == 0)
{
tm1_count = count1_reload;
// Agilent beacon tx timer
if (beacon_timer_enable == 1)
{
beacon_timer_count++;
if (beacon_timer_count == beacon_timer_val)
{
beacon_timer_count = 0;
beacon_timer_proc();
}
}
// Agilent beacon rx timer
if (beacon_rx_timer_enable == 1)
{
beacon_rx_timer_count++;
if (beacon_rx_timer_count == beacon_rx_timer_val)
{
beacon_rx_timer_count = 0;
beacon_rx_timer_proc();
}
}
}
}
/* Agilent beacon timer */
void setup_beacon_timer(unsigned char t, void (*proc)())
{
beacon_timer_val = t;
beacon_timer_proc = proc;
beacon_timer_count = 0;
beacon_timer_enable = 1;
}
/* Agilent beacon timer */
void setup_beacon_rx_timer(unsigned char t, void (*proc)())
{
beacon_rx_timer_val = t;
beacon_rx_timer_proc = proc;
beacon_rx_timer_count = 0;
beacon_rx_timer_enable = 1;
}
void send_table_frame()
{
unsigned int i,j;
// send to power line
uart_sendchar(0x77);
for(i=0;i<18;i++) uart_sendchar(plc_frame[i]);
sync_sendchar(BUFFER_WR_TXBUF);
send_block(plc_frame, 18);
send();
while(TX_BUSY == 0); // wait tx finish
RXOK = 0;
for(i=0;i<350;i++)
for(j=0;j<500;j++);
RXOK = 1;
RXOK = 0;
for(i=0;i<350;i++)
for(j=0;j<500;j++);
RXOK = 1;
}
void send_table()
{
// Totally the table will send out using (15*5)/13 = 75/13 = 6 frames.
unsigned char local_address_R, local_address_L, i, j;
local_address_R = METER_ADDR[0];
local_address_L = METER_ADDR[1];
plc_frame[0]=local_address_R;
plc_frame[1]=local_address_L;
plc_frame[2]=0x00;
plc_frame[3]=0x00;
// send first frame
plc_frame[4]=0xF0;
i = 0;
j = 4;
while(i<75)
{
if (++j>17)
{
send_table_frame();
j = 4;
plc_frame[4]++;
}
else plc_frame[j] = *(int code *)(&link_status_1[0].address_R+i++);
}
while(j ++ < 18) plc_frame[j] = 0xaa;
send_table_frame();
}
void clear_table()
{
uchar i;
// use all 00 to pad the table.
for(i=0;i<15;i++)
{
write_table(i,0,0,0,0,0);
}
}
/*===============================disable watchdog ==================================*/
void disable_watchdog()
{
EA = 0;
ACC = WDCON; //<------读取WDT控制寄存器
ACC = ACC & 0x00; //<------置位ACC.2,准备启动WDT
// WDL = 0xff; //<------设置8位倒计时初值
WDCON = ACC; //<------启动WDT
WFEED1 = 0xA5; //<------清第一部分
WFEED2 = 0x5A; //<------清第二部分
EA = 1;
}
/*=============================== send to LED ==================================*/
//
//void send_to_LED()
//{
// unsigned char send_to_LED[8];
//
// EA = 0; // disable Interrupt
// send_to_LED[0] = 0xFF;
// send_to_LED[1] = thres_ea; // thres1
// send_to_LED[2] = read_reg(0xF6); // thres2
// send_to_LED[3] = read_reg(0XEA); // ea
// send_to_LED[4] = read_reg(0XEB); // eb
// send_to_LED[5] = read_reg(0xFE); // AGC value
// send_to_LED[6] = read_reg(0xF7); // FIR
// send_to_LED[7] = 0x00; // automatic control
//
// EA = 1; // enable interrupt
//
// uart_sendblock(send_to_LED, 8);
//}
//void send_to_LED()
//{
// unsigned char send_to_LED[6];
//
// EA = 0; // disable Interrupt
// send_to_LED[0] = 0xEA;
// send_to_LED[1] = read_reg(0XEA); // ea
// send_to_LED[2] = thres_ea; // thres1
// send_to_LED[3] = read_reg(0XEB); // eb
// send_to_LED[4] = read_reg(0xF6); // thres2
// send_to_LED[5] = 0xEB;
//
// EA = 1; // enable interrupt
//
// uart_sendblock(send_to_LED, 6);
//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -