51单片机和stm32串口通信,stm32 上按键B11按下,51 单片机蜂鸣器鸣叫报警时间增加,分别写出51和32keil的代码
时间: 2024-02-12 18:02:33 浏览: 99
51单片机的代码:
```c
#include <reg51.h>
sbit beep = P2^3; //定义蜂鸣器控制引脚
unsigned char time = 0; //计时器
void main()
{
TMOD = 0x01; //设置定时器1为模式1
TH1 = 0xFC; //计时器初值
TL1 = 0x66;
TR1 = 1; //启动定时器1
while(1)
{
if(RI) //如果有数据接收
{
RI = 0; //清除接收标志位
if(SBUF == 0x01) //接收到按键B11的命令
{
beep = ~beep; //蜂鸣器状态取反
}
}
if(time >= 10) //每1秒钟增加1次时间
{
time = 0; //清零计时器
beep = ~beep; //蜂鸣器状态取反
}
}
}
void timer() interrupt 1 //定时器1中断服务函数
{
TH1 = 0xFC; //计时器初值
TL1 = 0x66;
time++; //计时器加1
}
```
STM32的代码:
```c
#include "stm32f10x.h"
#define B11 GPIO_Pin_11 //按键B11对应的引脚
#define beep GPIO_Pin_8 //蜂鸣器对应的引脚
unsigned char time = 0; //计时器
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //启用GPIOA和GPIOB时钟
GPIO_InitStructure.GPIO_Pin = B11; //配置B11引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置为上拉输入模式
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB
GPIO_InitStructure.GPIO_Pin = beep; //配置蜂鸣器引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置为推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置输出速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); //启用USART1和GPIOA时钟
USART_DeInit(USART1); //复位USART1
USART_InitStructure.USART_BaudRate = 9600; //设置波特率为9600
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; //设置无硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx; //仅接收模式
USART_Init(USART1, &USART_InitStructure); //初始化USART1
USART_Cmd(USART1, ENABLE); //使能USART1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //配置USART1_TX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置为复用推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置输出速率为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //配置USART1_RX引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置为浮空输入模式
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能USART1接收中断
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置中断分组
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //设置中断通道为USART1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //设置响应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断通道
NVIC_Init(&NVIC_InitStructure); //初始化中断向量表
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //启用TIM2时钟
TIM_TimeBaseStructure.TIM_Period = 999; //设置计数器周期为1000
TIM_TimeBaseStructure.TIM_Prescaler = 7199; //设置预分频系数为7200
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分频为1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数器为向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //初始化TIM2
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能更新中断
TIM_Cmd(TIM2, ENABLE); //使能TIM2
}
int main(void)
{
GPIO_Configuration(); //初始化GPIO
USART_Configuration(); //初始化USART
NVIC_Configuration(); //初始化中断向量表
TIM_Configuration(); //初始化定时器
while(1)
{
if(GPIO_ReadInputDataBit(GPIOB, B11) == RESET) //如果按键B11被按下
{
USART_SendData(USART1, 0x01); //向USART1发送命令
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //等待发送完成
}
if(time >= 10) //如果计时器达到1秒钟
{
time = 0; //重置计时器
GPIO_WriteBit(GPIOA, beep, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, beep))); //蜂鸣器状态取反
}
}
}
void USART1_IRQHandler(void) //USART1中断服务函数
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //如果接收到数据
{
if(USART_ReceiveData(USART1) == 0x01) //如果接收到按键B11的命令
{
GPIO_WriteBit(GPIOA, beep, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, beep))); //蜂鸣器状态取反
}
}
}
void TIM2_IRQHandler(void) //TIM2中断服务函数
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //如果更新中断标志位被置位
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除更新中断标志位
time++; //计时器加1
}
}
```
阅读全文