atmega 32 sample code

Status
Not open for further replies.

anita

New Member
where can i get a sample code for atmega32 that shows how to convert from analog to digital?
 
Hi,
These are codes in mikroBASIC I made when I was learning the AVR.

BASIC A-D conversion:
Code:
program ADC_Test
main:
     DDRB = $FF
     DDRD = $FF
     DDRC = 0
     ADMUX = 0
     DIDR0 = $3F
     ADCSRA = $85 
     while true
           ADCSRA.ADCS = 1
           while (ADCSRA.ADCS)
           wend
           PORTB = ADCL
           PORTD = ADCH
           delay_ms(2)
     wend
end.

ADC controlled by Timer and interrupt:
Code:
program ADC_int

dim done as byte

sub procedure ADC_Int() org $015
    PORTB = ADCH
end sub

sub procedure TMR0_Int() org $010
    ADCSRA.ADSC = 1 'ADCSRA.ADSC 'START CONVERSION
end sub

main:
     DDRB = $FF 'ALL OUTPUT
     DDRC = 0 'ALL INPUT
     TCCR0A = 0 'NORMAL OPERATION
     TCCR0B = 3 'PRESCALER 64
     SREG = $80 'GIE
     TIMSK0 = 1 'ENABLE TMR0 INTERRUPT
     ADMUX = $20 '8-bit, CH0
     DIDR0 = $3F 'ALL ANALOG
     ADCSRA = $8D 'ADC ON, INTERRUPT ENABLED, FOSC/64
     while true
     wend
end.

ADC on LCD (with mikroBASIC library function):
Code:
program ADC_LCD

symbol RW = PORTB.B1
symbol RW_Direction = DDRB.B1

dim LCD_RS as sbit at PORTB2_bit
dim LCD_EN as sbit at PORTB3_bit
dim LCD_D4 as sbit at PORTB4_bit
dim LCD_D5 as sbit at PORTB5_bit
dim LCD_D6 as sbit at PORTB6_bit
dim LCD_D7 as sbit at PORTB7_bit

dim LCD_RS_Direction as sbit at DDB2_bit
dim LCD_EN_Direction as sbit at DDB3_bit
dim LCD_D4_Direction as sbit at DDB4_bit
dim LCD_D5_Direction as sbit at DDB5_bit
dim LCD_D6_Direction as sbit at DDB6_bit
dim LCD_D7_Direction as sbit at DDB7_bit

dim Value as longword
dim Disp as word[3]
dim sValue as string[5]
dim sDisp as string[5]

main:
     RW_Direction = 1
     RW = 0
     LCD_Init()
     LCD_Cmd(_LCD_CLEAR)
     LCD_Cmd(_LCD_CURSOR_OFF)
     LCD_Out(1, 1, "Reading:")
     LCD_Out(2, 1, "Voltage:")
     sDisp = "4.99V"
     while true
           Value = ADC_Read(0)
           WordToStr(Value, sValue)
           LCD_Out(1, 10, sValue)
           Value = (Value * 500) >> 10
           Disp[0] = (Value div 100) + 48
           Disp[1] = ((Value div 10) mod 10) + 48
           Disp[2] = (Value mod 10) + 48
           sDisp[0] = Disp[0]
           sDisp[2] = Disp[1]
           sDisp[3] = Disp[2]
           LCD_Out(2, 10, sDisp)
     wend
end.

Hope this helps.
Tahmid.
 
Hi to all

My name is kike and i'm begginer into Atmega family microcontroler, i need to do a convertion of 3 signals of a ADXL 330 accelerometer. For to do this job, i am using a ATmega32 that have a ADC 8-channel multiplexer, the programing is in assembler. Have someone worked with more than 2 signals?, i am confused of how to programing the both ADMUX and ADCSRA registers.

If someone know how to work with the ADMUX and ADCSRA register, please help me :-( .

see you, and thank you.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…