0% found this document useful (0 votes)
150 views28 pages

Atmega128 Code

Atmega128 Code

Uploaded by

akbarsujiwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views28 pages

Atmega128 Code

Atmega128 Code

Uploaded by

akbarsujiwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

/*******************************************************

This program was created by the


CodeWizardAVR V3.12 Advanced
Automatic Program Generator
Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date

: 28-Jun-2016

Author :
Company :
Comments:

Chip type

: ATxmega128A3U

Program type

: Application

AVR Core Clock frequency: 16.000000 MHz


Memory model
Data Stack size

: Small
: 2048

*******************************************************/

// I/O Registers definitions


#include <io.h>

// Delay functions
#include <delay.h>

// Alphanumeric LCD functions


#include <alcd.h>

#include <stdio.h>

// Declare your global variables here


unsigned char buf[11], buf2[11];
float a, b;

// System Clocks initialization


void system_clocks_init(void)
{
unsigned char n,s;

// Optimize for speed


#pragma optsize// Save interrupts enabled/disabled state
s=SREG;
// Disable interrupts
#asm("cli")

// External 16000.000 kHz clock source on XTAL1 initialization


OSC.XOSCCTRL=OSC_XOSCSEL_EXTCLK_gc;
// Enable the external clock source
OSC.CTRL|=OSC_XOSCEN_bm;

// System Clock prescaler A division factor: 1


// System Clock prescalers B & C division factors: B:1, C:1
// ClkPer4: 16000.000 kHz
// ClkPer2: 16000.000 kHz
// ClkPer: 16000.000 kHz
// ClkCPU: 16000.000 kHz
n=(CLK.PSCTRL & (~(CLK_PSADIV_gm | CLK_PSBCDIV1_bm | CLK_PSBCDIV0_bm))) |

CLK_PSADIV_1_gc | CLK_PSBCDIV_1_1_gc;
CCP=CCP_IOREG_gc;
CLK.PSCTRL=n;

// Select the system clock source: External Oscillator or Clock


n=(CLK.CTRL & (~CLK_SCLKSEL_gm)) | CLK_SCLKSEL_XOSC_gc;
CCP=CCP_IOREG_gc;
CLK.CTRL=n;

// Disable the unused oscillators: 2 MHz, 32 MHz, internal 32 kHz, PLL


OSC.CTRL&= ~(OSC_RC2MEN_bm | OSC_RC32MEN_bm | OSC_RC32KEN_bm |
OSC_PLLEN_bm);

// ClkPer output disabled


PORTCFG.CLKEVOUT&= ~(PORTCFG_CLKOUTSEL_gm | PORTCFG_CLKOUT_gm);
// Restore interrupts enabled/disabled state
SREG=s;
// Restore optimization for size if needed
#pragma optsize_default
}

// Event System initialization


