📄 8363_pedometercode.asm
字号:
.INCLUDE "C:\avrtools\appnotes\2313def.inc";*********** To name registers into meaningful names and name memory spaces for specific uses.equ no_step1= $0060.equ no_step2= $0061 ;Number of steps.equ step_val= $0062 ;Step value.equ dist1= $0063.equ dist2= $0064.equ dist3= $0065 ;Total distance.equ stat= $0066.equ slept= $0067.equ tim0m= $0068.equ tim1m= $0069.equ tim2m= $006A.equ tim3m= $006B.equ statm= $006C.equ interrupt= $006D.def no_stepR1= R1.def no_stepR2= R2.def step_valR= R3.def distR1=R4.def distR2=R5.def distR3=R6.def status= R21.def slp = R22.def temp= R17;Display function usage.def bcd= R7.def disp_temp1= R8.def disp_temp2= R9.def disp_temp3= R10.def div1=R17.def div2=R18.def div3=R19.def temp_lcd= r20.def low_del=r21 ; can be reused, because they are pushed.def hi_del=r22.def lcd_cmd=r28.def lcd_dat=r28.def one = R11.def zero= R12; Timer definitions.def tim0 = r13 ; can be pushes and reused.def tim1 = r14.def tim2 = r15.def tim3 = r16;To put lock;Used when any button is pressed.def tims0= r23.def tims1= r24.def tims2= r25.def tims3= r0.def temp_tim= r27 ; can be used otherwise, used in end of sw and tim subroutine;For velocity calculation.DEF const1= R17.DEF const2= R18.DEF rmh = R19 ; Interim storage.DEF step_for_vel= R20 ; Binary number to be multiplicated with (8 Bit).DEF vel1 = R21 ;.DEF vel2 = R22;.DEF vel3 = R23;.DEF rd1u = R17 ; interim register.DEF rd2u = R18.DEF tim_diff1 = R19 ; 8-bit-number to divide with.DEF tim_diff2 = R20;.DEF re1= R24 ; LSB result.DEF re2 = R25 ; MSB result.DEF re3 = R26;.def tim_vel1= R17.def tim_vel2= R18.def tim_vel1_bef= R29.def tim_vel2_bef= R30;For switch case.def input= R22.def inp_debounce= R26; For start halt.def in_ss= R31 ;.def temp_sh = R20;Delay for debounce.def del_temp1= R17.def del_temp2= R18;for interrupt.def temp_int= r17.CSEG.ORG 0000RJMP StartRJMP intretiretiretiretiRJMP timovflretiretiretireti.ORG $000b;************************To be executed on RESET Interrupt************************************* Start:;*****Initializing O/P, I/P ports LDI R18, 0b00000000 ; port d for input out ddrd, r18 LDI R18, 0b11111111 ; internal pullup for portd OUT PORTD, R18 ldi temp, 0b11111111 ;configure PORT B for all outputs out DDRB, temp ldi R22, 0b00000000 ; Just a check out portb, R22;***** Initializing the Stack Pointer LDI R18,$DF ; OUT SPL ,R18;;***** Clearing the tims registers clr tims1 clr tims2 clr tims3 clr tims0;***** Setting Interrupt Structure;***** Also setting Power down mode enable and idling mode LDI R22, 0b00100111 ; Leading edge interrupt ; Idle mode set, on command sleep OUT MCUCR,R22 ldi r17, 0b00000010 ; Overflow interrupt of timer 0 enabled out timsk, r17;*****Checking whether the program flow has come from SLEEP mode lds slp, slept cpi slp, 0b10110101 breq lock_open;*****To be executed only RESET on Power ON;*****Setting the default status ldi status, 0b00000001 sts stat, status;*****Initializing the step register to average step length of 60cm ldi r22, 60 STS step_val, r22;*****Initialising the distance, nummber of steps, timer register memory spaces. clr r22 STS no_step1, r22 STS no_step2, r22 STS dist1, r22 STS dist2, r22 STS dist3, r22 STS tim0m, r22 STS tim1m, r22 STS tim2m, r22 STS tim3m, r22 STS interrupt, r22;******Initialising the LCD display rcall init_lcd;*****To be always executed, whenever the RESET interrupt is called.lock_open:;***** To be always executed, even when just to remove LOCK;***** To get values of all parameters from SRAM LDS no_stepR1, no_step1 LDS no_stepR2, no_step2 LDS step_valR, step_val LDS distR1, dist1 LDS distR2, dist2 LDS distR3, dist3 LDS status, stat;***** Setting the interrupt structure lds temp_tim, interrupt OUT GIMSK, temp_tim ldi temp_tim, 0b10000001 OUT ACSR, temp_tim;***** To set timer 0 characterstics;***** Initialize the tim registers LDS tim0, tim0m LDS tim1, tim1m LDS tim2, tim2m LDS tim3, tim3m OUT tcnt0, tim0 LDI R17, 0b00000101 ; To start clock /1024 OUT TCCR0, R17 clr one ; Registers for addition purposes clr zero inc one;*******To display the current value of step/distance according to chosen mode rcall clrdisp SBRC status, 0 rcall disp_step SBRC status, 1 rcall disp_dist rcall tim ; It sets the tims registers to current value of timer registers ; If time exceeds 10seconds more than 10seconds of tims ; then processor is put in sleep mode ; tims is regularily when a button is pressed;***** Switch Case to poll different portsrecheck: IN input, PIND ANDI input, 0b01111011 SBRS input, 0 rcall step_set ; Step setting module SBRS input, 1 rcall st_hl ; Start Halt module SBRS input, 3 rcall res_count ; To reset step, distance SBRS input, 4 rcall vel ; To set to display velocity SBRS input, 5 rcall dist ; To set to display distance SBRS input, 6 rcall no_step ; To display number of steps ldi temp_tim, $45 ; Add 10 seconds to tims registers add tims0, temp_tim ldi temp_tim, $9E adc tims1, temp_tim adc tims2, zero adc tims3, zero CLI ; Stop all interrupts in tim0, tcnt0 cp tims0, tim0 ; Comparing the tims with tim resiters cpc tims1, tim1 ; tims is already added with 10 sec cpc tims2, tim2 ; So if tims is less than tim cpc tims3, tim3 ; then processor goes to sleep brcc down SEI ; To enable interrupts again ldi temp_tim, $45 ; Restoring tims as earlier sub tims0, temp_tim ldi temp_tim, $9E sbc tims1, temp_tim sbc tims2, zero sbc tims3, zero ldi slp, 0b10110101 ; Noting that the processor has gone to sleep mode sts slept, slp in temp_tim, gimsk ; To store the interupt state sts interrupt, temp_tim sts stat, status ; Noting the staus of the processor ldi temp_tim, 0b00000001 ; Just a external signal that sleep mode is activated out portb, temp_tim ; the led remains no more litsl: sleep nop ldi temp_tim, 0b00000001 ; Just a external signal that sleep mode is activated out portb, temp_tim rjmp sldown: SEI ; Enabling interrupts, starting clock, restoring tims ldi temp_tim, $45 sub tims0, temp_tim ldi temp_tim, $9E sbc tims1, temp_tim sbc tims2, zero sbc tims3, zerorjmp recheck;**********************To manage timer0 overflow****************************timovfl: ; To make a concatinated timer of four registers push temp_tim ; Push temp_tim in temp_tim, sreg ; Push sreg push temp_tim ;rcall disp_tim ;rcall big_delay add tim1, one adc tim2, zero adc tim3, zero in tim0, tcnt0 STS tim0m, tim0 STS tim1m, tim1 STS tim2m, tim2 STS tim3m, tim3 pop temp_tim out sreg, temp_tim pop temp_tim reti;*********************Interrupt routine for int0 line**********************************int: push temp_int in temp_int, sreg push temp_int IN temp_int, PIND ANDI temp_int, 0b00000100 CPI temp_int, 0b00000100 brne end_int add no_stepR2, one adc no_stepR1, zero add distR3, step_valR adc distR2, zero adc distR1, zero STS no_step1, no_stepR1 STS no_step2, no_stepR2 STS dist1, distR1 STS dist2, distR2 STS dist3, distR3 SBRC status, 0 rcall disp_stepi SBRC status, 1 rcall disp_disti SBRC status, 2 rcall calc_velend_int: pop temp_int out sreg, temp_int pop temp_int reti;*********************ROUTINES FOR ANSWERING THE INPUT PORTS*************;*********************ROUTINES FOR STEP_SET EXECUTION********************step_set: rcall debounce push in_ss ;CLI LDI in_ss, 0b00000000 OUT GIMSK, in_ss rcall disp_step_valopt_again: IN in_ss, PIND ANDI in_ss, 0b01100001 CPI in_ss , 0b00100001 brne opt_dec rcall debounce_st INC Step_valR rcall disp_step_val rjmp opt_againopt_dec: CPI in_ss, 0b01000001 brne opt_done rcall debounce_st DEC Step_valR rcall disp_step_val rjmp opt_againopt_done: CPI in_ss , 0b01100000 brne opt_again rcall debounce_st STS step_val, step_valR SBRC status, 0 rcall disp_step SBRC status, 1 rcall disp_dist SBRC status, 2 rcall calc_vel rcall tim pop in_ss retdebounce_st: rcall delaywait_st: IN inp_debounce, PIND ANDI inp_debounce, 0b01100001 CPI inp_debounce, 0b01100001 BRNE wait_st rcall delay ret;******************END OF STEP SET ROUTINES********************************;***** Start/halt buttonst_hl: rcall debounce IN temp_sh, GIMSK ANDI temp_sh, 0b11000000 CPI temp_sh, 0b00000000 BREQ int_disab LDI temp_sh, 0b00000000 OUT GIMSK, temp_sh OUT gifr, temp_sh rjmp end_of_subint_disab: LDI temp_sh, 0b01000000 OUT GIMSK, temp_shend_of_sub: rcall tim RET;**************** To reset number of steps, distance ***********************res_count: rcall debounce LDI temp_sh, 0b00000000 OUT GIMSK, temp_sh clr no_stepR1 clr no_stepR2 clr distR1 clr distR2 clr distR3 STS no_step1, no_stepR1 STS no_step2, no_stepR2 STS dist1, distR1 STS dist2, distR2 STS dist3, distR3 SBRC status, 0 rcall disp_step SBRC status, 1 rcall disp_dist SBRC status, 2 rcall calc_vel rcall tim RET;******************* To switch to velocity mode *****************************vel: rcall debounce ldi status, 0b00000100 sts stat, status rcall tim RET;******************** To switch to distance mode *****************************dist: rcall debounce ldi status, 0b00000010 sts stat, status rcall disp_dist rcall tim RET;******************* To switch to number of steps mode *****************************no_step: rcall debounce ldi status, 0b00000001 sts stat, status rcall disp_step rcall tim RETdebounce: rcall delaywait: IN inp_debounce, PIND ANDI inp_debounce, 0b01111011 CPI inp_debounce, 0b01111011 BRNE wait rcall delay ret; ***************** Delay routines to be used for use in debounce routines **************delay: push del_temp1 push del_temp2 ldi del_temp1, 33outerloop: ldi del_temp2, 255innerloop: nop nop nop nop nop nop nop dec del_temp2 brne innerloop dec del_temp1 brne outerloop pop del_temp2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -