📄 minid0712.c
字号:
/*****************************************************************
* 文件名: main.c
* 版本号:
* 创作日期: 2005.7.12
* 作者: fengzm
* 功能说明: 主函数
* 其它说明: 使用AVR单片机Atmega48, 晶振:8Mhz
*****************************************************************/
/*****************************************************************
* 修改日期:
* 修改人:
* 修改原因:
*******************************************************************/
#include "minid.h"
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00; //0000 0000
PORTC = 0x00;
DDRC = 0x0C; //0000 1100
PORTD = 0x00;
DDRD = 0x61; //0110 0001
}
//Watchdog initialize
//prescale: 2K,16ms
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCSR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 9.984mSec (0.2%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0xB2; //set count
TCCR0A = 0x00;
TCCR0B = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 0xB2; //TIMER0 has overflowed
//****************当前转速值*****************
g_Temp = g_DeltaEnc * 125;
g_Temp /=1000;
g_Temp *= 72;
g_Temp = 60000000 / g_Temp;
//****************当前转速值******************
g_AdcNum = 7; //脚踏板通道
sPID.SetValue = AdcPedal(g_ValueAdc) & 0xfff; //PID输入预设值
g_IicData = PIDCalc(&sPID, g_Temp); //PID运算
g_flag10ms = 0xff;
//**********************************
timer1_ovf_isr_();
//***********************************
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
//watchdog_init();
timer0_init();
timer1_init();
twi_init();
adc_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x21; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00;
SEI(); //re-enable interrupts
}
void main(void)
{
init_devices();
PIDInit (&sPID);
START_ADC();
SumDu = 0; //增量式PID清零
while (1)
{
//if (g_flag10ms == 0xff)
//{
// g_flag10ms = 0;
// STARTIIC(); //开始发送
//}
det_err();
electromagne_ctrl(1,0);
}//End while
}
// 使用定时器的溢出中断
// 10 ms 中断 一次
// 需要提供低于220V 的信号g_singleLowVol
// 电机停机信号g_MotorStop
// 高于400V 的信号g_singleOverVol
// 电机正在运行标志g_MotorRun
// 电机速度小于10 RPM 标志g_LowSpeed
void timer1_ovf_isr_(void)
{
if (!g_MotorRun)
{
if (g_SingleLowVol)
{
g_Lower220VCnt ++;
}
else
{
g_Over220VCnt ++;
}
if (g_Lower220VCnt + g_Over220VCnt > 300)
{
if (g_Lower220VCnt > g_Over220VCnt)
{
g_LowVol = 1;
}
else
{
g_LowVol = 0;
}
g_Lower220VCnt = 0;
g_Over220VCnt = 0;
}
g_LowSpeedCnt = 0;
}
else
{
g_Lower220VCnt = 0;
g_Over220VCnt = 0;
if (g_LowSpeed)
{
g_LowSpeedCnt ++;
}
else
{
g_LowSpeedCnt = 0;
}
if (g_LowSpeedCnt > 200)
{
g_MotorBlock = 1;
g_LowSpeedCnt = 0;
}
}
if (g_SingleOverVol)
{
g_Lower400VCnt ++;
}
else
{
g_Over400VCnt ++;
}
if (g_Lower400VCnt + g_Over400VCnt > 300)
{
if (g_Lower400VCnt >= g_Over400VCnt)
{
g_OverVol = 1;
}
else
{
g_OverVol = 0;
}
g_Lower400VCnt = 0;
g_Over400VCnt = 0;
}
if (g_StartLedFlick)
{
if (g_LedTimesCnt)
{
if (g_LedTimesT1 < LEDFLICKTIME1)
{
if (g_LedType)
{
RED_ON();
}
else
{
GREEN_ON();
}
g_LedTimesT1 ++;
}
else
{
if (g_LedType)
{
RED_OFF();
}
else
{
GREEN_OFF();
}
g_LedTimesT2 ++;
if (g_LedTimesT2 == LEDFLICKTIME2)
{
g_LedTimesT2 = 0;
g_LedTimesT1 = 0;
g_LedTimesCnt--;
}
}
}
if (g_LedTimesCnt == 0)
{
g_LedTimesT3 ++;
if (g_LedTimesT3 == LEDFLICKTIME3)
{
g_LedTimesT3 = 0;
g_LedTimesCnt = g_LedTimes;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -