📄 mpasub.c
字号:
/****************************************************
*---------------------------------------------------*
* 功能描述: 初始化、基本函数、中断等 *
*---------------------------------------------------*/
#include "mpa101.h"
extern U16 times_500ms;
extern U16 DREG[MAXDREG];
extern volatile S32 count;
extern volatile U32 count1;
extern volatile U32 indistance;
volatile U8 bit_a1,bit_a2,bit_b1,bit_b2;
/*------------------------------------------*/
/* Function : system_init */
/* Description: 初始化,IO设定中断设定 */
/*------------------------------------------*/
void system_init(void)
{
UARTMODE uart0_set;
SCS = 0x01; // 端口0选择快速GPIO
FIO0MASK = 0x00008073; // 0000 0000 0000 0000 1000 0000 0111 0011
// FIO0DIR = 0xfe007a88; // 1111 1110 0000 0000 0111 1010 1000 1000
FIO0DIR = 0xfe007b80; // 1111 1110 0000 0000 0111 1011 1000 0000
// 1514 1312 1110 0908 0706 0504 0302 0100
// PINSEL0=0x480015a5; // 0100 1000 0000 0000 0001 0101 1010 0101;
PINSEL0=0x40001505; // 0100 0000 0000 0000 0001 0101 0000 0101;
// 3130 2928 2726 2524 2322 2120 1918 1716
PINSEL1=0x00000001; // 0000 0000 0000 0000 0000 0000 0000 0000;
IO0DIR =0xfe007b88; // 1111 1110 0000 0000 0111 1010 1000 1000 0:IN 1:OUT
// IO0DIR =0x00000000;
//FIO0SET = Y3|Y4|Y5|Y6|Y7|Y8;
FIO0CLR = Y3|Y4|Y5|Y6|Y7|Y8;
xinit();
T3PR = 0; // 设置定时器分频为100分频,得160000Hz
T3MCR = 0x03; // 匹配通道0匹配中断并复位T0TC
T3MR0 = 320; // 比较值 16000k/100/160=1ms
T3TCR = 0x03; // 启动并复位T0TC
T3TCR = 0x01;
// 设置串口参数
uart0_set.datab = 7; // 7位数据长(8位数据长)
uart0_set.stopb = 1; // 1位停止
uart0_set.parity = 2; // 偶校验(无校验)
UART0_Ini(9600,uart0_set);
U0IER = 0x01; // 使能接收中断
FM25X_Init(2000000);
VICIntSelect = 0x00; // 所有中断通道设置为IRQ中断
// 设置定时器2中断IRQ
VICVectCntl2 = 0x20|26; // 定时器0中断通道分配最高优先级
VICVectAddr2 = (int)IRQ_Timer2; // 设置中断服务程序地址向量
// 设置定时器0中断IRQ
VICVectCntl3 = 0x20|4; // 定时器1中断通道分配优先级
VICVectAddr3 = (int)IRQ_Timer0; // 设置中断服务程序地址向量
// 设置定时器1中断IRQ
VICVectCntl4 = 0x20|5; // 定时器1中断通道分配优先级
VICVectAddr4 = (int)IRQ_Timer1; // 设置中断服务程序地址向量
// 设置串口中断IRQ
VICVectCntl5 = 0x20|6;
VICVectAddr5 = (int)IRQ_UART0;
// 设置定时器3中断IRQ
VICVectCntl0 = 0x20|27; // 定时器0中断通道分配最高优先级 0x20+ch
VICVectAddr0 = (int)IRQ_Timer3; // 设置中断服务程序地址向量
// 设置外部输入0中断IRQ
EXTMODE = 0x01; // 边缘触发
EXTPOLAR = 0x01; // 上升沿有效
VICVectCntl1 = 0x20|14; // 外部中断输入2中断通道分配优先级
VICVectAddr1 = (int)IRQ_Eint0; // 设置中断服务程序地址向量
EXTINT = 0x01;
/* // 设置SPI0中断IRQ
S0SPCR = 0x90; // SPI从模式
VICVectCntl2 = 0x20|10; // SPI0通道分配到IRQ slot 0,即优先级最高
VICVectAddr2 = (int)SPI0_Handler; // 设置SPI0中断向量地址
// 设置定时器0中断IRQ
VICVectCntl3 = 0x20|4; // 定时器0中断通道分配最高优先级 0x20+ch
VICVectAddr3 = (int)Timer0_Handler; // 设置中断服务程序地址向量
// 设置外部输入2中断IRQ
EXTMODE |= 0x04; // 边缘触发
EXTPOLAR = 0x00; // 下降沿有效
VICVectCntl4 = 0x20|16; // 外部中断输入2中断通道分配优先级
VICVectAddr4 = (int)Eint2_Handler; // 设置中断服务程序地址向量
EXTINT |= 0x04;
// 使能中断 EINT2->TIMER1->TIMER1
*/
VICIntEnable = (1<<4)|(1<<5)|(1<<6)|(1<<26)|(1<<27)|(1<<14);
IRQEnable(); // 打开IRQ中断
}
//------------------------------------------//
// Function : IRQ_Timer3 //
// Description: 中断 1ms //
//------------------------------------------//
void __irq IRQ_Timer3(void)
{
U32 bak;
bak = VICIntEnable; // 备份当前VICIntEnable的值
VICIntEnClr = (1<<27)|(1<<14)|(1<<4)|(1<<5)|(1<<26)|(1<<6); // 禁止当前优先级中断及低优先级中断
VICVectAddr = 0x00; // 清除中断逻辑,以便VIC可以响应更高优先级IRQ中断
bit_a1 = bit_a2;
bit_b1 = bit_b2;
bit_a2 = (U8)((FIO0PIN&IN1) >> 10);
bit_b2 = (U8)((FIO0PIN&IN10) >> 24);
if((bit_a1^bit_a2)||(bit_b1^bit_b2)){
if((bit_a1^bit_b2)&(~(bit_a2^bit_b1))){
count--;
}else if((bit_a2^bit_b1)&(~(bit_a1^bit_b2))){
count++;
}
count1 = abs(count);
if(WF6){
if(count1>=indistance){Y_Stop();CLRF6();}
}
}
//count1++;
T3IR = 0x01; // 打开MR2中断
VICIntEnable = bak;
}
/*------------------------------------------*/
/* Function : IRQ_Eint0 */
/* Description: 外部中断处理 */
/*------------------------------------------*/
void __irq IRQ_Eint0(void)
{
uint32 bak;
bak = VICIntEnable;
VICIntEnClr = (1<<14)|(1<<4)|(1<<5)|(1<<26)|(1<<6);;
VICVectAddr = 0x00;
if(WF4){
CLRF4();
Y_Stop();
}else{
SETF5();
}
EXTINT = 0x01;
VICIntEnable = bak;
}
/*------------------------------------------*/
/* Function : IRQ_Eint2 */
/* Description: 外部中断处理 */
/*------------------------------------------*/
/*void _IRQ_Eint2(void)
{
uint32 bak;
bak = VICIntEnable; // 备份当前VICIntEnable的值
VICIntEnClr = (1<<4)|(1<<5)|(1<<16); // 禁止当前优先级中断及低优先级中断
VICVectAddr = 0x00; // 清除中断逻辑,以便VIC可以响应更高优先级IRQ中断
// if(!save_time) save_flag = 1;
EXTINT = 0x04;
VICIntEnable = bak;
}*/
/*------------------------------------------*/
/* Function : DelayMs */
/* Description: 延时 */
/*------------------------------------------*/
void DelayMs(U32 dly)
{
U32 i;
for (; dly>0; dly--){
SendRS();
if(FIO0PIN&WDI) FIO0CLR = WDI; else FIO0SET = WDI;
for (i=0; i<6410; i++);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -