提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
1.“软件I2C”和“硬件I2C”
所谓硬件I2C对应芯片上的I2C外设,有相应I2C驱动电路,其所使用的I2C管脚bai也是专用的;软件I2C一般是用GPIO管脚,用软件控制管脚状态以模拟I2C通信波形。
硬件I2C的效率要远高于软件的,而软件I2C由于不受管脚限制,接口比较灵活。
本次实验中使用的软件I2C
I2C 协议的物理层和协议层
①物理层
I2C是一个支持设备的总线。可连接多个 I2C 通讯设备,支持多个通讯主机及多个通讯从机。对于I2C 总线,只使用两条总线线路,一条双向串行数据线(SDA) ,一条串行时钟线(SCL)。
I2C 通讯设备常用连接方式(引用野火资料中的图)
②协议层
主要是定义了通讯的起始和停止信号、数据有效性、响应、仲裁、时钟同步和地址广播等。
通讯的起始和停止信号
从图中可以看出I2C在通讯的时候,只有在SCL处于高电平时,SDA的数据传输才是有效的。SDA 信号线是用于传输数据,SCL 信号线是保证数据同步。
响应
当SDA传输数据后,接收方对接受到的数据进行一个应答。如果希望继续进行传输数据,则回应应答信号(低电平),否则回应非应答信号(高电平)。
I2C的两种方式——硬件I2C和软件I2C
①硬件I2C
直接利用 STM32 芯片中的硬件 I2C 外设。
硬件I2C的使用
只要配置好对应的寄存器,外设就会产生标准串口协议的时序。在初始化好 I2C 外设后,只需要把某寄存器位置 1,此时外设就会控制对应的 SCL 及 SDA 线自动产生 I2C 起始信号,不需要内核直接控制引脚的电平。
②软件I2C
直接使用 CPU 内核按照 I2C 协议的要求控制 GPIO 输出高低电平,从而模拟I2C。
软件I2C的使用
需要在控制产生 I2C 的起始信号时,控制作为 SCL 线的 GPIO 引脚输出高电平,然后控制作为 SDA 线的 GPIO 引脚在此期间完成由高电平至低电平的切换,最后再控制SCL 线切换为低电平,这样就输出了一个标准的 I2C 起始信号。
③两者的差别
硬件 I2C 直接使用外设来控制引脚,可以减轻 CPU 的负担。不过使用硬件I2C 时必须使用某些固定的引脚作为 SCL 和 SDA,软件模拟 I2C 则可以使用任意 GPIO 引脚,相对比较灵活。对于硬件I2C用法比较复杂,软件I2C的流程更清楚一些。如果要详细了解I2C的协议,使用软件I2C可能更好的理解这个过程。在使用I2C过程,硬件I2C可能通信更加快,更加稳定。
main32.c
#include "delay.h"
#include "usart.h"
#include "bsp_i2c.h"
int main(void)
{
delay_init(); //?óê±oˉêy3?ê??ˉ
uart_init(115200); //′??ú3?ê??ˉ?a115200
IIC_Init();
while(1)
{
printf("?aê?2aá?£???é?μè£o");
read_AHT20_once();
delay_ms(1500);
}
}
usart.c
#include "sys.h"
#include "usart.h"
//STM32F103o?D?°?ày3ì
//?aoˉêy°?±?ày3ì
/********** mcudev.taobao.com 3??· ********/
//
//è?1?ê1ó?ucos,?ò°üà¨????μ?í·???t?′?é.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h" //ucos ê1ó?
#endif
//
//STM32?a·¢°?
//′??ú13?ê??ˉ
//
//
//?óè?ò???′ú??,?§3?printfoˉêy,??2?Dèòa????use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×??aDèòaμ??§3?oˉêy
struct __FILE
{
int handle;
};
FILE __stdout;
//?¨ò?_sys_exit()ò?±ü?aê1ó?°??÷?ú?£ê?
void _sys_exit(int x)
{
x = x;
}
//???¨ò?fputcoˉêy
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//?-?··¢?í,?±μ?·¢?ííê±?
USART1->DR = (u8) ch;
return ch;
}
#endif
/*ê1ó?microLibμ?·?·¨*/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
#if EN_USART1_RX //è?1?ê1?üá??óê?
//′??ú1?D??·t??3ìDò
//×¢òa,?áè?USARTx->SR?ü±ü?a?a??????μ?′í?ó
u8 USART_RX_BUF[USART_REC_LEN]; //?óê??o3?,×?′óUSART_REC_LEN??×??ú.
//?óê?×′ì?
//bit15£? ?óê?íê3é±ê??
//bit14£? ?óê?μ?0x0d
//bit13~0£? ?óê?μ?μ?óDD§×??úêy??
u16 USART_RX_STA=0; //?óê?×′ì?±ê??
void uart_init(u32 bound){
//GPIO???úéè??
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ê1?üUSART1£?GPIOAê±?ó
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//?à??ó??è??3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óó??è??3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1?ü
NVIC_Init(&NVIC_InitStructure); //?ù?Y???¨μ?2?êy3?ê??ˉVIC??′??÷
//USART 3?ê??ˉéè??
USART_InitStructure.USART_BaudRate = bound;//ò?°?éè???a9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×?3¤?a8??êy?Y??ê?
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò???í£?1??
USART_InitStructure.USART_Parity = USART_Parity_No;//?T????D£?é??
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//?Tó2?têy?Yá÷????
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ê?·¢?£ê?
USART_Init(USART1, &USART_InitStructure); //3?ê??ˉ′??ú
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//?a???D??
USART_Cmd(USART1, ENABLE); //ê1?ü′??ú
}
void USART1_IRQHandler(void) //′??ú1?D??·t??3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //?óê??D??(?óê?μ?μ?êy?Y±?D?ê?0x0d 0x0a?á?2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //?áè??óê?μ?μ?êy?Y
if((USART_RX_STA&0x8000)==0)//?óê??′íê3é
{
if(USART_RX_STA&0x4000)//?óê?μ?á?0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?óê?′í?ó,??D??aê?
else USART_RX_STA|=0x8000; //?óê?íê3éá?
}
else //?1??ê?μ?0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//?óê?êy?Y′í?ó,??D??aê??óê?
}
}
}
}
#ifdef OS_TICKS_PER_SEC //è?1?ê±?ó?ú??êy?¨ò?á?,?μ?÷òaê1ó?ucosIIá?.
OSIntExit();
#endif
}
#endif
bsp.c
#include "bsp_i2c.h"
#include "delay.h"
uint8_t ack_status=0;
uint8_t readByte[6];
uint8_t AHT20_status=0;
uint32_t H1=0; //Humility
uint32_t T1=0; //Temperature
uint8_t AHT20_OutData[4];
uint8_t AHT20sendOutData[10] = {0xFA, 0x06, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //í?íìê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
IIC_SCL=1;
IIC_SDA=1;
}
//2úéúIIC?eê?D?o?
void IIC_Start(void)
{
SDA_OUT(); //sda??ê?3?
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y
}
//2úéúIICí£?1D?o?
void IIC_Stop(void)
{
SDA_OUT();//sda??ê?3?
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL=1;
IIC_SDA=1;//·¢?íI2C×ü???áê?D?o?
delay_us(4);
}
//μè′yó|′eD?o?μ?à′
//·μ???μ£o1£??óê?ó|′e꧰ü
// 0£??óê?ó|′e3é1|
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDAéè???aê?è?
IIC_SDA=1;delay_us(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL=0;//ê±?óê?3?0
return 0;
}
//2úéúACKó|′e
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//2?2úéúACKó|′e
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//IIC·¢?íò???×??ú
//·μ??′ó?úóD?Tó|′e
//1£?óDó|′e
//0£??Tó|′e
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//à-μíê±?ó?aê?êy?Y′?ê?
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(2); //??TEA5767?aèy???óê±??ê?±?D?μ?
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
}
//?á1??×??ú£?ack=1ê±£?·¢?íACK£?ack=0£?·¢?ínACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDAéè???aê?è?
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(1);
}
if (!ack)
IIC_NAck();//·¢?ínACK
else
IIC_Ack(); //·¢?íACK
return receive;
}
void IIC_WriteByte(uint16_t addr,uint8_t data,uint8_t device_addr)
{
IIC_Start();
if(device_addr==0xA0) //eepromμ??·′óóú1×??ú
IIC_Send_Byte(0xA0 + ((addr/256)<<1));//·¢?í??μ??·
else
IIC_Send_Byte(device_addr); //·¢?÷?tμ??·
IIC_Wait_Ack();
IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
IIC_Wait_Ack();
IIC_Send_Byte(data); //·¢?í×??ú
IIC_Wait_Ack();
IIC_Stop();//2úéúò???í£?1ì??t
if(device_addr==0xA0) //
delay_ms(10);
else
delay_us(2);
}
uint16_t IIC_ReadByte(uint16_t addr,uint8_t device_addr,uint8_t ByteNumToRead) //?á??′??÷?ò?áêy?Y
{
uint16_t data;
IIC_Start();
if(device_addr==0xA0)
IIC_Send_Byte(0xA0 + ((addr/256)<<1));
else
IIC_Send_Byte(device_addr);
IIC_Wait_Ack();
IIC_Send_Byte(addr&0xFF); //·¢?íμíμ??·
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(device_addr+1); //·¢?÷?tμ??·
IIC_Wait_Ack();
if(ByteNumToRead == 1)//LM75???èêy?Y?a11bit
{
data=IIC_Read_Byte(0);
}
else
{
data=IIC_Read_Byte(1);
data=(data<<8)+IIC_Read_Byte(0);
}
IIC_Stop();//2úéúò???í£?1ì??t
return data;
}
/**********
*é???2?·??aIO?ú?£?éI2C????
*
*′ó?aò????aê??aAHT20μ?????I2C
*oˉêy??óDIICoíI2Cμ???±e£???×¢òa£?£?£?£?£?
*
*2020/2/23×?oóDT??è??ú
*
***********/
void read_AHT20_once(void)
{
delay_ms(10);
reset_AHT20();
delay_ms(10);
init_AHT20();
delay_ms(10);
startMeasure_AHT20();
delay_ms(80);
read_AHT20();
delay_ms(5);
}
void reset_AHT20(void)
{
I2C_Start();
I2C_WriteByte(0x70);
ack_status = Receive_ACK();
if(ack_status) printf("1");
else printf("1-n-");
I2C_WriteByte(0xBA);
ack_status = Receive_ACK();
if(ack_status) printf("2");
else printf("2-n-");
I2C_Stop();
/*
AHT20_OutData[0] = 0;
AHT20_OutData[1] = 0;
AHT20_OutData[2] = 0;
AHT20_OutData[3] = 0;
*/
}
void init_AHT20(void)
{
I2C_Start();
I2C_WriteByte(0x70);
ack_status = Receive_ACK();
if(ack_status) printf("3");
else printf("3-n-");
I2C_WriteByte(0xE1);
ack_status = Receive_ACK();
if(ack_status) printf("4");
else printf("4-n-");
I2C_WriteByte(0x08);
ack_status = Receive_ACK();
if(ack_status) printf("5");
else printf("5-n-");
I2C_WriteByte(0x00);
ack_status = Receive_ACK();
if(ack_status) printf("6");
else printf("6-n-");
I2C_Stop();
}
void startMeasure_AHT20(void)
{
//------------
I2C_Start();
I2C_WriteByte(0x70);
ack_status = Receive_ACK();
if(ack_status) printf("7");
else printf("7-n-");
I2C_WriteByte(0xAC);
ack_status = Receive_ACK();
if(ack_status) printf("8");
else printf("8-n-");
I2C_WriteByte(0x33);
ack_status = Receive_ACK();
if(ack_status) printf("9");
else printf("9-n-");
I2C_WriteByte(0x00);
ack_status = Receive_ACK();
if(ack_status) printf("10");
else printf("10-n-");
I2C_Stop();
}
void read_AHT20(void)
{
uint8_t i;
for(i=0; i<6; i++)
{
readByte[i]=0;
}
//-------------
I2C_Start();
I2C_WriteByte(0x71);
ack_status = Receive_ACK();
readByte[0]= I2C_ReadByte();
Send_ACK();
readByte[1]= I2C_ReadByte();
Send_ACK();
readByte[2]= I2C_ReadByte();
Send_ACK();
readByte[3]= I2C_ReadByte();
Send_ACK();
readByte[4]= I2C_ReadByte();
Send_ACK();
readByte[5]= I2C_ReadByte();
SendNot_Ack();
//Send_ACK();
I2C_Stop();
//--------------
if( (readByte[0] & 0x68) == 0x08 )
{
H1 = readByte[1];
H1 = (H1<<8) | readByte[2];
H1 = (H1<<8) | readByte[3];
H1 = H1>>4;
H1 = (H1*1000)/1024/1024;
T1 = readByte[3];
T1 = T1 & 0x0000000F;
T1 = (T1<<8) | readByte[4];
T1 = (T1<<8) | readByte[5];
T1 = (T1*2000)/1024/1024 - 500;
AHT20_OutData[0] = (H1>>8) & 0x000000FF;
AHT20_OutData[1] = H1 & 0x000000FF;
AHT20_OutData[2] = (T1>>8) & 0x000000FF;
AHT20_OutData[3] = T1 & 0x000000FF;
}
else
{
AHT20_OutData[0] = 0xFF;
AHT20_OutData[1] = 0xFF;
AHT20_OutData[2] = 0xFF;
AHT20_OutData[3] = 0xFF;
printf("꧰üá?");
}
printf("\r\n");
printf("???è:%d%d.%d",T1/100,(T1/10)%10,T1%10);
printf("êa?è:%d%d.%d",H1/100,(H1/10)%10,H1%10);
printf("\r\n");
}
uint8_t Receive_ACK(void)
{
uint8_t result=0;
uint8_t cnt=0;
IIC_SCL = 0;
SDA_IN();
delay_us(4);
IIC_SCL = 1;
delay_us(4);
while(READ_SDA && (cnt<100))
{
cnt++;
}
IIC_SCL = 0;
delay_us(4);
if(cnt<100)
{
result=1;
}
return result;
}
void Send_ACK(void)
{
SDA_OUT();
IIC_SCL = 0;
delay_us(4);
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 1;
delay_us(4);
IIC_SCL = 0;
delay_us(4);
SDA_IN();
}
void SendNot_Ack(void)
{
SDA_OUT();
IIC_SCL = 0;
delay_us(4);
IIC_SDA = 1;
delay_us(4);
IIC_SCL = 1;
delay_us(4);
IIC_SCL = 0;
delay_us(4);
IIC_SDA = 0;
delay_us(4);
}
void I2C_WriteByte(uint8_t input)
{
uint8_t i;
SDA_OUT();
for(i=0; i<8; i++)
{
IIC_SCL = 0;
delay_ms(5);
if(input & 0x80)
{
IIC_SDA = 1;
//delaymm(10);
}
else
{
IIC_SDA = 0;
//delaymm(10);
}
IIC_SCL = 1;
delay_ms(5);
input = (input<<1);
}
IIC_SCL = 0;
delay_us(4);
SDA_IN();
delay_us(4);
}
uint8_t I2C_ReadByte(void)
{
uint8_t resultByte=0;
uint8_t i=0, a=0;
IIC_SCL = 0;
SDA_IN();
delay_ms(4);
for(i=0; i<8; i++)
{
IIC_SCL = 1;
delay_ms(3);
a=0;
if(READ_SDA)
{
a=1;
}
else
{
a=0;
}
//resultByte = resultByte | a;
resultByte = (resultByte << 1) | a;
IIC_SCL = 0;
delay_ms(3);
}
SDA_IN();
delay_ms(10);
return resultByte;
}
void set_AHT20sendOutData(void)
{
/* --------------------------
* 0xFA 0x06 0x0A temperature(2 Bytes) humility(2Bytes) short Address(2 Bytes)
* And Check (1 byte)
* -------------------------*/
AHT20sendOutData[3] = AHT20_OutData[0];
AHT20sendOutData[4] = AHT20_OutData[1];
AHT20sendOutData[5] = AHT20_OutData[2];
AHT20sendOutData[6] = AHT20_OutData[3];
// AHT20sendOutData[7] = (drf1609.shortAddress >> 8) & 0x00FF;
// AHT20sendOutData[8] = drf1609.shortAddress & 0x00FF;
// AHT20sendOutData[9] = getXY(AHT20sendOutData,10);
}
void I2C_Start(void)
{
SDA_OUT();
IIC_SCL = 1;
delay_ms(4);
IIC_SDA = 1;
delay_ms(4);
IIC_SDA = 0;
delay_ms(4);
IIC_SCL = 0;
delay_ms(4);
}
void I2C_Stop(void)
{
SDA_OUT();
IIC_SDA = 0;
delay_ms(4);
IIC_SCL = 1;
delay_ms(4);
IIC_SDA = 1;
delay_ms(4);
}
delay.c
#include "delay.h"
#include "sys.h"
//STM32F103o?D?°?ày3ì
//?aoˉêy°?±?ày3ì
/********** mcudev.taobao.com 3??· ********/
//
//è?1?ê1ó?ucos,?ò°üà¨????μ?í·???t?′?é.
#if SYSTEM_SUPPORT_UCOS
#include "includes.h" //ucos ê1ó?
#endif
//
//STM32?a·¢°?
//ê1ó?SysTickμ???í¨??êy?£ê????ó3ù??DD1üàí
//°üà¨delay_us,delay_ms
//
static u8 fac_us=0;//us?óê±±?3?êy
static u16 fac_ms=0;//ms?óê±±?3?êy
#ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
//systick?D??·t??oˉêy,ê1ó?ucosê±ó?μ?
void SysTick_Handler(void)
{
OSIntEnter(); //??è??D??
OSTimeTick(); //μ÷ó?ucosμ?ê±?ó·t??3ìDò
OSIntExit(); //′¥·¢è????D??èí?D??
}
#endif
//3?ê??ˉ?ó3ùoˉêy
//μ±ê1ó?ucosμ?ê±oò,′?oˉêy?á3?ê??ˉucosμ?ê±?ó?ú??
//SYSTICKμ?ê±?ó1ì?¨?aHCLKê±?óμ?1/8
//SYSCLK:?μí3ê±?ó
void delay_init()
{
#ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
u32 reload;
#endif
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //????ía2?ê±?ó HCLK/8
fac_us=SystemCoreClock/8000000; //?a?μí3ê±?óμ?1/8
#ifdef OS_CRITICAL_METHOD //è?1?OS_CRITICAL_METHOD?¨ò?á?,?μ?÷ê1ó?ucosIIá?.
reload=SystemCoreClock/8000000; //?????óμ???êy′?êy μ¥???aK
reload*=1000000/OS_TICKS_PER_SEC;//?ù?YOS_TICKS_PER_SECéè?¨ò?3?ê±??
//reload?a24????′??÷,×?′ó?μ:16777216,?ú72M??,??o?1.86s×óóò
fac_ms=1000/OS_TICKS_PER_SEC;//′ú±íucos?éò??óê±μ?×?éùμ¥??
SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk; //?a??SYSTICK?D??
SysTick->LOAD=reload; //??1/OS_TICKS_PER_SEC???D??ò?′?
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //?a??SYSTICK
#else
fac_ms=(u16)fac_us*1000;//·?ucos??,′ú±í????msDèòaμ?systickê±?óêy
#endif
}
#ifdef OS_CRITICAL_METHOD //ê1ó?á?ucos
//?óê±nus
//nus?aòa?óê±μ?usêy.
void delay_us(u32 nus)
{
u32 ticks;
u32 told,tnow,tcnt=0;
u32 reload=SysTick->LOAD; //LOADμ??μ
ticks=nus*fac_us; //Dèòaμ??ú??êy
tcnt=0;
told=SysTick->VAL; //????è?ê±μ???êy?÷?μ
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow=ticks)break;//ê±??3?1y/μèóúòa?ó3ùμ?ê±??,?òí?3?.
}
};
}
//?óê±nms
//nms:òa?óê±μ?msêy
void delay_ms(u16 nms)
{
if(OSRunning==TRUE)//è?1?osò??-?ú?üá?
{
if(nms>=fac_ms)//?óê±μ?ê±??′óóúucosμ?×?éùê±???ü?ú
{
OSTimeDly(nms/fac_ms);//ucos?óê±
}
nms%=fac_ms; //ucosò??-?T·¨ìá1??a?′D?μ??óê±á?,2éó???í¨·?ê??óê±
}
delay_us((u32)(nms*1000)); //??í¨·?ê??óê±,′?ê±ucos?T·¨???ˉμ÷?è.
}
#else//2?ó?ucosê±
//?óê±nus
//nus?aòa?óê±μ?usêy.
void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us; //ê±???ó??
SysTick->VAL=0x00; //??????êy?÷
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //?aê?μ1êy
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//μè′yê±??μ?′?
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1?±???êy?÷
SysTick->VAL =0X00; //??????êy?÷
}
//?óê±nms
//×¢òanmsμ?·??§
//SysTick->LOAD?a24????′??÷,?ùò?,×?′ó?óê±?a:
//nms<=0xffffff*8*1000/SYSCLK
//SYSCLKμ¥???aHz,nmsμ¥???ams
//??72Mì??t??,nms<=1864
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms;//ê±???ó??(SysTick->LOAD?a24bit)
SysTick->VAL =0x00; //??????êy?÷
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //?aê?μ1êy
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//μè′yê±??μ?′?
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //1?±???êy?÷
SysTick->VAL =0X00; //??????êy?÷
}
#endif
sys.c
#include "sys.h"
//STM32F103o?D?°?ày3ì
//?aoˉêy°?±?ày3ì
/********** mcudev.taobao.com 3??· ********/
//
//STM32?a·¢°?
//?μí3?D??·?×ééè???ˉ
//********************************************************************************
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éè??NVIC?D??·?×é2:2???à??ó??è??£?2???ìó|ó??è??
)
https://github.com/Sunlight-Dazzling/stm32-AHT20/blob/master/User/stm32f10x_conf.h
https://blog.csdn.net/hhhhhh277523/article/details/111397514