参考库函数例程APP_DMA_AD和APP_HSPWM
AI8051U传统风格库函数例程包发布-持续更新, 20250214 - 软件库函数/原理图库/PCB库/最小包装 国芯技术交流网站 - AI32位8051交流社区
DMA_AD:
- #include "app.h"
-
- /*---------------------------------------------------------------------*/
- /* --- Web: www.STCAI.com ---------------------------------------------*/
- /*---------------------------------------------------------------------*/
-
- #include "APP_DMA_AD.h"
- #include "AI8051U_GPIO.h"
- #include "AI8051U_ADC.h"
- #include "AI8051U_UART.h"
- #include "AI8051U_DMA.h"
- #include "AI8051U_NVIC.h"
- #include "string.h"
-
- /************* 功能说明 **************
-
- 本程序演示多路ADC DMA采样.
-
- 初始化时先把要ADC转换的引脚设置为高阻输入.
-
- 设置数据批量传输(DMA)功能,所有通道一次循环采集的数据自动存放到DMA定义的xdata空间.
-
- 通过串口1(P3.0 P3.1)将DMA定义的xdata空间所收到的数据发送给上位机,波特率115200,N,8,1
-
- 用定时器做波特率发生器,建议使用1T模式(除非低波特率用12T),并选择可被波特率整除的时钟频率,以提高精度.
-
- 下载时, 选择时钟 40MHz (可以在配置文件"config.h"中修改).
-
- ******************************************/
-
-
- //========================================================================
- // 本地常量声明
- //========================================================================
-
- #define ADC_CH 16 /* 1~16, ADC转换通道数, 需同步修改转换通道 */
- #define ADC_DATA 12 /* 6~n, 每个通道ADC转换数据总数, 2*转换次数+4, 需同步修改转换次数 */
-
- //========================================================================
- // 本地变量声明
- //========================================================================
-
- u8 chn = 0;
- u8 xdata DmaAdBuffer[ADC_CH][ADC_DATA];
-
- //========================================================================
- // 本地函数声明
- //========================================================================
-
-
- //========================================================================
- // 外部函数和变量声明
- //========================================================================
-
-
- //========================================================================
- // 函数: ADtoUART_init
- // 描述: 用户初始化程序.
- // 参数: None.
- // 返回: None.
- // 版本: V1.0, 2020-09-28
- //========================================================================
- void DMA_AD_init(void)
- {
- ADC_InitTypeDef ADC_InitStructure; //结构定义
- COMx_InitDefine COMx_InitStructure; //结构定义
- DMA_ADC_InitTypeDef DMA_ADC_InitStructure; //结构定义
- memset(&ADC_InitStructure,0,sizeof(ADC_InitTypeDef)); //结构体初始化
- memset(&COMx_InitStructure,0,sizeof(COMx_InitDefine)); //结构体初始化
- memset(&DMA_ADC_InitStructure,0,sizeof(DMA_ADC_InitTypeDef)); //结构体初始化
-
- //----------------------------------------------
- P0_MODE_IN_HIZ(GPIO_Pin_LOW | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6); //P0.0~P0.6 设置为高阻输入
- P1_MODE_IN_HIZ(GPIO_Pin_All); //P1.0~P1.7 设置为高阻输入
- P3_MODE_IO_PU(GPIO_Pin_0 | GPIO_Pin_1); //P3.0,P3.1 设置为准双向口
-
- //----------------------------------------------
- COMx_InitStructure.UART_Mode = UART_8bit_BRTx; //模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
- COMx_InitStructure.UART_BRT_Use = BRT_Timer1; //选择波特率发生器, BRT_Timer1/BRT_Timer2
- COMx_InitStructure.UART_BaudRate = 115200ul; //波特率, 110 ~ 115200
- COMx_InitStructure.UART_RxEnable = ENABLE; //接收允许, ENABLE或DISABLE
- UART_Configuration(UART1, &COMx_InitStructure); //初始化串口 UART1,UART2,UART3,UART4
- NVIC_UART1_Init(ENABLE,Priority_1); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- //----------------------------------------------
- ADC_InitStructure.ADC_SMPduty = 31; //ADC 模拟信号采样时间控制, 0~31(注意: SMPDUTY 一定不能设置小于 10)
- ADC_InitStructure.ADC_CsSetup = 0; //ADC 通道选择时间控制 0(默认),1
- ADC_InitStructure.ADC_CsHold = 1; //ADC 通道选择保持时间控制 0,1(默认),2,3
- ADC_InitStructure.ADC_Speed = ADC_SPEED_2X16T; //设置 ADC 工作时钟频率 ADC_SPEED_2X1T~ADC_SPEED_2X16T
- ADC_InitStructure.ADC_AdjResult = ADC_RIGHT_JUSTIFIED; //ADC结果调整, ADC_LEFT_JUSTIFIED,ADC_RIGHT_JUSTIFIED
- ADC_Inilize(&ADC_InitStructure); //初始化
- ADC_PowerControl(ENABLE); //ADC电源开关, ENABLE或DISABLE
- NVIC_ADC_Init(DISABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
-
- //----------------------------------------------
- DMA_ADC_InitStructure.DMA_Enable = ENABLE; //DMA使能 ENABLE,DISABLE
- DMA_ADC_InitStructure.DMA_Channel = 0xffff; //ADC通道使能寄存器, 1:使能, bit15~bit0 对应 ADC15~ADC0
- DMA_ADC_InitStructure.DMA_Buffer = (u16)DmaAdBuffer; //ADC转换数据存储地址
- DMA_ADC_InitStructure.DMA_Times = ADC_4_Times; //每个通道转换次数, ADC_1_Times,ADC_2_Times,ADC_4_Times,ADC_8_Times,ADC_16_Times,ADC_32_Times,ADC_64_Times,ADC_128_Times,ADC_256_Times
- DMA_ADC_Inilize(&DMA_ADC_InitStructure); //初始化
- NVIC_DMA_ADC_Init(ENABLE,Priority_0,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0~Priority_3; 总线优先级(低到高) Priority_0~Priority_3
- DMA_ADC_TRIG(); //触发启动转换
- }
-
- //========================================================================
- // 函数: Sample_DMA_AD
- // 描述: 用户应用程序.
- // 参数: None.
- // 返回: None.
- // 版本: V1.0, 2020-09-24
- //========================================================================
- void Sample_DMA_AD(void)
- {
- u8 i,n;
- u16 j;
-
-
-
- if(DmaADCFlag)
- {
- DmaADCFlag = 0;
- for(i=0; i<ADC_CH; i++)
- {
- for(n=0; n<ADC_DATA; n++)
- {
- #if defined(_USE_KEILC251_COMPILER_) || defined(_USE_IAR_COMPILER_) || defined(_USE_SDCC_COMPILER_)
- printf("0x%02x ",DmaAdBuffer[i][n]); //第1组数据,...,第n组数据,AD通道,平均余数,平均值
- #else
- printf("0x%02bx ",DmaAdBuffer[i][n]); //第1组数据,...,第n组数据,AD通道,平均余数,平均值
- #endif
-
- }
- printf("\r\n");
- }
- printf("\r\n");
- DMA_ADC_TRIG(); //重新触发启动ADC转换
- }
- delay_ms(500);
- }
-
复制代码
HSPWM使用:
- #include "app.h"
-
- /*---------------------------------------------------------------------*/
- /* --- Web: www.STCAI.com ---------------------------------------------*/
- /*---------------------------------------------------------------------*/
-
- #include "APP_HSPWM.h"
- #include "AI8051U_Clock.h"
- #include "AI8051U_GPIO.h"
- #include "AI8051U_PWM.h"
- #include "AI8051U_NVIC.h"
-
- /************* 功能说明 **************
-
- 高速高级PWM定时器 PWM1P/PWM1N,PWM2P/PWM2N,PWM3P/PWM3N,PWM4P/PWM4N 每个通道都可独立实现PWM输出,或者两两互补对称输出.
-
- 8个通道PWM设置对应P0的8个端口.
-
- 高级PWM定时器 PWM5,PWM6,PWM7,PWM8 每个通道都可独立实现PWM输出.
-
- 通过P0口上连接的8个LED灯,利用PWM实现呼吸灯效果.
-
- PWM周期和占空比可以根据需要自行设置,最高可达65535.
-
- 下载时, 选择时钟 24MHz (可以在配置文件"config.h"中修改).
-
- ******************************************/
-
- //========================================================================
- // 本地常量声明
- //========================================================================
-
-
- //========================================================================
- // 本地变量声明
- //========================================================================
-
- PWMx_Duty PWMA_Duty;
- bit PWM1_Flag;
- bit PWM2_Flag;
- bit PWM3_Flag;
- bit PWM4_Flag;
- PWMx_Duty PWMB_Duty;
- bit PWM5_Flag;
- bit PWM6_Flag;
- bit PWM7_Flag;
- bit PWM8_Flag;
- //========================================================================
- // 本地函数声明
- //========================================================================
-
-
- //========================================================================
- // 外部函数和变量声明
- //========================================================================
-
- extern PWMx_Duty PWMA_Duty;
- extern bit PWM1_Flag;
- extern bit PWM2_Flag;
- extern bit PWM3_Flag;
- extern bit PWM4_Flag;
-
- extern PWMx_Duty PWMB_Duty;
- extern bit PWM5_Flag;
- extern bit PWM6_Flag;
- extern bit PWM7_Flag;
- extern bit PWM8_Flag;
-
- //========================================================================
- // 函数: HSPWM_init
- // 描述: 用户初始化程序.
- // 参数: None.
- // 返回: None.
- // 版本: V1.0, 2021-05-27
- //========================================================================
- void HSPWM_init(void)
- {
- HSPWMx_InitDefine PWMx_InitStructure;
- InitStruct(PWMx_InitStructure);
- InitStruct(PWMA_Duty);
- InitStruct(PWMB_Duty);
-
- PWMA_Duty.PWM1_Duty = 128;
- PWMA_Duty.PWM2_Duty = 256;
- PWMA_Duty.PWM3_Duty = 512;
- PWMA_Duty.PWM4_Duty = 1024;
-
- PWMB_Duty.PWM5_Duty = 128;
- PWMB_Duty.PWM6_Duty = 256;
- PWMB_Duty.PWM7_Duty = 512;
- PWMB_Duty.PWM8_Duty = 1024;
-
- HSPllClkConfig(MCKSEL_HIRC,PLL_96M,0); //系统时钟选择,PLL时钟选择,时钟分频系数
-
- PWMx_InitStructure.PWM_EnoSelect= ENO1P|ENO2P|ENO3P|ENO4P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- PWMx_InitStructure.PWM_Period = 2047; //周期时间, 0~65535
- PWMx_InitStructure.PWM_DeadTime = 0; //死区发生器设置, 0~255
- PWMx_InitStructure.PWM_MainOutEnable= ENABLE; //主输出使能, ENABLE,DISABLE
- PWMx_InitStructure.PWM_CEN_Enable = ENABLE; //使能计数器, ENABLE,DISABLE
- HSPWM_Configuration(PWMA, &PWMx_InitStructure, &PWMA_Duty); //初始化PWM通用寄存器, PWMA,PWMB
- PWMx_InitStructure.PWM_EnoSelect= ENO5P|ENO6P|ENO7P|ENO8P; //输出通道选择, ENO1P,ENO1N,ENO2P,ENO2N,ENO3P,ENO3N,ENO4P,ENO4N / ENO5P,ENO6P,ENO7P,ENO8P
- HSPWM_Configuration(PWMB, &PWMx_InitStructure, &PWMB_Duty); //初始化PWM通用寄存器, PWMA,PWMB
-
- PWM1_USE_P00P01();
- PWM2_USE_P02P03();
- PWM3_USE_P04P05();
- PWM4_USE_P06P07();
-
- PWM5_USE_P01();
- PWM6_USE_P03();
- PWM7_USE_P05();
- PWM8_USE_P07();
-
- P0_MODE_OUT_PP(GPIO_Pin_All); //P0 设置为推挽输出
- P4_MODE_IO_PU(GPIO_Pin_0); //P4.0 设置为准双向口
- NVIC_PWM_Init(PWMA,DISABLE,Priority_0);
- NVIC_PWM_Init(PWMB,DISABLE,Priority_0);
- P40 = 0; //打开实验箱LED电源
- }
-
- //========================================================================
- // 函数: Sample_HSSPI
- // 描述: 用户应用程序.
- // 参数: None.
- // 返回: None.
- // 版本: V1.0, 2021-05-27
- //========================================================================
- void Sample_HSPWM(void)
- {
- if(!PWM1_Flag)
- {
- PWMA_Duty.PWM1_Duty++;
- if(PWMA_Duty.PWM1_Duty >= 2047) PWM1_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM1_Duty--;
- if(PWMA_Duty.PWM1_Duty <= 0) PWM1_Flag = 0;
- }
-
- if(!PWM2_Flag)
- {
- PWMA_Duty.PWM2_Duty++;
- if(PWMA_Duty.PWM2_Duty >= 2047) PWM2_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM2_Duty--;
- if(PWMA_Duty.PWM2_Duty <= 0) PWM2_Flag = 0;
- }
-
- if(!PWM3_Flag)
- {
- PWMA_Duty.PWM3_Duty++;
- if(PWMA_Duty.PWM3_Duty >= 2047) PWM3_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM3_Duty--;
- if(PWMA_Duty.PWM3_Duty <= 0) PWM3_Flag = 0;
- }
-
- if(!PWM4_Flag)
- {
- PWMA_Duty.PWM4_Duty++;
- if(PWMA_Duty.PWM4_Duty >= 2047) PWM4_Flag = 1;
- }
- else
- {
- PWMA_Duty.PWM4_Duty--;
- if(PWMA_Duty.PWM4_Duty <= 0) PWM4_Flag = 0;
- }
-
- if(!PWM5_Flag)
- {
- PWMB_Duty.PWM5_Duty++;
- if(PWMB_Duty.PWM5_Duty >= 2047) PWM5_Flag = 1;
- }
- else
- {
- PWMB_Duty.PWM5_Duty--;
- if(PWMB_Duty.PWM5_Duty <= 0) PWM5_Flag = 0;
- }
-
- if(!PWM6_Flag)
- {
- PWMB_Duty.PWM6_Duty++;
- if(PWMB_Duty.PWM6_Duty >= 2047) PWM6_Flag = 1;
- }
- else
- {
- PWMB_Duty.PWM6_Duty--;
- if(PWMB_Duty.PWM6_Duty <= 0) PWM6_Flag = 0;
- }
-
- if(!PWM7_Flag)
- {
- PWMB_Duty.PWM7_Duty++;
- if(PWMB_Duty.PWM7_Duty >= 2047) PWM7_Flag = 1;
- }
- else
- {
- PWMB_Duty.PWM7_Duty--;
- if(PWMB_Duty.PWM7_Duty <= 0) PWM7_Flag = 0;
- }
-
- if(!PWM8_Flag)
- {
- PWMB_Duty.PWM8_Duty++;
- if(PWMB_Duty.PWM8_Duty >= 2047) PWM8_Flag = 1;
- }
- else
- {
- PWMB_Duty.PWM8_Duty--;
- if(PWMB_Duty.PWM8_Duty <= 0) PWM8_Flag = 0;
- }
-
- UpdateHSPwm(PWMA, &PWMA_Duty);
- UpdateHSPwm(PWMB, &PWMB_Duty);
- }
-
复制代码
|