⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 led.s

📁 单片机中的ADC模块
💻 S
📖 第 1 页 / 共 3 页
字号:
	.module led.c
	.area lit(rom, con, rel)
_led_data::
	.word 192
	.word 249
	.word 164
	.word 176
	.word 153
	.word 146
	.word 130
	.word 248
	.word 128
	.word 144
	.word 136
	.word 131
	.word 198
	.word 161
	.word 134
	.word 142
	.dbfile F:\芯生光盘资料\范例代码\ADC\4led\led.c
	.dbsym e led_data _led_data A[32:16]kI
	.area text(rom, con, rel)
	.dbfile F:\芯生光盘资料\范例代码\ADC\4led\led.c
	.dbfunc e delay_1ms _delay_1ms fV
;              i -> R16,R17
	.even
_delay_1ms::
	.dbline -1
	.dbline 25
; /**********************************
; ** file_name led.c             **
; ** describe  七段数码管函数        **
; ** auther    芯生  **
; ** Time      2007-12-14          **
; ***********************************/
; 
; #include <iom128v.h>
; #include <macros.h>
; #include "stdtypes.h"
; #include "led.h"
; 
; //共阳极,LED数据 不显点,亮的段为1,连接为P0~P7对应a~g,dp
; const led_data[16]={
;       0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
; 	    0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
; 	  };
; 
; /***********************
; **8M时为 1000.25us
; **4M时为 999.5us
; **xtal 为晶振频率,单位为Mhz
; ************************/
; void delay_1ms(void)
; {
	.dbline 27
;  unsigned int i;
;  for(i=200;i>0;i--)
	ldi R16,200
	ldi R17,0
	xjmp L5
L2:
	.dbline 28
L3:
	.dbline 27
	subi R16,1
	sbci R17,0
L5:
	.dbline 27
	cpi R16,0
	cpc R16,R17
	brne L2
X0:
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbsym r i 16 i
	.dbend
	.dbfunc e delay_ms _delay_ms fV
;              i -> R20,R21
;              n -> R22,R23
	.even
_delay_ms::
	xcall push_gset2
	movw R22,R16
	.dbline -1
	.dbline 34
;   ;
; }
; /***********************
; **毫秒级延时
; ************************/
; void delay_ms(unsigned int n)
; {
	.dbline 36
;  unsigned int i;
;  for(i=n;i>0;i--)
	movw R20,R22
	xjmp L10
L7:
	.dbline 37
	xcall _delay_1ms
L8:
	.dbline 36
	subi R20,1
	sbci R21,0
L10:
	.dbline 36
	cpi R20,0
	cpc R20,R21
	brne L7
X1:
	.dbline -2
L6:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r i 20 i
	.dbsym r n 22 i
	.dbend
	.dbfunc e led_init _led_init fV
	.even
_led_init::
	.dbline -1
	.dbline 46
;  delay_1ms();
; }
; 	  	  
; /***********************
; ** 初始化对应端口为输出 
; ** ABCD 和 abcdefg dpi 共12个IO口
; ** 默认为 PD(4567) 和 PA(012345678)                                                                                                                                                                                                                                                                    
; ***********************/
; void led_init(void)
; {
	.dbline 47
;  led_ddr = 0xFF; //abcdefg dpi               各位设为输出
	ldi R24,255
	out 0x1a,R24
	.dbline 48
;  led_contrl_ddr |= (1<<led_a)|(1<<led_b)|(1<<led_c)|(1<<led_d); //ABCD四个控制设为输出
	in R24,0x11
	ori R24,240
	out 0x11,R24
	.dbline 51
;  
;  //初始化全灭
;  led_port = 0xFF;
	ldi R24,255
	out 0x1b,R24
	.dbline 52
;  led_contrl_port |= (1<<led_a)|(1<<led_b)|(1<<led_c)|(1<<led_d);
	in R24,0x12
	ori R24,240
	out 0x12,R24
	.dbline -2
L11:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e display_one _display_one fV
;           data -> R20
;          count -> R22
	.even
_display_one::
	xcall push_gset2
	mov R20,R18
	mov R22,R16
	.dbline -1
	.dbline 60
; }
; 
; /***********************
; ** 显示一位
; ** 输入:count显示在第几位(3210),data 要显示的数(0~f)
; ************************/
; void display_one(UNS8 count, UNS8 data)
; {
	.dbline 62
; 
;  led_contrl_port &= ~(1<<count); //选中对应要显示的位,从右至左,4567
	ldi R16,1
	mov R17,R22
	xcall lsl8
	mov R2,R16
	com R2
	in R3,0x12
	and R3,R2
	out 0x12,R3
	.dbline 63
;  led_port = led_data[data];    //显示的数
	ldi R24,2
	mul R24,R20
	movw R30,R0
	ldi R24,<_led_data
	ldi R25,>_led_data
	add R30,R24
	adc R31,R25
	lpm R0,Z+
	lpm R1,Z
	movw R30,R0
	out 0x1b,R30
	.dbline -2
L12:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r data 20 c
	.dbsym r count 22 c
	.dbend
	.dbfunc e display _display fV
;           temp -> R20
;           mode -> R10
;           data -> R22,R23
	.even
_display::
	xcall push_gset3
	mov R10,R18
	movw R22,R16
	.dbline -1
	.dbline 74
;  
;  //如果对位进行了调整,就不在是4567,而是对应的值
; }
; 
; /***********************
; ** 显示四位整数
; ** 输入:要显示的四位数data,显示模式mode,1为补零显示模式,默认不显示零
; ** 说明:可以小于四位数,mode=1时 自动补零
; ************************/
; void display(UNS16 data,UNS8 mode)
; {
	.dbline 77
; UNS8 temp;
;  //千位
;  if(data>=1000)
	cpi R22,232
	ldi R30,3
	cpc R23,R30
	brlo L14
	.dbline 78
;  {//补零
	.dbline 79
;    temp=data/1000;
	ldi R18,1000
	ldi R19,3
	movw R16,R22
	xcall div16u
	mov R20,R16
	.dbline 80
;    display_one(7,temp);
	mov R18,R20
	ldi R16,7
	xcall _display_one
	.dbline 81
;    delay_ms(6);  //6ms 是个经验值,刚好无闪烁,并且亮度较高
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 82
;    led_contrl_port |= (1<<7);
	sbi 0x12,7
	.dbline 84
;  
;  }
	xjmp L15
L14:
	.dbline 86
;  else
;   {  if(mode==1)
	.dbline 86
	mov R24,R10
	cpi R24,1
	brne L16
	.dbline 87
;     {
	.dbline 88
;     display_one(7,0);
	clr R18
	ldi R16,7
	xcall _display_one
	.dbline 89
;     delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 90
;     led_contrl_port |= (1<<7);
	sbi 0x12,7
	.dbline 91
; 	}
L16:
	.dbline 92
;   } 
L15:
	.dbline 95
;  
;  //百位
;  if(data>=100)
	cpi R22,100
	ldi R30,0
	cpc R23,R30
	brlo L18
	.dbline 96
;  {
	.dbline 97
;   temp=(data%1000)/100;
	ldi R18,1000
	ldi R19,3
	movw R16,R22
	xcall mod16u
	ldi R18,100
	ldi R19,0
	xcall div16u
	mov R20,R16
	.dbline 98
;   display_one(6,temp);
	mov R18,R20
	ldi R16,6
	xcall _display_one
	.dbline 99
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 100
;   led_contrl_port |= (1<<6);
	sbi 0x12,6
	.dbline 101
;  }
	xjmp L19
L18:
	.dbline 103
;  else
;  {
	.dbline 104
;   if(mode==1)
	mov R24,R10
	cpi R24,1
	brne L20
	.dbline 105
;   {
	.dbline 106
;   display_one(6,0);
	clr R18
	ldi R16,6
	xcall _display_one
	.dbline 107
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 108
;   led_contrl_port |= (1<<6);
	sbi 0x12,6
	.dbline 109
;   }
L20:
	.dbline 110
;  }
L19:
	.dbline 112
;  //十位
;  if(data>=10)
	cpi R22,10
	ldi R30,0
	cpc R23,R30
	brlo L22
	.dbline 113
;  {
	.dbline 114
;   temp=(data%100)/10;
	ldi R18,100
	ldi R19,0
	movw R16,R22
	xcall mod16u
	ldi R18,10
	ldi R19,0
	xcall div16u
	mov R20,R16
	.dbline 115
;   display_one(5,temp);
	mov R18,R20
	ldi R16,5
	xcall _display_one
	.dbline 116
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 117
;   led_contrl_port |= (1<<5);
	sbi 0x12,5
	.dbline 118
;  }
	xjmp L23
L22:
	.dbline 120
;  else
;  {
	.dbline 121
;   if(mode==1)
	mov R24,R10
	cpi R24,1
	brne L24
	.dbline 122
;   {
	.dbline 123
;   display_one(5,0);
	clr R18
	ldi R16,5
	xcall _display_one
	.dbline 124
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 125
;   led_contrl_port |= (1<<5);
	sbi 0x12,5
	.dbline 126
;   }
L24:
	.dbline 127
;  }
L23:
	.dbline 129
;  //个位
;   temp=data%10;
	ldi R18,10
	ldi R19,0
	movw R16,R22
	xcall mod16u
	mov R20,R16
	.dbline 130
;   display_one(4,temp);
	mov R18,R20
	ldi R16,4
	xcall _display_one
	.dbline 131
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 132
;   led_contrl_port |= (1<<4);
	sbi 0x12,4
	.dbline -2
L13:
	xcall pop_gset3
	.dbline 0 ; func end
	ret
	.dbsym r temp 20 c
	.dbsym r mode 10 c
	.dbsym r data 22 s
	.dbend
	.dbfunc e display_float _display_float fV
;          temp2 -> R22,R23
;           temp -> R20
;           data -> y+22
	.even
_display_float::
	xcall push_arg4
	xcall push_gset3
	sbiw R28,16
	.dbline -1
	.dbline 142
; }
; 
; /***********************
; ** 显示四位浮点数 本函数占用 mega16 的空间 19%
; ** 输入:要显示的四位数
; ** 说明:可以小于四位数,自动补零
; ************************/
; 
; void display_float(float data)
; {
	.dbline 146
;  UNS8 temp;
;  UNS16 temp2; //用于把浮点数变为整形
;  // 人为保证data的值,可以不要错误处理。
;  if (data>=1000)
	movw R30,R28
	ldd R2,z+22
	ldd R3,z+23
	ldd R4,z+24
	ldd R5,z+25
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L29
	ldi R17,>L29
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L27
	.dbline 147
;  {
	.dbline 148
;   led_error();
	xcall _led_error
	.dbline 149
;  }
L27:
	.dbline 150
;  if (data<=0)
	ldi R16,<L32
	ldi R17,>L32
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	movw R30,R28
 ; stack offset 4
	ldd R2,z+26
	ldd R3,z+27
	ldd R4,z+28
	ldd R5,z+29
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	xcall cmp32f
	brlt L30
	.dbline 151
;  {
	.dbline 152
;   led_error();
	xcall _led_error
	.dbline 153
;  }
L30:
	.dbline 155
;  //
;  if(data<1)       //比如 0.123
	movw R30,R28
	ldd R2,z+22
	ldd R3,z+23
	ldd R4,z+24
	ldd R5,z+25
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L35
	ldi R17,>L35
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt X2
	xjmp L33
X2:
	.dbline 156
;  {
	.dbline 157
;   display_one(7,0);   //显示0.
	clr R18
	ldi R16,7
	xcall _display_one
	.dbline 158
;   led_port &= 0x7F; //点亮对应小数点
	in R24,0x1b
	andi R24,127
	out 0x1b,R24
	.dbline 159
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 160
;   led_contrl_port |= (1<<7);
	sbi 0x12,7
	.dbline 162
;   
;   temp=(data*10);   // 0.123*10 = 1
	ldi R16,<L38
	ldi R17,>L38
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	movw R30,R28
 ; stack offset 4
	ldd R2,z+26
	ldd R3,z+27
	ldd R4,z+28
	ldd R5,z+29
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	xcall empy32f
	movw R30,R28
	std z+0,R16
	std z+1,R17
	std z+2,R18
	std z+3,R19
	movw R30,R28
	ldd R2,z+0
	ldd R3,z+1
	ldd R4,z+2
	ldd R5,z+3
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L39
	ldi R17,>L39
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L36
	movw R30,R28
	ldd R2,z+0
	ldd R3,z+1
	ldd R4,z+2
	ldd R5,z+3
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L39
	ldi R17,>L39
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall sub32f
	xcall fp2int
	movw R20,R16
	subi R20,0  ; offset = 32768
	sbci R21,128
	xjmp L37
L36:
	movw R30,R28
	ldd R16,z+0
	ldd R17,z+1
	ldd R18,z+2
	ldd R19,z+3
	xcall fp2int
	movw R20,R16
L37:
	.dbline 163
;   display_one(6,temp);
	mov R18,R20
	ldi R16,6
	xcall _display_one
	.dbline 164
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 165
;   led_contrl_port |= (1<<6);
	sbi 0x12,6
	.dbline 167
;   
;   temp2=(data*100);
	ldi R16,<L42
	ldi R17,>L42
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	movw R30,R28
 ; stack offset 4
	ldd R2,z+26
	ldd R3,z+27
	ldd R4,z+28
	ldd R5,z+29
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	xcall empy32f
	movw R30,R28
	std z+4,R16
	std z+5,R17
	std z+6,R18
	std z+7,R19
	movw R30,R28
	ldd R2,z+4
	ldd R3,z+5
	ldd R4,z+6
	ldd R5,z+7
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L39
	ldi R17,>L39
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L40
	movw R30,R28
	ldd R2,z+4
	ldd R3,z+5
	ldd R4,z+6
	ldd R5,z+7
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L39
	ldi R17,>L39
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall sub32f
	xcall fp2int
	movw R24,R16
	subi R24,0  ; offset = 32768
	sbci R25,128
	movw R10,R24
	xjmp L41
L40:
	movw R30,R28
	ldd R16,z+4
	ldd R17,z+5
	ldd R18,z+6
	ldd R19,z+7
	xcall fp2int
	movw R10,R16
L41:
	movw R22,R10
	.dbline 168
;   temp=temp2%10; // 0.123*100%10 = 2
	ldi R18,10
	ldi R19,0
	movw R16,R22
	xcall mod16u
	mov R20,R16
	.dbline 169
;   display_one(5,temp);
	mov R18,R20
	ldi R16,5
	xcall _display_one
	.dbline 170
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 171
;   led_contrl_port |= (1<<5);
	sbi 0x12,5
	.dbline 173
;   
;   temp2=(data*1000);
	ldi R16,<L29
	ldi R17,>L29
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	movw R30,R28
 ; stack offset 4
	ldd R2,z+26
	ldd R3,z+27
	ldd R4,z+28
	ldd R5,z+29
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	xcall empy32f
	movw R30,R28
	std z+8,R16
	std z+9,R17
	std z+10,R18
	std z+11,R19
	movw R30,R28
	ldd R2,z+8
	ldd R3,z+9
	ldd R4,z+10
	ldd R5,z+11
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L39
	ldi R17,>L39
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L43
	movw R30,R28
	ldd R2,z+8
	ldd R3,z+9
	ldd R4,z+10
	ldd R5,z+11
	st -y,R5
	st -y,R4
	st -y,R3
	st -y,R2
	ldi R16,<L39
	ldi R17,>L39
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall sub32f
	xcall fp2int
	movw R24,R16
	subi R24,0  ; offset = 32768
	sbci R25,128
	movw R10,R24
	xjmp L44
L43:
	movw R30,R28
	ldd R16,z+8
	ldd R17,z+9
	ldd R18,z+10
	ldd R19,z+11
	xcall fp2int
	movw R10,R16
L44:
	movw R22,R10
	.dbline 174
;   temp=temp2%10; // 0.123*100%10 = 3
	ldi R18,10
	ldi R19,0
	movw R16,R22
	xcall mod16u
	mov R20,R16
	.dbline 175
;   display_one(4,temp);
	mov R18,R20
	ldi R16,4
	xcall _display_one
	.dbline 176
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -