Starting up the ADC ATmega32U4!

Status
Not open for further replies.

Winch

Member
Hi,
Never worked with an ATmega32U4 before, until now worked with PIC processors!
Try to start the ADC but it seems to be different from a PIC? What is wrong with the bottom code?
The LCD display shows the maximum information from the ADC. (1023)
A potentiometer is connected to the input, but the ADC input does not respond to this.
When I use such a code with a PIC it doesn't cause any problems.

Code:
'device ATmega32u4

Define CLOCK_FREQUENCY = 16

Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 4
Define LCD_EREG = PORTD
Define LCD_EBIT = 7
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 0

Define ADC_CLOCK = 3
Define ADC_VREF = 0
Define ADC_SAMPLEUS = 20

ConfigPin PORTF.5 = Input

Dim pot As Word

Lcdinit 0
WaitMs 500

pot = 0

main:

        Adcin 5, pot
        Lcdcmdout LcdLine1Home
        Lcdout "Potentiometer"
        Lcdcmdout LcdLine2Home
        Lcdout #pot
        WaitMs 100
       
Goto main
End
 
Portf.5 is also the TMS pin You need to reset the fuse for the JTAGEN to disable it ( 0 ) .... I get that on the pic32... Piggin annoying..
 
No, I tried with the code below.
I've added the "MCUCR" MCU control register
but without result.
Meanwhile, I tried another port the "ADCin 1" still the same problem.

Now I am unsure whether Oshonsoft supports the ADC of the ATmega32U4 with the AVR Basic compiler?
I have the version AVR Basic Compiler v2.85 it is the latest version that I know for sure.


Code:
'device ATmega32u4

Define CLOCK_FREQUENCY = 16

Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 4
Define LCD_EREG = PORTD
Define LCD_EBIT = 7
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 0

Define ADC_CLOCK = 3
Define ADC_VREF = 0
Define ADC_SAMPLEUS = 20

MCUCR = %00000000

DDRF.1 = 0  'input

Dim pot As Word

Lcdinit 0
WaitMs 500

pot = 0
A = 0

main:
        Adcin 1, pot
        Lcdcmdout LcdLine1Home
        Lcdout "Potentiometer"
        Lcdcmdout LcdLine2Home
        Lcdout #pot
        WaitMs 100

Goto main
End
 
The oshonsoft ide has a config settings... You need to edit them.. The IDE said it supports that chip fully... I can help any more than that as I use Pic's and I'm not buying stuff just for this..

I can write code to suit but I cannot test it..
 
You mean the "configurations bits" under "environment" in the PIC compiler.
These are not present in the AVR! So we have to figure this out yourself in the AVR environment.
 
Yup...After re-reading Adcin used with just 5 will read singe ended channel.. But it looks like the default reference is the Aref pin so you may need to attach it to Vcc ( pin 42 ) The MUX register will allow channel + ref + justification.. At the very least the AREF pin need a cap on it..
 
Oke,
Small step further today.
The arduino board has the AREF pin with a 100n to the ground as standard.
And the AVCC is connected with a coil and a capacitor according the specification of the datasheet ATmega32U4.

With this in mind, the following setting has been chosen in the ADMUX register REFS1 = 0 and REFS0 = 1.
All according to the datasheet.

With the result that there is now a visible measure on the display.
Only not stable, it slowly increases to 1023?

Still searching but with a little result, If there are any good ideas please!

Code:
'Hardware comes from the Arduino Leonardo PCB
'device ATmega32u4

Define CLOCK_FREQUENCY = 16

Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 4
Define LCD_EREG = PORTD
Define LCD_EBIT = 7
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 0

Define ADC_SAMPLEUS = 20
Define ADC_CLOCK = 3
Define ADC_VREF = 0

'MCUCR = %00010000

ADCSRA.ADEN = 1  'Writing this bit to one enables the ADC

'ADMUX = ADC Multiplexer selection register
ADMUX.REFS1 = 0  'AVcc with external capacitor on AREF pin as it is on the Leonardo board
ADMUX.REFS0 = 1  '"
ADMUX.ADLAR = 0  'set to 1 = ADC Left Adjust Result
ADMUX.MUX4 = 0  'Analog channel selection bits in this case Port5
ADMUX.MUX3 = 0  '"
ADMUX.MUX2 = 1  '"
ADMUX.MUX1 = 0  '"
ADMUX.MUX0 = 1  '"

DDRF.5 = 0  'input ADC5, input PF5

Dim pot As Word

Lcdinit 0
WaitMs 500

pot = 0

main:
    
    Adcin 5, pot
        Lcdcmdout LcdLine1Home
        Lcdout "Potentiometer"
        Lcdcmdout LcdLine2Home
        Lcdout #pot
        WaitMs 100

Goto main
End
 
Hi,
It turned out to be the define settings of the ADC that were wrong.
And especially the ADC_VREF setting.
Now it works fine.


Code:
'****************************************************************
'* Hardware version  : Hardware = Arduino Leonardo PCB
'* Firmware-version  : Beta (version 1.0)
'* Microcontroller   : ATmega32U4
'* Notes             :
'*
'****************************************************************

Define CLOCK_FREQUENCY = 16

Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 4
Define LCD_EREG = PORTD
Define LCD_EBIT = 7
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 0

Define ADC_SAMPLEUS = 20
Define ADC_CLOCK = 7  'ADC input Prescaler Selections = 128
Define ADC_VREF = 1  'AVCC with external capacitor on AREF pin

DDRD.3 = 1  'output
DDRF.1 = 0  'input ADC1, input PF1

Dim pot As Word

Lcdinit 0
WaitMs 500

pot = 0

main:

    Adcin 1, pot
    
        Lcdcmdout LcdLine1Home
        Lcdout "Potentiometer"
        Lcdcmdout LcdLine2Home
        Lcdout #pot
        WaitMs 200
        
Goto main
End
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…