📄 appli.c
字号:
/****************************************************************************/
/* */
/* Copyright (c) 2005, 老树工作室 */
/* All rights reserved. */
/* */
/* Email:laoshu0902@163.com */
/* www.laoshu0902.bokee.com */
/* */
/****************************************************************************/
/****************************************************************************/
/* 文件名:comapp.c */
/* 版 本:Version 1.0 */
/* 描 述:实现计算机和单片机的通讯协议,根据计算机发给的命令进行相应操作 */
/* 作 者:spot */
/* 函 数: */
/* clear_timer */
/* com_command_receive */
/* com_send_command */
/* main_message_process */
/* message_loop */
/* msg_first_push */
/* msg_last_push */
/* send_command */
/* set_timer */
/* system_init */
/* ext1_int_proc */
/* geT_LUMINity */
/* time0_over_int */
/* com_int_proc */
/* command_decoder */
/* */
/* 历史记录: */
/* spot 2005-07-10 Creat Inital version. (Version 1.0) */
/****************************************************************************/
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include "../includes/com.h"
#include "../includes/sd2k3drv.h"
#include "../includes/lcd.h"
#include "../includes/hc595drv.h"
#include "keyboard.h"
u_char calc_lumin(u_int fhz); /* 计算当前光强 */
extern void write_iic_data(u_char write_data[], u_char address, u_char num); /* 向iic设备写入N个字节 */
extern void read_iic_data(u_char read_data[], u_char address, u_char num); /* 从iic设备读取N个字节 */
extern void dac_5615(u_int data_in);/* 写入DAC的10bit输入数据 */
extern u_int adc_1549(void); /* 读取模拟量输入 */
extern u_char get_temperature(void);/* 读取温度子程序 */
extern void key_setmenu_lcd(void); /* 键盘扫描 */
extern u_char key_code;
ALARM_DATA_INFO alarm_limit; /* 报警阈值结构变量 */
sbit SWITCH_IN = P3^2; /* 开关量输入信号 */
sbit CTRL_RELAY = P3^4; /* 控制继电器开关 */
sbit WDI = P0^5; /* 喂狗信号 */
/* 定时器变量 */
bit timer_int; /* 定时到标志 */
u_char idata timer_sign[MAX_TNUM]; /* 1:已定时 */
u_char idata timer_type[MAX_TNUM]; /* 1:循环定时 0:一般 */
u_char idata timer_len[MAX_TNUM]; /* 定时时长 单位: 2ms */
u_char idata timer_count[MAX_TNUM]; /* 当前定时计数 */
u_char idata timer_id[MAX_TNUM]; /* 定时标识号 */
u_char idata timer_map[MAX_TNUM]; /* 定时映射 */
u_char timer_number; /* 定时器数目 */
u_char idata msg_type[MAX_MSGNUM]; /* 消息类型 */
u_char idata msg_value[MAX_MSGNUM]; /* 消息数值 */
u_char msg_number; /* 消息总数 */
u_char msg_nowtype; /* 当前消息类型 */
u_char msg_nowvalue; /* 当前消息数值 */
u_char msg_readp; /* 消息队列读指针 */
u_char msg_writep; /* 消息队列写指针 */
u_char idata prec_buf[MAX_COMMAND_LEN];/* 命令接收缓冲区 */
u_char prec_num; /* 命令接收缓冲区字节数 */
u_char idata pint_buf[MAX_RINTL]; /* 串口缓冲区 */
u_char pint_read; /* 串口缓冲区读指针 */
u_char pint_write; /* 串口缓冲区写指针 */
bit psend_int; /* 串口发送允许标志 */
u_int int_times;
u_char serial_flag = 0; /* 串口接收数据标志位 */
u_char serial_lengthl = 0; /* 消息命令长度低8位 */
u_int serial_length = 0; /* 消息命令长度16位 */
u_char send_buf[15]; /* 消息发送缓冲区 */
extern u_char date[7]; /* 实时时钟数组 */
u_char watchdog_date[7]; /* WATCHDOG数据 */
u_int analog_in_data; /* 当前模拟量输入 */
u_int analog_out_data; /* 当前模拟量输出 */
u_char cur_temp; /* 当前温度数值 */
u_char cur_lumin; /* 当前光强数值 */
bit lcd_flag = 1; /* 液晶显示页面标志*/
u_int fhz_tlc555; /* tlc555输出频率 */
bit tsample_flag = 0; /* 采样定时标志1:启动,0:停止 */
u_char ADDRESS[2]; /* byte0:通讯组地址, byte1:开发板地址 */
/* 外部中断1中断服务程序,使用第2组寄存器 */
void ext1_int_proc(void) interrupt 2 using 2
{
fhz_tlc555++;
}
/* 计算当前光强 */
u_char calc_lumin(u_int fhz)
{
u_char lumin = 0;
if (fhz <= 2000)/* 光强为0 */
{
return 0;
}
else if ( (fhz > 2000) && (fhz <= 4770) )/* 光强为0~10 */
{
lumin = fhz / 277 - 7.2;
return lumin;
}
else if ( (fhz > 4770) && (fhz <= 5400) )/* 光强为10~20 */
{
lumin = fhz / 63 - 65.7;
return lumin;
}
else if ( (fhz > 5400) && (fhz <= 8200) )/* 光强为20~30 */
{
lumin = fhz / 280 + 0.7;
return lumin;
}
else if ( (fhz > 8200) && (fhz <= 9600) )/* 光强为30~40 */
{
lumin = fhz / 140 - 28.6;
return lumin;
}
else if ( (fhz > 9600) && (fhz <= 10100) )/* 光强为40~50 */
{
lumin = fhz / 50 - 152;
return lumin;
}
else if ( (fhz > 10100) && (fhz <= 12100) )/* 光强为50~60 */
{
lumin = fhz / 200;
return lumin;
}
else if ( (fhz > 12100) && (fhz <= 12900) )/* 光强为60~100 */
{
lumin = fhz / 20 - 545;
return lumin;
}
}
/* 定时器0中断服务程序 ,使用第1组寄存器 */
void time0_over_int(void) interrupt 1 using 1
{
TF0=0;
timer_int=1;
TH0 = -(46080 / 256);
TL0 = -(46080 % 256);
}
/* 串口中断服务程序,使用第3组寄存器 */
void com_int_proc(void) interrupt 4 using 3
{
u_char temp;
u_char temp1;
if (TI == 1)
{
TI = 0;
psend_int = 1; /* 可以发送 */
}
if (RI == 1)
{
RI = 0; /* 清串口接收中断 */
temp1 = SBUF;
temp = pint_write + 1; /* 判断是否可以写入 */
if (temp == MAX_RINTL)
{
temp=0;
}
if (temp != pint_read)
{
pint_buf[pint_write] = temp1; /* 读取数据 */
pint_write = temp;
}
}
}
/* 确认正在定时的定时器映射关系 */
void count_timer_map(void)
{
u_char loop;
u_char temp;
timer_number = 0;
for (loop=0; loop<MAX_TNUM; loop++)
{
if (timer_sign[loop] != 0)
{
temp = timer_number;
timer_map[temp] = loop;
timer_number++;
}
}
}
/* 设置定时参数 */
void set_timer(u_int time_len, u_char type, u_char id)
{
u_char loop;
for (loop=0; loop<MAX_TNUM; loop++)
{
if (timer_sign[loop] == 0)
{
timer_count[loop] = 0; /* 定时计数 */
timer_sign[loop] = 1; /* 定时标志 */
timer_type[loop] = type; /* 定时类型 0:一般 1:循环 */
timer_len[loop] = time_len; /* 时长 10ms 的倍数 */
timer_id[loop] = id; /* 定时器id */
count_timer_map();
return;
}
}
}
/* 清除指定定时器 */
void clear_timer(u_char id)
{
u_char loop;
for (loop=0; loop<MAX_TNUM; loop++)
{
if ( (timer_sign[loop] == 1) && (timer_id[loop] == id) )
{
timer_sign[loop] = 0;
timer_count[loop] = 0; /*定时计数*/
}
}
count_timer_map();
}
/* 命令解码子程序 */
void command_decoder(void)
{
u_char i = 0;
if (prec_buf[4] == 0x31) /* 设置报警阈值 */
{
alarm_limit.temp_uthreshold = prec_buf[5]; /* 超低温度 */
alarm_limit.temp_othreshold = prec_buf[6]; /* 超高温度 */
alarm_limit.lumin_uthreshold = prec_buf[7]; /* 超低光强 */
alarm_limit.lumin_othreshold = prec_buf[8]; /* 超高光强 */
return;
}
else if (prec_buf[4] == 0x32) /* 请求报警阈值 */
{
msg_last_push(MSG_GET_LIMIT,0);
return;
}
else if (prec_buf[4] == 0x33) /* 修改当前时间 */
{
for (i=0; i<7; i++)
{
date[i] = prec_buf[i+5];
}
sd2300_write_date(); /* 写入时间数据 */
return;
}
else if (prec_buf[4] == 0x34) /* 请求当前时间 */
{
sd2300_read_date();
msg_last_push(MSG_READ_DATE,0);
return;
}
else if (prec_buf[4] == 0x35) /* 请求当前数据 */
{
msg_last_push(MSG_CUR_DATA,0);
return;
}
else if (prec_buf[4] == 0x36) /* 请求看门狗信息*/
{
msg_last_push(MSG_WATCHDOG,0);
return;
}
else if (prec_buf[4] == 0x37) /* 请求报警情况 */
{
msg_last_push(MSG_ALARM_STATE,0);
return;
}
else if (prec_buf[4] == 0x38) /* 配置设备地址 */
{
ADDRESS[0] = prec_buf[5]; /* 通讯组地址 */
ADDRESS[1] = prec_buf[6]; /* 开发板地址 */
write_iic_data(ADDRESS, 0, 2);/* 将地址信息写入EEPROM */
return;
}
else if (prec_buf[4] == 0x39) /* 请求设备地址 */
{
msg_last_push(MSG_GET_ADDRESS,0);
return;
}
else if (prec_buf[4] == 0x3a) /* 控制模拟量输出 */
{
analog_out_data |= prec_buf[6];
analog_out_data <<= 8;
analog_out_data |= prec_buf[5];
dac_5615(analog_out_data); /* 输出模拟量 */
return;
}
else if (prec_buf[4] == 0x3b) /* 控制开关量输出 */
{
if (prec_buf[5] == 0)
CTRL_RELAY = 0; /* 继电器关 */
else
CTRL_RELAY = 1; /* 继电器开 */
return;
}
}
/* 该子程序可根据需要修改 搜索系统消息 */
void message_loop(void)
{
u_char i = 0;
u_char j = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -