- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.00
- * $Revision: 3 $
- * $Date: 19/06/20 10:26a $
- * [url=home.php?mod=space&uid=247401]@brief[/url] NUC230_240 Series GPIO Driver Sample Code
- *
- * [url=home.php?mod=space&uid=536309]@NOTE[/url] PA0~PA3(PIN71~PIN74) will output signal to driver stepmotor
- * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
- ******************************************************************************/
- #include <stdio.h>
- #include "NUC230_240.h"
- #define PLL_CLOCK 72000000
- #define d360 (4096)
- #define d180 (4096/2)
- #define d90 (4096/4)
- #define d45 (4096/8)
- unsigned char Table_StepMotor_Output[8] = {0x08, 0x0C, 0x04, 0x06, 0x02, 0x03, 0x01, 0x09};
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable Internal RC 22.1184MHz and HXT clock */
- CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk | CLK_PWRCON_XTL12M_EN_Msk);
- /* Waiting for Internal RC and HXT clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk);
- /* Set core clock as PLL_CLOCK from PLL */
- CLK_SetCoreClock(PLL_CLOCK);
- /* Enable UART module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- /* Select UART module clock source */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set GPB multi-function pins for UART0 RXD and TXD */
- SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
- SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
- }
- void UART0_Init()
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Reset UART0 */
- SYS_ResetModule(UART0_RST);
- /* Configure UART0 and set UART0 Baudrate */
- UART_Open(UART0, 115200);
- }
- void CW_MOTOR(unsigned int deg)
- {
- int i = 0, j = 0;
- for (j = 0; j < deg; j += 8)
- {
- for (i = 0; i < 8; i++) // Clockwise rotation
- {
- PA->DOUT = Table_StepMotor_Output[i];
- CLK_SysTickDelay(5000);
- }
- }
- }
- void CCW_MOTOR(unsigned int deg)
- {
- int i = 0, j = 0;
- for (j = 0; j < deg; j += 8)
- {
- for (i = 7; i >= 0; i--) // Anticlockwise rotation
- {
- PA->DOUT = Table_StepMotor_Output[i];
- CLK_SysTickDelay(5000);
- }
- }
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* MAIN function */
- /*---------------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- uint32_t Cnt ;
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, peripheral clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init UART0 for printf */
- UART0_Init();
- /* PA0~PA3(PIN71~PIN74) is configured as output to driver stepmotor */
- GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
- GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
- GPIO_SetMode(PA, BIT2, GPIO_PMD_OUTPUT);
- GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
- while (1)
- {
- CW_MOTOR(d360);
- for (Cnt = 0; Cnt < 10000000; ++Cnt) __nop();
- CCW_MOTOR(d180);
- for (Cnt = 0; Cnt < 100000000; ++Cnt) __nop();
- };
- }
- /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
|