Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
130 views

Step by Step Programming in C (CodeVisionAVR V2.05.3)

AVR Programming in C

Uploaded by

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

Step by Step Programming in C (CodeVisionAVR V2.05.3)

AVR Programming in C

Uploaded by

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

www.picpars.

com

AVR
C

AVR CodeVisionAVR

:
1391

AVR C

1391

PDF

.
. .


avr 44
LM35
While, for, switch, break, continue if define, LCD, sprintf, keypad, 7segment, timer, timer interrupt, ADC,
LM35,

2
www.picpars.com

AVR C

1391


.
3......................................................................................................................................................................................................................
5.................................................................................................................................................................................................................
6 ...................................................................................................................................................................................................................
7 .......................................................................................................................................................................................................................................
7 ..................................................................................................................................................................................................................................
8.....................................................................................................................................................................................................................
8...................................................................................................................................................... CodeVisionAVR
9 ..................................................................................................................................................................... Proteus
13 ....................................................................................................................................................................... C
13 .................................................................................................................................................................................................
14 .......................................................................................................................................... C
)(14........................................................................................................................................................................................ :
15.................................................................................................................................................................................................................... :
16 ................................................................................................................................................................................................................... :
1 17 ...........................................................................................................................................................................................................
17................................................................................................................................................................................................ for
for 17................................................................................................................................................................................................
for 17...........................................................................................................................................................................................
18........................................................................................................................................................................................ while
18 ............................................................................................................................................................................................................ while
19 .................................................................................................................................................................................................... do while
19............................................................................................................................................................................................. switch
2 20 ........................................................................................................................................................................................................... IF
21................................................................................................................................................................................................ #define
3 23 ............................................................................................................................................................................................................. C
24...........................................................................................................................................................................................
25 ..................................................................................................................................................................................................
4 LCD )( 26 ...................................................................................................................................................................................
26 ............................................................................................................................................................................................................. LCD
27 .................................................................................................................................................................................................. LCD
27........................................................................................................................................................................................
LCD 28 .........................................................................................................................................................................................................
29 ..............................................................................................................................................................................
5 4*4 31 ..........................................................................................................................................................................
6 34 ............................................................................................................................................................................................................

3
www.picpars.com

AVR C

1391

35........................................................................................................................................................................................
35...............................................................................................................................................................
35......................................................................................................................................................... 74LS247
36 ......................................................................................................................................................................................
36...............................................................................................................................................................
36 ......................................................................................................................................................... 74LS248
)( 37 .......................................................................................................................................................................
7 39..............................................................................................................................................................................................................................
39 ............................................................................................................................................................................................................
) ( 41 ................................................................................................................................................
) (42.............................................................................................................................................................
43 ........................................................................................................................................................................CTC
44 ......................................................................................................................................................................PWM
46 ....................................................................................................................................................
8 46 ..............................................................................................................................................................................
16 48 ..............................................................................................................................................................................
8 50 ................................................................................................................................................................................ ADC
54 ....................................................................................................................................................................................................................................
55............................................................................................................................................................................................

4
www.picpars.com

AVR C

1391


.
)17.............................................................................................................................................................................. (Proj-1
20........................................................................................................................................................... (Proj-2) define
)23.......................................................................................................................................................................................... (Proj-3
LCD)26 .......................................................................................................................................................................(Proj-4
LCD 26...............................................................................................................................................................................
31.......................................................................................................................................................................................... (Proj-5) 44
)35..............................................................................................................................(Proj-6
35....................................................................................................... (Proj-7) 74LS247
)36........................................................................................................................... (Proj-8
36 ..................................................................................................... (Proj-9) 74LS248
4 )37...................................................................................................................................... (Proj-10
1 )41......................................................................................................................................... (Proj-11
1 )42.................................................................................................................................................... (Proj-12
OCR0 CTC 30 )43.................................................................................................................... (Proj-13
PWM )44......................................................................................................................................................................... (Proj-13
1 46 .................................................................................................................................................................
1 ) (47...........................................................................................................................................................................
1 49.................................................................................................................................................................
1 ) ( 49............................................................................................................................................................................
ADC 52..................................................................................................................................... LM35

5
www.picpars.com

AVR C

1391


1 14........................................................................................................................................................................................................
1-4 LCD 26 ........................................................................................................................................................................
2-4 27.................................................................................................................................................................................. LCD
2-4 28........................................................................................................................................................................
1-7 40.................................................................................................................................................................
2-7 44............................................................................................................................................................................... PWM

6
www.picpars.com

AVR C

1391

) (http://www.picpars.com 1388
. AVR
.
.
.

AVR C
. .

.
:
Copy-Paste

.

7
www.picpars.com

AVR C

1391


CodeVisionAVR
CodeVisionAVR File New project
OK confirm Yes . CodeWizardAVR
AT90,ATtiny,ATmega,FPSLIC Ok . CodeWizardAVR

LCD ...

File

Generate, save and Exit . * C.


project codeWizardAVR . CodeVisionAVR .
* C. Note
.
CodeWizardAVR

.
... .
project Build All
HEX
Exe ) . = (

8
www.picpars.com

AVR C

1391

Proteus
:
:
Selection mode .
Component mode
. .
Junction dot mode
) ( .
Wire label mode . .
Text script mode .
Terminals mode ... .
Devise pins mode
. subcircuit mode .
Generator mode .
dc ...
voltage probe mode current probe mode
. WLO WHI .
.
virtual instrument mode :
.
.
DC AC ...
:
...
.

9
www.picpars.com

AVR C

1391

90
90 .
.
Pick parts from library
keywords
.

.

active Device ...
active .
PCB Device . active

PCB

Device ... PCB .


PCB PCB
. PCB

PCB

.
toggle wire autorouter
.


.

10
www.picpars.com

AVR C

1391

.
delete
.

Play .
.
)( :
Edit properties
. Ctrl+E .
program file .

Clock

frequency . :

.
virtual instrument mode OSCILLOSCOPE
. A,B,C,D .
. Play
.
A . time division
.
time division
.
time division position
.

11
www.picpars.com

AVR C

1391

time division 1
.
:

. .
2 .

12
www.picpars.com

AVR C

1391

C

C (1 .
// . //picpars.com :
C (2 .
* / */ .:
/*WEB: www.picpars.com
Email: picpars[at]gmail.com
designer: Seyed mohsen ghasemian*/

: C
) (main
. main .
a A
:
>#include <mega16.h
;Char a,A
{)(Void main

...

: ; ) ( .
main { } . ...
}{ .
}{ . :

13
www.picpars.com

AVR C

1391

;if(a==1) PORTC=55
{)if(a==1
;PORTC=55
;PORTD=80
}


. { } .
:
#asm

#endasm

C
)(:
Type

)Size (Bits
Range
1
0,1
Bit
8
-128 to 127
char
8
0 to 255
unsigned char
8
-128 to 127
signed char
16
-32768 to 32767
int
16
-32768 to 32767
short int
16
0 to 65535
unsigned int
16
-32768 to 32767
signed int
32
-2147483648 to 2147483647
long int
32
0 to 4294967295
unsigned long int
32
-2147483648 to 2147483647
signed long int
32
1.175e-38 to 3.402e38
float
32
1.175e-38 to 3.402e38
double
1

) (Type :
;Bit a
;char b

; .
:

14
www.picpars.com

AVR C

1391
-1 .

-2 .
;Char a,b,c
;ungined int j,k
;a=0x1E; b=0b00011110; b=30; j=44; k=90
;Char a=0x1E,b=0b00011110,b=30
;ungined int j=44,k=90


0b b

0x

. : !

:
. ][
.
.

;} unsigned Char reyazifizek[4]={12,15,9,16 4
.
:
;}unsigned Char a[4]={22,45,76,90
;]unsigned Char a[4
;a[0]=22; a[1]=45; a[2]=76; a[3]=90

a 8 unsigned Char 4 .
][ . .
:

15
www.picpars.com

AVR C

1391


unsigned Char dabirestan [3][4]; : 3
) (0 ) (1 ) (2 4 . :
{]unsigned Char dabirestan[3][4
}{12,15,9,16},{10,11,8,13},{11,13,7,10
;}

3 :

;]a=dabirestan[1][2

: ][
.
.
.

:
Char .
:
;"Char a[]="picpars.com

picpars.com .

16
www.picpars.com

AVR C

1391

)(Proj-1

for
CPU
...

for
{)for(i=0; i<=255; i++
;PORTA=i
;)delay_ms(200
}

) i=0 i 255 i ( i
.
.

for
{)for(i=255; i>=0; i--
;PORTA=i
;)delay_ms(200
}

) i=255 i 0 i (
.
: i++ i+=2 !
: signed int i ! ) (

17
www.picpars.com

AVR C

1391
: signed unsigned !

break , continue
break CPU
.
continue break continue
CPU
.
.

while
)( true false True . 1
CPU
false . 0
CPU . while :

while
{)While(1
;PORTA=~PORTA
;)delay_ms(400
}

1 )(
break . A 400 .
{)while(i<10
;PORTA=~PORTA
;)delay_ms(400
;i++
}

18
www.picpars.com

AVR C

1391

i<10 i
. 10
.
{)while(1
;PORTA=~PORTA
;)delay_ms(400
;i++
;if(i>10) break
}

i 10 if
.
.

do while
while
. .
{do
;PORTA=~PORTA
;)delay_ms(400
;i++
;)}while(i<10

switch
.
) (case case
. .

19
www.picpars.com

AVR C

1391

;i=1
{)switch(i
case 1:
;PORTA=10
;break
case 2:
;PORTA=20
;break
default:
;PORTA=0b10101010
}

i case .
i=1 10 A break case
. case
. i 1 2 default
A . i 1
.

2 IF

(Proj-2) define

if .
:
;If(PINB.0==1) a=50
;If(a==30) PORTB.2=1

B a 50 .
a 30 2 B .
;If(a==30) PORTB.2=1
;Else PORTB.3=1

20
www.picpars.com

AVR C

1391

a 30 2 B 3 B ) .
=(else
:
;If(a==30) PORTB.2=1
;Else if(a==31) PORTB.3=1
;Else if(a==31) PORTB.4=1
;Else if(a==32) PORTB.5=1
;else PORTB.6=1

.
Else if else
.
: CPU

else

.
}{
:
{)If(a==30
;PORTB.2=1
;X=10
{)}else if(a==31
;PORTB.3=1
;X=20
{)}else if(a==32
;PORTB.4=1
;X=30
{ } else
;PORTB.5=1
;X=40
}

#define
Define .
.

21
www.picpars.com

AVR C

1391

#define val 170

170 val .

170 170
170 150 .
define 170 val
. 170 150 .
#define data_port PORTA

PORTA data_port
PORTA data_port
. .
.
define if
200
.
: PORTX PINX .
define .

22
www.picpars.com

AVR C

1391

3 C

)(Proj-3

C )( main
.
.
:
.1 ) sin(x ) cos(x ) delay_ms(x...
.2 .
.
.

.

CPU .
:
:
.1 )( main.
.2 )( main )( main
.
) (void .
.

23
www.picpars.com

AVR C

1391

{)void main(void

)( main .
(1 :main
{)void keypad(void

{)void main(void
//www.picpars.com
;)(keypad
}

keypad main
.
(2 :main
;)(void keypad
{)void main(void
//
;)(keypad
}
{)void keypad (void
//
}

keypad main keypad .


{)(void blink_led
;PORTA=~PORTA
;)delay_ms(500
}

500
. main )( blink_led .
{)void blink_led2(unsigned int d
;PORTA=~PORTA
;)delay_ms(d
}

24
www.picpars.com

AVR C

1391

0) int (65535 .
. blink_led2(1000)
A 1000 . 1 65535
. .


{)unsigned int blink_led3(unsigned int d
;d=d*1000
;return d
}

1000 .
.
1000 ) (1

25
www.picpars.com

AVR C

1391

4 LCD )(
LCD )(Proj-4

LCD

LCD
C C .
CodeVisionAVR V2.05.3 LCD .
> #include <alcd.h . stdio.h
.

LCD
LCD

5V+
) 1 (

VSS

2
3

VCC
VEE

RS

R/W

DB0

DB0 DB7 ) 8(

8
9
10
11

DB1
DB2
DB3
DB4

DB4 DB7 4

12
13
14
15

DB5
DB6
DB7
A

16

0 DB0-DB7 1
0 LCD 1

LCD

5V+ 15 16 LED

1-4 LCD

26
www.picpars.com

AVR C

1391

LCD
HEX

0X01

)(HOME

0X02

0X04

0X05

0X06

0X07

0X08

0X0A

0X0C

0X0E

0X0F

0X10

0X14

0X18

8 75

0X38

4 75

0X28

0X10

0XC0

2-4 LCD


LCD ) (stdio.h
. LCD .

c%

d%
i%
e%
E%

10
10



27
www.picpars.com

AVR C

1391
f%
s%
u%
X%
x%
P%
%%

LCD

SRAM
10

FLASH

2-4

;) lcd_init(16 LCD .

LCD

. LCD 202 20 .

;)( lcd_clear . :
;)(lcd_clear

;)( lcd_puts SRAM . :


;)( lcd_putsf

FLASH

;)lcd_puts(buf

. :

;)"lcd_putsf("LCD test project

;)( lcd_putsfe

EEPROM

. :

;)lcd_putsfe(ebuf

;)( lcd_putchar SRAM

;)' lcd_putchar('H

FLASH

. :

;)lcd_putchar(c

;)( lcd_gotoxy x , y 7 .
lcd_gotoxy(0,6); : !
;)(lcd_write_data

LCD

. :

;)lcd_write_data(0x0f

28
www.picpars.com

AVR C

1391

;) lcd_write_byte(addr,data ) (
LCD .
;)(lcd_read_byte

RAM

LCD


;) :lcd_puts(buffer char .
sprintf char ] buffer[32
SRAM .

LCD 2*16

32 .
;)(lcd_clear
;)sprintf(buffer,"Temp=%.2f\xdfC\nwww.picpars.com",temp
;)lcd_puts(buffer
;)delay_ms(1500

temp

%.2f

%f . \xdf C
\n . .
.


LCD

Project Configure C Compiler Libraries

(Alphanumeric LCD (alcd.h Enable Alphanumeric LCD Support


LCD ) . (

29
www.picpars.com

1391

Sprintf

AVR C

Project Configure C Compiler Code Generation

(s)printf Features .
float sprintf ) (%f

float, width, precision

.
.

30
www.picpars.com

AVR C

1391

5 4*4

(Proj-5) 44


.
16
44 44 4 4 .
16 8 .

PORTC.0 PORTC.3 ) (R PORTC.0 PORTC.3 )
(C ) . Pullup (
)
0 3 ( .

) (c 0 3 . c 255
) (r r 4 c
0 15 arrkey
.
. 50
.
;}unsigned char scan[4]={0XFE,0XFD,0XFB,0XF7
;char key
{=]char arrkey[16
'7','8','9','/',
'4','5','6','*',
'1','2','3','-',
;}''C','0','=','+

31
www.picpars.com

AVR C

1391

scan 4 PORTC.0) .
(PORTC.3
arrkey ) .
(.
#define
#define
#define
#define
#define

c1 PINC.4
c2 PINC.5
c3 PINC.6
c4 PINC.7
keypad_port PORTC

.
) .(
{)(char keypad
;unsigned char r,c,k
;DDRC=0X0F
;keypad_port=0XFF
{)while(1
{)for (r=0; r<4; r++
;c=255
;]keypad_port=scan[r
;)delay_us(10
;if(c1==0) c=0
;if(c2==0) c=1
;if(c3==0) c=2
;if(c4==0) c=3
{)if (c!=255
;]k=arrkey[(r*4)+c
;)while(c1==0
;)while(c2==0
;)while(c3==0
;)while(c4==0
;)delay_ms(50
;return k
}
}
}
}

;)( key=keypad ) .
key char(
:

32
www.picpars.com

AVR C

1391

{)while(1
;)(key=keypad
;)(lcd_clear
;)sprintf(buffer,"key=%c",key
;)lcd_puts(buffer
}

:c=255 255

. : 255
0 3 .

33
www.picpars.com

AVR C

1391

6
.
LED 0 9 .

a,b,c,d,e,f,g

LED
PicPars Tools
.

) ( LED ) (
. 8
7 " ".
. 7
X.7
PORTX.7 .
) ( .

34
www.picpars.com

AVR C

1391

)(Proj-6

PicPars Tools
anode_seg . for 0 9
.

]PORTC = anode_seg[i

.
for .

signed char i

{)for(i=9; i>=0; i--


;]PORTC = anode_seg[i
;)delay_ms(500
}

74LS247
(Proj-7) 74LS247

BCD 4
2 .
bcd.h BCD .

35
www.picpars.com

AVR C

1391



)(Proj-8

74LS248
(Proj-9) 74LS248

74LS248 .

36
www.picpars.com

AVR C

1391

)(
4 )(Proj-10

.
4 A-G DP 4-1
. :
.
1
4 . 1 .
2 3 ) .
( .

control

cathode_seg .

)( display ) c (
data . for data 4
i for control . 4
.

37
www.picpars.com

AVR C

1391

{)(void display
;unsigned int z
;unsigned int i,c1,c2,c3,c4
;]c4 = c%10; data[3]=cathode_seg[c4
;]z=c/10; c3 = z%10; data[2]=cathode_seg[c3
;]z=c/100; c2 = z%10; data[1]=cathode_seg[c2
;]z=c/1000; c1 = z%10; data[0]=cathode_seg[c1
{)for (i=0; i<4; i++
;]PORTB = control[i
;]PORTA = data[i
;)delay_ms(4
}
;PORTB=0x0f
}

:
delay
.
delay_7segmeny
.
{)delay_7segmeny(unsigned int d
;unsigned char i
;d/=16
{)for(i=0; i<=d; i++
;)(display
}
}

16 display .
16 display 4
. 16=44 16 display d
16 display .
.

38
www.picpars.com

AVR C

1391

. ) (
.
. AVR .
PWM CTC
... .
CodeVision .


AVR :



At90S , ATTiny .
ATmega ATmega8 ATmega163 ) .
(
AVR ATTiny13 , ATTiny2313 .
codewizard CodeVision
) ( .
codewizard
.
:

39
www.picpars.com

AVR C

1391

PWM ) (

CTC

PWM ) (

:
OCR0

TCNT0

OC0 .
TCNT0 .
TCCR0
PWM . 8
codewizard . TIFR
TOV0 ...

codewizard

.
TCCR0 :

TCCR0

CS00

CS01

CS02

CS00

CS01

CS02

) (

64

256

1024

) T0 (
) T0 (

1-7

40
www.picpars.com

AVR C

1391

) (
CPU .
.

.

.

) (

1 )(Proj-11

.1 . TCNT0
.
.

.2 ) (Base TCNT0
.

. 8
1 :
FT=8000000/64=125000Hz

N=64

F=8000000

T=1/125000=8us

1ms/8ms=125

255-125=130 TCNT0=130

125 1 . TCNT0
255 255
130 255 125 .

41
www.picpars.com

AVR C

1391

1
timer0_1ms . 500
.
{)(void timer0_1ms
TCCR0=0x03; // Clock value: 125.000 kHz
TCNT0=130; //255-125=130
;))while(!(TIFR & 0x02
;TIFR |= 0x02
}

) (

1 )(Proj-12


CPU
.

. CPU LCD
LCD ;)( lcd_init
.
{)interrupt [TIM0_OVF] void timer0_ovf_isr(void
;TCNT0=130
;PORTA.0=~PORTA.0
}

TCNT0 .

42
www.picpars.com

AVR C

1391

CTC

OCR0 CTC 30 )(Proj-13

TCNT0 OCR0 ) (

TCNT0

. OC0
:

OC0 .

OC0 .

OC0 .

Clear

OC0 .

Set

Disconnected

Toggle

OCF0
OCR0 . OCR0
CTC .
30ms . 30ms 30ms
. 30ms 8MHz

N=1024

7.813KHz .
N .
FT=8000000/1024=7812.5Hz

N=1024

F=8000000

T=1/7812.5=128us

30ms/128us=234

OCR0 =234

{)void main(void
;PORTB=0x00
;DDRB=0x08
;TCCR0=0x1D
TCNT0=0x00
;OCR0=234
;)while (1
}

: OCR0 PORTB.3 .

43
www.picpars.com

AVR C

1391

PWM

PWM )(Proj-13

PWM Pulse Width Modulation

Pulse Duration

Mode . . PWM
AC DC ... . PWM
WGM01 , WGM00 TCCR0 4 .
SET TOV0 OCR0

WGM01 WGM00

0xFF

0xFF

PWM 1

OCR0

0xFF
2-7

PWM

PWM

PWM 3 CTC OCR0


TCNT0 ) (OCF0 OC0
. 3 xFF0
TCNT0=0 . PWM OC0
:
OC0 .1
OC0 .2 .
OC0 .3
: / 1 2 .
PWM :
)FPWM=(fclk_IO)/(N*256

N 128643281 1024256 fclk_IO


.

44
www.picpars.com

AVR C

1391

%30 %100 %30 1 %70


OCR0 %30 .

OCR0=(255*30)/100=76
FPWM=(8000000)/(1024*256)=30Hz

{)void main(void
;PORTB.3=0
DDRB.3=1;//OC0 OUT PUT
;TCNT0=0
;OCR0=76
TCCR0=0x6D;// Clock value: 7.813 kHz & NON INVERT
;)while (1
}

:Codewizard
Clock Source: System Clock
Clock Salue: 7.813kHZ Mode:
Fast PWM top=ffh
Output: Non inverted PWM 0 Overflow Intrrupt 0 Compare Match Interrupt
Timer Value: 0h Compare: 0h

OCR0 %30
.

45
www.picpars.com

AVR C

1391


) 8( ) 16(
.
.
atmega16 LED
.
Perscale .
.
Perscale

) . (
4000000 F=4MHz .
Codewizard .

8
1

Perscale=64 FF
. TCCR0=0x03 . :
Ft0= 4MHz/64=62500Hz


8 250 ms4 16us*250=4ms
250 1 1sec=1000ms 1000/4ms=250
Tt0=1/62500=16us

46
www.picpars.com

AVR C

1391

250 3 4
. 4ms 250 0 FF
6 256
250 . 4ms
. c . 1
250 . 125 .
{)while (1
{ )if(c>=250
;led=~led
;c=0
}
}

c 250 A.0 .
. .
1 ) (

1 ) (

.

. !
{)(void delay_1s
{)for(c=0; c<=250; c++
;CCR0=0x03
;TCNT0=0x06
;OCR0=0x00
while(!(TIFR & 0x02)){//wait for 1s
//other program with out delay
;}
TCCR0=0x00; //timer0 off
TIFR|=0x02; //default flag timer0 ==> change to 1
}
}

1 delay_1s .
if c 250 for .
TIFR

47
www.picpars.com

AVR C

1391

0 1 2
.
02 . AND TIFR & 0x02 TIFR ) (TOV0
! .
TOV0 ! .
TIFR OR TOV0
.
: TOV0 .
.

.
ms4
1 .
:
CPU ms4
. sprintf LCD lcd_puts
LCD .

) . (

16
256 16 65536 .
1

48
www.picpars.com

AVR C

1391

16 0 FFFF 0 65535 .
62500 16us 1 16us 62500
3036=62500-65536 36 30 .
16 c
.
{)interrupt [TIM1_OVF] void timer1_ovf_isr(void
;TCNT1H=0xBDC >> 8
;TCNT1L=0xBDC & 0xff
;led=~led
}

3036 .
A.0 .
1 ) (

1 ) (

2 TOV1 04 TIFR
.
{)(void delay_1s
;TCCR1A=0x00
;TCCR1B=0x03
;TCNT1H=0x0B
;TCNT1L=0xDC
;ICR1H=0x00
;ICR1L=0x00
;OCR1AH=0x00
;OCR1AL=0x00
;OCR1BH=0x00
;OCR1BL=0x00
while(!(TIFR & 0x04)){//wait for 1s
//other program with out delay
;}
TCCR1B=0x00; //timer0 off
TIFR|=0x04; //default flag timer1 ==> change to 1
}

49
www.picpars.com

AVR C

1391

8 ADC
AVR C

Code Vision

.
. ADC

8 10 ADC .
ADMUX
7
6
5
4
3
2
1
0
REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
REFS0,1 ADC :

REFS0=0 REFS1=0 AREF .

REFS0=1 REFS1=0 AVCC .

REFS0=1 REFS1=1 2.56 .

REFS0=0 REFS1=1

: . 2.56

AREF.
ADLAR 8 16 ADC . ADCH,ADCL
MUX0-4 .
ADCSRA
7
6
5
4
3
2
1
0
ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
ADEN ADC .
ADSC Single
. Free .
ADATE ADC .
ADTS SFIOR .
ADIF ADC ( .
(

50

www.picpars.com

AVR C

1391

ADIE
ADC .
ADPS0-3 ADC :

ADPS2 ADPS1 ADPS0

2
2
4
8
16
32
64
128

0
1
0
1
0
1
0
1

0
0
1
1
0
0
1
1

0
0
0
0
1
1
1
1

(ADCH,ADCL) ADC

ADC ADCL ADCH


ADCW 16 .
SFIOR
7
6
5 4
3
2
1
0
ADTS2 ADTS1 ADTS0 - ACME PUD PSR2 PSR1
ADTS0-2 :
ADC

ADTS2 ADTS1 ADTS0

)( ) B ) 1

)( ) )
)( ) )
)( ) )
)(

: ) 10( ADC 50KHZ 200KHZ

ADC .
ADPS0-2
ADC .

51
www.picpars.com

AVR C

1391

LM35 LCD
.
ADC LM35

Code Wizard ADC :

1 ADC Enabled
Volt Ref: Int
Clock: 125KHz
Auto trigger: None
#define ADC_VREF_TYPE 0xC0 //H1
SFIOR=0x00; //H2
ADMUX=ADC_VREF_TYPE & 0xff; //H3
ADCSRA=0x86; //H4

:H1 ADC_VREF_TYPE 0xC0 read_adc AND


OR ADMU .
:H2
:H3 AND FF ADLAR
ADCW .
:H4 ADC 64 ADC KHZ125
.
read_adc Code Wizard .
float t;//H1
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);//H2
delay_us(10);//H3
ADCSRA|=0x40;//H4
while ((ADCSRA & 0x10)==0);//H5
ADCSRA|=0x10;//H6
t=((ADCW*2.56)/1023)*100;//H7
return t;//H8

:H1
:H2 C0 FF ADN C0 1
C0 OR 1 ADMUX= C1 1
.
:H3 10 ADC
:H4 ADCSRA=0x86 10000110 X400 01000000 .
OR OR4086 11000110 6 1 ADSC
.
:H5 4 ADIF
1 .
:H6 ADIF OR 10 .

52
www.picpars.com

AVR C

1391

:H7 ADCW 2.56


ADCW 2.56 1023 100
. 1023 n=10 10
n^2 - 1 2^10 -1 = 1023

:H8 t .

...

53
www.picpars.com

AVR C

1391

www.picpars.com
Help CodeVisionAVR V2.05.3

1.2

54
www.picpars.com

AVR C

1391


: AVR
: C

) CodeVisionAVR V2.05.3 :(
) Proteus 7.10 SP0 :(
) PicPars Tools v1.2(

1391/02/28 :

1391/04/16 :

picpars@gmail.com :

http://www.picpars.com/351/ :

.
.
/ !

www.picpars.com :

picpars@gmail.com :
!

55
www.picpars.com

You might also like