昨天上午拿到的st-link-ii和一块最小系统;
呵呵,东西到了,不好好看看心里过意不去,就着手做了一个简单的控制台(串口),顺便做了一下遥控解码,测试TIMx,
以前一直在看103的资料,最小系统上的却是101,我也懒得去换了,就用它了.
接线如下:
UART2----PIN12(TX),PIN13(RX)--->对接到PL2303的TX,RX上去,便于笔记本调试嘛,
IR信号---PIN21
(图方便装,直接把遥控头的PIN2(GND)-->PIN23,PIN3(VS)-->PIN24,PIN1(信号)-->PIN21,因为是DEMO嘛,不用加电容了,呵呵,)
因为是48PIN封装,有些地方不能直接用LIB库中的源码,
下面是要注意的地方,使用UART2的PIN脚,不需重映射
用例程里的代码时注意去掉这句话:
//GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
至于串口调通就不说了,以前好多兄弟都写过了,
下面主要说下遥控解码对时钟的基本设置:
配置TIM2的时钟为1MHz,这个每个cnt即为1us,便于计算时钟值,
/* PCLK1 = HCLK/4 */
RCC_PCLK1Config(RCC_HCLK_Div4);
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* ---------------------------------------------------------------
TIM2 Configuration: Output Compare Timing Mode:
TIM2CLK = 36 MHz, Prescaler = 36, TIM2 counter clock = 1 MHz
---------------------------------------------------------------*/
/* Prescaler configuration */
TIM_PrescalerConfig(TIM2, 36, TIM_PSCReloadMode_Immediate);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
配置PIN21下降沿中断
/* Configure PB.10 as input floating (EXTI Line 10) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable the EXTI15_10 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
/* Configure EXTI Line10 to generate an interrupt on falling edge */
EXTI_InitStructure.EXTI_Line = EXTI_Line10;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Generate software interrupt: simulate a falling edge applied on EXTI line 9 */
EXTI_GenerateSWInterrupt(EXTI_Line9);
在中断中处理如下:
if(EXTI_GetITStatus(EXTI_Line10) != RESET)
{
IrDecode(TIM_GetCounter(TIM2));
/* Toggle PB8 pin */
GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8))));
/* Clear the EXTI line 9 pending bit */
EXTI_ClearITPendingBit(EXTI_Line10);
}
主循环任务也很简单,如下:
UartConsoleMain(); // 检查有没有串口输入:
// decode ir
if (gIrMsgTx) // 看看有没有检测到IR按下:
{ key = gIrMsgTx;
gIrMsgTx = MSG_NONE;
printf("IrCode: %02x-%02x-%02x-%02x
",gIrBuf[0],gIrBuf[1],gIrBuf[2],gIrBuf[3]);
}
//写得很乱,呵呵,
半天多点吧,呵呵
一边帮旁边的兄弟调他的电子新板,
一边做了这个STM32的软件,
以前没用过STM32,查时钟,中断等费了点事,呵呵,
对了,有点要注意的是,我的串口控制台使用提中断接收PC端的数据.
给出其他的源码:
串口中断接收的:
if(USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
uartx_rxBuffer[uartx_rxIndexIn++] = (USART_ReceiveData(USARTx) & 0xFF);;
/* Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
if (uartx_rxIndexIn>=_MAX_UART_BUFFER_LEN) uartx_rxIndexIn=0;
}
串口初始化及其他的函数:
void VarInit (void)
{
uartx_rxIndexIn = 0;
uartx_rxIndexOut = 0;
}
u8 UartxRS_ready(void)
{
if( uartx_rxIndexIn == uartx_rxIndexOut ) return 0;
else return 1;
}
u8 Uartx_rx (void)
{
u8 ret;
ret = uartx_rxBuffer[uartx_rxIndexOut];
uartx_rxIndexOut++;
if(uartx_rxIndexOut >= _MAX_UART_BUFFER_LEN)
uartx_rxIndexOut = 0;
return ret;
}
void UartxInit (u32 baudrate)
{
USART_Cmd(USARTx, DISABLE);
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the second edge
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_InitStructure.USART_BaudRate = baudrate; // 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USARTx */
USART_Init(USARTx, &USART_InitStructure);
/* Enable the USART Transmoit interrupt: this interrupt is generated when the
USART1 transmit data register is empty */
//USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); ---walter 20080422
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
/* Enable the USARTx */
USART_Cmd(USARTx, ENABLE);
}
软件不是流程很简单的,
中断接收PC端的串口数据并缓存,
然后主循环取出数据,解析收到的指令,以回车为结束符进行匹配查找,
如果支持该指令,则执行,不支持,接收下一指令,并报错,
遥控接收只是处理外部中断的一个时序信号而已,解析出时序信号中的数据.