使用PIC18单片机的ADC转换模块对RA0口输入的模拟电压信号进行转换,然后通过PORTD端口输出,而这里与PORTD对应引脚相连接的是8个LED。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
list P = 18F452 ;指明单片机型号为PIC18F452
#include P18F452.INC ;包含一个头文件,其中定义了一些端口及一些特殊寄存器的地址
 
	org	0000h  	;PIC上电时从0000h单元开始执行
	goto	main  	;跳转到主程序
	ORG	0008H  	;中断向量入口
	BTFSS	PIR1, ADIF 	; AD转换完成中断
	RETFIE
	GOTO	AD_ISR
 
	ORG	0030H  	;主程序定位
MAIN
	CLRF	TRISD  	;设定D口方向为输出
	CLRF 	PORTD 	;设定C口方向为输出
	BSF 	TRISA, 0 	; 使用AN0输入
	MOVLW	81H 	;FOSC/32, AN0, 开启
	MOVWF	ADCON0
	MOVLW	0EH 	;左对齐,AN0为模拟输入
	MOVWF	ADCON1 	;VDD & VSS为参考电压
	BCF 	INTCON, TMR0IF
	BCF 	PIR1, ADIF 	;清AD中断标志位
	BSF 	PIE1, ADIE 	;开AD中断
	BSF 	INTCON, PEIE 	;开外围中断
	BSF  	NTCON, GIE 	;开总中断
	MOVLW 	C7H 	;TMR0 8位,分频比为1:256
	MOVWF	T0CON
LOOP
	CALL	DELAY
	BSF  	DCON0, GO 	;开启 A/D转换
	GOTO	LOOP
 
DELAY
 
	BTFSS  	NTCON, TMR0IF 	; 等待延时,采样保持
	GOTO  	DELAY
	BCF  	NTCON, TMR0IF
	RETURN
 
AD_ISR ;AD转换完成时调用的中断服务程序,将转换结果输出
	ORG 	0200H
	MOVFF	ADRESH, PORTD 	;显示转换结果
	BCF 	PIR1, ADIF 	;清AD中断标志位
	RETFIE
END 	;程序结束
Tags: ,.
Home

No Comments Now!

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (required),(will not be published)
  10 + 5 = ?
Please leave these two fields as-is:
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://www.dutor.net),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.