野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10532|回复: 0

串口点灯实验

[复制链接]
发表于 2020-1-16 14:28:15 | 显示全部楼层 |阅读模式
/*
串口点灯实验
串口输入按键
0 关灯
1 开灯
2 翻转
串口输出灯状态
0 亮
1 灭
*/


#include"stm32f10x.h"

//中断处理函数
void USART1_IRQHandler(void)
{
        u8 res;
        if(USART_GetITStatus(USART1,USART_IT_RXNE))//确认串口中断
        {
                res= USART_ReceiveData(USART1);//收端口数据

                if (res=='0') GPIO_SetBits(GPIOB,GPIO_Pin_0);//关灯;
                if (res=='1') GPIO_ResetBits(GPIOB,GPIO_Pin_0);//开灯;
                if (res=='2') //翻转灯
                {
                    if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) //检测led电平
                        GPIO_ResetBits(GPIOB,GPIO_Pin_0);//开灯
                        else GPIO_SetBits(GPIOB,GPIO_Pin_0);//关灯
                }

                //显示灯状态
                if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) //检测led电平
                        USART_SendData(USART1,'0');//发送串口数据
                else USART_SendData(USART1,'1');//发送串口数据
        }
}
       
int main(void)
{
        GPIO_InitTypeDef GPIO_InitStruct;//端口结构体
        USART_InitTypeDef USART_InitStruct;//串口结构体
        NVIC_InitTypeDef  NVIC_InitStruct;//中断控制结构体

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1,ENABLE);//开时钟
       
        //GPIOA9串口输出
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;//复位推挽输出
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9 ;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStruct);
        //GPIOA10串口输入
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
        GPIO_Init(GPIOA,&GPIO_InitStruct);
        //B口0脚led输出
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0 ;
        GPIO_Init(GPIOB,&GPIO_InitStruct);

        //串口设置
        USART_InitStruct.USART_BaudRate=115200;//波特率
        USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//硬件流
        USART_InitStruct.USART_Mode= USART_Mode_Rx | USART_Mode_Tx;//模式
        USART_InitStruct.USART_Parity=USART_Parity_No;//非奇偶校验
        USART_InitStruct.USART_StopBits=USART_StopBits_1;//1位停止位
        USART_InitStruct.USART_WordLength=USART_WordLength_8b;//8位数据位
        USART_Init(USART1,&USART_InitStruct);//串口初始化
        USART_Cmd(USART1,ENABLE);//串口使能
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//实现中断

        //中断控制设置
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置组
        NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;//通道
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;//开中断
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;//主优先级
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;//子优先级
        NVIC_Init(&NVIC_InitStruct);//中断设置

        while(1);       
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-20 16:47 , Processed in 0.053133 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表