串口控制led,流水灯顺时针流,逆时针流,z型:
#include "stm32f10x.h"
#include "stm32_eval.h"
#include "stdio.h"
#define buff_size 16; //宏定义一个buff_size=16
GPIO_InitTypeDef GPIO_InitStructure; //结构体的命名
char rx_buff[],rx_buff_count=0; //定义一个字符型数组 rx_buff[] ,并且定义一个字符变量rx_buff_count=0
void RCC_Configuration(void)
{
RCC_DeInit(); //复位外设RCC的所有寄存器至缺省值(默认值)
RCC_HSICmd(ENABLE); //使能外设RCC高速内部时钟
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); //判断RCC的标志位是否被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); //使能外设PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //判断RCC的标志位是否被RESET
RCC_ADCCLKConfig(RCC_PCLK2_Div4); ADC速率为RCC_PCLK2/4 ,配置低速ADC时钟等于SYSCLK/4
RCC_PCLK2Config(RCC_HCLK_Div1); // 配置高速APB时钟等于SYSCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //配置低速APB时钟等于SYSCLK/2
RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB时钟 AHB时钟等于SYSCLK
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟SYSCLK=PLL时钟
while(RCC_GetSYSCLKSource() != 0x08); //关闭蜂鸣器
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //使能外设 GPIOD端口时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//disable JTAG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //端口配置PD.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; ;//GPIO速率
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; ;/GPIO输出模式
GPIO_Init(GPIOD, &GPIO_InitStructure); //应用结构体的成员
GPIO_ResetBits(GPIOD,GPIO_Pin_2); //重置GPIOD.2
}
void GPIO_INIT() //输入输出的初始化
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能外设GPIOC端口时钟
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; //端口配置PC.0至PC.7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; ;//GPIO速率
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //GPIOS输出模式 (推挽输出)
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOC.0至GPIOC.7
GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); //根据设定参数初始化GPIOC.0至GPIOC.7输出为
高电平
}
void delay_us(u32 n) //延时1us
{
u8 j;
while(n--)
for(j=0;j<10;j++);
}
void delay_ms(u32 n) //延时1ms
{
while(n--)
delay_us(1000);
}
void USART_int(long BaudRate) // USART的初始化
{ USART_InitTypeDef USART_InitStructure; //结构体的命名
USART_ClockInitTypeDef USART_ClockInitStructure; // 结构体USART_ClockInit的命名
NVIC_InitTypeDef NVIC_InitStructure; // 结构体 NVIC_Init的命名
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE); //使能外设GPIOC端口时钟和外设APB2下的USART1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //端口配置PC.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //GPIO速率为10MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //GPIOS输出模式
GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化,引用结构的的成员
/* PA10 USART1_Rx */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //端口配置PC.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //使能外设GPIOC端口时钟
GPIO_Init(GPIOA, &GPIO_InitStructure); /根据设定参数初始化,引用结构的的成员
/* USARTx configured as follow:
- BaudRate = 115200 baud //波特率设置为115200
- Word Length = 8 Bits //字长为8位数据格式
- One Stop Bit //一位停止位
- No parity //无奇偶校验位
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = BaudRate; //波特率设置为115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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_ClockInitStructure.USART_Clock = USART_Clock_Disable; //失能 USART的端口时钟
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); //使能外设RCC
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); //使能USART1
/* 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); //初始化中断
}
void USART_SendStr(char *str) //USART发送数据
{
while((*str)!='\0')
{USART_SendData(USART1,*str++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
char strcmp(char *S,char *C,char LEN) //比较字符串是否相等
{ char count;
for(count=0;count<LEN;count++)
{
if(S[count]!=C[count])
{return 0;} //不相等 返回0
}
return 1; //相等 返回值为1
}
void commcmp(char *S,char LEN) //比较字符串的长度 用switch语句
{
int i,j;
unsigned char flag=0;
if((LEN!=7)&(LEN!=8)&(LEN!=9))
{USART_SendStr("\r\n Erro input!!!\r\n");}
else{
switch(LEN)
{
case 7:
{
flag = strcmp(S,"time=50",7);
if(flag==1)
{
for(i=0;i<=18;i++) //循环19次
{
GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|
GPIO_Pin_7); //输出高电平
i=0x01;
GPIO_ResetBits(GPIOC,i);
delay_ms(50);
GPIO_SetBits(GPIOC,i);
for(j=1;j<=3;j++)
{
i<<=1;
GPIO_ResetBits(GPIOC,i);
delay_ms(50);
GPIO_SetBits(GPIOC,i);
}
i=0x0100;
for(j=1;j<=4;j++)
{
i>>=1;
GPIO_ResetBits(GPIOC,i);
delay_ms(50);
GPIO_SetBits(GPIOC,i);
}
}
break;
}
}
case 8:
{
flag = strcmp(S,"time=100",8);
if(flag==1)
{
for(i=0;i<=18;i++)
{
i=0x0010;
for(j=1;j<=4;j++)
{
i>>=1;
GPIO_ResetBits(GPIOC,i);
delay_ms(100);
GPIO_SetBits(GPIOC,i);
}
i=0x08;
for(j=1;j<=4;j++)
{
i<<=1;
GPIO_ResetBits(GPIOC,i);
delay_ms(100);
GPIO_SetBits(GPIOC,i);
}
}
break;
}
}
case 9:
{
flag = strcmp(S,"time=1000",9);
if(flag==1)
{
for(i=0;i<=18;i++)
{
GPIO_SetBits(GPIOC,0x000000ff);
i=0x00100;
for(j=1;j<=8;j++)
{
i>>=1;
GPIO_ResetBits(GPIOC,i);
delay_ms(1000);
GPIO_SetBits(GPIOC,i);
}
}
break;
}
}
}
}
}
void input_ASK()
{
char j;
commcmp(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);
USART_SendStr("SyStem booting......\r\n");
while(1){}
}
void USART1_IRQHandler(void) // USART1中断服务函数
{
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);
}