前期工作在“16号同学的作业本”,链接在此:
http://forum.eepw.com.cn/thread/260879/5/#43
任务目标:
在串口调试助手向STM32发送数据,控制LED流水等循环闪烁的速率。
发送格式:
data=n 即相邻两个LED的闪烁间隔为n毫秒,n的范围是1~9999。
完成进度:
led1~led8的初始状态为循环闪烁,一边闪烁一边等待中断;
通过串口发送data=n,使相邻的两个LED闪烁间隔为n毫秒,以此速率8个LED循环闪烁3轮后又全部熄灭,等待下一次发送的新数据。
疑难:
1.主函数以一个while(1)的循环,一边循环点亮led1~led8,一边等待中断,但是第一次的中断函数执行完以后(即led1~led8以n毫秒为间隔时间闪烁3轮),程序没有继续回到主函数的while(1)循环点亮led1~led8,这是为什么?
2.我目前只能让流水灯循环闪烁有限次,完成有限次的循环后,才能执行新来的中断。有没有办法让led闪烁速率保持在n毫秒的状态下接收新的中断,再保持新的闪烁速率……以此循环 ?
视频地址:http://player.youku.com/player.php/sid/XODIzMDY2MDMy/v.swf
代码如下:
#include "stm32f10x.h" #include "stm32_eval.h" #include "stdio.h" #include "math.h" #define buff_size 16; char rx_buff[], rx_buff_count=0; //static unsigned int sum;//静态局部变量sum GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; void RCC_Configuration(void) { RCC_DeInit(); RCC_HSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); RCC_HSEConfig(RCC_HSE_OFF); RCC_LSEConfig(RCC_LSE_OFF); RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_9); // 72HMz RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); RCC_ADCCLKConfig(RCC_PCLK2_Div4); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_ResetBits(GPIOD,GPIO_Pin_2);//关闭蜂鸣器 } void GPIO_INIT() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能PC时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PC0-PC7 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化PC } void USART_int(long BaudRate) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//使能PA USART1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX位于PA9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//TX初始化 PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX位于PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//悬空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//RX初始化 PA10 /* 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_InitStructure.USART_BaudRate = BaudRate;//传输速率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8比特 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 USART_InitStructure.USART_Parity = USART_Parity_No;// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控位none USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//接收和发送模式 USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1, &USART_ClockInitStructure);//初始化USART1时钟 USART_Init(USART1, &USART_InitStructure);//初始化USART1 USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); /* Configure four bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//优先级 /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择USART1中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);//初始化中断 } /*delay_us*/ void delay_us(u32 n) { u8 j; while(n--) for(j=0;j<10;j++); } /*delay_ms*/ void delay_ms(u32 n) { while(n--) delay_us(1000); } void led_on_2(unsigned int sum) //led循环闪烁3次 { int i;char j; GPIO_SetBits(GPIOC,0x000000ff); for(j=0;j<3;j++) { for(i=0x00000080;i!=0x00000000;)//LED { GPIO_ResetBits(GPIOC,i);// delay_ms(50); GPIO_SetBits(GPIOC,i);// delay_ms(sum); // i>>=1; } } } void USART_SendStr(char *str)//发送字符串 { while((*str)!='\0')//到字符串的末尾才停止发送数据 { USART_SendData(USART1,*str++); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } } unsigned int translate(char *S,char j)//将字符串组成的数字转换成整型数据 { unsigned int a[4],result=0; char i; for(i=0;i"); } int main(void) { int i=0x00000080; RCC_Configuration(); GPIO_INIT(); USART_int(9600); USART_SendStr("SyStem booting......\r\n");// USART_SendStr("\n>");// GPIO_SetBits(GPIOC,0x000000ff);//LED while(1)//LED循环闪烁 { GPIO_ResetBits(GPIOC,i);// delay_ms(90); GPIO_SetBits(GPIOC,i);// i>>=1; if(i==0x00000001){i=0x00000080;} } } void USART1_IRQHandler(void) { while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET) { } if(USART_ReceiveData(USART1)==0x0d)//如果输入完毕,按下了回车 {input_ASK();} else { USART_SendData(USART1,USART_ReceiveData(USART1)); rx_buff[rx_buff_count]= USART_ReceiveData(USART1); rx_buff_count++; } USART_ClearFlag(USART1, USART_FLAG_RXNE); }