void event_system_init(void)
{
// Event System Channel 0 source: None
EVSYS.CH0MUX=EVSYS_CHMUX_OFF_gc;
// Event System Channel 1 source: None
EVSYS.CH1MUX=EVSYS_CHMUX_OFF_gc;
// Event System Channel 2 source: None
EVSYS.CH2MUX=EVSYS_CHMUX_OFF_gc;

// Event System Channel 3 source: None


EVSYS.CH3MUX=EVSYS_CHMUX_OFF_gc;
// Event System Channel 4 source: None
EVSYS.CH4MUX=EVSYS_CHMUX_OFF_gc;
// Event System Channel 5 source: None
EVSYS.CH5MUX=EVSYS_CHMUX_OFF_gc;
// Event System Channel 6 source: None
EVSYS.CH6MUX=EVSYS_CHMUX_OFF_gc;
// Event System Channel 7 source: None
EVSYS.CH7MUX=EVSYS_CHMUX_OFF_gc;

// Event System Channel 0 Digital Filter Coefficient: 1 Sample


// Quadrature Decoder: Of
EVSYS.CH0CTRL=(EVSYS.CH0CTRL & (~(EVSYS_QDIRM_gm | EVSYS_QDIEN_bm |
EVSYS_QDEN_bm | EVSYS_DIGFILT_gm))) |
EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 1 Digital Filter Coefficient: 1 Sample
EVSYS.CH1CTRL=EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 2 Digital Filter Coefficient: 1 Sample
// Quadrature Decoder: Of
EVSYS.CH2CTRL=(EVSYS.CH2CTRL & (~(EVSYS_QDIRM_gm | EVSYS_QDIEN_bm |
EVSYS_QDEN_bm | EVSYS_DIGFILT_gm))) |
EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 3 Digital Filter Coefficient: 1 Sample
EVSYS.CH3CTRL=EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 4 Digital Filter Coefficient: 1 Sample
// Quadrature Decoder: Of
EVSYS.CH4CTRL=(EVSYS.CH4CTRL & (~(EVSYS_QDIRM_gm | EVSYS_QDIEN_bm |
EVSYS_QDEN_bm | EVSYS_DIGFILT_gm))) |
EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 5 Digital Filter Coefficient: 1 Sample

EVSYS.CH5CTRL=EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 6 Digital Filter Coefficient: 1 Sample
EVSYS.CH6CTRL=EVSYS_DIGFILT_1SAMPLE_gc;
// Event System Channel 7 Digital Filter Coefficient: 1 Sample
EVSYS.CH7CTRL=EVSYS_DIGFILT_1SAMPLE_gc;

// Event System Channel output: Disabled


PORTCFG.CLKEVOUT&= ~PORTCFG_EVOUT_gm;
PORTCFG.EVOUTSEL&= ~PORTCFG_EVOUTSEL_gm;
}

// Ports initialization
void ports_init(void)
{
// PORTA initialization
// OUT register
PORTA.OUT=0x00;
// Pin0: Input
// Pin1: Input
// Pin2: Input
// Pin3: Input
// Pin4: Input
// Pin5: Input
// Pin6: Input
// Pin7: Input
PORTA.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of

PORTA.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges
// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTA.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin2 Output/Pull configuration: Totempole/No
// Pin2 Input/Sense configuration: Sense both edges
// Pin2 Inverted: Of
// Pin2 Slew Rate Limitation: Of
PORTA.PIN2CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin3 Output/Pull configuration: Totempole/No
// Pin3 Input/Sense configuration: Sense both edges
// Pin3 Inverted: Of
// Pin3 Slew Rate Limitation: Of
PORTA.PIN3CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin4 Output/Pull configuration: Totempole/No
// Pin4 Input/Sense configuration: Sense both edges
// Pin4 Inverted: Of
// Pin4 Slew Rate Limitation: Of
PORTA.PIN4CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin5 Output/Pull configuration: Totempole/No
// Pin5 Input/Sense configuration: Sense both edges
// Pin5 Inverted: Of
// Pin5 Slew Rate Limitation: Of
PORTA.PIN5CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin6 Output/Pull configuration: Totempole/No
// Pin6 Input/Sense configuration: Sense both edges
// Pin6 Inverted: Of
// Pin6 Slew Rate Limitation: Of

PORTA.PIN6CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin7 Output/Pull configuration: Totempole/No
// Pin7 Input/Sense configuration: Sense both edges
// Pin7 Inverted: Of
// Pin7 Slew Rate Limitation: Of
PORTA.PIN7CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTA.INTCTRL=(PORTA.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
// Pin2 Pin Change interrupt 0: Of
// Pin3 Pin Change interrupt 0: Of
// Pin4 Pin Change interrupt 0: Of
// Pin5 Pin Change interrupt 0: Of
// Pin6 Pin Change interrupt 0: Of
// Pin7 Pin Change interrupt 0: Of
PORTA.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
// Pin2 Pin Change interrupt 1: Of
// Pin3 Pin Change interrupt 1: Of
// Pin4 Pin Change interrupt 1: Of
// Pin5 Pin Change interrupt 1: Of
// Pin6 Pin Change interrupt 1: Of
// Pin7 Pin Change interrupt 1: Of
PORTA.INT1MASK=0x00;

// PORTB initialization

// OUT register
PORTB.OUT=0x00;
// Pin0: Input
// Pin1: Input
// Pin2: Input
// Pin3: Input
// Pin4: Input
// Pin5: Input
// Pin6: Input
// Pin7: Input
PORTB.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of
PORTB.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges
// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTB.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin2 Output/Pull configuration: Totempole/No
// Pin2 Input/Sense configuration: Sense both edges
// Pin2 Inverted: Of
// Pin2 Slew Rate Limitation: Of
PORTB.PIN2CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin3 Output/Pull configuration: Totempole/No
// Pin3 Input/Sense configuration: Sense both edges
// Pin3 Inverted: Of
// Pin3 Slew Rate Limitation: Of

PORTB.PIN3CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin4 Output/Pull configuration: Totempole/No
// Pin4 Input/Sense configuration: Sense both edges
// Pin4 Inverted: Of
// Pin4 Slew Rate Limitation: Of
PORTB.PIN4CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin5 Output/Pull configuration: Totempole/No
// Pin5 Input/Sense configuration: Sense both edges
// Pin5 Inverted: Of
// Pin5 Slew Rate Limitation: Of
PORTB.PIN5CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin6 Output/Pull configuration: Totempole/No
// Pin6 Input/Sense configuration: Sense both edges
// Pin6 Inverted: Of
// Pin6 Slew Rate Limitation: Of
PORTB.PIN6CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin7 Output/Pull configuration: Totempole/No
// Pin7 Input/Sense configuration: Sense both edges
// Pin7 Inverted: Of
// Pin7 Slew Rate Limitation: Of
PORTB.PIN7CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTB.INTCTRL=(PORTB.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
// Pin2 Pin Change interrupt 0: Of
// Pin3 Pin Change interrupt 0: Of
// Pin4 Pin Change interrupt 0: Of

// Pin5 Pin Change interrupt 0: Of


// Pin6 Pin Change interrupt 0: Of
// Pin7 Pin Change interrupt 0: Of
PORTB.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
// Pin2 Pin Change interrupt 1: Of
// Pin3 Pin Change interrupt 1: Of
// Pin4 Pin Change interrupt 1: Of
// Pin5 Pin Change interrupt 1: Of
// Pin6 Pin Change interrupt 1: Of
// Pin7 Pin Change interrupt 1: Of
PORTB.INT1MASK=0x00;

// PORTC initialization
// OUT register
PORTC.OUT=0x00;
// Pin0: Input
// Pin1: Input
// Pin2: Input
// Pin3: Input
// Pin4: Input
// Pin5: Input
// Pin6: Input
// Pin7: Input
PORTC.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of

PORTC.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges
// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTC.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin2 Output/Pull configuration: Totempole/No
// Pin2 Input/Sense configuration: Sense both edges
// Pin2 Inverted: Of
// Pin2 Slew Rate Limitation: Of
PORTC.PIN2CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin3 Output/Pull configuration: Totempole/No
// Pin3 Input/Sense configuration: Sense both edges
// Pin3 Inverted: Of
// Pin3 Slew Rate Limitation: Of
PORTC.PIN3CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin4 Output/Pull configuration: Totempole/No
// Pin4 Input/Sense configuration: Sense both edges
// Pin4 Inverted: Of
// Pin4 Slew Rate Limitation: Of
PORTC.PIN4CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin5 Output/Pull configuration: Totempole/No
// Pin5 Input/Sense configuration: Sense both edges
// Pin5 Inverted: Of
// Pin5 Slew Rate Limitation: Of
PORTC.PIN5CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin6 Output/Pull configuration: Totempole/No
// Pin6 Input/Sense configuration: Sense both edges
// Pin6 Inverted: Of
// Pin6 Slew Rate Limitation: Of

PORTC.PIN6CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin7 Output/Pull configuration: Totempole/No
// Pin7 Input/Sense configuration: Sense both edges
// Pin7 Inverted: Of
// Pin7 Slew Rate Limitation: Of
PORTC.PIN7CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// PORTC Peripheral Output Remapping
// OC0A Output: Pin 0
// OC0B Output: Pin 1
// OC0C Output: Pin 2
// OC0D Output: Pin 3
// USART0 XCK: Pin 1
// USART0 RXD: Pin 2
// USART0 TXD: Pin 3
// SPI MOSI: Pin 5
// SPI SCK: Pin 7
PORTC.REMAP=(0<<PORT_SPI_bp) | (0<<PORT_USART0_bp) | (0<<PORT_TC0D_bp)
| (0<<PORT_TC0C_bp) | (0<<PORT_TC0B_bp) | (0<<PORT_TC0A_bp);
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTC.INTCTRL=(PORTC.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
// Pin2 Pin Change interrupt 0: Of
// Pin3 Pin Change interrupt 0: Of
// Pin4 Pin Change interrupt 0: Of
// Pin5 Pin Change interrupt 0: Of
// Pin6 Pin Change interrupt 0: Of
// Pin7 Pin Change interrupt 0: Of

PORTC.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
// Pin2 Pin Change interrupt 1: Of
// Pin3 Pin Change interrupt 1: Of
// Pin4 Pin Change interrupt 1: Of
// Pin5 Pin Change interrupt 1: Of
// Pin6 Pin Change interrupt 1: Of
// Pin7 Pin Change interrupt 1: Of
PORTC.INT1MASK=0x00;

// PORTD initialization
// OUT register
PORTD.OUT=0x00;
// Pin0: Input
// Pin1: Input
// Pin2: Input
// Pin3: Input
// Pin4: Input
// Pin5: Input
// Pin6: Input
// Pin7: Input
PORTD.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of
PORTD.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges

// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTD.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin2 Output/Pull configuration: Totempole/No
// Pin2 Input/Sense configuration: Sense both edges
// Pin2 Inverted: Of
// Pin2 Slew Rate Limitation: Of
PORTD.PIN2CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin3 Output/Pull configuration: Totempole/No
// Pin3 Input/Sense configuration: Sense both edges
// Pin3 Inverted: Of
// Pin3 Slew Rate Limitation: Of
PORTD.PIN3CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin4 Output/Pull configuration: Totempole/No
// Pin4 Input/Sense configuration: Sense both edges
// Pin4 Inverted: Of
// Pin4 Slew Rate Limitation: Of
PORTD.PIN4CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin5 Output/Pull configuration: Totempole/No
// Pin5 Input/Sense configuration: Sense both edges
// Pin5 Inverted: Of
// Pin5 Slew Rate Limitation: Of
PORTD.PIN5CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin6 Output/Pull configuration: Totempole/No
// Pin6 Input/Sense configuration: Sense both edges
// Pin6 Inverted: Of
// Pin6 Slew Rate Limitation: Of
PORTD.PIN6CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin7 Output/Pull configuration: Totempole/No
// Pin7 Input/Sense configuration: Sense both edges

// Pin7 Inverted: Of
// Pin7 Slew Rate Limitation: Of
PORTD.PIN7CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTD.INTCTRL=(PORTD.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
// Pin2 Pin Change interrupt 0: Of
// Pin3 Pin Change interrupt 0: Of
// Pin4 Pin Change interrupt 0: Of
// Pin5 Pin Change interrupt 0: Of
// Pin6 Pin Change interrupt 0: Of
// Pin7 Pin Change interrupt 0: Of
PORTD.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
// Pin2 Pin Change interrupt 1: Of
// Pin3 Pin Change interrupt 1: Of
// Pin4 Pin Change interrupt 1: Of
// Pin5 Pin Change interrupt 1: Of
// Pin6 Pin Change interrupt 1: Of
// Pin7 Pin Change interrupt 1: Of
PORTD.INT1MASK=0x00;

// PORTE initialization
// OUT register
PORTE.OUT=0x00;
// Pin0: Input

// Pin1: Input
// Pin2: Input
// Pin3: Input
// Pin4: Input
// Pin5: Input
// Pin6: Input
// Pin7: Input
PORTE.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of
PORTE.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges
// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTE.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin2 Output/Pull configuration: Totempole/No
// Pin2 Input/Sense configuration: Sense both edges
// Pin2 Inverted: Of
// Pin2 Slew Rate Limitation: Of
PORTE.PIN2CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin3 Output/Pull configuration: Totempole/No
// Pin3 Input/Sense configuration: Sense both edges
// Pin3 Inverted: Of
// Pin3 Slew Rate Limitation: Of
PORTE.PIN3CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin4 Output/Pull configuration: Totempole/No
// Pin4 Input/Sense configuration: Sense both edges

// Pin4 Inverted: Of
// Pin4 Slew Rate Limitation: Of
PORTE.PIN4CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin5 Output/Pull configuration: Totempole/No
// Pin5 Input/Sense configuration: Sense both edges
// Pin5 Inverted: Of
// Pin5 Slew Rate Limitation: Of
PORTE.PIN5CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin6 Output/Pull configuration: Totempole/No
// Pin6 Input/Sense configuration: Sense both edges
// Pin6 Inverted: Of
// Pin6 Slew Rate Limitation: Of
PORTE.PIN6CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin7 Output/Pull configuration: Totempole/No
// Pin7 Input/Sense configuration: Sense both edges
// Pin7 Inverted: Of
// Pin7 Slew Rate Limitation: Of
PORTE.PIN7CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTE.INTCTRL=(PORTE.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
// Pin2 Pin Change interrupt 0: Of
// Pin3 Pin Change interrupt 0: Of
// Pin4 Pin Change interrupt 0: Of
// Pin5 Pin Change interrupt 0: Of
// Pin6 Pin Change interrupt 0: Of
// Pin7 Pin Change interrupt 0: Of

PORTE.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
// Pin2 Pin Change interrupt 1: Of
// Pin3 Pin Change interrupt 1: Of
// Pin4 Pin Change interrupt 1: Of
// Pin5 Pin Change interrupt 1: Of
// Pin6 Pin Change interrupt 1: Of
// Pin7 Pin Change interrupt 1: Of
PORTE.INT1MASK=0x00;

// PORTF initialization
// OUT register
PORTF.OUT=0x00;
// Pin0: Input
// Pin1: Input
// Pin2: Input
// Pin3: Input
// Pin4: Input
// Pin5: Input
// Pin6: Input
// Pin7: Input
PORTF.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of
PORTF.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges

// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTF.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin2 Output/Pull configuration: Totempole/No
// Pin2 Input/Sense configuration: Sense both edges
// Pin2 Inverted: Of
// Pin2 Slew Rate Limitation: Of
PORTF.PIN2CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin3 Output/Pull configuration: Totempole/No
// Pin3 Input/Sense configuration: Sense both edges
// Pin3 Inverted: Of
// Pin3 Slew Rate Limitation: Of
PORTF.PIN3CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin4 Output/Pull configuration: Totempole/No
// Pin4 Input/Sense configuration: Sense both edges
// Pin4 Inverted: Of
// Pin4 Slew Rate Limitation: Of
PORTF.PIN4CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin5 Output/Pull configuration: Totempole/No
// Pin5 Input/Sense configuration: Sense both edges
// Pin5 Inverted: Of
// Pin5 Slew Rate Limitation: Of
PORTF.PIN5CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin6 Output/Pull configuration: Totempole/No
// Pin6 Input/Sense configuration: Sense both edges
// Pin6 Inverted: Of
// Pin6 Slew Rate Limitation: Of
PORTF.PIN6CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin7 Output/Pull configuration: Totempole/No
// Pin7 Input/Sense configuration: Sense both edges

// Pin7 Inverted: Of
// Pin7 Slew Rate Limitation: Of
PORTF.PIN7CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTF.INTCTRL=(PORTF.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
// Pin2 Pin Change interrupt 0: Of
// Pin3 Pin Change interrupt 0: Of
// Pin4 Pin Change interrupt 0: Of
// Pin5 Pin Change interrupt 0: Of
// Pin6 Pin Change interrupt 0: Of
// Pin7 Pin Change interrupt 0: Of
PORTF.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
// Pin2 Pin Change interrupt 1: Of
// Pin3 Pin Change interrupt 1: Of
// Pin4 Pin Change interrupt 1: Of
// Pin5 Pin Change interrupt 1: Of
// Pin6 Pin Change interrupt 1: Of
// Pin7 Pin Change interrupt 1: Of
PORTF.INT1MASK=0x00;

// PORTR initialization
// OUT register
PORTR.OUT=0x00;
// Pin0: Input

// Pin1: Input
PORTR.DIR=0x00;
// Pin0 Output/Pull configuration: Totempole/No
// Pin0 Input/Sense configuration: Sense both edges
// Pin0 Inverted: Of
// Pin0 Slew Rate Limitation: Of
PORTR.PIN0CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Pin1 Output/Pull configuration: Totempole/No
// Pin1 Input/Sense configuration: Sense both edges
// Pin1 Inverted: Of
// Pin1 Slew Rate Limitation: Of
PORTR.PIN1CTRL=PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc;
// Interrupt 0 level: Disabled
// Interrupt 1 level: Disabled
PORTR.INTCTRL=(PORTR.INTCTRL & (~(PORT_INT1LVL_gm | PORT_INT0LVL_gm))) |
PORT_INT1LVL_OFF_gc | PORT_INT0LVL_OFF_gc;
// Pin0 Pin Change interrupt 0: Of
// Pin1 Pin Change interrupt 0: Of
PORTR.INT0MASK=0x00;
// Pin0 Pin Change interrupt 1: Of
// Pin1 Pin Change interrupt 1: Of
PORTR.INT1MASK=0x00;
}

// Virtual Ports initialization


void vports_init(void)
{
// PORTA mapped to VPORT0
// PORTB mapped to VPORT1
PORTCFG.VPCTRLA=PORTCFG_VP13MAP_PORTB_gc | PORTCFG_VP02MAP_PORTA_gc;

// PORTC mapped to VPORT2


// PORTD mapped to VPORT3
PORTCFG.VPCTRLB=PORTCFG_VP13MAP_PORTD_gc | PORTCFG_VP02MAP_PORTC_gc;
}

// ADCA initialization
void adca_init(void)
{
// ADCA is enabled
// Resolution: 8 Bits
// Free Running mode: Of
// Gain stage impedance mode: High-impedance sources
// Current consumption: No limit
// Conversion mode: Unsigned
ADCA.CTRLB=(0<<ADC_IMPMODE_bp) | ADC_CURRLIMIT_NO_gc |
(0<<ADC_CONMODE_bp) | ADC_RESOLUTION_8BIT_gc;

// Clock frequency: 31.250 kHz


ADCA.PRESCALER=ADC_PRESCALER_DIV512_gc;

// Reference: AREF pin on PORTB


// Temperature reference: Of
ADCA.REFCTRL=ADC_REFSEL_AREFB_gc | (0<<ADC_TEMPREF_bp) |
(0<<ADC_BANDGAP_bp);

// Initialize the ADC Compare register


ADCA.CMPL=0x00;
ADCA.CMPH=0x00;

// ADC channel 0 gain: 1

// ADC channel 0 input mode: Single-ended positive input signal


ADCA.CH0.CTRL=(0<<ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc |
ADC_CH_INPUTMODE_SINGLEENDED_gc;

// ADC channel 0 positive input: ADC2 pin


// ADC channel 0 negative input: GND
ADCA.CH0.MUXCTRL=ADC_CH_MUXPOS_PIN2_gc;

// ADC channel 1 gain: 1


// ADC channel 1 input mode: Single-ended positive input signal
ADCA.CH1.CTRL=(0<<ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc |
ADC_CH_INPUTMODE_SINGLEENDED_gc;

// ADC channel 1 positive input: ADC2 pin


// ADC channel 1 negative input: GND
ADCA.CH1.MUXCTRL=ADC_CH_MUXPOS_PIN2_gc;

// ADC channel 2 gain: 1


// ADC channel 2 input mode: Single-ended positive input signal
ADCA.CH2.CTRL=(0<<ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc |
ADC_CH_INPUTMODE_SINGLEENDED_gc;

// ADC channel 2 positive input: ADC2 pin


// ADC channel 2 negative input: GND
ADCA.CH2.MUXCTRL=ADC_CH_MUXPOS_PIN2_gc;

// ADC channel 3 gain: 1


// ADC channel 3 input mode: Single-ended positive input signal
ADCA.CH3.CTRL=(0<<ADC_CH_START_bp) | ADC_CH_GAIN_1X_gc |
ADC_CH_INPUTMODE_SINGLEENDED_gc;

// ADC channel 3 positive input: ADC2 pin


// ADC channel 3 negative input: GND
ADCA.CH3.MUXCTRL=ADC_CH_MUXPOS_PIN2_gc;

// AD conversion is started by software


ADCA.EVCTRL=ADC_EVACT_NONE_gc;

// Channel 0 interrupt: Disabled


ADCA.CH0.INTCTRL=ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc;
// Channel 1 interrupt: Disabled
ADCA.CH1.INTCTRL=ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc;
// Channel 2 interrupt: Disabled
ADCA.CH2.INTCTRL=ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc;
// Channel 3 interrupt: Disabled
ADCA.CH3.INTCTRL=ADC_CH_INTMODE_COMPLETE_gc | ADC_CH_INTLVL_OFF_gc;

// Enable the ADC


ADCA.CTRLA|=ADC_ENABLE_bm;
// Insert a delay to allow the ADC common mode voltage to stabilize
delay_us(2);
}

// ADCA channel data read function using polled mode


unsigned char adca_read(unsigned char channel)
{
ADC_CH_t *pch=&ADCA.CH0+channel;
unsigned char data;

// Start the AD conversion


pch->CTRL|= 1<<ADC_CH_START_bp;

// Wait for the AD conversion to complete


while ((pch->INTFLAGS & ADC_CH_CHIF_bm)==0);
// Clear the interrupt flag
pch->INTFLAGS=ADC_CH_CHIF_bm;
// Read the AD conversion result
data=pch->RESL;
return data;
}

// ADCA sweeped channel(s) data read function


// for software triggered mode
void adca_sweep_read(unsigned char nch, unsigned char *pdata)
{
ADC_CH_t *pch=&ADCA.CH0;
unsigned char i,j,m;

// Sweep starts with channel 0


j=ADC_CH0START_bm;
// Prepare the AD conversion start mask for the sweeped channel(s)
m=0;
i=0;
do
{
m|=j;
j<<=1;
}
while (++i<nch);
// Ensure the interrupt flags are cleared
ADCA.INTFLAGS=ADCA.INTFLAGS;
// Start the AD conversion for the sweeped channel(s)

ADCA.CTRLA=(ADCA.CTRLA & (ADC_DMASEL_gm | ADC_FLUSH_bm |


ADC_ENABLE_bm)) | m;
// Read and store the AD conversion results for all the sweeped channels
for (i=0; i<nch; i++)
{
// Wait for the AD conversion to complete
while ((pch->INTFLAGS & ADC_CH_CHIF_bm)==0);
// Clear the interrupt flag
pch->INTFLAGS=ADC_CH_CHIF_bm;
// Read the AD conversion result
*pdata=pch->RESL;
pdata++;
pch++;
}
}

void main(void)
{
// Declare your local variables here
unsigned char n;

// Interrupt system initialization


// Optimize for speed
#pragma optsize// Make sure the interrupts are disabled
#asm("cli")
// Low level interrupt: Of
// Round-robin scheduling for low level interrupt: Of
// Medium level interrupt: Of
// High level interrupt: Of

// The interrupt vectors will be placed at the start of the Application FLASH section
n=(PMIC.CTRL & (~(PMIC_RREN_bm | PMIC_IVSEL_bm | PMIC_HILVLEN_bm |
PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm)));
CCP=CCP_IOREG_gc;
PMIC.CTRL=n;
// Set the default priority for round-robin scheduling
PMIC.INTPRI=0x00;
// Restore optimization for size if needed
#pragma optsize_default

// System clocks initialization


system_clocks_init();

// Event system initialization


event_system_init();

// Ports initialization
ports_init();

// Virtual Ports initialization


vports_init();

// ADCA initialization
adca_init();

// Alphanumeric LCD initialization


// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Pin 0
// RD - PORTC Pin 1

// EN - PORTC Pin 2
// D4 - PORTC Pin 4
// D5 - PORTC Pin 5
// D6 - PORTC Pin 6
// D7 - PORTC Pin 7
// Characters/line: 16
lcd_init(16);

while (1)
{
// Place your code here
lcd_clear();
a=adca_read(1);
lcd_gotoxy(0,0);
sprintf(buf,"%1.0f",a);
lcd_puts(buf);
lcd_gotoxy(0,1);
b=a*5/255;
sprintf(buf2,"volt = %1.2f",b);
lcd_puts(buf2);
delay_ms(100);

}
}

You might also like