/***********************************
功能:编码器接口测试程序
MCU:dsPIC30F4011
TOOLS:MPLAB X IDE V3.15 + XC16 V1.25
震荡器:外部4M晶振
芯艺设计室 http://www.chipart.cn
2015-12-08
***********************************/
#include "xc.h"
#include <stdint.h>
#include <stdio.h>
#define XTFREQ 4000000 // xtal = 4Mhz;
#define PLLMODE 16 // PLLx16
#define FCY XTFREQ*PLLMODE/4 // Instruction Cycle Frequency
#include <libpic30.h>
#define UART_BAUD 9600
#define LED_PORT_INIT TRISBbits.TRISB6=0
#define LED_ON LATBbits.LATB6=1
#define LED_OFF LATBbits.LATB6=0
#define LED_FLASH LATBbits.LATB6^=1
_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
#define DelayMs __delay_ms
void Uart1Init(void)
{
U1MODE=0x0400; //格式:N,8,1,无校验 使用备用引脚RC13,RC14
U1BRG = ((FCY/16)/UART_BAUD)-1; //波特率设置
U1STAbits.UTXISEL = 1; //发送过程中发送缓冲器空时产生中断
U1STAbits.URXISEL = 0; //接收一个字节后中断
//IEC0bits.U1RXIE = 1; //接收中断允许
//IEC0bits.U1TXIE = 1; //发送中断允许
U1MODEbits.UARTEN = 1; //发送允许
U1STAbits.UTXEN = 1; //使能
}
void QEIInit(void)
{
ADPCFG|=0x0030; //??adc??????IO??
MAXCNT=1000;//???,???
POSCNT=10;//???
DFLTCONbits.QECK=2;//?????
DFLTCONbits.QEOUT=1;//???????
DFLTCONbits.CEID=1;//??????
QEICONbits.QEIM=5; //2X MAXCNT????
}
int main( void )
{
LED_PORT_INIT;
Uart1Init();
QEIInit();
while(1)
{
DelayMs(1000);
LED_FLASH;
printf("%d ",POSCNT);
}
return 0;
}
|
|