📄 main.c
字号:
/*
ATMega16 电机测速控制 程序
main.c
硬件:Mega16
时钟:16MHz
陈昊 2006-04-24
*/
#include <avr/io.h>
#include <avr/interrupt.h>
//#include <avr/signal.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include "main.h"
#include "function.h"
//#include "progbcd.h"
volatile uchar temp_pinc;
volatile uint detect_val; //motor_speed_detect_value
volatile uint setting_val=60; //setting_compare_motor_speed_value
volatile uint last_detect_val=0x0000;
volatile uchar d_num1; //测量值bcd 的个位
volatile uchar d_num2; //测量值bcd 的十位
volatile uchar d_num3; //测量值bcd 的百位
volatile uchar d_num4; //测量值bcd 的千位
volatile uchar s_num1; //设定值bcd 的个位
volatile uchar s_num2; //设定值bcd 的十位
volatile uchar s_num3; //设定值bcd 的百位
volatile uchar s_num4; //设定值bcd 的千位
volatile uint clock1ms; //中断中的1ms计数器
volatile uchar clock10ms;
volatile uchar clock100ms;
volatile uchar clock1s;
volatile uchar cnt10_1ms=9;
volatile uchar cnt10_10ms=9;
volatile uchar cnt10_100ms=9;
volatile uint cnt_int=0; //外部中断
volatile uint t_plus_1=0;
volatile uint t_plus_2=0;
volatile uint t_plus_start=0;
volatile uchar step_setting=0;
volatile uchar cunt_flash=0;
volatile uchar t_detect=0;
volatile uchar step_detect=0;
volatile uint t_between_pluses=0;
volatile uchar step_delay_s=0;
volatile uchar t_delay;
volatile uchar key_buff;
volatile uchar key_data;
volatile uchar t_display; //数码管的时间暂时ram
volatile uchar temp_time_display;
volatile uchar num_display; //位选数值
volatile uchar step_display; //显示的step
volatile uchar marka=0; //标志寄存器
/******************************************************************************/
SIGNAL (SIG_OVERFLOW0) //1ms中断一次
{
sei();
TCNT0 = 0xD1;
clock1ms++;
if (cnt10_1ms>0)
{
cnt10_1ms--;
}
else
{
cnt10_1ms=9;
clock10ms++;
//----------------------------------------------------------------------------
/* if (bit_is_clear(PIND,0)) //测试
{
PORTD|=_BV(0);
}
else
{
PORTD&=~_BV(0);
}
*/
//----------------------------------------------------------------------------
if ( PINC == key_buff)
{
temp_pinc=key_buff; //同一键值
}
else
{
key_buff=PINC; //不同键值,读入
}
if (temp_pinc == 0xff) //没键按下
{
marka|=_BV(f_key); //置位标志marka的第3位
}
else
{
key_data=temp_pinc; //有效键按下,读入键值
marka&=~_BV(f_key); //清有按键标志位f_key
}
//----------------------------------------------------------------------------
if (cnt10_10ms>0)
{
cnt10_10ms--;
}
else
{
cnt10_10ms=9;
clock100ms++;
//----------------------------------------------------------------------------
if (cnt10_100ms>0)
{
cnt10_100ms--;
}
else
{
cnt10_100ms=9;
clock1s++;
}
}
}
}
//*******************************************************************
SIGNAL(SIG_INTERRUPT0)
{
// switch (cnt_int) //按中断次数判断跳转
// {
// case 0 : //第一个有效脉冲
// t_plus_start=clock1ms;
// cnt_int++;
// break;
// default : //非第一个有效脉冲,则计算间隔,与0。5S比较
cnt_int++;
t_between_pluses = clock1ms - t_plus_start;
if (t_between_pluses > 500) //大于0。5S,则置位标志,进行计算显示
{
marka|=_BV(f_detect);
}
// break;
// }
}
//----------------------------------------------------------------------------
//SIGNAL(SIG_INTERRUPT0)
//{
// switch (cnt_int) //按中断次数判断跳转
// {
// case 0 : //第一个有效脉冲
// t_plus_start=clock1ms;
// cnt_int++;
// break;
// default : //非第一个有效脉冲,则计算间隔,与0。5S比较
// cnt_int++;
// t_between_pluses = clock1ms - t_plus_start;
// if (t_between_pluses > 500) //大于0。5S,则置位标志,进行计算显示
// {
// marka|=_BV(f_detect);
// }
// break;
// }
//}
/******************************************************************************/
int main(void)
{
init_devices( );
//----------------------------------------------------------------------------
cli();
eeprom_busy_wait();
setting_val=eeprom_read_word(0); //从EEPROM 0 地址处读取一字节赋给RAM 变量val
if (setting_val == 0XFFFF)
{
setting_val=60;
}
sei();
//----------------------------------------------------------------------------
num_display=0; //ram initial
step_display=0;
marka|=_BV(f_star); //define 开机标志位
//----------------------------------------------------------------------------
while(1)
{
motor_speed_detect();
wordtobcd();
display();
if (bit_is_set(marka,f_key))
{
key_select();
key_data=0xff;
}
key_ring();
if (bit_is_set(marka,f_star))
{
jd_delay ();
}
else
{
jd_compare ();
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -