STC8外部中断0和串口0的冲突
本帖最后由 fbj 于 2024-5-30 16:53 编辑为什么只要将Exti_config();注释掉就能获得串口输出,不注释就没有。外部中断0中断函数中什么也没做,串口用的是官方的库函数,使用队列模式发送
代码如下:
#include "config.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_Switch.h"
#include "STC8G_H_Timer.h"
#include "STC8G_H_Exti.h"
#include "main.h"
/***************IO初始化函数 *****************/
u8 GPIO_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // 结构定义
// 初始化P1.0~1.7
GPIO_InitStructure.Pin = GPIO_Pin_All; // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_PullUp; // 指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
if (GPIO_Inilize(GPIO_P1, &GPIO_InitStructure))
return FAIL; // 初始化
GPIO_InitStructure.Pin = GPIO_Pin_0; // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_OUT_PP; // 指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
if (GPIO_Inilize(GPIO_P1, &GPIO_InitStructure))
return FAIL; // 初始化
// 初始化P3.0~3.7
GPIO_InitStructure.Pin = GPIO_Pin_All; // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_PullUp; // 指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
if (GPIO_Inilize(GPIO_P3, &GPIO_InitStructure))
return FAIL; // 初始化
// 初始化P5.4~5.5
GPIO_InitStructure.Pin = GPIO_Pin_5 | GPIO_Pin_4; // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
GPIO_InitStructure.Mode = GPIO_PullUp; // 指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
if (GPIO_Inilize(GPIO_P5, &GPIO_InitStructure))
return FAIL; // 初始化
return SUCCESS;
}
/***************串口初始化函数 *****************/
u8 UART_config(void)
{
COMx_InitDefine COMx_InitStructure; // 结构定义
COMx_InitStructure.UART_Mode = UART_8bit_BRTx; // 模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
COMx_InitStructure.UART_BRT_Use = BRT_Timer2;// 选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
COMx_InitStructure.UART_BaudRate = 115200ul; // 波特率, 一般 110 ~ 115200
COMx_InitStructure.UART_RxEnable = DISABLE; // 接收允许, ENABLE或DISABLE
COMx_InitStructure.BaudRateDouble = DISABLE; // 波特率加倍, ENABLE或DISABLE
if (UART_Configuration(UART1, &COMx_InitStructure))
return FAIL; // 初始化串口1 UART1,UART2,UART3,UART4
if (NVIC_UART1_Init(ENABLE, Priority_1))
return FAIL; // 中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
UART1_SW(UART1_SW_P30_P31); // UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
return SUCCESS;
}
/******************** INT配置 ********************/
void Exti_config(void)
{
EXTI_InitTypeDef Exti_InitStructure; //结构定义
Exti_InitStructure.EXTI_Mode = EXT_MODE_Fall;//中断模式, EXT_MODE_RiseFall,EXT_MODE_Fall
Ext_Inilize(EXT_INT0,&Exti_InitStructure); //初始化
NVIC_INT0_Init(ENABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
}
/**********************************************/
void main(void)
{
u16 max_time = 0;
u16 min_time = 0xffff;
EAXSFR(); /* 扩展寄存器访问使能 */
GPIO_config();
UART_config();
Exti_config();
EA = 1;
while(1)
{
printf("1");
}
}
项目里面有添加INT0中断函数吗? 乘风飞扬 发表于 2024-5-30 18:32
项目里面有添加INT0中断函数吗?
添加了,但再中断函数里什么都不做 fbj 发表于 2024-5-31 13:38
添加了,但再中断函数里什么都不做
方便的话将项目打包发出来看看 本帖最后由 fbj 于 2024-5-31 14:17 编辑
乘风飞扬 发表于 2024-5-31 13:45
方便的话将项目打包发出来看看
问题解决了,原因是我用vs来编辑的,我添加了外部中断的两个源文件但没有在keil中重新rebuild工程.不过为啥这样编译不会报错呢{:4_167:},代码上还查了半天,还是感谢你的提醒,我才去用Keil打开看,发现源文件没有添加进工程 解决了就好
页:
[1]