avm.asm 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. ; Àâòîð: shilow@ukr.net
  2. ; Äàòà: ìàé-èþíü 2012
  3. ; Íàçâàíèå: avm
  4. ; Âåðñèÿ: 1
  5. ; Èìÿ ôàéëà: avm.asm, avm.spl7, avm.lay
  6. ; Äëÿ AVR: ATtiny24A
  7. ; Òàêòîâàÿ ÷àñòîòà: 8MHz, âíóòðåííèé RC-ãåíåðàòîð
  8. ; Âûïîëíÿåìûå ôóíêöèè: Àâòîìîáèëüíûé âîëüòåìòð
  9. ; ñì. ReadMe.txt
  10. ; ñõåìà ïî "Âàðèàíò 5"
  11. ;******************************
  12. ; èíêëóäû
  13. .nolist
  14. .include "tn24Adef.inc"
  15. .list
  16. ;******************************
  17. ; îïðåäëåíèÿ
  18. .def job0 = r0
  19. .def job1 = r1
  20. .def job2 = r2
  21. ;.def job3 = r3
  22. .def LED1 = r4
  23. .def LED2 = r5
  24. .def LED3 = r6
  25. .def LED4 = r7
  26. .def msrL = r14
  27. .def msrH = r15
  28. .def temp = r16 ; ðàáî÷àÿ ïåðåìåííàÿ
  29. .def tmp1 = r17
  30. .def cnt1 = r18
  31. .def cnt2 = r19
  32. .def LEDN = r20
  33. .def msek5 = r24 ; 5-òè ìñåê èíòåðâàëû
  34. .def flags = r25
  35. ;******************************
  36. ; êîíñòàíòû
  37. .equ AtBCD0 = 0 ;address of job0
  38. .equ AtBCD2 = 2 ;address of job2
  39. .equ AtLED1 = 4 ;address of LED1
  40. .equ MSEK100 = 7 ; 7bit of flags == 0.1 sek
  41. .equ NEED_DOT = 6 ; íóæíà òî÷êà
  42. .equ DISP_PORT = PORTA
  43. .equ DISP_DDR = DDRA
  44. .equ LA1 = 4
  45. .equ LA2 = 5
  46. .equ LA3 = 6
  47. .equ LA4 = 7
  48. .equ SER_PORT = PORTB
  49. .equ SER_DDR = DDRB
  50. .equ SCK = 0
  51. .equ DATA = 1
  52. .equ LOCK = 2
  53. .equ T1DIVL = 0x8F
  54. .equ T1DIVH = 0xFD ; 0x10000-8MHz/64/200Hz
  55. .equ VREF = 500 ; îïîðíîå íàïðÿæåíèå * 10 ìÂ
  56. .equ SPH = 0x3e
  57. .equ DOT_BIT = 5 ; bit5 -- òî÷êà
  58. ;******************************
  59. ; ìàêðîñû
  60. ; ñîõðàíÿåì â ñòåê SREG è R16
  61. .MACRO PUSHF
  62. PUSH R16
  63. IN R16,SREG
  64. PUSH R16
  65. .ENDM
  66. ; âîññòàíàâëèâàåì èç ñòåêà SREG è R16
  67. .MACRO POPF
  68. POP R16
  69. OUT SREG,R16
  70. POP R16
  71. .ENDM
  72. ;******************************
  73. ; ÿ÷åéêè â ÑÎÇÓ
  74. ;.DSEG
  75. ;.ORG SRAM_START
  76. ;Display: .byte 4 ; 4 áàéòà äëÿ èíäèêàòîðà
  77. ;******************************
  78. ; êîíñòàíòû â EEPROM
  79. ;.ESEG
  80. ;smpl1: .DW 0x0000 ; sample 1
  81. ;smpl2: .DB 0x05 ; sample 2
  82. ;******************************
  83. ; ïàìÿòü ïðîãðàìì
  84. .CSEG
  85. .ORG 0
  86. ;******************************
  87. ; Òàáëèöà âåêòîðîâ ïðåðûâàíèé
  88. rjmp RESET ; Reset Handler
  89. reti;jmp INT0 ; IRQ0 Handler
  90. reti;jmp PCINT0 ; PCINT0 Handler
  91. reti;jmp PCINT1 ; PCINT1 Handler
  92. reti;jmp WDT ; Watchdog Interrupt Handler
  93. reti;jmp TIM1_CAPT ; Timer1 Capture Handler
  94. reti;jmp TIM1_COMPA ; Timer1 Compare A Handler
  95. reti;jmp TIM1_COMPB ; Timer1 Compare B Handler
  96. rjmp TIM1_OVF ; Timer1 Overflow Handler
  97. reti;jmp TIM0_COMPA ; Timer0 Compare A Handler
  98. reti;jmp TIM0_COMPB ; Timer0 Compare B Handler
  99. reti;jmp TIM0_OVF ; Timer0 Overflow Handler
  100. reti;jmp ANA_COMP ; Analog Comparator Handler
  101. rjmp ADCC ; ADC Conversion Handler
  102. ; rjmp EE_RDY ; EEPROM Ready Handler
  103. ; rjmp USI_STR ; USI STart Handler
  104. ; rjmp USI_OVF ; USI Overflow Handler
  105. ;******************************
  106. ;;;;; Ïåðâè÷íàÿ èíèöèàëèçàöèÿ
  107. RESET: ldi temp,high(RAMEND); Main program start
  108. out SPH,temp
  109. ldi temp,low(RAMEND)
  110. out SPL,temp; Set Stack Pointer to top of RAM
  111. ; âûêë. àíàëîã, êîìïàðàòîð
  112. ldi temp,1<<ACD
  113. out ACSR,temp
  114. ; íàñòðîèì ÀÖÏ. îïîðà - Vcc, êàíàë 1, íåïðåðûâíîå ïðåîáðàçîâàíèå. temp=0b00100010
  115. ldi temp,1<<MUX0
  116. out ADMUX,temp
  117. ldi temp,(1<<ADEN)|(1<<ADSC)|(6<<ADPS0)
  118. out ADCSRA,temp
  119. ; timer1 - ÷àñòîòà 200 ãö, ïðè òàêòîâîé 8ÌÃö, ïðåñêàëåð=64, TCNT1L=0xñ0, TCNT1H=0x63
  120. ldi temp,(3<<CS10)
  121. out TCCR1B,temp
  122. ldi temp,T1DIVH
  123. out TCNT1H,temp
  124. ldi temp,T1DIVL
  125. out TCNT1L,temp ; çàïóñòèëè òàéìåð 1
  126. ; ðàçðåøàåì ïðåðûâàíèå îò òàéìåðà
  127. ldi temp,1<<TOIE1
  128. out TIMSK1,temp
  129. ; init
  130. ldi LEDN,0b11101111 ; ïåðâûé èíäèêàòîð
  131. ldi temp,(1<<LA1)|(1<<LA2)|(1<<LA3)|(1<<LA4)
  132. out DISP_DDR,temp
  133. out DISP_PORT,temp
  134. ldi temp,(1<<SCK)|(1<<DATA)|(1<<LOCK)
  135. out SER_DDR,temp
  136. ldi msek5,20
  137. clr YH
  138. ldi YL,AtLED1 ; çàãðóæàåì àäðåñ 1-ãî èíäèêàòîðà
  139. ldi temp,0x30
  140. mov LED1,temp
  141. mov LED2,temp
  142. mov LED3,temp
  143. mov LED4,temp
  144. ; Enable interrupts
  145. sei
  146. ;******************************
  147. ;;;;; Îñíîâíàÿ ïðîãðàììà
  148. BEGIN:
  149. sbrs flags,MSEK100 ; ïîðà ïðîèçâîäèòü èçìåðåíèÿ?
  150. rjmp MLBL1 ; íåò
  151. ;!! ADATE@ADCSRA==1, ïðè ADTS@ADCSRB==0 -- çàïóñê free running mode
  152. clr msrL
  153. clr msrH ; î÷èñòèëè õðàíèëèùå ðåçóëüòàòà
  154. ldi cnt2,16 ; çàãðóçèëè ñ÷¸ò÷èê èçìåðåíèé
  155. sbi ADCSRA,ADATE ; çàïóñê free running mode
  156. sbi ADCSRA,ADIE ; ðàçðåøàåì ïðåðûâàíèÿ îò ÀÖÏ
  157. sbi ADCSRA,ADSC ; çàïóñê íîâ. ïðåîáðàçîâàíèÿ
  158. ML1: rjmp PC+1 ; nop x 2 -- 1 world 2 takta
  159. tst cnt2 ; ãîíÿåì ïóñòîé öèêë ïîêà áóäóò ïðîèñõîäèòü 16 èçìåðåíèé
  160. brne ML1
  161. cbi ADCSRA,ADIE ; çàïðåùàåì ïðåðûâàíèÿ îò ÀÖÏ
  162. cbi ADCSRA,ADATE ; stop free running mode
  163. lsr msrH
  164. ror msrL
  165. lsr msrH
  166. ror msrL ; ïîäåëèëè ðåçóëüòàò íà 2^2
  167. ; òåïåðü ìíîæèì íà Vref è äåëèì íà 4096
  168. ldi temp,low(VREF)
  169. ldi tmp1,high(VREF)
  170. rcall MPY16U
  171. lsr job1 ; âûáðîñèëè job0 == ïîäåëèëè íà 256
  172. ror job0
  173. lsr job1
  174. ror job0
  175. ;; lsr job1
  176. ;; ror job0
  177. ;; lsr job1
  178. ;; ror job0 ; äîäåëèëè íà 16, èòîãî ïîäåëèëè íà 4096
  179. ; ldi cnt1,4
  180. ;ML2: lsr job2
  181. ; ror job1
  182. ; dec cnt1
  183. ; brne ML2
  184. mov msrL,job0
  185. mov msrH,job1 ; â MSR - íàïðÿæåíèå â 10ìÂ
  186. ;!!! óìíîæèòü íà âõîäíîé äåëèòåëü
  187. ;; -- íå äîäåëèëè íà 4 == óìíîæèëè òåïåðü íà 4.
  188. ;; â ðåàëå íóæíî áóäåò ìíîæèòü íàïðèìåð íà 400 è äåëèòü íà 100 -- ïîä ðåàëüíûé äåëèòåëü.
  189. ; òóò ìîæíî äîáàâèòü êîððåêöèþ óñèëåíèÿ.
  190. rcall bin2BCD16
  191. ; 4 çíà÷àùèõ öèôðû â job1:job0
  192. ldi XL,AtLED1
  193. clr XH
  194. mov temp,job1
  195. swap temp
  196. rcall OUT_NIBBL ; íèááë 3, äåñÿòêè
  197. mov temp,job1
  198. sbr flags,1<<NEED_DOT
  199. rcall OUT_NIBBL ; íèááë 2, åäèíèöû
  200. cbr flags,1<<NEED_DOT
  201. mov temp,job0
  202. swap temp
  203. rcall OUT_NIBBL ; íèááë 1, äåñÿòûå
  204. mov temp,job0
  205. rcall OUT_NIBBL ; íèááë 0, ñîòûå
  206. MLBL1:
  207. ;******************************
  208. ; äåëàòü íå÷åãî - ïîñïèì...
  209. ldi temp,1<<SE
  210. out MCUCR,temp ; ïðîñòîé ðåæèì ñíà
  211. sleep ; ñïèì.
  212. clr temp
  213. out MCUCR,temp ; ñî ñíà
  214. rjmp BEGIN
  215. ;******************************
  216. ;;;;; Ïîäïðîãðàììû
  217. ;******************************
  218. ; âûäà¸ò â ïîðò 8 áèò èç temp, ïîðòèò cnt1
  219. LEDOUT: ldi cnt1,8 ; ñ÷åò÷èê áèò
  220. LO1: cbi SER_PORT,SCK ; SCK=0
  221. lsl temp ; ñòàðøèé áèò â ïåðåíîñ
  222. brcc LO0 ; åñëè â ïåðåíîñå 0 — ïåðåéòè
  223. sbi SER_PORT,DATA ; âûäàëè 1
  224. rjmp LOE
  225. LO0: cbi SER_PORT,DATA ; âûäàëè 0
  226. LOE: sbi SER_PORT,SCK ; SCK=1, ñäâèã äàííûõ
  227. dec cnt1
  228. brne LO1
  229. ret
  230. ;******************************
  231. ;** ïåðåâîäèì ìëàäøèé íèááë temp â êîä äëÿ 7-ñãì èíä è êëàä¸ì åãî â îçó ïî àäð Y
  232. OUT_NIBBL:
  233. ldi ZL,low(LEDnd*2)
  234. ldi ZH,high(LEDnd*2); àäðåñ êîäîâ äëÿ èíäèêàòîðà
  235. andi temp,0x0F ; îòñåêàåì ñò íèááë. ïîëó÷àåì ñìåùåíèå
  236. add ZL,temp
  237. adc ZH,XH ; äîáàâèëè ñìåùåíèå
  238. lpm temp,Z ; ïðî÷èòàëè êîä
  239. sbrc flags,NEED_DOT ; íóæíà òî÷êà?
  240. cbr temp,1<<DOT_BIT ; âêëþ÷àåì òî÷êó
  241. st X+,temp ; çàïèñàëè êîä â ïîçèöèþ èíäèêàòîðà
  242. ret
  243. ;******************************
  244. ; ïåðåìíîæåíèå äâóõ 16-ðàçðÿäíûõ âåëè÷èí, ðåçóëüòàò 3 áàéòà, ðàáî÷àÿ
  245. ; ìíîæèìîå msrH:msrL, ìíîæèòåëü tmp1:temp, ðåçóëüòàò job2:job1:job0
  246. ; èñïîëüçóåò cnt1
  247. MPY16U: clr job2 ;clear 1 highest bytes of result
  248. ldi cnt1,16 ;init loop counter
  249. m16u_1: lsr tmp1
  250. ror temp
  251. brcc noad8 ;if bit 0 of multiplier set
  252. add job1,msrL ;add multiplicand Low to byte 1 of res
  253. adc job2,msrH ;add multiplicand high to byte 2 of res
  254. noad8: ror job2 ;shift right result byte 2
  255. ror job1 ;rotate right result byte 1
  256. ror job0 ;rotate result byte 0 and multiplier Low
  257. dec cnt1 ;decrement loop counter
  258. brne m16u_1 ;if not done, loop more
  259. ret
  260. ;******************************
  261. ;* This subroutine converts a 16-bit number (msrH:msrL) to a 5-digit
  262. ;* packed BCD number represented by 3 bytes (job2:job1:job0).
  263. ;* MSD of the 5-digit number is placed in the lowermost nibble of job2.
  264. bin2BCD16:
  265. ldi cnt1,16 ;Init loop counter
  266. clr job2 ;clear result (3 bytes)
  267. clr job1
  268. clr job0
  269. clr ZH ;clear ZH
  270. bBCDx_1:lsl msrL ;shift input value
  271. rol msrH ;through all bytes
  272. rol job0
  273. rol job1
  274. rol job2
  275. dec cnt1 ;decrement loop counter
  276. brne bBCDx_2 ;if counter not zero
  277. ret ; return
  278. bBCDx_2:ldi ZL,AtBCD2+1 ;Z points to result MSB + 1
  279. bBCDx_3:ld temp,-Z ;get (Z) with pre-decrement
  280. subi temp,-$03 ;add 0x03
  281. sbrc temp,3 ;if bit 3 not clear
  282. st Z,temp ; store back
  283. ld temp,Z ;get (Z)
  284. subi temp,-$30 ;add 0x30
  285. sbrc temp,7 ;if bit 7 not clear
  286. st Z,temp ; store back
  287. cpi ZL,AtBCD0 ;done all three?
  288. brne bBCDx_3 ;loop again if not
  289. rjmp bBCDx_1
  290. ;******************************
  291. ;;;;; Îáðàáîò÷èêè ïðåðûâàíèé
  292. TIM1_OVF: ; Timer1 Overflow Handler
  293. ; âûâîäèì äàííûå íà 7-ìè ñåãìåíòíûå èíäèêàòîðû
  294. ; îòñ÷èòûâàåì 100 ìñåê èíòåðâàë äëÿ çàïóñêà èçìåðåíèé
  295. PUSHF
  296. dec msek5
  297. brne T1L1
  298. ldi msek5,20
  299. sbr flags,1<<MSEK100
  300. T1L1:
  301. ;***************************
  302. ; âûâîä íà èíäèêàòîð î÷åðåäíîé öèôðû
  303. ldi temp,0xF0
  304. out DISP_PORT,temp ; ïîãàñèëè âñå èíäèêàòîðû
  305. cbi SER_PORT,LOCK ; ïîäãòîâèëè çàù¸ëêó
  306. ld temp,Y+ ; â òåìï - òåêóùàÿ öèôðà, àäðåñ=+1
  307. rcall LEDOUT ; âûïèõíóëè î÷åðåäíîé áàéò â ðåãèñòð
  308. sbi SER_PORT,LOCK ; èç 0 â 1 -- çàù¸ëêèâàíèå äàííûõ â ðåãèñòðå
  309. mov temp,LEDN
  310. andi temp,0xF0
  311. out DISP_PORT,temp ; âêëþ÷àåì î÷åðåäíîé èíäèêàòîð
  312. lsl LEDN ; ñäâèíóëè - ñëåäóþùèé èíäèêàòîð
  313. brlo T1L2 ; âûøëè çà ãðàíèöû? ïðîâåðêà
  314. ldi LEDN,0b11101111 ; äà, çàãðóæàåì íà÷àëüíîå çíà÷åíèå
  315. ldi YL,AtLED1 ; çàãðóæàåì àäðåñ 1-ãî èíäèêàòîðà
  316. T1L2:
  317. ldi temp,T1DIVH
  318. out TCNT1H,temp
  319. ldi temp,T1DIVL
  320. out TCNT1L,temp ; ïåðåçàïóñòèëè òàéìåð 1
  321. POPF
  322. reti
  323. ; ADC Conversion Handler
  324. ; ñóììèðóåì ðåçóëüòàòû èçìåðåíèé, óìåíüøàåì ñ÷¸ò÷èê.
  325. ADCC: in temp,ADCL
  326. add msrL,temp
  327. in temp,ADCH
  328. adc msrH,temp
  329. dec cnt2
  330. reti
  331. ;***************************
  332. ; áàéòû äëÿ âûâîäà 0-9 íà èíäèêàòîðû ñ ÎÀ, áåç òî÷êè. òî÷êà âêë óñòàíîâêîé áèòà 5
  333. LEDnd: .DB 0x30,0xF3,0x2A,0xA2,0xE1,0xA4,0x24,0xF2,0x20,0xA0
  334. .exit
  335. INT0: ; IRQ0 Handler
  336. reti
  337. PCINT0: ; PCINT0 Handler
  338. reti
  339. PCINT1: ; PCINT1 Handler
  340. reti
  341. WDT: ; Watchdog Interrupt Handler
  342. reti
  343. TIM1_CAPT: ; Timer1 Capture Handler
  344. reti
  345. TIM1_COMPA: ; Timer1 Compare A Handler
  346. reti
  347. TIM1_COMPB: ; Timer1 Compare B Handler
  348. reti
  349. TIM0_COMPA: ; Timer0 Compare A Handler
  350. reti
  351. TIM0_COMPB: ; Timer0 Compare B Handler
  352. reti
  353. TIM0_OVF: ; Timer0 Overflow Handler
  354. reti
  355. ANA_COMP: ; Analog Comparator Handler
  356. reti
  357. EE_RDY: ; EEPROM Ready Handler
  358. reti
  359. USI_STR: ; USI STart Handler
  360. reti
  361. USI_OVF: ; USI Overflow Handler
  362. reti
  363. ;
  364. .exit