mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
180 lines
5.4 KiB
C
180 lines
5.4 KiB
C
#include "stm32_p103.h"
|
|
#include "stm32f10x.h"
|
|
#include "stm32f10x_gpio.h"
|
|
#include "stm32f10x_rcc.h"
|
|
#include "stm32f10x_usart.h"
|
|
#include "stm32f10x_exti.h"
|
|
#include "stm32f10x_adc.h"
|
|
#include "misc.h"
|
|
|
|
void init_led(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
/* Enable GPIO C clock. */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
|
|
|
/* Set the LED pin state such that the LED is off. The LED is connected
|
|
* between power and the microcontroller pin, which makes it turn on when
|
|
* the pin is low.
|
|
*/
|
|
GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_SET);
|
|
|
|
/* Configure the LED pin as push-pull output. */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
}
|
|
|
|
void init_button(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
/* Enable GPIO A clock */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
/* Configure the button pin as a floating input. */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
}
|
|
|
|
void enable_button_interrupts(void)
|
|
{
|
|
EXTI_InitTypeDef EXTI_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
/* Enable the AFIO clock. GPIO_EXTILineConfig sets registers in
|
|
* the AFIO.
|
|
*/
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
|
|
|
|
/* Connect EXTI Line 0 to the button GPIO Pin */
|
|
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
|
|
|
|
/* Configure the EXTI line to generate an interrupt when the button is
|
|
* pressed. The button pin is high when pressed, so it needs to trigger
|
|
* when rising from low to high. */
|
|
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
|
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
|
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
|
EXTI_Init(&EXTI_InitStructure);
|
|
|
|
/* Enable and set Button EXTI Interrupt to the lowest priority */
|
|
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
}
|
|
|
|
void init_rs232(void)
|
|
{
|
|
USART_InitTypeDef USART_InitStructure;
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
/* Enable peripheral clocks. */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
|
|
/* Configure USART2 Rx pin as floating input. */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
/* Configure USART2 Tx as alternate function push-pull. */
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
/* Configure the USART2 */
|
|
USART_InitStructure.USART_BaudRate = 9600;
|
|
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_Init(USART2, &USART_InitStructure);
|
|
USART_Cmd(USART2, ENABLE);
|
|
}
|
|
|
|
void enable_rs232_interrupts(void)
|
|
{
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
/* Enable transmit and receive interrupts for the USART2. */
|
|
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
|
|
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
|
|
|
/* Enable the USART2 IRQ in the NVIC module (so that the USART2 interrupt
|
|
* handler is enabled). */
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
}
|
|
|
|
void enable_rs232(void)
|
|
{
|
|
/* Enable the RS232 port. */
|
|
USART_Cmd(USART2, ENABLE);
|
|
}
|
|
|
|
void rs232_print_str(const char *str)
|
|
{
|
|
const char *curr_char = str;
|
|
|
|
while(*curr_char != '\0') {
|
|
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
|
|
USART_SendData(USART2, *curr_char);
|
|
curr_char++;
|
|
}
|
|
}
|
|
|
|
/* Functions for sending numbers through the UART */
|
|
char hex_to_char(unsigned hex_number)
|
|
{
|
|
if(hex_number < 0xA) {
|
|
return hex_number + '0';
|
|
} else {
|
|
return hex_number - 0xA + 'A';
|
|
}
|
|
}
|
|
|
|
void send_byte(uint8_t b)
|
|
{
|
|
/* Wait until the RS232 port can receive another byte. */
|
|
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
|
|
|
|
/* Toggle the LED just to show that progress is being made. */
|
|
GPIOC->ODR ^= 0x00001000;
|
|
|
|
/* Send the byte */
|
|
USART_SendData(USART2, b);
|
|
}
|
|
|
|
void send_number(unsigned long sample, int radix)
|
|
{
|
|
int digit;
|
|
unsigned long mod;
|
|
char str[100];
|
|
|
|
digit = 0;
|
|
do {
|
|
mod = sample % radix;
|
|
str[digit] = hex_to_char(mod);
|
|
sample /= radix;
|
|
digit++;
|
|
} while(sample != 0);
|
|
|
|
while(digit != 0) {
|
|
digit--;
|
|
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
|
|
USART_SendData(USART2, str[digit]);
|
|
}
|
|
}
|