Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.0 kB
1
Indexable
Never
/******************************************************************************
 * @file     main.c
 * @version  V1.00
 * $Revision: 4 $
 * $Date: 15/12/27 1:12p $
 * @brief    A project template for Nano103 MCU.
 *
 * @note
 * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "Nano103.h"

#include "uart.h"

#define PLLCON_SETTING      CLK_PLLCON_36MHz_HXT
#define PLL_CLOCK           36000000

// 1ms = 1000us = 36000
#define delay_us(x)  CLK_SysTickDelay(x)
#define delay_ms(x)  CLK_SysTickDelay(x * 10000)

//###########################################
// Pin Defines
#define SW_EXTRA1 		PB3
#define SW_EXTRA2 		PC0
#define SW_DOOR_OPEN 	PB2
#define SW_ENABLE	 	PB1
#define SW_MID		 	PB0
#define SW_STRT		 	PB5
#define SW_END		 	PB4

#define RELAY_DOOR	 	PC1

#define MTR_PWM		 	PA9
#define MTR_EN		 	PA14
#define MTR_EN_LEFT	 	PA9
#define MTR_EN_RIGHT 	PA8

#define ACS712_DATA	 	PA6

#define PIN_SCL		 	PA5
#define PIN_SDA		 	PA4

#define BT_RXD		 	PA3
#define BT_TXD		 	PA2
//###########################################

#define RXBUFSIZE 1024
uint8_t g_u8SendData[12] = {0};
uint8_t g_u8RecData[RXBUFSIZE]  = {0};

volatile uint32_t g_u32comRbytes = 0;
volatile uint32_t g_u32comRhead  = 0;
volatile uint32_t g_u32comRtail  = 0;
volatile int32_t g_bWait         = TRUE;
volatile int32_t g_i32pointer = 0;

void UART1_TEST_HANDLE(void);

char* msg = "Furkan\n";

void UART1_IRQHandler(void)
{
    UART1_TEST_HANDLE();
}

void UART1_TEST_HANDLE()
{
	uint8_t u8InChar=0xFF;
	uint32_t u32IntSts= UART1->INTSTS;

	/* Check Receive Data */
	if((u32IntSts & UART_INTSTS_RDAIF_Msk) || (u32IntSts & UART_INTSTS_RXTOIF_Msk))
	{
		printf("\nInput:");

		/* Get all the input characters */
		while(UART_IS_RX_READY(UART1))
		{
			/* Get the character from UART Buffer */
			u8InChar = UART_READ(UART1);           /* Rx trigger level is 1 byte*/

			printf("%c ", u8InChar);

			if(u8InChar == '0')
			{
				g_bWait = FALSE;
			}

			/* Check if buffer full */
			if(g_u32comRbytes < RXBUFSIZE)
			{
				/* Enqueue the character */
				g_u8RecData[g_u32comRtail] = u8InChar;
				g_u32comRtail = (g_u32comRtail == (RXBUFSIZE-1)) ? 0 : (g_u32comRtail+1);
				g_u32comRbytes++;
			}
		}
		printf("\nTransmission Test:");
	}

	/* Check Tx empty */
	if(u32IntSts & UART_INTSTS_THREIF_Msk)
	{
		uint16_t tmp;
		tmp = g_u32comRtail;
		if(g_u32comRhead != tmp)
		{
			u8InChar = g_u8RecData[g_u32comRhead];
			/* print the received char on screen */
			UART_WRITE(UART1,u8InChar);
			g_u32comRhead = (g_u32comRhead == (RXBUFSIZE-1)) ? 0 : (g_u32comRhead+1);
			g_u32comRbytes--;
		}
	}
}

void SYS_Init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable External XTAL (4~24 MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
    //CLK->PWRCTL = (CLK->PWRCTL & ~CLK_PWRCTL_HXTEN_Msk) | (0x1 << CLK_PWRCTL_HXTEN_Pos); // HXT Enabled

    /* Waiting for 12MHz clock ready */
    CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

    /* Switch HCLK clock source to HXT */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT,CLK_HCLK_CLK_DIVIDER(1));
    //CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_HCLKSEL_Msk)|CLK_CLKSEL0_HCLKSEL_HXT;

    /* Enable UART clock */
    CLK_EnableModuleClock(UART1_MODULE);

    /* Select UART clock source from HXT */
    CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL2_UART1SEL_HXT, CLK_UART1_CLK_DIVIDER(1));

    CLK->APBCLK |= CLK_APBCLK_UART1_EN;

    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
    SystemCoreClockUpdate();


    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Set GPB multi-function pins for UART0 RXD and TXD */
    //SYS->GPB_MFPL &= ~(SYS_GPB_MFPL_PB0MFP_Msk | SYS_GPB_MFPL_PB1MFP_Msk);
    //SYS->GPB_MFPL |= (SYS_GPB_MFPL_PB0MFP_UART0_RXD | SYS_GPB_MFPL_PB1MFP_UART0_TXD);
    SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
    SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART1_TXD | SYS_GPA_MFPL_PA2MFP_UART1_RXD);
    /* Lock protected registers */
    SYS_LockReg();

    UART_Open(UART1, 9600);

    UART1->LINE = (UART1->LINE &~ UART_LINE_RFITL_Msk) | UART_LINE_RFITL_8BYTES;
    UART_SetTimeoutCnt(UART1,0x3E);
    UART_ENABLE_INT(UART1, (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk | UART_INTEN_RXTOIEN_Msk));
    NVIC_EnableIRQ(UART1_IRQn);
}

void GPIO_Init(void)
{
	GPIO_SetMode(PC, BIT1, GPIO_PMD_OUTPUT);

	GPIO_SetMode(PA, BIT15, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT14, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT9, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT8, GPIO_PMD_OUTPUT);

	RELAY_DOOR = 0;

	MTR_PWM = 0;
	MTR_EN = 1;
	MTR_EN_LEFT = 0;
	MTR_EN_RIGHT = 0;
}


int main()
{
	SYS_Init();
	GPIO_Init();

	MTR_EN = 0;
	MTR_PWM = 1;

	MTR_EN_LEFT = 0;
	MTR_EN_RIGHT = 0;

    /* Got no where to go, just loop forever */
	while(1)
	{
		//int i = 0;
		for(int i = 0; msg[i] != '\0'; i++){
			UART_WRITE(UART1, msg[i]);
		}

		printf("printf\n");

	    //UART_Write(UART1, "Furkan\n", 7);
		/*
		MTR_EN_LEFT = 1;
		delay_ms(3000);
		MTR_EN_LEFT = 0;

		delay_ms(5000);

		MTR_EN_RIGHT = 1;
		delay_ms(3000);
		MTR_EN_RIGHT = 0;

		delay_ms(5000);
		*/
		/*
		RELAY_DOOR = 0;
		delay_ms(1000);
		RELAY_DOOR = 1;
		delay_ms(1000);
		*/
		delay_ms(5000);
	}
}

/*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/