📄 function.c
字号:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
//#include <avr/signal.h>
#include <avr/eeprom.h>
#include "main.h"
#include "function.h"
#include "progbcd.h"
extern volatile uint detect_val; //motor_speed_detect_value
extern volatile uint setting_val; //setting_compare_motor_speed_value
extern volatile uint last_detect_val;
extern volatile uchar d_num1; //测量值bcd 的个位
extern volatile uchar d_num2; //测量值bcd 的十位
extern volatile uchar d_num3;
extern volatile uchar d_num4;
extern volatile uchar s_num1; //设定值bcd 的个位
extern volatile uchar s_num2; //设定值bcd 的十位
extern volatile uchar s_num3;
extern volatile uchar s_num4;
extern volatile uint clock1ms; //中断中的1ms计数器
extern volatile uchar clock10ms;
extern volatile uchar clock100ms;
extern volatile uchar clock1s;
extern volatile uchar cnt10_1ms;
extern volatile uchar cnt10_10ms;
extern volatile uchar cnt10_100ms;
extern volatile uint cnt_int;
extern volatile uint t_plus_1;
extern volatile uint t_plus_2;
extern volatile uint t_plus_start;
extern volatile uchar step_setting;
extern volatile uchar cunt_flash;
extern volatile uchar t_detect;
extern volatile uchar step_detect;
extern volatile uint t_between_pluses;
extern volatile uchar step_delay_s;
extern volatile uchar t_delay;
extern volatile uchar key_buff;
extern volatile uchar key_data;
extern volatile uchar t_display; //数码管的时间暂时ram
extern volatile uchar temp_time_display;
extern volatile uchar num_display; //位选数值
extern volatile uchar step_display; //显示的step
extern volatile uchar marka; //标志寄存器
//----------------------------------------------------------------------------
void port_init(void)
{
//全部使能内部上拉电阻,减少耗电
PORTA = 0xFF; //PA口,输出,段选
DDRA = 0xFF;
PORTB = 0xFF; //PB口,输出,位选
DDRB = 0xFF;
PORTC = 0xFF; //PC口,输入,上拉,接按键
DDRC = 0x00;
PORTD = 0x3D; //00111101,PD7 PD6-输出低电平,PD5 PD4-输出高电平
DDRD = 0xF3; //11110011,PD3 PD2-中断输入上拉,PD1-扬声器输出低
}
//----------------------------------------------------------------------------
void timer0_init(void)
{
TCCR0 = 0x00;//停止定时器
TCNT0 = 0xD1;//初始值
OCR0 = 0xD0;//匹配值
TIMSK |= 0x01;//中断允许
TCCR0 = 0x04;//启动定时器
}
//----------------------------------------------------------------------------
void int_init(void)
{
MCUCR |= 0x03; //上升沿触发,外部中断0
MCUCSR|= 0x80; //关JTAG功能
GICR |= 0x40; //使能外部中断INT0;
}
//----------------------------------------------------------------------------
void init_devices(void)
{
cli(); //禁止所有中断
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
timer0_init();
int_init();
sei();//开全局中断
}
//----------------------------------------------------------------------------
void motor_speed_detect(void)
{
if (bit_is_set(marka,f_detect))
{
last_detect_val = detect_val; //whaul add
detect_val = cnt_int * 60000 / t_between_pluses;
marka&=~_BV(f_detect);
cnt_int = 0;
t_plus_start = clock1ms; //whaul add
}
else
{
uint temp_t_detect;
temp_t_detect = clock1ms - t_plus_start; //;;;;;;;;;;;;;;;;此时的clock1ms非中断产生时的clock1ms
if (temp_t_detect >1500)
{
detect_val = 0;
cnt_int=0;
}
}
if(detect_val <= last_detect_val) //whaul add
{
marka|=_BV(f_less); //whaul add
}
else
{
marka&=~_BV(f_less);
}
/* uint temp_t_detect;
t_between_pluses = t_plus_1 - t_plus_2;
detect_val = 60000/t_between_pluses;
temp_t_detect = clock1ms - t_plus_1;
if (temp_t_detect >2000)
{
detect_val = 0;
cnt_int=0;
}
*/
/* uchar temp_t_detect;
switch (step_detect)
{
case 0 :
step_detect=1;
t_detect=clock1s;
break;
case 1 :
temp_t_detect=clock1s - t_detect;
if ( temp_t_detect > 3)
{
last_detect_val=detect_val;
detect_val = cnt_int*15;
cnt_int = 0;
step_detect = 0;
}
break;
}
*/
}
//----------------------------------------------------------------------------
void wordtobcd(void)
{
uint number1,number2;
number1=detect_val;
number2=setting_val;
d_num4=number1/1000;
d_num3=(number1-d_num4*1000)/100;
d_num2=(number1-d_num4*1000-d_num3*100)/10;
d_num1=(number1-d_num4*1000-d_num3*100-d_num2*10);
if (detect_val<1000)
{
d_num4=10;
}
if (detect_val<100)
{
d_num3=10;
}
/* if (detect_val<10)
{
d_num2=10;
}
*/
s_num4=number2/1000;
s_num3=(number2-s_num4*1000)/100;
s_num2=(number2-s_num4*1000-s_num3*100)/10;
s_num1=(number2-s_num4*1000-s_num3*100-s_num2*10);
if (bit_is_clear(marka,f_set))
{
if (setting_val<1000)
{
s_num4=10;
}
if (setting_val<100)
{
s_num3=10;
}
}
}
//----------------------------------------------------------------------------
void display(void)
{
uchar i;
switch(step_display)
{
case 0 :
t_display = clock1ms;
step_display++;
break;
case 1 :
temp_time_display= clock1ms-t_display;
if( temp_time_display > 1 )
step_display++;
break;
case 2:
switch(num_display)
{
case 0 :
i=d_num1;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b11111110;
num_display++;
step_display=0;
break;
case 1 :
i=d_num2;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b11111101;
num_display++;
step_display=0;
break;
case 2 :
i=d_num3;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b11111011;
num_display++;
step_display=0;
break;
case 3 :
i=d_num4;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b11110111;
num_display++;
step_display=0;
break;
case 4 :
i=s_num3;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b11101111;
num_display++;
step_display=0;
if (bit_is_set(marka,f_set))
{
if (step_setting==2)
{
cunt_flash++;
if (cunt_flash < 5)
PORTA=0xff;
if (cunt_flash > 10)
cunt_flash = 0;
}
}
break;
case 5 :
i=s_num4;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b11011111;
num_display++;
step_display=0;
if (bit_is_set(marka,f_set))
{
if (step_setting==3)
{
cunt_flash++;
if (cunt_flash < 5)
PORTA=0xff;
if (cunt_flash > 10)
cunt_flash = 0;
}
}
break;
case 6 :
i=s_num1;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b10111111;
num_display++;
step_display=0;
if (bit_is_set(marka,f_set))
{
if (step_setting==0)
{
cunt_flash++;
if (cunt_flash < 5)
PORTA=0xff;
if (cunt_flash > 10)
cunt_flash = 0;
}
}
break;
case 7 :
i=s_num2;
PORTB = pgm_read_byte(prog_bcd + i);
PORTA = 0b01111111;
num_display=0;
step_display=0;
if (bit_is_set(marka,f_set))
{
if (step_setting==1)
{
cunt_flash++;
if (cunt_flash < 5)
PORTA=0xff;
if (cunt_flash > 10)
cunt_flash = 0;
}
}
break;
}
break;
}
}
//----------------------------------------------------------------------------
void key_select(void)
{
switch (key_data)
{
case 0b11101111 : //设定键
marka|=_BV(f_set);
// marka|=_BV(f_star);
// PORTD&=~_BV(io1);
// PORTD&=~_BV(io2);
// PORTD|=_BV(io3);
// PORTD|=_BV(io4);
break;
case 0b11110111 : //左移键
if (bit_is_set(marka,f_set))
{
step_setting++;
if (step_setting > 3)
{
step_setting=0;
}
}
break;
case 0b11111101 : //上移键
if (bit_is_set(marka,f_set))
{
if (step_setting==0)
setting_val=setting_val+1;
if (step_setting==1)
setting_val=setting_val+10;
if (step_setting==2)
setting_val=setting_val+100;
if (step_setting==3)
setting_val=setting_val+1000;
if (setting_val>9000)
setting_val=9000;
}
break;
case 0b11111011 : //下移键
if (bit_is_set(marka,f_set))
{
if (step_setting==0)
setting_val=setting_val-1;
if (step_setting==1)
setting_val=setting_val-10;
if (step_setting==2)
setting_val=setting_val-100;
if (step_setting==3)
setting_val=setting_val-1000;
if ( (setting_val>9000) && (setting_val<12000) )
setting_val=9000;
if (setting_val<60)
setting_val=60;
if ( setting_val>12000 )
setting_val=60;
}
break;
case 0b11111110 : //确定键
marka|=_BV(f_star); //whaul 按确定后延时5秒钟,开始检测,确定/复位两用
PORTD&=~_BV(io1);
PORTD&=~_BV(io2);
PORTD|=_BV(io3);
PORTD|=_BV(io4);
if (bit_is_set(marka,f_set))
{
marka&=~_BV(f_set);
cli();
eeprom_busy_wait();
eeprom_write_word(0,setting_val); //从EEPROM 0 地址处写入setting_val;
sei();
}
break;
default :
break;
}
}
//----------------------------------------------------------------------------
void key_ring (void)
{
if (PINC == 0xFF)
{
PORTD&=~_BV(1);
}
else
{
PORTD|=_BV(1);
}
}
//----------------------------------------------------------------------------
void jd_delay ( void )
{
uchar temp_t_delay;
switch (step_delay_s)
{
case 0 :
t_delay=clock1s;
step_delay_s++;
break;
case 1 :
temp_t_delay=clock1s-t_delay;
if (temp_t_delay > 3)
{
marka&=~_BV(f_star);
step_delay_s=0;
}
break;
}
}
//----------------------------------------------------------------------------
//void jd_compare (void)
//{
// if ( (detect_val > setting_val) || (detect_val > last_detect_val) )
// {
// //PORTD&=~_BV(1);
// PORTD&=~_BV(io1);
// PORTD&=~_BV(io2);
// PORTD|=_BV(io3);
// PORTD|=_BV(io4);
// }
// else
// {
// PORTD|=_BV(1);
// PORTD|=_BV(io1);
// PORTD|=_BV(io2);
// PORTD&=~_BV(io3);
// PORTD&=~_BV(io4);
// }
//}
//*****************************************************************
void jd_compare (void)
{
// if (bit_is_clear(marka,f_set)) //whaul add,非设置状态,运行比较
// {
if ((detect_val < setting_val) && bit_is_set(marka,f_less))
{
// PORTD|=_BV(1);
PORTD|=_BV(io1);
PORTD|=_BV(io2);
PORTD&=~_BV(io3);
PORTD&=~_BV(io4);
}
else
{
//PORTD&=~_BV(1);
PORTD&=~_BV(io1);
PORTD&=~_BV(io2);
PORTD|=_BV(io3);
PORTD|=_BV(io4);
}
// last_detect_val = detect_val; //whaul add
// }
// else //设置状态时,闪烁并复位输出,整机处于调试状态
// {
//PORTD&=~_BV(1);
// PORTD&=~_BV(io1);
// PORTD&=~_BV(io2);
// PORTD|=_BV(io3);
// PORTD|=_BV(io4);
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -