📄 timer1.c
字号:
#include "timer1.h"
#include "command_ID.h"
#include <stdio.h>
#include "encoder.h"
extern unsigned char command_ID;
unsigned char tx_buffer[8];
extern unsigned int KP_coeff;
extern unsigned int KI_coeff;
extern unsigned int KD_coeff;
signed long pulse_per_5ms;
signed long last_encoder_value=0;
extern unsigned int encoder_overflow;
extern signed long encoder_value;
extern unsigned int ADCValue;
/******************************************************************************************
This function first disables the 16-bit timer interrupt and then turns off the timer module.
The Interrupt Flag bit (TxIF) is also cleared.
*******************************************************************************************/
void CloseTimer1(void)
{
_T1IE = 0; /* Disable the Timer1 interrupt */
T1CONbits.TON = 0; /* Disable timer1 */
_T1IF = 0; /* Clear Timer interrupt flag */
}
/*********************************************************************************************
This function clears the 16-bit Interrupt Flag (TxIF) bit and then sets the interrupt priority
and enables/disables the interrupt.
**********************************************************************************************/
void ConfigIntTimer1(unsigned int config)
{
_T1IF = 0; /* clear IF bit */
_T1IP = (config &0x0007); /* assigning Interrupt Priority */
_T1IE = (config &0x0008)>>3; /* Interrupt Enable /Disable */
}
/***********************************************************
This function configures the 16-bit Timer Control register
and sets the period match value into the PR register.
***********************************************************/
void OpenTimer1(unsigned int config,unsigned int period)
{
TMR1 = 0; /* Reset Timer1 to 0x0000 */
PR1 = period; /* assigning Period to Timer period register */
T1CON = config; /* Configure timer control reg */
}
/************************************************************
This function returns the contents of the 16-bit TMR register
*************************************************************/
//unsigned int ReadTimer1(void)
//{
// return (TMR1); /* Return the Timer1 register */
//}
/************************************************************
This function writes the 16-bit value into the Timer register.
*************************************************************/
//void WriteTimer1(unsigned int timer)
//{
// TMR1 = timer; /* assign timer value to Timer1 Register */
//}
void timer1_init(void)
{
unsigned int match_value;
ConfigIntTimer1(T1_INT_PRIOR_4 & T1_INT_ON);//Enable Timer1 Interrupt and Priority to "1"
//WriteTimer1(0);
TMR1=0;
match_value = 2304;//5 ms
OpenTimer1(T1_ON & T1_GATE_OFF & T1_IDLE_STOP & T1_PS_1_64 & T1_SYNC_EXT_OFF & T1_SOURCE_INT, match_value);
}
/* Interrupt Service Routine */
/* Fast context save (using push.s and pop.s) */
void __attribute__((__interrupt__, __no_auto_psv__ , __shadow__)) _T1Interrupt(void)
{
static unsigned char tx_index=8;
//speed calculation
read_encoder();
pulse_per_5ms=encoder_value-last_encoder_value;
last_encoder_value=encoder_value;
//tx routine
if (command_ID!=ID_NULL)
{
if(tx_index==8)
{
switch (command_ID)
{
case ID_KP:
sprintf(tx_buffer,"%u",KP_coeff);
tx_buffer[7]='P';
break;
case ID_KI:
sprintf(tx_buffer,"%u",KI_coeff);
tx_buffer[7]='I';
break;
case ID_KD:
sprintf(tx_buffer,"%u",KD_coeff);
tx_buffer[7]='D';
break;
case ID_VELOCITY_COMMAND:
sprintf(tx_buffer,"%ld",pulse_per_5ms);
tx_buffer[7]='V';
break;
case ID_ENCODER_COMMAND:
sprintf(tx_buffer,"%u",ENCODER_VALUE);
tx_buffer[7]='E';
break;
case ID_ENCODER_OVER_FLOW:
sprintf(tx_buffer,"%d",encoder_overflow);
tx_buffer[7]='o';
break;
case ID_CLEAR_ENCODER:
ENCODER_VALUE=ENCODER_ZERO_VALUE;
encoder_overflow=0;
encoder_value=0;
command_ID=ID_NULL;
break;
case ID_ADC:
sprintf(tx_buffer,"%d",ADCValue);
tx_buffer[7]='A';
break;
};
tx_buffer[5]=13;
tx_buffer[6]=10;
tx_index=0;
};
if(tx_buffer[tx_index]==0)tx_buffer[tx_index]=' ';
U1TXREG=tx_buffer[tx_index];
tx_index++;
};
//_LATB2= ~_LATB2;
IFS0bits.T1IF = 0;/* clear interrupt flag */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -