作业六:串口发送信息,控制LED闪烁间隔时间(偶数)
输入格式举例:data=n,那么led闪烁的时间间隔就是n毫秒。难点:将字符数组保存的n转换成整型n。
此处n取1到9999之间
视频地址:http://player.youku.com/player.php/sid/XODE5NjI0MTcy/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;
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 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],sum=0;
char i;
for(i=0;i<j;i++)
{
a[i]=S[5+j-1-i]-48;//将字符转成整型数据
sum+=a[i]*pow(10,i);//按照每一位的权值相乘再相加,还原data=n的本来面貌
}
return sum;//sum保存目标整型数据
}
void func(char *S,char LEN)
{
char count;//
unsigned int sum,i,j,k;
if((LEN!=6)&(LEN!=7)&(LEN!=8)&(LEN!=9))//data=1~999
{USART_SendStr("\r\n Erro input!!!\r\n");}//如果输入格式错误,发送提示
else
{
count=LEN-5;//从“=”后面开始是目标数据
sum = translate(S,count);//count记录“=”后有多少位,传递到translate中
GPIO_SetBits(GPIOC,0x000000ff);//all LED off
for(k=0;k<3;k++)//循环点亮D8-D1 3次
{
i=0x00000100;
for(j=1;j<=8;j++)//LED D8-D1
{
i>>=1;
GPIO_ResetBits(GPIOC,i); // on PC7-PC0 D8-D1
delay_ms(50);
GPIO_SetBits(GPIOC,i);
delay_ms(sum);//每两盏灯发光,间隔时间sum
}
}
}
}
void input_ASK()
{
char j;
func(rx_buff,rx_buff_count);
rx_buff_count=0;
for (j=0;j<rx_buff_count;j++)
{rx_buff[j]='\0';}//判断读入信息是否结束,如果结束则准备下一次读入
USART_SendStr("\n>");
}
int main(void)
{
RCC_Configuration();
GPIO_INIT();
USART_int(9600);
GPIO_ResetBits(GPIOC,0x000000ff);//led全亮,提示程序开始
delay_ms(200);
GPIO_SetBits(GPIOC,0x000000ff);//led全灭
USART_SendStr("SyStem booting......\r\n");//
USART_SendStr("\n>");//
while(1)
{}
}
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);
}