📄 main.c.bak
字号:
#include <iom128v.h>
#include <macros.h>
#include "stdio.h"
#include "../api/schedule.h"
#include "../api/cc1100.h"
#include "../api/led.h"
#include "../api/timer.h"
#include "../api/comm.h"
#define LEN_SEND_MSG 12
#define SYNC_INTERVAL 50 //实际间隔为50ms 因为发送过程有30ms的延时
#define CLUSTER_NUMBER 2
#define NODE_NUMBER 2
COMM_MSG_CMD SYNC_MSG, RADIO_ACK_MSG;
unsigned char SyncSeqNo;
unsigned char ask_seqno;
unsigned char unreceive_timer[CLUSTER_NUMBER]= {0,0};
unsigned char receive_flag[CLUSTER_NUMBER]= {0,0};
unsigned int crc;
static unsigned char Data[28]; //server-sink串口数据
typedef struct NodeData{
unsigned char ID;
unsigned char temp;
unsigned char vol;
}NodeData;
NodeData *Node[CLUSTER_NUMBER][NODE_NUMBER];
//unsigned char len_COMM_MSG = (sizeof(COMM_MSG))-1;
/**************************************************
* port init
*
* cc1100
* IN G1 o
* GD0O D1 i
* CSN C2 o
* SCLK G0 o
* OUT C1 i
*
* led
* led0 C4 o
* led1 C5 o
* led2 C6 o
************************************************** */
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x74;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x03;
}
/**************************************************
//UART0 initialize
// desired baud rate: 57600
// actual: baud rate:57600 (0.0%)
// char size: 8 bit
// parity: Disabled
**************************************************/
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x07; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0xD8;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init(); //端口初始化
led_init(); //所有灯灭
T0_init();
T2_init();
T1_init();
T3_init();
CC1100_init(); //RF初始化
uart0_init(); //串口初始化
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/******************************************************
* crc校验
*
*******************************************************/
unsigned short int xcrc_byte(unsigned short intcrc, unsigned char b)
{
unsigned char i;
crc = crc ^ b << 8;
i = 8;
do{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}while (--i);
return crc;
}
unsigned int xcrc_calc(char *packet, int index, int count)
{
int crc = 0;
while (count > 0)
{
crc = xcrc_byte(crc, packet[index++]);
count--;
}
return crc;
}
/*
* T0计时中断
*/
void T0_timeout()
{
SyncSeqNo++;
SYNC_MSG.data[0] = SyncSeqNo;
CC1100_send(((char *)&SYNC_MSG),LEN_SEND_MSG+1);
start_T0(0,SYNC_INTERVAL);
if (SyncSeqNo>=2)
stop_T0();
}
void T1_timeout()
{
unsigned char i, j;
for(i=1; i<=CLUSTER_NUMBER; i++)
{
if(receive_flag[i]==1)
{
unreceive_timer[i] = 0;
receive_flag[i] =0;
}
else
unreceive_timer[i]++;
if(unreceive_timer[i]>=3)
{
/* for(j=0; j<NODE_NUMBER; j++)
{
Node[i-1][j]->temp = 0;
Node[i-1][j]->vol = 0;
}
*/
//发送一个故障报告
}
}
}
void T2_timeout()
{
led0Toggle();
}
void T3_timeout()
{
led1Toggle();
}
void CC1100_sendDone()
{
//led0Off();
return;
}
/*
* ACK sink
*/
void radio_ack_task()
{
RADIO_ACK_MSG.len = LEN_SEND_MSG;
RADIO_ACK_MSG.ID = 0x00;
RADIO_ACK_MSG.GID = 0x00;
RADIO_ACK_MSG.type = CLUSTER_ACK;
// RADIO_ACK_MSG->crc = xcrc_calc(RADIO_ACK_MSG, 2,17);
CC1100_send((((char *)&RADIO_ACK_MSG)),LEN_SEND_MSG+1);
}
extern unsigned char rxBuffer[32];
void CC1100_receive()
{
COMM_MSG *receive_msg;
unsigned char cluster;
unsigned short int target;
unsigned char *msg = (&rxBuffer[0]);
//printf("recv\n");
receive_msg = (COMM_MSG *)msg;
cluster = receive_msg->GID;
target = cluster;
target = ((target<<8) & 0xff00);
unreceive_timer[cluster-1] = 0;
receive_flag[cluster-1]= 1;
switch(receive_msg->type)
{
int i;
case CLUSTER_DATA:
{
/*for(i=0;i<NODE_NUMBER;i++) //NODE_NUMBER=2
{
Node[cluster-1][i]->ID = receive_msg->data[(i*3)+0];
Node[cluster-1][i]->temp = receive_msg->data[(i*3)+1];
Node[cluster-1][i]->vol = receive_msg->data[(i*3)+2];
// //
*/
Data[0]=29;
Data[1]=70;
Data[2]=receive_msg->GID;
Data[3]=receive_msg->data[1];
Data[4]=receive_msg->data[2];
Data[5]=receive_msg->data[3];
Data[6]=receive_msg->data[4];
Data[7]=receive_msg->GID;
Data[8]=receive_msg->data[5];
Data[9]=receive_msg->data[6];
Data[10]=receive_msg->data[7];
Data[11]=receive_msg->data[8];
Data[12]=receive_msg->GID;
Data[13]=receive_msg->data[9];
Data[14]=receive_msg->data[10];
Data[15]=receive_msg->data[11];
Data[16]=receive_msg->data[12];
Data[17]=receive_msg->GID;
Data[18]=receive_msg->data[13];
Data[19]=receive_msg->data[14];
Data[20]=receive_msg->data[15];
Data[21]=receive_msg->data[16];
Data[22]=receive_msg->GID;
Data[23]=receive_msg->data[17];
Data[24]=receive_msg->data[18];
Data[25]=receive_msg->data[19];
Data[26]=receive_msg->data[20];
Data[27]=0;//crc receive_msg->GID;
Data[28]=0;//crc receive_msg->data[5];
printf("ko\n");
//serial_put(Data,Data[0]);
RADIO_ACK_MSG.DA = target;
radio_ack_task();
break;
}
case INIT_NODE_PARAMEATER_ACK: //初始化节点响应
{
Data[0]=6;
Data[1]=INIT_NODE_PARAMEATER_ACK;
Data[2]=receive_msg->GID;
Data[3]=receive_msg->data[0];
Data[4]=0;//crc
Data[5]=0;//crc
serial_put(Data,Data[0]);
break;
}
default: break;
} //end of switch
}
int uart_receive_CMD(Uart_CMD *command)
{
Uart_server_MSG *ASK_MSG;
COMM_MSG *INIT_NODE_MSG;
unsigned char CMD_type;
unsigned short int msg_crc;
unsigned char RTC_TIMER0,RTC_TIMER1;
CMD_type = command->type;
switch(CMD_type)
{
case SINK_SERVER_SYNC: //更改RTC时间
{
RTC_TIMER0 = command->data[0];
RTC_TIMER1 = command->data[1];
break;
}
case SERVER_RESET : //同步
{
unsigned sync_seqno = 0;
SYNC_MSG.len = LEN_SEND_MSG;
SYNC_MSG.ID = 0;
SYNC_MSG.GID = 0;
SYNC_MSG.DA = 0xffff;
SYNC_MSG.type = SINK_CLUSTER_SYNC;
SYNC_MSG.data[0] = sync_seqno;
//SYNC_MSG->crc = xcrc_calc(SYNC_MSG, 0, 18);
CC1100_send( (char *)&SYNC_MSG, LEN_SEND_MSG+1);
start_T0(0,SYNC_INTERVAL);
led0Toggle();
break;
}
/*
case SERVER_ASK_SINKDATA: //要数据
{
int n=0;
ASK_MSG->startbyte = 0x7e;
ASK_MSG->len = 28;
ASK_MSG->type = SINKDATA;
ASK_MSG->data = CURRENT_DATA;
for(n=0; n<=23; n++)
{
ASK_MSG->data[n] = 0;
}
ASK_MSG->CRC = xcrc_calc(ASK_MSG, 1, 26);
serial_put(ASK_MSG);
break;
}
*/
case SERVER_INIT_NODE: //初始化节点参数
{
int n=0; // 是否需要切换通道?
SYNC_MSG.len = LEN_SEND_MSG;
SYNC_MSG.ID = 0;
SYNC_MSG.GID = 0;
SYNC_MSG.DA = 0x0000;
SYNC_MSG.type = SINK_INIT_NODE;
for(n=0; n <= 5; n++) //5个字节的长度
{
SYNC_MSG.data[n] = command->data[n];
}
//SYNC_MSG->data[0] = command->data[0] ;
//SYNC_MSG->crc = xcrc_calc(SYNC_MSG, 0, 18);
CC1100_send((unsigned char *)&SYNC_MSG, LEN_SEND_MSG+1);
break;
}
default: break;
}
return 1;
}
extern unsigned char put_done;
void main()
{
init_devices();
put_done =1; //使能串口输出
sched_init();
CC1100_setInterrupt(0x01); //RF接收状态
// start_T0(0,2000);
// start_T1(1,12000);
/*my program*/
//led0Toggle();
//start_T1(1,8000);
//start_T3(1,1000);
//start_T0(1,1000);
//start_T2(1,1000);
//CC1100_send(a,4);
//puts("hello\n");
/*my program end*/
/*schedule*/
while(1)
{
run_task();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -