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

📄 led.s

📁 一个关于AVR单片机的例程
💻 S
📖 第 1 页 / 共 3 页
字号:
	.module led.c
	.area lit(rom, con, rel)
_led_data::
	.word 63
	.word 6
	.word 91
	.word 79
	.word 102
	.word 109
	.word 125
	.word 7
	.word 127
	.word 111
	.word 119
	.word 115
	.word 57
	.word 99
	.word 121
	.byte 0,0
	.dbfile C:\DOCUME~1\administrator\桌面\教程\7断数码管\led.c
	.dbsym e led_data _led_data A[32:16]kI
	.area text(rom, con, rel)
	.dbfile C:\DOCUME~1\administrator\桌面\教程\7断数码管\led.c
	.dbfunc e led_init _led_init fV
	.even
_led_init::
	.dbline -1
	.dbline 20
; /**********************************
; ** file_name led.c             **
; ** describe  七段数码管函数        **
; ** auther    古欣 www.avrvi.com  **
; ** Time      200-2-25           **
; ** temp=(data*1000)%1; 这个式子中的%为取余数,temp为data的第三位小数,0.023(data) --> 3(temp) 
; ***********************************/
; #include "config.h"
; 
; 
; //LED数据 不显点,亮的段为1,连接为P0~P7对应a~g,dp
; const led_data[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x73,0x39,0x63,0x79};
; 
; /***********************
; ** 初始化对应端口为输出 
; ** ABCD 和 abcdefg dpi 共12个IO口
; ** 默认为 PB(0123) 和 PA
; ***********************/
; void led_init(void)
; {
	.dbline 21
;  led_ddr = 0xFF; //abcdefg dpi 各位设为输出
	ldi R24,255
	out 0x1a,R24
	.dbline 22
;  led_contrl_ddr |= (1<<led_a)|(1<<led_b)|(1<<led_c)|(1<<led_d); //ABCD四个控制设为输出
	in R24,0x17
	ori R24,15
	out 0x17,R24
	.dbline -2
L1:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e display_one _display_one fV
;           data -> R22
;          count -> R20
	.even
_display_one::
	xcall push_gset2
	mov R22,R18
	mov R20,R16
	.dbline -1
	.dbline 34
;  
;  // 以下两句将会使LED全亮,如果立即更改状态,将看不出来效果
;  // led_port = 0xFF;
;  // led_contrl_port |= (1<<led_a)|(1<<led_b)|(1<<led_c)|(1<<led_d);
; }
; 
; /***********************
; ** 显示一位
; ** 输入:count显示在第几位(3210),data 要显示的数(0~f)
; ************************/
; void display_one(uint8 count, uint8 data)
; {
	.dbline 35
;  led_port = led_data[data];    //显示的数
	ldi R24,2
	mul R24,R22
	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 36
;  led_contrl_port |= (1<<count); //选中对应要显示的位,从右至左,0123
	ldi R16,1
	mov R17,R20
	xcall lsl8
	in R2,0x18
	or R2,R16
	out 0x18,R2
	.dbline -2
L2:
	xcall pop_gset2
	.dbline 0 ; func end
	ret
	.dbsym r data 22 c
	.dbsym r count 20 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 46
;  //如果对位进行了调整,就不在是0123,而是对应的值
; }
; 
; /***********************
; ** 显示四位整数
; ** 输入:要显示的四位数data,显示模式mode,1为补零显示模式,默认不显示零
; ** 说明:可以小于四位数,mode=1时 自动补零
; ************************/
; void display(uint16 data,uint8 mode)
; {
	.dbline 49
;  uint8 temp;
;  //千位
;  if(data>=1000)
	cpi R22,232
	ldi R30,3
	cpc R23,R30
	brlo L4
	.dbline 50
;  {
	.dbline 51
;   temp=data/1000;
	ldi R18,1000
	ldi R19,3
	movw R16,R22
	xcall div16u
	mov R20,R16
	.dbline 52
;   display_one(3,temp);
	mov R18,R20
	ldi R16,3
	xcall _display_one
	.dbline 53
;   delay_ms(6);  //6ms 是个经验值,刚好无闪烁,并且亮度较高
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 54
;   led_contrl_port &=~ (1<<3);
	cbi 0x18,3
	.dbline 55
;  }
	xjmp L5
L4:
	.dbline 57
;  else
;  {//补零
	.dbline 58
;   if(mode==1)
	mov R24,R10
	cpi R24,1
	brne L6
	.dbline 59
;   {
	.dbline 60
;   display_one(3,0);
	clr R18
	ldi R16,3
	xcall _display_one
	.dbline 61
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 62
;   led_contrl_port &=~ (1<<3);
	cbi 0x18,3
	.dbline 63
;   }
L6:
	.dbline 64
;  }
L5:
	.dbline 66
;  //百位
;  if(data>=100)
	cpi R22,100
	ldi R30,0
	cpc R23,R30
	brlo L8
	.dbline 67
;  {
	.dbline 68
;   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 69
;   display_one(2,temp);
	mov R18,R20
	ldi R16,2
	xcall _display_one
	.dbline 70
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 71
;   led_contrl_port &=~ (1<<2);
	cbi 0x18,2
	.dbline 72
;  }
	xjmp L9
L8:
	.dbline 74
;  else
;  {
	.dbline 75
;   if(mode==1)
	mov R24,R10
	cpi R24,1
	brne L10
	.dbline 76
;   {
	.dbline 77
;   display_one(2,0);
	clr R18
	ldi R16,2
	xcall _display_one
	.dbline 78
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 79
;   led_contrl_port &=~ (1<<2);
	cbi 0x18,2
	.dbline 80
;   }
L10:
	.dbline 81
;  }
L9:
	.dbline 83
;  //十位
;  if(data>=10)
	cpi R22,10
	ldi R30,0
	cpc R23,R30
	brlo L12
	.dbline 84
;  {
	.dbline 85
;   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 86
;   display_one(1,temp);
	mov R18,R20
	ldi R16,1
	xcall _display_one
	.dbline 87
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 88
;   led_contrl_port &=~ (1<<1);
	cbi 0x18,1
	.dbline 89
;  }
	xjmp L13
L12:
	.dbline 91
;  else
;  {
	.dbline 92
;   if(mode==1)
	mov R24,R10
	cpi R24,1
	brne L14
	.dbline 93
;   {
	.dbline 94
;   display_one(1,0);
	clr R18
	ldi R16,1
	xcall _display_one
	.dbline 95
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 96
;   led_contrl_port &=~ (1<<1);
	cbi 0x18,1
	.dbline 97
;   }
L14:
	.dbline 98
;  }
L13:
	.dbline 100
;  //个位
;   temp=data%10;
	ldi R18,10
	ldi R19,0
	movw R16,R22
	xcall mod16u
	mov R20,R16
	.dbline 101
;   display_one(0,temp);
	mov R18,R20
	clr R16
	xcall _display_one
	.dbline 102
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 103
;   led_contrl_port &=~ (1<<0);
	cbi 0x18,0
	.dbline -2
L3:
	xcall pop_gset3
	.dbline 0 ; func end
	ret
	.dbsym r temp 20 c
	.dbsym r mode 10 c
	.dbsym r data 22 i
	.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 113
; }
; 
; /***********************
; ** 显示四位浮点数 本函数占用 mega16 的空间 19%
; ** 输入:要显示的四位数
; ** 说明:可以小于四位数,自动补零
; ************************/
; 
; void display_float(float data)
; {
	.dbline 117
;  uint8 temp;
;  uint16 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,<L19
	ldi R17,>L19
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L17
	.dbline 118
;  {
	.dbline 119
;   led_error();
	xcall _led_error
	.dbline 120
;  }
L17:
	.dbline 121
;  if (data<=0)
	ldi R16,<L22
	ldi R17,>L22
	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 L20
	.dbline 122
;  {
	.dbline 123
;   led_error();
	xcall _led_error
	.dbline 124
;  }
L20:
	.dbline 126
;  //
;  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,<L25
	ldi R17,>L25
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt X0
	xjmp L23
X0:
	.dbline 127
;  {
	.dbline 128
;   display_one(3,0);   //显示0.
	clr R18
	ldi R16,3
	xcall _display_one
	.dbline 129
;   led_port |= 0x80; //点亮对应小数点
	sbi 0x1b,7
	.dbline 130
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 131
;   led_contrl_port &=~ (1<<3);
	cbi 0x18,3
	.dbline 133
;   
;   temp=(data*10);   // 0.123*10 = 1
	ldi R16,<L28
	ldi R17,>L28
	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,<L29
	ldi R17,>L29
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L26
	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,<L29
	ldi R17,>L29
	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 L27
L26:
	movw R30,R28
	ldd R16,z+0
	ldd R17,z+1
	ldd R18,z+2
	ldd R19,z+3
	xcall fp2int
	movw R20,R16
L27:
	.dbline 134
;   display_one(2,temp);
	mov R18,R20
	ldi R16,2
	xcall _display_one
	.dbline 135
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 136
;   led_contrl_port &=~ (1<<2);
	cbi 0x18,2
	.dbline 138
;   
;   temp2=(data*100);
	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 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,<L29
	ldi R17,>L29
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L30
	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,<L29
	ldi R17,>L29
	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 L31
L30:
	movw R30,R28
	ldd R16,z+4
	ldd R17,z+5
	ldd R18,z+6
	ldd R19,z+7
	xcall fp2int
	movw R10,R16
L31:
	movw R22,R10
	.dbline 139
;   temp=temp2%10; // 0.123*100%10 = 2
	ldi R18,10
	ldi R19,0
	movw R16,R22
	xcall mod16u
	mov R20,R16
	.dbline 140
;   display_one(1,temp);
	mov R18,R20
	ldi R16,1
	xcall _display_one
	.dbline 141
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 142
;   led_contrl_port &=~ (1<<1);
	cbi 0x18,1
	.dbline 144
;   
;   temp2=(data*1000);
	ldi R16,<L19
	ldi R17,>L19
	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,<L29
	ldi R17,>L29
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt L33
	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,<L29
	ldi R17,>L29
	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 L34
L33:
	movw R30,R28
	ldd R16,z+8
	ldd R17,z+9
	ldd R18,z+10
	ldd R19,z+11
	xcall fp2int
	movw R10,R16
L34:
	movw R22,R10
	.dbline 145
;   temp=temp2%10; // 0.123*100%10 = 3
	ldi R18,10
	ldi R19,0
	movw R16,R22
	xcall mod16u
	mov R20,R16
	.dbline 146
;   display_one(0,temp);
	mov R18,R20
	clr R16
	xcall _display_one
	.dbline 147
;   delay_ms(6);
	ldi R16,6
	ldi R17,0
	xcall _delay_ms
	.dbline 148
;   led_contrl_port &=~ (1<<0);
	cbi 0x18,0
	.dbline 149
;  }
	xjmp L24
L23:
	.dbline 150
;  else if(data<10)  //比如1.234
	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,<L28
	ldi R17,>L28
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall cmp32f
	brlt X1
	xjmp L35
X1:
	.dbline 151
;  {
	.dbline 152
;   temp=(data/1);   // 1.234/1 = 1
	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,<L25
	ldi R17,>L25
	xcall lpm32
	st -y,R19
	st -y,R18
	st -y,R17
	st -y,R16
	xcall div32f
	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,<L29

⌨️ 快捷键说明

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