- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- volatile uint8_t PWM0_Duty_Cycle = 50; //PWM占空比
- void Init_Uart (void);
- void UART_INT_HANDLE(uint32_t u32IntStatus);
- /*****************************
- ** Name: UART_INT_HANDLE
- ** Function: UART Callback function
- ** Input: u32IntStatus
- ** OutPut: None
- ** Data: 2011-03-17
- ** Note:
- ****************************/
- void UART_INT_HANDLE(uint32_t u32IntStatus)
- {
- uint8_t bInChar[1]={0xFF};
- if(u32IntStatus & DRVUART_RDAINT)
- {
- /* Get all the input characters */
- while(UART0->ISR.RDA_IF==1)
- {
- /* Get the character from UART Buffer */
- DrvUART_Read(UART_PORT0,bInChar,1);
- if ((bInChar[0]<='9')&&(bInChar[0]>='0'))
- {
- PWM0_Duty_Cycle= (bInChar[0]-0x30)*10;
- printf("您输入的占空比为:%d:100.\n",PWM0_Duty_Cycle);
- }
- else
- printf("您的输入有误!\n");
- }
- }
- }
- void Init_Uart(void)
- {
- STR_UART_T param;
- /*
- 声明 UART设置的结构体 位于DRVUART.H
- 结构体如下
- typedef struct DRVUART_STRUCT
- {
- uint32_t u32BaudRate;
- E_DATABITS_SETTINS u8cDataBits;
- E_STOPBITS_SETTINS u8cStopBits;
- E_PARITY_SETTINS u8cParity;
- E_FIFO_SETTINGS u8cRxTriggerLevel;
- uint8_t u8TimeOut ;
- }STR_UART_T;
- */
- DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); //使能UART时钟
- //SYSCLK->CLKSEL1.UART_S = 0; //UART时钟源选择. 00 =外部12MHz 晶振 01 = PLL 1x =内部 22MHz 振荡器
- DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能
- //outpw(&SYS->GPBMFP, inpw(&SYS->GPBMFP) | (0xF<<0));
- param.u32BaudRate = 115200; // 波特率
- param.u8cDataBits = DRVUART_DATABITS_8; // 数据位
- param.u8cStopBits = DRVUART_STOPBITS_1; // 停止位
- param.u8cParity = DRVUART_PARITY_NONE; // 校验位
- param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; // FIFO存储深度 1 字节
- param.u8TimeOut = 0; // FIFO超时设定
- /* Set UART Configuration */
- if(DrvUART_Open(UART_PORT0,¶m) != E_SUCCESS) // 串口开启、结构体整体赋值
- printf("UART0 open failed\n");
- /* u32Port -[in] UART Channel: UART_PORT0 / UART_PORT1 /UART_PORT2 */
- /* sParam -[in] the struct parameter to configure UART */
- /* Enable Interrupt and install the call back function */
- DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE);
- /*u32Port -[in] UART Channel: UART_PORT0 / UART_PORT1 / UART_PORT2 */
- /*u32InterruptFlag -[in] DRVUART_LININT/DRVUART_WAKEUPINT/DRVUART_BUFERRINT/DRVUART_RLSINT */
- /* DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT */
- /*pfncallback -[in] A function pointer for callback function */
- }
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: Key1键按下 调整占空比,灯变亮
- Key2键按下 输出周期为 12M/2/(99*+1) = 60KHZ 占空比为90%
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- uint8_t test = 250;
- uint8_t PWM0_Duty_Cycle_tmp = 50;
- uint16_t PWM_CNR0_tmp = 0;
- Set_System(); //封装一些初始化模块
- Init_Uart();
- printf("\n");
- printf("/*==========================\n");
- printf("======菜农 %d 助学计划======\n",test);
- printf("========NUC120助学板========\n");
- printf("======程序参考新唐BSP库======\n");
- printf("=======2010年04月09日=======\n");
- printf("=========PWM实验=========\n");
- printf("PWM基本配置如下:\n");
- printf("PWMA 预分频为:0x%x\n",PWMA->PPR.CP01);
- printf("PWMA 时钟选择寄存器:0x%x\n",PWMA->CSR.CSR0);
- PWM_CNR0_tmp = PWMA->CNR0;
- printf("PWM 计数器寄存器CNR0: 0x%x\n",PWM_CNR0_tmp);
- printf("PWM 比较寄存器 CMR0: 0x%x\n", PWMA->CMR0);
- printf("请输入0~9(占空比*10/100)调整占空比开始测试!\n");
- printf("==========================*/\n");
- while(1)
- {
- KeyCode = GetKey();
- switch(KeyCode)
- {
- case KEY1_DOWN_USER:
- {
- PWMA->CNR0 = 99;
- if(PWMA->CMR0 > 0)
- {
- PWMA->CMR0 = PWMA->CMR0 - 10;
- PWM0_Duty_Cycle_tmp = PWMA->CMR0;
- PWM0_Duty_Cycle = PWMA->CMR0;
- }
- else
- {
- PWMA->CMR0 = 90;
- PWM0_Duty_Cycle_tmp = PWMA->CMR0;
- PWM0_Duty_Cycle = PWMA->CMR0;
- }
- }
- break;
- case KEY2_DOWN_USER:
- {
- PWMA->CNR0 = 99;
- PWMA->CMR0 = 90;
- }
- break;
- default:break;
- }
- if (PWM0_Duty_Cycle_tmp!=PWM0_Duty_Cycle)
- {
- //更改CMR0 调整PWM0输出占空比
- PWM0_Duty_Cycle_tmp = PWM0_Duty_Cycle;
- if (0==PWM0_Duty_Cycle)
- {
- PWMA->CMR0 = 0;
- }
- else
- {
- PWMA->CMR0 = PWM0_Duty_Cycle;
- }
- }
- }
- }
初始化
-
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void Set_System(void)
- {
- InitButtonVar(); //按键初始化
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
-
- TIMER_Configuration(); //配置TIMER
-
- PWM_Configuration(); //配置PWM
- }
- /*************************************************************************************
- ** Function name: RCC_Configuration
- ** Descriptions: 系统时钟源设置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void RCC_Configuration(void)
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
- // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
- // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
- // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
- delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: GPIO配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void GPIO_Configuration()
- {
- DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT ); //led端口设置为输出
- DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- DrvGPIO_Open( E_GPA, 12, E_IO_OUTPUT );//PWM01端口设置为输出
- DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
- DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
- }
- /*************************************************************************************
- ** Function name: TIMER_Configuration
- ** Descriptions: TIMER配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void TIMER_Configuration()
- {
- DrvTIMER_Init(); //初始化定时器
- DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,0); //使用外设时注意必须设置该外设的时钟 设置TIMER0的时钟源为外部12MHZ
- DrvTIMER_Open(E_TMR0,1000,E_PERIODIC_MODE); //设定定时器timer0的tick周期,并且启动定时器:定时器通道 TMR0 每秒1000次 周期模式
-
- DrvTIMER_SetTimerEvent(E_TMR0,10,(TIMER_CALLBACK) Timer0_Callback,0); //安装一个10ms定时处理事件到 timer0通道
- DrvTIMER_EnableInt(E_TMR0); //使能定时器中断 //TIMER0->TCSR.IE = 1
- DrvTIMER_Start(E_TMR0); //定时器timer0开始计数 //TIMER0->TCSR.CEN = 1;
- }
- /*************************************************************************************
- ** Function name: Timer0_Callback
- ** Descriptions: 定时处理事件 用来做按键扫描
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void Timer0_Callback (void)
- {
- KeyPro(); //按键扫描 该函数在 button.c 中实现
- }
- /*************************************************************************************
- ** Function name: PWM_Configuration
- ** Descriptions: PWM配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void PWM_Configuration()
- {
- S_DRVPWM_TIME_DATA_T sPt;
- /*
- 声明 PWM Timer设置的结构体 位于DRVPWM.H
- 结构体如下:
- typedef struct
- {
- uint8_t u8Mode;
- uint8_t u8HighPulseRatio;
- uint8_t u8ClockSelector;
- uint8_t u8PreScale;
- uint32_t u32Frequency;
- uint32_t u32Duty;
- int32_t i32Inverter;
- }S_DRVPWM_TIME_DATA_T;
- */
-
- /* PWM Timer property */
- sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式*/
- sPt.u32Frequency = 100; /*PWM 频率 为400HZ即2500us为一周期*/
- sPt.u8HighPulseRatio =25; /* 高脉冲宽度时间所占周期的百分比: 50%*/
- sPt.i32Inverter = 0; /*反向关闭*/
- /* Enable PWM clock */
- DrvPWM_Open(); //打开 PWM 时钟并且复位PWM
-
- /* Select PWM engine clock */
- //DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M);//设置PWM 定时器0 为外部12 MHz crystal 时钟
- DrvSYS_SelectIPClockSource(E_SYS_PWM01_CLKSRC,0); //使用外设时注意必须设置该外设的时钟 设置PWM01的时钟源为外部12MHZ
- /* Set PWM Timer0 Configuration */
- DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt); //配置PWM 定时器0的一些参数 如配置频率/脉冲/模式/逆转功能
- /* Enable Output for PWM Timer0 */
- DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1); //使能或关闭PWM定时器0对应的IO口输出使能
- /* Set PWM pins */
- DrvGPIO_InitFunction(E_FUNC_PWM01); //指定多功能引脚 即 PA12,PA13为PWM0和PWM1
-
- /* Enable the PWM Timer 0 */
- DrvPWM_Enable(DRVPWM_TIMER0, 1); //使能/关闭PWM定时器0
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void delay_ms(uint32_t count)
- {
- uint32_t i,j;
- for(i=count;i>0;i--)
- for(j=2395;j>0;j--);
- }
工程