/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technology Corp.
// E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
// Application: ADC Function
// ADC Channel 0(P0.1 default) as ADC input
//
// Output : UART show ADC output result on hyper-terminal
//***********************************************************************************************************
//------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
// <o0.6> UART pin Select
// <0=> Select P1.0, P1.1 as UART pin(default)
// <1=> Select P2.6, P2.7 as UART pin(28 pin only)
//-------------------------------- <<< end of configuration section >>> -------------------------------------
#define Uart_Port_Sel 0x00
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "ADC.h"
#include "Version.h"
#include <intrins.h>
#include <stdio.h>
#include <string.h>
//-----------------------------------------------------------------------------------------------------------
void Enable_ADC_Interrupt(void)
{
EADC = 1;
EA = 1;
}
//-----------------------------------------------------------------------------------------------------------
void ADC_Channel_Sel(E_ADCCNL_SEL channel)
{
switch (channel)
{
case E_CHANNEL0: // P0.1 (default)
clr_AADR2;
clr_AADR1;
clr_AADR0;
break;
case E_CHANNEL1: // P0.2
clr_AADR2;
clr_AADR1;
set_AADR0;
break;
case E_CHANNEL2: // P0.3
clr_AADR2;
set_AADR1;
clr_AADR0;
break;
case E_CHANNEL3: // P0.4
clr_AADR2;
set_AADR1;
set_AADR0;
break;
case E_CHANNEL4: // P0.5
set_AADR2;
clr_AADR1;
clr_AADR0;
break;
case E_CHANNEL5: // P0.6
set_AADR2;
clr_AADR1;
set_AADR0;
break;
case E_CHANNEL6: // P0.7
set_AADR2;
set_AADR1;
clr_AADR0;
break;
case E_CHANNEL7: // P2.6
set_AADR2;
set_AADR1;
set_AADR0;
break;
}
}
//-----------------------------------------------------------------------------------------------------------
void Set_ADC_Input_Mode(E_ADCCNL_SEL channel)
{
switch (channel)
{
case E_CHANNEL0: // ADC0(P0.1) is input-only mode
P0DIDS |= SET_BIT1; // Disable digital function for P0.1
P0M1 |= SET_BIT1;
P0M2 &= CLR_BIT1;
break;
case E_CHANNEL1: // ADC1(P0.2) is input-only mode
P0DIDS |= SET_BIT2; // Disable digital function for P0.2
P0M1 |= SET_BIT2;
P0M2 &= CLR_BIT2;
break;
case E_CHANNEL2: // ADC2(P0.3) is input-only mode
P0DIDS |= SET_BIT3; // Disable digital function for P0.3
P0M1 |= SET_BIT3;
P0M2 &= CLR_BIT3;
break;
case E_CHANNEL3: // ADC3(P0.4) is input-only mode
P0DIDS |= SET_BIT4; // Disable digital function for P0.4
P0M1 |= SET_BIT4;
P0M2 &= CLR_BIT4;
break;
case E_CHANNEL4: // ADC4(P0.5) is input-only mode
P0DIDS |= SET_BIT5; // Disable digital function for P0.5
P0M1 |= SET_BIT5;
P0M2 &= CLR_BIT5;
break;
case E_CHANNEL5: // ADC5(P0.6) is input-only mode
P0DIDS |= SET_BIT6; // Disable digital function for P0.6
P0M1 |= SET_BIT6;
P0M2 &= CLR_BIT6;
break;
case E_CHANNEL6: // ADC6(P0.7) is input-only mode
P0DIDS |= SET_BIT7; // Disable digital function for P0.7
P0M1 |= SET_BIT7;
P0M2 &= CLR_BIT7;
break;
case E_CHANNEL7: // ADC7(P2.6) is input-only mode(28 pin only)
AUXR1 |= SET_BIT3; // Disable digital function for P2.6
P2M1 |= SET_BIT6;
P2M2 &= CLR_BIT6;
break;
}
}
//-----------------------------------------------------------------------------------------------------------
void Trigger_ADC_Convertion(void)
{
clr_ADCI; // Clear ADC flag (ADCI=0)
set_ADCS; // ADC run (ADCS = 1)
PCON |= SET_BIT0; // Enter idle mode
}
//-----------------------------------------------------------------------------------------------------------
void ADC_Init(void)
{
Set_ADC_Input_Mode(E_CHANNEL0); // Set ADC0 (P0.1 default) is input only mode
ADC_Channel_Sel(E_CHANNEL0); // ADC Channel Select (P0.1 default)
Enable_ADC_Interrupt();
set_ADCEN; // Enable ADC Function
}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{
UINT16 u16ADCL;
UINT16 u16ADC;
AUXR1 |= Uart_Port_Sel; // Select P10/P11 as UART pin(default)
InitialUART0_Timer1(9600); // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
Show_Version_Number_To_PC();
printf ("\n*===================================================================");
printf ("\n* Name: N79E715 Series ADC Sample Code.");
printf ("\n*===================================================================");
ADC_Init();
while(1)
{
Trigger_ADC_Convertion();
u16ADCL = ADCCON0;
u16ADCL = u16ADCL >> 6; // ADC[1:0]
u16ADC = ADCH;
u16ADC = (u16ADC << 2 ) + u16ADCL; // ADC[9:2] + ADC[1:0]
printf ("\nADC Value = %d",u16ADC); // Show 10 bit ADC
Delay1ms(500);
}
}
//-----------------------------------------------------------------------------------------------------------
void ADC_ISR(void) interrupt 11 // Vector @ 0x5B
{
clr_ADCI; // Clear ADC flag (ADCI = 0)
clr_ADCS; // ADC stop (ADCS = 0)
}
//-----------------------------------------------------------------------------------------------------------
|