|
pwm duty cycle似乎自己變化。以下部分代碼,試圖實現:串口接收1時,輸出pwm,接收0時,停止輸出pwm。
有的時候正常,有的時候發送0時,不但pwm不disable,反而pwm供電的led變得更亮,再給串口發送1時,亮度降低,再發送0,才滅掉。
pwm_info_t pwm_info;
void test_uart_irqhandler(int IRQn, void *param)
{
ls1c_uart_t uartx = uart_irqn_to_uartx(IRQn);
void *uart_base = uart_get_base(uartx);
unsigned char iir = reg_read_8(uart_base + LS1C_UART_IIR_OFFSET);
unsigned char data='0';
// 判断是否为接收超时或接收到有效数据
if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
{
// 是,则读取数据,并原样发送回去
while (LSR_RXRDY & reg_read_8(uart_base + LS1C_UART_LSR_OFFSET))
{
data=reg_read_8(uart_base+LS1C_UART_DAT_OFFSET);
if (data=='0')
{
pwm_disable(&pwm_info);
}
if (data=='1')
{
pwm_enable(&pwm_info);
}
// uart_putc(uartx, reg_read_8(uart_base + LS1C_UART_DAT_OFFSET));
}
}
return ;
}
/*
* 测试串口2的收发功能是否正常
*/
void test_uart2_send_recv(void)
{
unsigned int tx_gpio = 37;
unsigned int rx_gpio = 36;
ls1c_uart_info_t uart2_info = {0};
pwm_info.gpio=LS1C_PWM0_GPIO06;
pwm_info.mode=PWM_MODE_NORMAL;
pwm_info.duty=0.25;
pwm_info.period_ns=5*1000*1000;
pwm_init(&pwm_info);
printf("\n\n[%s] uart2 echo, please input\r\n", __FUNCTION__);
// 设置复用
pin_set_remap(tx_gpio, PIN_REMAP_SECOND);
pin_set_remap(rx_gpio, PIN_REMAP_SECOND);
// 重新初始化串口2(使能接收中断)
uart2_info.UARTx = LS1C_UART2;
uart2_info.baudrate = 115200;
uart2_info.rx_enable= TRUE; // 使能接收中断
uart_init(&uart2_info);
// 设置中断处理函数
irq_install(LS1C_UART2_IRQ, test_uart_irqhandler, NULL);
irq_enable(LS1C_UART2_IRQ);
while (1)
{
delay_s(1);
}
}
|
|