一直都在用库来写程序,写起来也比较方便也好学。很多东西很快就上手了,不过近来才发现项目要求操作的速度很快,老板又不想买更快更贵的芯片,没有办法,看看用操作寄存器的方法是否跑得快些。加油!!先晒两个copy的程序吧。
IO口输出:
include "stm32f10x_lib.h"
#include "sys.h"
#include "delay.h"
#define LED0 PAout(8)
#define LED1 PDout(2)
void Led_Init(void);
int main(void)
{
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
Led_Init();
while(1)
{
LED0 = 0;
LED1 = 0;
delay_ms(100);
LED0 = 1;
LED1 = 1;
delay_ms(100);
}
}
void Led_Init(void)
{
RCC->APB2ENR|= 1<<2;
RCC->APB2ENR|= 1<<5;
GPIOA->CRH&=0XFFFFFFF0;
GPIOA->CRH|=0X00000003;
GPIOA->ODR|=1<<8;
GPIOD->CRL&=0XFFFFF0FF;
GPIOD->CRL|=0X00000300;
GPIOD->ODR|=1<<2;
}
按键程序
#include "stm32f10x_lib.h"
#include "sys.h"
#include "delay.h"
#define LED0 PAout(8)
#define LED1 PDout(2)
#define Key1 PAin(15)
void Led_Init(void);
void Key_Init(void);
int main(void)
{
Stm32_Clock_Init(9); //系统时钟设置
delay_init(72); //延时初始化
Led_Init();
Key_Init();
while(1)
{
if(Key1 == 0)
{
delay_ms(10);
if(Key1 == 0)
{
LED0 = 0;
LED1 = 0;
delay_ms(100);
LED0 = 1;
LED1 = 1;
delay_ms(100);
}
}
}
}
void Led_Init(void)
{
RCC->APB2ENR|= 1<<2;
RCC->APB2ENR|= 1<<5;
GPIOA->CRH&=0XFFFFFFF0;
GPIOA->CRH|=0X00000003;
GPIOA->ODR|=1<<8;
GPIOD->CRL&=0XFFFFF0FF;
GPIOD->CRL|=0X00000300;
GPIOD->ODR|=1<<2;
}
void Key_Init(void)
{
RCC->APB2ENR|=1<<2;
GPIOA->CRH&=0X0FFFFFFF;
GPIOA->CRH|=0X80000000;
GPIOA->ODR|=1<<15;
}
串口发送程序:
#include "stm32f10x_lib.h"
#include "sys.h"
#include "delay.h"
u8 *a="suqingxiao!" ;
void Led_Init(void);
void Key_Init(void);
void USART_Initaize(u32 pclk2,u32 bound);
void Uart1_PutChar(u8 ch);
void Uart1_PutString(u8 *Buf, u8 Len);
int main(void)
{
Stm32_Clock_Init(9);
delay_init(72);
USART_Initaize(72,9600);
while(1)
{
Uart1_PutChar('0');
Uart1_PutString(a,11);
delay_ms(100);
}
}
void USART_Initaize(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);//得到USARTDIV
mantissa=temp; //得到整数部分
fraction=(temp-mantissa)*16; //得到小数部分
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2; //使能PORTA口时钟
RCC->APB2ENR|=1<<14; //使能串口时钟
GPIOA->CRH&=0XFFFFF00F;
GPIOA->CRH|=0X000008B0;//IO状态设置
RCC->APB2RSTR|=1<<14; //复位串口1
RCC->APB2RSTR&=~(1<<14);//停止复位
//波特率设置
USART1->BRR=mantissa; // 波特率设置
USART1->CR1|=0X200C; //1位停止,无校验位.
#ifdef EN_USART1_RX //如果使能了接收
//使能接收中断
USART1->CR1|=1<<8; //PE中断使能
USART1->CR1|=1<<5; //接收缓冲区非空中断使能
MY_NVIC_Init(3,3,USART1_IRQChannel,2);//组2,最低优先级
#endif
}
void Uart1_PutChar(u8 ch)
{
USART1->DR = (u8) ch;
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
}
void Uart1_PutString(u8 *Buf, u8 Len)
{
u8 i;
for(i= 0; i<Len; i++)
{
Uart1_PutChar(*Buf++);
}
}
|