标题:
STM32 GPIO/USART/timer 控制(寄存器版)
[打印本页]
作者:
thaiqi
时间:
2021-9-8 14:34
标题:
STM32 GPIO/USART/timer 控制(寄存器版)
本文出自:zhishi-zhishi点net
在开发板上试验了GPIO点亮LED,串口中断和定时器。
#include "stm32f10x.h"
/*
GPIO :
PC.14 PC.15 <---> LED
PB.7 <---> Button
PA.9 PA.10 <---> USART
*/
#define RCC_APB2ENR *(uint32_t*)(0x40021000+0x18)
#define RCC_APB1ENR *(uint32_t*)(0x40021000+0x1C)
#define GPIOC_CRH *(uint32_t*)(0x40011000+0x04)
#define GPIOC_ODR *(uint32_t*)(0x40011000+0x0C)
#define GPIOB_CRL *(uint32_t*)(0x40010C00+0x00)
#define GPIOB_ODR *(uint16_t*)(0x40010C00+0x0C)
#define GPIOB_BSRR *(uint16_t*)(0x40010C00+0x10)
#define SETENA_ISER0 *(uint32_t*)(0xe000E100+0x00)
#define EXTI_IMR *(uint32_t*)(0x40010400+0x00)
#define EXTI_RTSR *(uint32_t*)(0x40010400+0x08)
#define EXTI_FTSR *(uint32_t*)(0x40010400+0x0C)
#define EXTI_PR *(uint32_t*)(0x40010400+0x14)
#define AFIO_EXTICR2 *(uint32_t*)(0x40010000+0x0C)
#define GPIOA_IDR *(uint32_t*)(0x40010800+0x08)
#define GPIOA_ODR *(uint32_t*)(0x40010800+0x0C)
#define GPIOA_CRH *(uint32_t*)(0x40010800+0x04)
#define SETENA_ISER1 *(uint32_t*)(0xe000E100+0x04)
//#define USART1_BASE *(uint32_t*)(0x40013800+0x00) //bug : redefinition
#define USART1_SR *(uint32_t*)(0x40013800+0x00)
#define USART1_DR *(uint32_t*)(0x40013800+0x04)
#define USART1_BRR *(uint32_t*)(0x40013800+0x08)
#define USART1_CR1 *(uint32_t*)(0x40013800+0x0C)
#define USART1_CR2 *(uint32_t*)(0x40013800+0x10)
#define USART1_CR3 *(uint32_t*)(0x40013800+0x14)
#define TIM1_CR1 *(uint32_t*)(0x40012c00+0x00)
#define TIM1_DIER *(uint32_t*)(0x40012c00+0x0C)
#define TIM1_SR *(uint32_t*)(0x40012c00+0x10)
#define TIM1_PSC *(uint32_t*)(0x40012c00+0x28)
#define TIM1_ARR *(uint32_t*)(0x40012c00+0x2C)
#define TIM3_CR1 *(uint32_t*)(0x40000400+0x00)
#define TIM3_DIER *(uint32_t*)(0x40000400+0x0C)
#define TIM3_SR *(uint32_t*)(0x40000400+0x10)
#define TIM3_PSC *(uint32_t*)(0x40000400+0x28)
#define TIM3_ARR *(uint32_t*)(0x40000400+0x2C)
#if 1
int m = 0;
void EXTI9_5_IRQHandler(void)
{
EXTI_PR = 0x1 << 7;
m++;
if(m%2 == 1)
GPIOC_ODR = 0x0 << 14;
else if(m%2 == 0)
GPIOC_ODR = 0x3 << 14;
}
#endif
char test_str[128];
char* p;
int i;
volatile int mm = 0;
volatile int old_mm = 0;
char recv_buf[64];
int num = 0;
int is_one_line = 0;
void USART1_IRQHandler(void)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
mm += 10;
recv_buf[num++] = USART1_DR;
if(num >= 64)
num = 0;
if(recv_buf[num-1] == 0x0a && recv_buf[num-2] == 0x0d)
is_one_line = 1;
}
int time1_out = 0;
int old_time1_out= 0;
#if 1
void TIM1_UP_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update);
time1_out++;
}
#else
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_FLAG_Update);
time1_out++;
}
#endif
int main()
{
RCC_APB2ENR = 0x1 << 4 | 0x1 << 3 | 0x1 << 0 | 0x1 << 2 | 0x1 << 14 /*| 0x1 << 11 */ ; //p53
GPIOC_CRH = 0x03 << 28 | 0x03 << 24; //p68
GPIOB_CRL = 0x00 <<28 | /*0x01*/ /*0x10 --bug */0x02 << 30;
GPIOC_ODR = 0x3 << 14; //p69
EXTI_IMR = 0x01 << 7;
//EXTI_RTSR = 0x01 << 7;
EXTI_FTSR = 0x01 << 7;
AFIO_EXTICR2 = 0x01 << 12;
GPIOA_ODR = 0x1 << 9;
GPIOA_CRH = 0x3 << 4 | 0x2 << 6 | 0x0 << 8 | 0x1 << 10;
SETENA_ISER0 = 0x1 << 23;
SETENA_ISER1 = 0x1 << 5;
USART1_BRR = 0x0271;
USART1_CR1 |= 0x03 << 2; //TE/RE
USART1_CR1 |= 0x1 << 5 /* | 0x1 <<7*/;
USART1_CR1 |= 0x1 << 13;
#if 1
RCC_APB2ENR |= (0x1 << 11);
TIM1_CR1 |= 0x1 << 0; //CEN
TIM1_DIER |= 0x1 << 0; //UIE
TIM1_SR &= 0xFFFE; //UIF
TIM1_PSC = 7199;
TIM1_ARR = 9999;
SETENA_ISER0 |= 0x01 << 25;
#else
RCC_APB1ENR |= 0x1 << 1;
TIM3_CR1 |= 0x1 << 0; //CEN
TIM3_DIER |= 0x1 << 0; //UIE
TIM3_SR &= 0xFFFE; //UIF
TIM3_PSC = 7199;
TIM3_ARR = 9999;
SETENA_ISER0 |= 0x01 << 29;
#endif
while(1)
{
if(is_one_line == 1)
{
memset(test_str, 0, sizeof(test_str));
sprintf( test_str, "%s", recv_buf);
p = test_str;
while((*p)!='\0')
{
USART_SendData(USART1, *(p++));
for(i=0; i < 1000; i++)
;
}
num = 0;
is_one_line = 0;
memset(recv_buf, 0, sizeof(recv_buf));
}
if(old_time1_out == time1_out)
;
else
{
memset(test_str, 0, sizeof(test_str));
sprintf( test_str, "old: %d, time1_out:%d", old_time1_out, time1_out);
p = test_str;
while((*p)!='\0')
{
USART_SendData(USART1, *(p++));
for(i=0; i < 1000; i++)
;
}
old_time1_out = time1_out;
}
}
return 0;
}
复制代码
至此试验就告完成。用过仿真软件Proteus(后来好象串口COMPIM失效了。)和编译器Keil MDK5,表示谢意。
源文件打包在此:
test_timer.7z
(268.11 KB, 下载次数: 8)
2021-9-8 15:57 上传
点击文件名下载附件
下载积分: 黑币 -5
本文出自:www点zhishi-zhishi点net/working/stm32_timer.html
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1