串口的程序前面已经说过了,这里就把前面串口与LED的程序结合起来,做成串口控制LED的模式。
LS 那么快占楼来了,呵呵,继续。我现在的进度是比我发帖要快点的,视频嘛我还是觉得我满意的程度再上传吧,自己那关都过不了何必上传呢。
至于我现在模块调得还不多,但相对功能代码量是越来越大了,所以今天破例贴一次完整的代码,至于如何实现解析,看各位自己理解了,基本是结合前面所说的LED循环方法,与串口与电脑通讯的方法用的都是固件库的函数,理解起来还是非常容易的
代码:
#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include <stdio.h>
#include <stdarg.h>
/****************************************************************************/
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
USART_SendData(USART1, (u8) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
/****************************************************************************/
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
u32 TimeCount;
u8 Receive_buffer;
union
{
u8 ALL;
struct
{
u8 Led_F1:1;
u8 Led_F2:1;
u8 Led_F3:1;
u8 Led_F4:1;
u8 Led_F5:1;
u8 Led_F6:1;
u8 Led_F7:1;
u8 Led_F8:1;
}LedFlag;
}LedFlag_State;
/*Private functions-------------------------------------------------------------*/
void RCC_Configuration(void);
void Usart_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void Usart_Display(u8);
void delayms(u32 nCount);
void delayns(u32 nCount);
int main(void)
{
u16 LedData=0x0000;
u8 LedState;
u8 TempState;
u8 i;
TempState=LedState;
RCC_Configuration();
NVIC_Configuration();
Usart_Configuration();
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
GPIO_Configuration();
LedFlag_State.ALL=0x00;
Receive_buffer='1';
LedState='1';
GPIO_Write(GPIOE,0xffff);
delayms(1000);
while (1)
{
GPIO_Write(GPIOE,LedData);
delayms(300);
if(TempState!=LedState)
{TempState=LedState;Usart_Display(TempState);delayms(1000);}
switch(LedState)
{
case '1': //十字循环
if(LedFlag_State.LedFlag.Led_F1!=1)
{
LedFlag_State.ALL=0x00;
LedFlag_State.LedFlag.Led_F1=1;
LedData=0xFFFF;
}
else
{
LedData=0x7EEE;
for(i=0;i<4;i++)
{
GPIO_Write(GPIOE,LedData);
delayms(500);
LedData=(LedData>>1|0x8FFF)&(LedData<<1|0xF001);
}
GPIO_Write(GPIOE,0xFFFF);
delayms(300);
LedData=0xE777;
for(i=0;i<4;i++)
{
GPIO_Write(GPIOE,LedData);
delayms(500);
LedData=(LedData<<1|0x1FFF)&(LedData>>1|0xF800);
}
LedData=0xFFFF;
}
break;
/**************************************************************/
case '2': //竖循环
if(LedFlag_State.LedFlag.Led_F2!=1)
{
LedFlag_State.ALL&=0x00;
LedFlag_State.LedFlag.Led_F2=1;
LedData=0xeFeF;
}
else
{
LedData=((LedData>>3)|0xffef)&(LedData<<1|0xff0f)&((LedData>>3)|0xEFFF&(LedData<<1));
}
break;
/**************************************************************/
case '3': //横循环
if(LedFlag_State.LedFlag.Led_F3!=1)
{
LedFlag_State.ALL&=0x00;
LedFlag_State.LedFlag.Led_F3=1;
LedData=0xF7fe;
}
else
{
LedData=((LedData<<1|0xfff0)|((LedData>>3)&0x0001))&((LedData<<3)|0xF7FF)&(LedData>>1|0xf0ff);
}
break;
case '4': //竖控
if(LedFlag_State.LedFlag.Led_F4!=1)
{
LedFlag_State.ALL&=0x00;
LedFlag_State.LedFlag.Led_F4=1;
LedData=0x0F0F;
// Receive_buffer=0x00;
}
else
{
// if(Receive_buffer!=0x00){Receive_buffer=0x00;LedData=~LedData|0x0F0F;}
}
break;
case '5': //横控
if(LedFlag_State.LedFlag.Led_F5!=1)
{
LedFlag_State.ALL&=0x00;
LedFlag_State.LedFlag.Led_F5=1;
LedData=0xF0F0;
// Receive_buffer=0x00;
}
else
{
// if(Receive_buffer!=0x00){Receive_buffer=0x00;LedData=~LedData|0xF0F0;}
}
break;
case '6': //全亮
LedData=0x0000;
break;
case '7': //全灭
LedData=0xFFFF;
break;
default:
if(LedFlag_State.LedFlag.Led_F8!=1)
{
LedFlag_State.ALL&=0x01;
LedFlag_State.LedFlag.Led_F8=1;
printf("\n\n\r输入错误请重新输入!!");
}
else
GPIO_Write(GPIOE,0xFFFF);
break;
}
LedState=Receive_buffer;
}
}
void Usart_Display(u8 LedStatus)
{
printf("\r\n\n\n\n\n\r*****************************************************************\n\n\r");
printf()"\r-------------------------BRUCEG-----------------------------";
printf("\r\n\n\r*****************************************************************\n\n\r");
printf("\r 1.全循环闪烁\n");
printf("\r 2.竖循环闪烁\n");
printf("\r 3.横循环闪烁\n");
printf("\r 4.竖全亮控开关\n");
printf("\r 5.横全亮控开关\n");
printf("\r 6.全亮\n");
printf("\r 7.全灭\n");
printf("\n\n\r");
printf("\r当前运行状态:%c\n",LedStatus);
printf("\r请输入控制序号:");
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO|
RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|
RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|
RCC_APB2Periph_GPIOE,ENABLE);
}
void Usart_Configuration(void)
{
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate=9600;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ClearITPendingBit(USART1,USART_IT_TXE);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
// USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
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);
}
void delayns(u32 nCount)
{
if (SysTick_Config (SystemCoreClock / 1000000))
{ while (1); }
TimeCount=nCount;
while(TimeCount);
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
void delayms(u32 nCount)
{
if (SysTick_Config (SystemCoreClock / 1000))
{ while (1); }
TimeCount=nCount;
while(TimeCount);
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |