.module DAC.c
.area text(rom, con, rel)
.dbfile C:\mega8\DAC\DAC.c
.dbfunc e timer1_init _timer1_init fV
.even
_timer1_init::
.dbline -1
.dbline 23
; /*******************************************/
; /* 广州天河双龙电子公司 */
; /* http://www.sl.com.cn */
; /* 用PWM实现的DAC转换演示程序 */
; /* 外部中断演示程序 */
; /* 作者:ntzwq@wx88.net */
; /* 2002年5月11日 */
; /* 目标MCU:MEGA8 晶振:内部RC(INT) 8MHZ */
; /*******************************************/
; //使用内部RC振荡,PB6-G,PB7-DP短路块连接
; //使用INT0/INT1按键改变DAC输出电压大小
; //可以使用ADC0通道测量DAC的输出电压
; //演示了使用PC机对MEGA8开发板进行控制的方法
; #include
; #include
; #define Vref 500//参考电压值
; #include"uart.h"
; #include"adc.h"
; extern unsigned char read_flash(unsigned int add);
; unsigned int adc_old;
; //T1初始化程序
; void timer1_init(void)
; {
.dbline 24
; TCCR1B = 0; //stop
clr R2
out 0x2e,R2
.dbline 25
; TCNT1H = 0;
out 0x2d,R2
.dbline 26
; OCR1A=0;
clr R3
sts 74+1,R3
sts 74,R2
.dbline 27
; TCCR1A =(1 ldi R24,131
out 0x2f,R24
.dbline 28
; TCCR1B =(1 ldi R24,1
out 0x2e,R24
.dbline -2
.dbline 29
; }
L1:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 2
rjmp _int_isr
.area text(rom, con, rel)
.dbfile C:\mega8\DAC\DAC.c
.area vector(rom, abs)
.org 4
rjmp _int_isr
.area text(rom, con, rel)
.dbfile C:\mega8\DAC\DAC.c
.dbfunc e int_isr _int_isr fV
; i -> R20
.even
_int_isr::
rcall push_lset
rcall push_gset1
.dbline -1
.dbline 33
; //两个中断向量使用同一个中断处理函数,实现对INT0/INT1按键的处理
; #pragma interrupt_handler int_isr:iv_INT0 int_isr:iv_INT1
; void int_isr(void)
; {
.dbline 35
; unsigned char i;
; delay_ms(10);
ldi R16,10
ldi R17,0
rcall _delay_ms
.dbline 36
; i=PIND&0x0c;//键盘消抖动
in R20,0x10
andi R20,12
.dbline 37
; if(i==0x0c)
cpi R20,12
brne L3
.dbline 38
; return;
rjmp L2
L3:
.dbline 40
; else
; {
.dbline 41
; if (i==0x08)//INT0键按下
cpi R20,8
brne L5
.dbline 42
; {
.dbline 43
; if(OCR1A in R24,0x2a
in R25,0x2b
cpi R24,239
ldi R30,3
cpc R25,R30
brsh L7
.dbline 44
; OCR1A+=0x10;//递增
in R24,0x2a
in R25,0x2b
adiw R24,16
sts 74+1,R25
sts 74,R24
rjmp L8
L7:
.dbline 46
; else
; OCR1A=0x3ff;
ldi R24,1023
ldi R25,3
sts 74+1,R25
sts 74,R24
L8:
.dbline 47
; }
L5:
.dbline 48
; if (i==0x04)//INT1键按下
cpi R20,4
brne L14
.dbline 49
; {
.dbline 50
; if(OCR1A>0x10)
ldi R24,16
ldi R25,0
in R2,0x2a
in R3,0x2b
cp R24,R2
cpc R25,R3
brsh L11
.dbline 51
; OCR1A-=0x10;//递减
in R24,0x2a
in R25,0x2b
sbiw R24,16
sts 74+1,R25
sts 74,R24
rjmp L14
L11:
.dbline 53
; else
; OCR1A=0;
clr R2
clr R3
sts 74+1,R3
sts 74,R2
.dbline 54
; }
L13:
.dbline 56
L14:
.dbline 55
; while((PIND&0x0c)!=0x0c)//检查按键释放
in R24,0x10
andi R24,12
cpi R24,12
brne L13
.dbline 57
; ;
; }
.dbline -2
.dbline 58
; }
L2:
rcall pop_gset1
rcall pop_lset
.dbline 0 ; func end
reti
.dbsym r i 20 c
.dbend
.dbfunc e main _main fV
; temp -> R22,R23
; i -> R10
.even
_main::
.dbline -1
.dbline 61
; //MAIN程序
; void main(void)
; {
.dbline 64
; unsigned char i;
; unsigned int temp;
; DDRB=(1 ldi R24,2
out 0x17,R24
.dbline 65
; DDRD=0xf0;
ldi R24,240
out 0x11,R24
.dbline 66
; PORTD=0xff;
ldi R24,255
out 0x12,R24
.dbline 67
; OSCCAL=read_flash(0x1f20);
ldi R16,7968
ldi R17,31
rcall _read_flash
out 0x31,R16
.dbline 68
; adc_mux=0;
clr R2
sts _adc_mux,R2
.dbline 69
; uart_init();
rcall _uart_init
.dbline 70
; timer1_init();
rcall _timer1_init
.dbline 71
; adc_init();
rcall _adc_init
.dbline 72
; GICR = 0xC0;//int0,int1中断使能
ldi R24,192
out 0x3b,R24
.dbline 73
; SEI();
sei
.dbline 74
; adc_rel=0;
clr R2
clr R3
sts _adc_rel+1,R3
sts _adc_rel,R2
rjmp L18
L17:
.dbline 79
; //puts("广州天河双龙电子公司");
; //puts("HTTP://WWW.SL.COM.CN");
; //puts("MEGA8开发板DAC测试程序");
; while(1)
; {
.dbline 80
; i=getchar();
rcall _getchar
mov R10,R16
.dbline 81
; switch (i)
mov R20,R10
clr R21
cpi R20,77
ldi R30,0
cpc R21,R30
brne X2
rjmp L29
X2:
cpi R20,79
ldi R30,0
cpc R21,R30
breq L28
cpi R20,82
ldi R30,0
cpc R21,R30
brne X3
rjmp L30
X3:
cpi R20,83
ldi R30,0
cpc R21,R30
breq L23
cpi R20,84
ldi R30,0
cpc R21,R30
breq L25
rjmp L20
X0:
.dbline 82
; {
L23:
.dbline 84
; case 'S'://MEGA8 DAC程序ID
; {
.dbline 85
; puts("DAC TEST ver1.0");
ldi R16, ldi R17,>L24
rcall _puts
.dbline 86
; break;
rjmp L21
L25:
.dbline 89
; }
; case 'T'://调节输出电压
; {
.dbline 90
; temp=getchar();
rcall _getchar
mov R22,R16
clr R23
.dbline 91
; temp=temp mov R23,R22
clr R22
.dbline 92
; temp|=getchar();
rcall _getchar
mov R2,R16
clr R3
or R22,R2
or R23,R3
.dbline 93
; if (temp cpi R22,0
ldi R30,4
cpc R23,R30
brsh L26
.dbline 94
; OCR1A=temp;
sts 74+1,R23
sts 74,R22
rjmp L21
L26:
.dbline 96
; else
; OCR1A=0x3ff;
ldi R24,1023
ldi R25,3
sts 74+1,R25
sts 74,R24
.dbline 97
; break;
rjmp L21
L28:
.dbline 100
; }
; case 'O'://读取OCR1A寄存器值
; {
.dbline 101
; temp=OCR1A;
in R22,0x2a
in R23,0x2b
.dbline 102
; putchar(temp/256);
movw R16,R22
mov R16,R17
clr R17
rcall _putchar
.dbline 103
; putchar(temp%256);
mov R16,R22
andi R17,0
rcall _putchar
.dbline 104
; break;
rjmp L21
L29:
.dbline 107
; }
; case 'M'://设置ADC工作通道
; {
.dbline 108
; adc_mux=getchar()&0x03;
rcall _getchar
mov R24,R16
andi R24,3
sts _adc_mux,R24
.dbline 109
; break;
rjmp L21
L30:
.dbline 112
; }
; case 'R'://读取ADC通道及ADC转换结果
; {
.dbline 113
; putchar(adc_mux);
lds R16,_adc_mux
rcall _putchar
.dbline 114
; putchar(adc_old/256);
lds R16,_adc_old
lds R17,_adc_old+1
mov R16,R17
clr R17
rcall _putchar
.dbline 115
; putchar(adc_old%256);
lds R16,_adc_old
andi R17,0
rcall _putchar
.dbline 116
; }
.dbline 117
; }
L20:
L21:
.dbline 118
L18:
.dbline 78
rjmp L17
X1:
.dbline -2
.dbline 119
; }
; }
L16:
.dbline 0 ; func end
ret
.dbsym r temp 22 i
.dbsym r i 10 c
.dbend
.area bss(ram, con, rel)
.dbfile C:\mega8\DAC\DAC.c
_adc_old::
.blkb 2
.dbsym e adc_old _adc_old i
.area data(ram, con, rel)
.dbfile C:\mega8\DAC\DAC.c
L24:
.blkb 16
.area idata
.byte 'D,'A,'C,32,'T,'E,'S,'T,32,'v,'e,'r,49,46,48,0
.area data(ram, con, rel)
.dbfile C:\mega8\DAC\DAC.c