#include<stdio.h> #include<stm32f10x.h> void Init_NVIC(void); void InitGPIOB(void); void InitGPIOA(void); int main() { extern u32 Tick_Tenms,Tick_sec;//Tick_Tenms Tick_Sec u32 current,led_value; u32 cnt=0; u32 key1; u32 key2; u32 led_state=1; u32 led_dir=1; Init_NVIC();//SysTick³õʼ»¯£¬¶¨Ê±²úÉúÖÐ¶Ï InitGPIOB();//ÅäÖÃInitGPIoBÒý½ÅΪÊä³öģʽ£¬¿ØÖÆLEDµÄÁÁÃð£¬Êä³öµÍµçƽµÆÁÁ InitGPIOA(); GPIOB->ODR=0;//8¸öµÆ×î³õΪȫÁÁ״̬ current=Tick_Tenms+5; while(1) key1 = GPIOA->IDR&(1 << 8); key2 = GPIOA->IDR&(1 << 11); if(!key1){ while(!key1) //delay_ms(1): key1 = GPIOA->IDR&(1 << 8); led_state = !led_state; } if(!key2){ while( !key2); //delay_ms (1) ; key2 = GPIOA->IDR&(1 << 11) ; led_dir=!led_dir; } if(led_state){ if(current<=Tick_Tenms){ current=Tick_Tenms+50; if(led_dir) led_value=~(1<<cnt); else led_value=~(1<<(7-cnt)); led_value&=0x00ff; cnt++; if(cnt==8){ cnt=0; } } GPIOB->ODR=(GPIOB->ODR&0x0ff00)+led_value;//Êä³öÐźŠ} } void InitGPIOB() { RCC->APB2ENR|=0x0008;//ʱÖÓʹÄÜ£¬GPIOB¶ÔÓ¦bit4λ GPIOB -> CRL =0x33333333 ;//ÉèÖÃÒý½Å GPIOB .0~ GPIOB .7ΪÊä³ö50MHZ£¬ÍÆÍì»ò¿ªÂ©£¬×îµÍλ RCC ->APB2ENR|=0x01; AFIO -> MAPR |=0x02000000; } void InitGPIOA (){ RCC->APB2ENR|=0x0004;//ʱÖÓʹÄÜ, GPIOA¶ÔÓ¦bit3λ GPIOA -> CRH =( GPIOA ->CRH&0xffff00f)|0x00004004;//ÉèÖÃÒý½Å GPIOA .8ºÍ GPIOA .11Ϊ¸¡¿ÕÊäÈëģʽ£¬µ×°åµç·ÓÐÉÏÀµç×è } 给出上述代码的流程图
时间: 2024-02-05 14:04:47 浏览: 80
很抱歉,我无法在这个对话框中插入图片或流程图。但是,我可以为您简单描述一下该代码的流程:
1. 调用 Init_NVIC() 函数进行 SysTick 定时器的初始化。
2. 调用 InitGPIOB() 和 InitGPIOA() 函数分别对 GPIOB 和 GPIOA 进行初始化。
3. 在主函数中,使用 Tick_Tenms 和 Tick_sec 变量记录时间,设置当前时间 current,设置初始 LED 状态为 1,设置初始 LED 方向为 1。
4. 进入无限循环,先检测按键 K3 和 K4 是否被按下,如果 K3 被按下,则将 LED 状态取反;如果 K4 被按下,则将 LED 方向取反。
5. 如果 LED 状态为 1,且当前时间 current 小于等于 Tick_Tenms,则更新 current 和 led_value 变量,并将 led_value 传送到 GPIOB 的 ODR 寄存器输出。
6. 循环回到第 4 步。
相关问题
#include "bflb_adc.h" #include "bflb_mtimer.h" #include "board.h" struct bflb_device_s adc; #define TEST_ADC_CHANNELS 2 #define TEST_COUNT 10 struct bflb_adc_channel_s chan[] = { { .pos_chan = ADC_CHANNEL_2, .neg_chan = ADC_CHANNEL_GND }, { .pos_chan = ADC_CHANNEL_GND, .neg_chan = ADC_CHANNEL_3 }, }; int main(void) { board_init(); board_adc_gpio_init(); adc = bflb_device_get_by_name("adc"); / adc clock = XCLK / 2 / 32 */ struct bflb_adc_config_s cfg; cfg.clk_div = ADC_CLK_DIV_32; cfg.scan_conv_mode = true; cfg.continuous_conv_mode = false; cfg.differential_mode = true; cfg.resolution = ADC_RESOLUTION_16B; cfg.vref = ADC_VREF_3P2V; bflb_adc_init(adc, &cfg); bflb_adc_channel_config(adc, chan, TEST_ADC_CHANNELS); for (uint32_t i = 0; i < TEST_COUNT; i++) { bflb_adc_start_conversion(adc); while (bflb_adc_get_count(adc) < TEST_ADC_CHANNELS) { bflb_mtimer_delay_ms(1); } for (size_t j = 0; j < TEST_ADC_CHANNELS; j++) { struct bflb_adc_result_s result; uint32_t raw_data = bflb_adc_read_raw(adc); printf("raw data:%08x\r\n", raw_data); bflb_adc_parse_result(adc, &raw_data, &result, 1); printf("pos chan %d,neg chan %d,%d mv \r\n", result.pos_chan, result.neg_chan, result.millivolt); } bflb_adc_stop_conversion(adc); bflb_mtimer_delay_ms(100); } while (1) { } }根据以上代码对bl618程序的编写对以下stm32中代码#include "stm32f10x.h" #include "delay.h" #include "FSR.h" #include "usart.h" #include "adc.h" #define PRESS_MIN 20 #define PRESS_MAX 6000 #define VOLTAGE_MIN 150 #define VOLTAGE_MAX 3300 u8 state = 0; u16 val = 0; u16 value_AD = 0; long PRESS_AO = 0; int VOLTAGE_AO = 0; long map(long x, long in_min, long in_max, long out_min, long out_max); int main(void) { delay_init(); NVIC_Configuration(); uart_init(9600); Adc_Init(); delay_ms(1000); printf("Test start\r\n"); while(1) { value_AD = Get_Adc_Average(1,10); VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300); if(VOLTAGE_AO < VOLTAGE_MIN) { PRESS_AO = 0; } else if(VOLTAGE_AO > VOLTAGE_MAX) { PRESS_AO = PRESS_MAX; } else { PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX); } printf("ADÖµ = %d,µçѹ = %d mv,ѹÁ¦ = %ld g\r\n",value_AD,VOLTAGE_AO,PRESS_AO); delay_ms(500); } } long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; }移植到bl618进行改写
以下是将上述STM32代码移植到BL618进行改写后的代码:
```c
#include "bl602.h"
#include "bl_adc.h"
#include "bl_gpio.h"
#include "bl_mtimer.h"
#include "stdio.h"
#define PRESS_MIN 20
#define PRESS_MAX 6000
#define VOLTAGE_MIN 150
#define VOLTAGE_MAX 3300
#define ADC_CHANNEL 0
#define ADC_CLK_DIV 5
#define ADC_RESOLUTION ADC_RESOLUTION_12B
#define ADC_VREF ADC_VREF_3V3
long map(long x, long in_min, long in_max, long out_min, long out_max);
int main(void)
{
bl_mtimer_init();
bl_gpio_enable_output(PIN_11, 1);
bl_adc_init();
bl_adc_config_t cfg = {
.inputMode = ADC_INPUT_SINGLE_END_MODE,
.input = ADC_INPUT_CH0,
.clkDiv = ADC_CLK_DIV,
.resolution = ADC_RESOLUTION,
.refSel = ADC_VREF,
};
bl_adc_channel_config(ADC_CHANNEL, &cfg);
printf("Test start\r\n");
while (1)
{
bl_adc_start();
while (!bl_adc_fifo_data_count())
{
bl_mtimer_delay_us(100);
}
uint16_t value_AD = bl_adc_read_fifo_data();
int VOLTAGE_AO = map(value_AD, 0, 4095, 0, 3300);
long PRESS_AO = 0;
if (VOLTAGE_AO < VOLTAGE_MIN)
{
PRESS_AO = 0;
}
else if (VOLTAGE_AO > VOLTAGE_MAX)
{
PRESS_AO = PRESS_MAX;
}
else
{
PRESS_AO = map(VOLTAGE_AO, VOLTAGE_MIN, VOLTAGE_MAX, PRESS_MIN, PRESS_MAX);
}
printf("AD value = %d, voltage = %d mV, pressure = %ld g\r\n", value_AD, VOLTAGE_AO, PRESS_AO);
bl_mtimer_delay_us(500000);
}
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
```
在BL618中,需要使用`bl_adc_init()`进行ADC模块的初始化,并通过`bl_adc_channel_config()`配置ADC通道。需要注意的是,BL618的ADC模块不支持差分模式,因此需要将原来的差分模式改为单端模式。在进行ADC转换时,可以使用`bl_adc_start()`触发转换,并通过`bl_adc_fifo_data_count()`和`bl_adc_read_fifo_data()`获取转换结果。此外,由于BL618中没有提供延时函数,需要使用`bl_mtimer_delay_us()`进行延时。
串口中断接收文本数据实例,stm32f10x系列开发板
下面是一个使用stm32f10x系列开发板的串口中断接收文本数据的示例代码:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define RX_BUFFER_SIZE 128
uint8_t rx_buffer[RX_BUFFER_SIZE];
uint8_t rx_index = 0;
volatile uint8_t rx_complete = 0;
void USART1_IRQHandler(void)
{
uint8_t data = USART1->DR;
if ((data == '\n') || (data == '\r')) {
rx_buffer[rx_index] = '\0';
rx_complete = 1;
rx_index = 0;
} else {
rx_buffer[rx_index] = data;
rx_index = (rx_index + 1) % RX_BUFFER_SIZE;
}
}
void USART1_Init(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
// USART1 Tx (PA9) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1 Rx (PA10) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1 configuration
USART_InitStructure.USART_BaudRate = baudrate;
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(USART1, &USART_InitStructure);
// Enable USART1 interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable USART1 RX interrupt
}
int main(void)
{
USART1_Init(115200);
printf("Serial communication started.\n");
while (1) {
if (rx_complete) {
printf("Received data: %s\n", rx_buffer);
rx_complete = 0;
}
}
}
```
在以上代码中,我们使用USART1串口接收数据,并通过中断方式进行处理。当接收到一个字符时,如果该字符为换行符或回车符,则表示接收完整一条文本数据,将数据存入rx_buffer数组中,并将rx_complete标志位设为1。如果接收到的字符不是换行符或回车符,则将该字符存入rx_buffer数组中。在主循环中,我们检测rx_complete标志位是否为1,如果为1,则表示接收到完整一条文本数据,我们通过printf()函数打印该数据。
需要注意的是,在使用USART1之前,我们需要先对GPIO和USART进行初始化。在以上代码中,我们使用printf()函数进行输出,因此需要在工程中添加相应的库文件和初始化代码。
阅读全文