#include <pic18f4520.h>
#define DE RC5 //接MAX485 DE脚,为1时发送,为0时接收
static unsigned char rc_buff[4]; //接收缓存
static unsigned char tx_buff[6]; //发送缓存
static unsigned char *P; //缓存数组指针
static unsigned char rclenth; //接收字节长度
static unsigned char txlenth; //发送字节长度
static unsigned char rctime; //接收计时
static unsigned char CRC_NUM; //CRC校验字节长度
static unsigned char CRC_HIG; //CRC校验高字节
static unsigned char CRC_LOW; //CRC校验低字节
static volatile unsigned char flag @ 0x1a9; //标志位字节
static volatile bit send_last_flag @ (unsigned) &flag*8+0; //发送完成标志
void ini(void) //定时器及USART的初始化
{
INTCON=0b00000000; //GIE and PEIE disable;TO and INT enable;IntFlag cleared
PIE1=0b00100110; //TMER2中断,异步串行接收中断
PIR1=0;
PIR2=0;
PR2=249; //PR2=124;
T2CON=4; //TMR2ON=1;
SPBRG = 25; // 9600 = 4M / ( 16 * ( 52+1 ) ) HIGH
RCSTA = 0x90;
TXSTA = 0x06;
DE=0; //MAX485处于接收
CREN=1; // 允许接收
INTCON=0b11000000; //开启中断
flag=0; //清标志位
}
void portset(void) //端口设置
{
TRISA=0b00010111; //本来打算接几个LED作为接收各种数据的标志
TRISB=0b00000111;
TRISC=0b10010100;
PORTB=0;
PORTC=0b00001000;
PORTA=0;
}
void crc_check(void) //CRC校验
{
unsigned char temp;
unsigned char i;
u