本帖最后由 流火七月 于 2017-2-27 17:10 编辑
主控芯片:LPC1778
串口:usart2
485芯片:max13487e
问题:与其他硬件控制板通信,同时上电启动,在USART2初始化之时,会死在NVIC_EnableIRQ(irq_temp);这一步
irq_temp参数相关
typedef enum{
COM0,
COM1,
COM2,
COM3,
COM4,
}ALL_CH;
IRQn_Type irq_temp = UART0_IRQn;
ch = COM2;
irq_temp = UART0_IRQn + ch;
以下为初始化完整代码
/*****************************************************************************
函 数 名 : UART_init
功能描述 : 串口 初始化
输入参数 : unsigned long sysClk
unsigned long baudrate
入参clk:UART模块工作的系统频率
入参baudrate:UART工作的波特率
*****************************************************************************/
void UART_init(unsigned long sysClk, unsigned long baudrate, uint8 ch)//可用
{
uint16_t divisor;
LPC_UART_TypeDef *regaddr = NULL;
LPC_UART1_TypeDef *regaddr1 = NULL;
LPC_UART4_TypeDef *regaddr4 = NULL;
IRQn_Type irq_temp = UART0_IRQn;
switch(ch)
{
case 0:
LPC_IOCON->P0_2 &= 0xFFFFFFF8;
LPC_IOCON->P0_2 |= 0x00000001; /* Pin P0.2 used as TXD0 (Com0) */
LPC_IOCON->P0_3 &= 0xFFFFFFF8;
LPC_IOCON->P0_3 |= 0x00000001; /* Pin P0.3 used as RXD0 (Com0) */
regaddr = LPC_UART0;
break;
case 1:
LPC_IOCON->P2_0 &= 0xFFFFFFF8;
LPC_IOCON->P2_0 |= 0x00000002; /* Pin P2.0 used as TXD1 (UART1) */
LPC_IOCON->P2_1 &= 0xFFFFFFF8;
LPC_IOCON->P2_1 |= 0x00000002; /* Pin P2.1 used as RXD1 (UART1) */
regaddr1 = LPC_UART1;
break;
case 2:
LPC_SC->PCONP |= ((uint32_t)(1<<24));
#if 0//开发板使用引脚
LPC_IOCON->P0_10 &= 0xFFFFFFF8;
LPC_IOCON->P0_10 |= 0x00000001; /* Pin P0.10 used as TXD2 (Com0) */
LPC_IOCON->P0_11 &= 0xFFFFFFF8;
LPC_IOCON->P0_11 |= 0x00000001; /* Pin P0.11 used as RXD2 (Com0) */
#else//监控板使用引脚
LPC_IOCON->P2_8 &= 0xFFFFFFF8;
LPC_IOCON->P2_8 |= 0x00000002; /* Pin P2.8 used as TXD2 (Com0) */
LPC_IOCON->P2_9 &= 0xFFFFFFF8;
LPC_IOCON->P2_9 |= 0x00000002; /* Pin P2.9 used as RXD2 (Com0) */
#endif
regaddr = LPC_UART2;
break;
case 3:
LPC_SC->PCONP |= ((uint32_t)(1<<25));
LPC_IOCON->P0_2 &= 0xFFFFFFF8;
LPC_IOCON->P0_2 |= 0x00000002; /* Pin P0.2 used as TXD0 (Com0) */
LPC_IOCON->P0_3 &= 0xFFFFFFF8;
LPC_IOCON->P0_3 |= 0x00000002; /* Pin P0.3 used as RXD0 (Com0) */
regaddr = LPC_UART3;
break;
case 4:
LPC_SC->PCONP |= ((uint32_t)(1<<8));//复位时被禁用
LPC_IOCON->P5_4 &= 0xFFFFFFF8;
LPC_IOCON->P5_4 |= 0x00000004; /* Pin P5.4 used as TXD0 (Com0) */
LPC_IOCON->P5_3 &= 0xFFFFFFF8;
LPC_IOCON->P5_3 |= 0x00000004; /* Pin P5.3 used as RXD0 (Com0) */
regaddr4 = LPC_UART4;
break;
default:
break;
}
g_uartTxBufferHead[ch] = 0;
g_uartTxBufferTail[ch] = 0;
divisor = uartGetDivisorByBaudrate(sysClk, baudrate);
irq_temp = UART0_IRQn + ch;
if( ch == COM1)//023和1和4的结构体是不一样的
{
regaddr1->LCR = 0x83;//允许设置波特率
regaddr1->DLM = ((divisor >> 8) & 0x00FF);
regaddr1->DLL = (divisor & 0x00FF);
regaddr1->LCR = 0x03; //锁定波特率
regaddr1->FCR = 0x87;//RX FIFO在收到1个字节时产生中断
regaddr1->IER = IER_RBR | IER_THRE | IER_RLS;//配置中断
}
else if( ch == COM4)
{
regaddr4->LCR = 0x83;
regaddr4->DLM = ((divisor >> 8) & 0x00FF);
regaddr4->DLL = (divisor & 0x00FF);
regaddr4->LCR = 0x03;
regaddr4->FCR = 0x87;
regaddr4->IER = IER_RBR | IER_THRE | IER_RLS;
irq_temp = UART4_IRQn;
}
else
{
regaddr->LCR = 0x83;
regaddr->DLM = ((divisor >> 8) & 0x00FF);
regaddr->DLL = (divisor & 0x00FF);
regaddr->LCR = 0x03;
regaddr->FCR = 0x87;
regaddr->IER = IER_RBR | IER_THRE | IER_RLS;
}
if(irq_temp != 0)
{
My_print("%d
",irq_temp);
}
NVIC_EnableIRQ(irq_temp);//串口2初始化在这一步死机
irq_temp = 0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
抱歉抱歉 最近在做两个项目 脑子混了 这是另外一个项目的 不是stm32的
一周热门 更多>