?? 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計數(shù)器
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; //數(shù)碼管的時間暫時ram
volatile uchar temp_time_display;
volatile uchar num_display; //位選數(shù)值
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) //按中斷次數(shù)判斷跳轉
// {
// 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) //按中斷次數(shù)判斷跳轉
// {
// 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 地址處讀取一字節(jié)賦給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 + -