.module DAC.c
.area text(rom, con, rel)
.dbfile E:\icc\iccavr\mega8\mega8\DAC\DAC.c
.dbfunc e timer1_init _timer1_init fV
.even
_timer1_init::
.dbline -1
.dbline 24
; /*******************************************/
; /* 广州天河双龙电子公司 */
; /* 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"
; #define osc_address 0x1ff0
; extern unsigned char read_flash(unsigned int add);
; unsigned int adc_old;
; //T1初始化程序
; void timer1_init(void)
; {
.dbline 25
; TCCR1B = 0; //stop
clr R2
out 0x2e,R2
.dbline 26
; TCNT1H = 0;
out 0x2d,R2
.dbline 27
; OCR1A=0;
clr R3
out 0x2b,R3
out 0x2a,R2
.dbline 28
; TCCR1A =(1 ldi R24,131
out 0x2f,R24
.dbline 29
; TCCR1B =(1 ldi R24,1
out 0x2e,R24
.dbline -2
.dbline 30
; }
L1:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 2
rjmp _int_isr
.area text(rom, con, rel)
.dbfile E:\icc\iccavr\mega8\mega8\DAC\DAC.c
.area vector(rom, abs)
.org 4
rjmp _int_isr
.area text(rom, con, rel)
.dbfile E:\icc\iccavr\mega8\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 34
; //两个中断向量使用同一个中断处理函数,实现对INT0/INT1按键的处理
; #pragma interrupt_handler int_isr:iv_INT0 int_isr:iv_INT1
; void int_isr(void)
; {
.dbline 36
; unsigned char i;
; delay_ms(10);
ldi R16,10
ldi R17,0
rcall _delay_ms
.dbline 37
; i=PIND&0x0c;//键盘消抖动
in R20,0x10
andi R20,12
.dbline 38
; if(i==0x0c)
cpi R20,12
brne L3
.dbline 39
; return;
rjmp L2
L3:
.dbline 41
; else
; {
.dbline 42
; if (i==0x08)//INT0键按下
cpi R20,8
brne L5
.dbline 43
; {
.dbline 44
; if(OCR1A in R24,0x2a
in R25,0x2b
cpi R24,239
ldi R30,3
cpc R25,R30
brsh L7
.dbline 45
; OCR1A+=0x10;//递增
in R24,0x2a
in R25,0x2b
adiw R24,16
out 0x2b,R25
out 0x2a,R24
rjmp L8
L7:
.dbline 47
; else
; OCR1A=0x3ff;
ldi R24,1023
ldi R25,3
out 0x2b,R25
out 0x2a,R24
L8:
.dbline 48
; }
L5:
.dbline 49
; if (i==0x04)//INT1键按下
cpi R20,4
brne L14
.dbline 50
; {
.dbline 51
; if(OCR1A>0x10)
ldi R24,16
ldi R25,0
in R2,0x2a
in R3,0x2b
cp R24,R2
cpc R25,R3
brsh L11
.dbline 52
; OCR1A-=0x10;//递减
in R24,0x2a
in R25,0x2b
sbiw R24,16
out 0x2b,R25
out 0x2a,R24
rjmp L14
L11:
.dbline 54
; else
; OCR1A=0;
clr R2
clr R3
out 0x2b,R3
out 0x2a,R2
.dbline 55
; }
L13:
.dbline 57
L14:
.dbline 56
; while((PIND&0x0c)!=0x0c)//检查按键释放
in R24,0x10
andi R24,12
cpi R24,12
brne L13
.dbline 58
; ;
; }
.dbline -2
.dbline 59
; }
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 62
; //MAIN程序
; void main(void)
; {
.dbline 65
; unsigned char i;
; unsigned int temp;
; DDRB=(1 ldi R24,2
out 0x17,R24
.dbline 66
; DDRD=0xf0;
ldi R24,240
out 0x11,R24
.dbline 67
; PORTD=0xff;
ldi R24,255
out 0x12,R24
.dbline 68
; OSCCAL=read_flash(osc_address);
ldi R16,8176
ldi R17,31
rcall _read_flash
out 0x31,R16
.dbline 69
; adc_mux=0;
clr R2
sts _adc_mux,R2
.dbline 70
; uart_init();
rcall _uart_init
.dbline 71
; timer1_init();
rcall _timer1_init
.dbline 72
; adc_init();
rcall _adc_init
.dbline 73
; GICR = 0xC0;//int0,int1中断使能
ldi R24,192
out 0x3b,R24
.dbline 74
; SEI();
sei
.dbline 75
; adc_rel=0;
clr R2
clr R3
sts _adc_rel+1,R3
sts _adc_rel,R2
rjmp L18
L17:
.dbline 80
; //puts("广州天河双龙电子公司");
; //puts("HTTP://WWW.SL.COM.CN");
; //puts("MEGA8开发板DAC测试程序");
; while(1)
; {
.dbline 81
; i=getchar();
rcall _getchar
mov R10,R16
.dbline 82
; switch (i)
mov R20,R10
clr R21
cpi R20,77
ldi R30,0
cpc R21,R30
breq L29
cpi R20,79
ldi R30,0
cpc R21,R30
breq L28
cpi R20,82
ldi R30,0
cpc R21,R30
breq L30
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 83
; {
L23:
.dbline 85
; case 'S'://MEGA8 DAC程序ID
; {
.dbline 86
; puts("DAC TEST ver1.0");
ldi R16, ldi R17,>L24
rcall _puts
.dbline 87
; break;
rjmp L21
L25:
.dbline 90
; }
; case 'T'://调节输出电压
; {
.dbline 91
; temp=getchar();
rcall _getchar
mov R22,R16
clr R23
.dbline 92
; temp=temp mov R23,R22
clr R22
.dbline 93
; temp|=getchar();
rcall _getchar
mov R2,R16
clr R3
or R22,R2
or R23,R3
.dbline 94
; if (temp cpi R22,0
ldi R30,4
cpc R23,R30
brsh L26
.dbline 95
; OCR1A=temp;
out 0x2b,R23
out 0x2a,R22
rjmp L21
L26:
.dbline 97
; else
; OCR1A=0x3ff;
ldi R24,1023
ldi R25,3
out 0x2b,R25
out 0x2a,R24
.dbline 98
; break;
rjmp L21
L28:
.dbline 101
; }
; case 'O'://读取OCR1A寄存器值
; {
.dbline 102
; temp=OCR1A;
in R22,0x2a
in R23,0x2b
.dbline 103
; putchar(temp/256);
movw R16,R22
mov R16,R17
clr R17
rcall _putchar
.dbline 104
; putchar(temp%256);
mov R16,R22
andi R17,0
rcall _putchar
.dbline 105
; break;
rjmp L21
L29:
.dbline 108
; }
; case 'M'://设置ADC工作通道
; {
.dbline 109
; adc_mux=getchar()&0x03;
rcall _getchar
mov R24,R16
andi R24,3
sts _adc_mux,R24
.dbline 110
; break;
rjmp L21
L30:
.dbline 113
; }
; case 'R'://读取ADC通道及ADC转换结果
; {
.dbline 114
; putchar(adc_mux);
lds R16,_adc_mux
rcall _putchar
.dbline 115
; putchar(adc_old/256);
lds R16,_adc_old
lds R17,_adc_old+1
mov R16,R17
clr R17
rcall _putchar
.dbline 116
; putchar(adc_old%256);
lds R16,_adc_old
andi R17,0
rcall _putchar
.dbline 117
; }
.dbline 118
; }
L20:
L21:
.dbline 119
L18:
.dbline 79
rjmp L17
X1:
.dbline -2
.dbline 120
; }
; }
L16:
.dbline 0 ; func end
ret
.dbsym r temp 22 i
.dbsym r i 10 c
.dbend
.area bss(ram, con, rel)
.dbfile E:\icc\iccavr\mega8\mega8\DAC\DAC.c
_adc_old::
.blkb 2
.dbsym e adc_old _adc_old i
.area data(ram, con, rel)
.dbfile E:\icc\iccavr\mega8\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 E:\icc\iccavr\mega8\mega8\DAC\DAC.c