You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.8 KiB
C

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "SWM221.h"
void SerialInit(void);
void TestSignal(void);
int main(void)
{
SystemInit();
SerialInit();
TestSignal(); //产生测试信号供Input Capture功能测量
PORT_Init(PORTA, PIN14, PORTA_PIN14_TIMR0_IN, 1);
TIMR_Init(TIMR0, TIMR_MODE_IC, CyclesPerUs, 10000000, 0); //最大测量脉宽1us*10_000_000 = 10s
TIMR_IC_Init(TIMR0, 1, 1);
TIMR_Start(TIMR0);
while(1==1)
{
}
}
void TestSignal(void)
{
PWM_InitStructure PWM_initStruct;
PORT_Init(PORTA, PIN3, PORTA_PIN3_PWM0A, 0);
PWM_initStruct.Mode = PWM_EDGE_ALIGNED;
PWM_initStruct.Clkdiv = 60; //F_PWM = 60M/60 = 1M
PWM_initStruct.Period = 10000; //1M/10000 = 100Hz
PWM_initStruct.HdutyA = 2500; //2500/10000 = 25%高电平脉宽2.5ms低电平脉宽7.5ms
PWM_initStruct.DeadzoneA = 0;
PWM_initStruct.IdleLevelA = 0;
PWM_initStruct.IdleLevelAN= 0;
PWM_initStruct.OutputInvA = 0;
PWM_initStruct.OutputInvAN= 0;
PWM_initStruct.HdutyB = 7500; //5000/10000 = 50%
PWM_initStruct.DeadzoneB = 0;
PWM_initStruct.IdleLevelB = 0;
PWM_initStruct.IdleLevelBN= 0;
PWM_initStruct.OutputInvB = 0;
PWM_initStruct.OutputInvBN= 0;
PWM_initStruct.UpOvfIE = 0;
PWM_initStruct.DownOvfIE = 0;
PWM_initStruct.UpCmpAIE = 0;
PWM_initStruct.DownCmpAIE = 0;
PWM_initStruct.UpCmpBIE = 0;
PWM_initStruct.DownCmpBIE = 0;
PWM_Init(PWM0, &PWM_initStruct);
PWM_Init(PWM1, &PWM_initStruct);
PWM_Start(PWM0_MSK);
}
void TIMR0_Handler(void)
{
if(TIMR_IC_CaptureH_INTStat(TIMR0))
{
TIMR_IC_CaptureH_INTClr(TIMR0);
printf("H: %dus\r\n", TIMR_IC_GetCaptureH(TIMR0));
}
else if(TIMR_IC_CaptureL_INTStat(TIMR0))
{
TIMR_IC_CaptureL_INTClr(TIMR0);
printf("L: %dus\r\n", TIMR_IC_GetCaptureL(TIMR0));
}
}
void SerialInit(void)
{
UART_InitStructure UART_initStruct;
PORT_Init(PORTA, PIN0, PORTA_PIN0_UART0_RX, 1); //GPIOA.0配置为UART0 RXD
PORT_Init(PORTA, PIN1, PORTA_PIN1_UART0_TX, 0); //GPIOA.1配置为UART0 TXD
UART_initStruct.Baudrate = 57600;
UART_initStruct.DataBits = UART_DATA_8BIT;
UART_initStruct.Parity = UART_PARITY_NONE;
UART_initStruct.StopBits = UART_STOP_1BIT;
UART_initStruct.RXThreshold = 3;
UART_initStruct.RXThresholdIEn = 0;
UART_initStruct.TXThreshold = 3;
UART_initStruct.TXThresholdIEn = 0;
UART_initStruct.TimeoutTime = 10;
UART_initStruct.TimeoutIEn = 0;
UART_Init(UART0, &UART_initStruct);
UART_Open(UART0);
}
/******************************************************************************************************************************************
* 函数名称: fputc()
* 功能说明: printf()使用此函数完成实际的串口打印动作
* 输 入: int ch 要打印的字符
* FILE *f 文件句柄
* 输 出: 无
* 注意事项: 无
******************************************************************************************************************************************/
int fputc(int ch, FILE *f)
{
UART_WriteByte(UART0, ch);
while(UART_IsTXBusy(UART0));
return ch;
}