📄 m8.c
字号:
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.6 Standard
Automatic Program Generator
?Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:office@hpinfotech.com
Chip type : ATmega8
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
//*************************************
//内部文件
#include <mega8.h>
//常数定义
#define cst_sbw_ax 0 //开关按下标志
#define cst_sbw_fs 1 //开关放开标志
#define cst_sbw_k 1 //机械开关标志
#define cst_sbw_sb 0 //轻解开关标志
#define cst_first_work 0xAA //EEPROM开机标志
#define cst_out_j12_true 0x55 //输出换向标志
#define cst_out_j12_false 0x00 //输出不换向标志
#define cst_out_free 0 //无输出
#define cst_out_open 1 //输出J1
#define cst_out_close 2 //输出J2
#define cst_out_ip1 3 //输出IP1
#define cst_out_ip2 4 //输出IP2
#define cst_window_have 0 //通道存在标志
#define cst_window_null 1 //通道不存在标志
#define cst_rif_learn 0x80 //通道设置指令
#define cst_rif_open 0x01 //开指令
#define cst_rif_stop 0x02 //停机指令
#define cst_rif_close 0x03 //关指令
#define cst_rif_sel_open 0x10 //select+open
#define cst_rif_sel_stop 0x11 //select+stop
#define cst_rif_sel_close 0x12 //select+close
#define cst_rif_st_open 0x13 //stop+open
#define cst_rif_st_close 0x14 //stop+close
#define cst_tcnt0 0x3C //0x9F// //1M 3C--8M
#define cst_tccr0 0x05 //0x04// //05
#define cst_tcnt0_32us 0x00 //0xE0// //00
#define cst_tccr0_32us 0x01 //0x01// //05
#define cst_tcnt1h 0x85 //0xF0// //85
#define cst_tcnt1l 0xEE //0xBE// //EE
#define cst_tccr1b 0x04 //0x04// //04
#define cst_tcnt2 0x00 //0x00// //00
#define cst_tccr2 0x07 //0x05// //07
#define cst_xc_sum 64464
#define cst_xc_open 65000
#define cst_xc_close 536
#define cst_xc_currently 32500
#define cst_xc_yx 60
#define cst_run_min 3
//在线汇编
#define WDR() #asm("wdr")
#define SEI() #asm("sei")
#define CLI() #asm("cli")
#define NOP() #asm("nop")
//#define SPM() #asm("spm")
//#define LPM() #asm("lpm")
//位操作
#define set_bit(x,y) x|=1<<y //写1置1
#define clr_bit(x,y) x&=(~(1<<y)) //写0清0,用于中断标志以外的位操作
#define clr_int(x,y) x&=(1<<y) //置1清0,用于中断标志清除
#define com_bit(x,y) x^=(1<<y) //位取反
#define get_0(x,y) (!(x&(1<<y))) //读取位0
#define get_1(x,y) (x&(1<<y)) //读取位1
//====================================
//IO寄存器位定义
#define timer0_ovf_true get_1(TIFR,TOV0)
#define timer0_ovf_false get_0(TIFR,TOV0)
#define clr_timer0_ovf clr_int(TIFR,TOV0)
#define timer1_ovf_true get_1(TIFR,TOV1)
#define timer1_ovf_false get_0(TIFR,TOV1)
#define clr_timer1_ovf clr_int(TIFR,TOV1)
#define timer2_ovf_true get_1(TIFR,TOV2)
#define timer2_ovf_false get_0(TIFR,TOV2)
#define clr_timer2_ovf clr_int(TIFR,TOV2)
//====================================
//IO口输入输出定义
//指示灯LED_R
#define led_r 5
#define led_r_i PINC
#define led_r_p PORTC
#define led_r_d DDRC
#define set_led_r set_bit(led_r_p,led_r)
#define clr_led_r clr_bit(led_r_p,led_r)
#define com_led_r com_bit(led_r_p,led_r)
//电机上的开关按键
#define sbp_open 5
#define sbp_open_i PINB
#define sbp_open_p PORTB
#define sbp_open_d DDRB
#define sbp_open_ax (get_0(sbp_open_i,sbp_open) && get_1(sbp_close_i,sbp_close))
#define sbp_open_fs get_1(sbp_open_i,sbp_open)
#define sbp_close 1
#define sbp_close_i PINC
#define sbp_close_p PORTC
#define sbp_close_d DDRC
#define sbp_close_ax (get_0(sbp_close_i,sbp_close) && get_1(sbp_open_i,sbp_open))
#define sbp_close_fs get_1(sbp_close_i,sbp_close)
//行程设置SB_XC
#define sb_xc_ax (get_0(sbp_open_i,sbp_open) && get_0(sbp_close_i,sbp_close))
#define sb_xc_fs (get_1(sbp_open_i,sbp_open) && get_1(sbp_close_i,sbp_close))
#define sb_fx_ax (get_0(sbp_open_i,sbp_open) && get_0(sbp_close_i,sbp_close))
#define sb_fx_fs (get_1(sbp_open_i,sbp_open) && get_1(sbp_close_i,sbp_close))
//通道设置SB_LEARN
#define sb_learn 0
#define sb_learn_i PINC
#define sb_learn_p PORTC
#define sb_learn_d DDRC
#define sb_learn_ax get_0(sb_learn_i,sb_learn)
#define sb_learn_fs get_1(sb_learn_i,sb_learn)
//外置通道设置SBW_LEARN
#define sbw_learn 4
#define sbw_learn_i PINC
#define sbw_learn_p PORTC
#define sbw_learn_d DDRC
#define sbw_learn_ax get_0(sbw_learn_i,sbw_learn)
#define sbw_learn_fs get_1(sbw_learn_i,sbw_learn)
//通道设置SB_LEARN OR SBW_LEARN
#define sb_w_learn_ax (sb_learn_ax || sbw_learn_ax)
#define sb_w_learn_fs (sb_learn_fs && sbw_learn_fs)
//外置开按键SBW_OPEN
#define sbw_open 2
#define sbw_open_i PINC
#define sbw_open_p PORTC
#define sbw_open_d DDRC
#define sbw_open_ax get_0(sbw_open_i,sbw_open)
#define sbw_open_fs get_1(sbw_open_i,sbw_open)
//外置关按键SBW_CLOSE
#define sbw_close 3
#define sbw_close_i PINC
#define sbw_close_p PORTC
#define sbw_close_d DDRC
#define sbw_close_ax get_0(sbw_close_i,sbw_close)
#define sbw_close_fs get_1(sbw_close_i,sbw_close)
//外置停止操作SBW_OPEN+SBW_CLOSE
#define sbw_stop_ax (sbw_open_ax&&sbw_close_ax)
#define sbw_stop_fs (sbw_open_fs&&sbw_close_fs)
//无线信号输入RIF_INT
#define rif_int 7
#define rif_int_i PIND
#define rif_int_p PORTD
#define rif_int_d DDRD
#define rif_int_ax get_1(rif_int_i,rif_int)
#define rif_int_fs get_0(rif_int_i,rif_int)
//电源掉电监控POWER_WATCH
//低电平中断,最高中断级别INT0
#define power_watch 2
#define power_watch_i PIND
#define power_watch_p PORTD
#define power_watch_d DDRD
#define power_watch_ax get_0(power_watch_i,power_watch)
#define power_watch_fs get_1(power_watch_i,power_watch)
//电机活动监控MOT_WATCH
//变化中断,第二中断级别INT1
//MOT_WATCH发生中断时,如果:
//MOT_WATCH:MOT_COM==01,则表明电机正转,行程加1(open)
//MOT_WATCH:MOT_COM==11,则表明电机反转,行程减1(close)
//MOT_WATCH:MOT_COM==10或00无效状态,不用处理
#define mot_watch 3
#define mot_watch_i PIND
#define mot_watch_p PORTD
#define mot_watch_d DDRD
#define mot_watch_ax get_0(mot_watch_i,mot_watch)
#define mot_watch_fs get_1(mot_watch_i,mot_watch)
//电机活动方向比较MOT_COM
#define mot_com 4
#define mot_com_i PIND
#define mot_com_p PORTD
#define mot_com_d DDRD
#define mot_com_ax get_0(mot_com_i,mot_com)
#define mot_com_fs get_1(mot_com_i,mot_com)
//电机活动方向
//顺时钟方向(01)(dea)==deasil
//逆时钟方向(11)(ant)==anticlockwise
#define mot_inc (mot_watch_ax&&mot_com_fs)
#define mot_dec (mot_watch_fs&&mot_com_fs)
//电机控制输出J1
#define j1 5
#define j1_i PIND
#define j1_p PORTD
#define j1_d DDRD
#define j1_out set_bit(j1_p,j1)
#define j1_close clr_bit(j1_p,j1)
#define j1_out_true get_1(j1_p,j1)
#define j1_out_false get_0(j1_p,j1)
#define j1_i_true get_1(j1_i,j1)
#define j1_i_false get_0(j1_i,j1)
//电机控制输出J2
#define j2 6
#define j2_i PIND
#define j2_p PORTD
#define j2_d DDRD
#define j2_out set_bit(j2_p,j2)
#define j2_close clr_bit(j2_p,j2)
#define j2_out_true get_1(j2_p,j2)
#define j2_out_false get_0(j2_p,j2)
#define j2_i_true get_1(j2_i,j2)
#define j2_i_false get_0(j2_i,j2)
//*************************************
// Declare your global variables here
// 全局变量
//内存变量
unsigned char com_timer0; //通用定时器25ms*255
unsigned char com_timer1; //通用定时器1s*255
unsigned char com_timer2; //通用定时器25ms*255
unsigned char sbw_ax_timer1; //测试按键按下时间,大于7秒为机械开关
unsigned int xc_currently; //当前行程
unsigned int xc_end_cp; //行程目的地
unsigned char xc_work_old; //正在执行或已经执行完成的任务
unsigned int eep_xc_sum_sram;
unsigned int eep_xc_open_sram;
unsigned int eep_xc_close_sram;
unsigned int eep_xc_ip1_sram;
unsigned int eep_xc_ip2_sram;
//
unsigned char ip_timer; //记录设置IP指令
//位变量
bit bit_sbw_open; //SBW_OPEN状态标志,按下未放开为1,反之为0
bit bit_sbw_close; //SBW_CLOSE状态标志,按下未放开为1,反之为0
bit bit_sbw_sort; //外控开关类别标志,机械式=1,轻触式=0
bit bit_xc_man; //人工定位标志
bit bit_xc_change; //行程变化标志
bit bit_xc_tsend; //行程设置等待标志
bit __bit_sbp_open; //sbp_open state
bit __bit_sbp_close; //sbp_close state
//EEPROM变量
eeprom unsigned char eep_empty[16]; //第一行不要
eeprom unsigned char eep_sbw_sort; //保存开关类型
eeprom unsigned char eep_out_j12; //保存输出主向标志
eeprom unsigned char eep_first_work; //第一次开机标志
eeprom unsigned char eep_window_ptr; //保存通道位置
eeprom unsigned char eep_window[16][3]; //保存十六个通道号
eeprom unsigned int eep_xc_sum; //保存总行程
eeprom unsigned int eep_xc_open; //保存开极限值
eeprom unsigned int eep_xc_close; //保存关极限值
eeprom unsigned int eep_xc_ip1; //保存IP1位置
eeprom unsigned int eep_xc_ip2; //保存IP2位置
eeprom unsigned char eep_xc_ptr; //保存当前保存当前行程的位置的地址
eeprom unsigned int eep_xc_currently[200]; //保存当前行程值
//*************************************
//外部变量声明
//*************************************
//函数声明
//unsigned char subpr_rif(void);
void subpr_yms_com(unsigned char);
void subpr_ys_com(unsigned char);
void subpr_yms2_com(unsigned char);
void subpr_led_wink(unsigned char,unsigned char);
void subpr_rif_ok(void);
void subpr_sbw_sort_test(void);
void subpr_sbw_scan(void);
void subpr_mot_wink(void);
//*************************************
//************外部函数*****************
//*************************************
//******************函数*******************
/**************************************
外部文件:rif_re.c
函数:unsigned char subpr_rif(void)
全局变量:
unsigned char rif_val[5];
//rif_val[0]=addr0
//rif_val[1]=addr1
//rif_val[2]=addr2
//rif_val[3]=work
//rif_val[4]=add_check
返回值:
return(0); //表示接收正确
return(1); //表示接收错误
**************************************/
#include "rif_re.c"
//*************************************
//***********中断处理函数**************
//*************************************
//===================中断====================
// External Interrupt 0 service routine
// 掉电中断,低电平中断
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
//关闭INT0中断,开总中断
clr_bit(GICR,INT0);
NOP();
clr_int(GIFR,INTF0);
NOP();
//重置T0溢出中断,保证中断正常
TIMSK=0x45;
clr_timer0_ovf;
NOP();
SEI();
clr_led_r;
subpr_yms2_com(1);
if (power_watch_ax)
{
if (j1_out_true||j2_out_true)
{
j1_close;
j2_close;
xc_work_old=cst_out_free;
//subpr_ys_com(2);
subpr_yms_com(10);
}
if ((eep_xc_sum_sram!=0)&&(bit_xc_change==1))
{
//保存当前位置
CLI();
if (eep_xc_currently[eep_xc_ptr]!=xc_currently)
{
eep_tst:
eep_xc_currently[eep_xc_ptr]=xc_currently;
if (eep_xc_currently[eep_xc_ptr]!=xc_currently)
{
//所写单元已坏,换位置
eep_xc_ptr++;
if (eep_xc_ptr>=200)
{
eep_xc_ptr=0;
}
//eep_xc_currently[eep_xc_ptr]=xc_currently;
goto eep_tst;
}
}
}
//等待来电
SEI();
//
while (power_watch_ax)
{
subpr_led_wink(1,0);
//com_led_r;
WDR();
}
clr_led_r;
}
CLI();
NOP();
set_bit(GICR,INT0);
NOP();
clr_int(GIFR,INTF0);
NOP();
}
//===================中断====================
// External Interrupt 1 service routine
// 运行中断,任意变化中断
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
if (mot_inc)
{
//清除行程控制定时器
com_timer2=0;
xc_currently++;
if (xc_currently>=eep_xc_open_sram)
{
j1_close;
j2_close;
bit_xc_tsend=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -