标题:
stm32利用pwm控制呼吸灯源程序
[打印本页]
作者:
cxteng
时间:
2021-7-28 17:33
标题:
stm32利用pwm控制呼吸灯源程序
#include "stm32f10x.h"
#include "BSP.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "stm32f10x_it.h"
#include "stm32f10x_tim.h"
#define LED1 PAout(1)
#define LED2 PAout(2)
#define LED3 PAout(3)
#define LED4 PAout(8)
#define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)
int fputc(int ch,FILE *f);
char *itoa(int value, char *string, int radix);
unsigned int buf = 0;
/*int main(void)
{
LED_Init();
TIM_Init();
USART_Config();
TIM2_IRQHandler();
USART1_IRQHandler();
while(1)
{
if(i==1000)
{
i=0;
LED1=~LED1;
printf("1\r\n");
}
}
}
*/
///////////////////////////////////////
/*int main()
{
LED_Init();
USART_Config();
USART1_IRQHandler();
while(1)
{
if(USART_ReceiveData(USART1)=='1')
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
}
if(USART_ReceiveData(USART1)=='0')
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
}
}
}*/
////////////////////////////////////
uint16_t j;
int i;
int main(void)
{
delay_init();
bsp_Init();
while(1)
{
for(j=0;j<300;j++)
{
TIM_SetCompare3(TIM2,j);
delay_ms(2);
}
for(j=300;j>0;j--)
{
TIM_SetCompare3(TIM2,j);
delay_ms(2);
}
//3表示通道//TIM2表示定时器2,j代表占空比
}
}
/********************************************以下不要擅自乱修改*****************************************/
/********************************************此段函数可以实现printf的输出*****************************************/
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef’ d in stdio.h. */
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
/*********************************************************
printf重定义
**********************************************************/
//int fputc(int ch, FILE *f)
//{
// while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
// USART2->DR = (u8) ch;
// return ch;
//}
#endif
/******************************************************
整形数据转字符串函数
char *itoa(int value, char *string, int radix)
radix=10 标示是10进制 非十进制,转换结果为0;
例:d=-379;
执行 itoa(d, buf, 10); 后
buf="-379"
**********************************************************/
char *itoa(int value, char *string, int radix)
{
int i, d;
int flag = 0;
char *ptr = string;
/* This implementation only works for decimal numbers. */
if (radix != 10)
{
*ptr = 0;
return string;
}
if (!value)
{
*ptr++ = 0x30;
*ptr = 0;
return string;
}
/* if this is a negative value insert the minus sign. */
if (value < 0)
{
*ptr++ = '-';
/* Make the value positive. */
value *= -1;
}
for (i = 10000; i > 0; i /= 10)
{
d = value / i;
if (d || flag)
{
*ptr++ = (char)(d + 0x30);
value -= (d * i);
flag = 1;
}
}
/* Null terminate the string. */
*ptr = 0;
return string;
} /* NCL_Itoa */
复制代码
代码工程文件:
program.7z
(176.9 KB, 下载次数: 48)
2021-7-28 19:21 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
7631001
时间:
2021-8-15 18:25
可以试试效果
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1