找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4401|回复: 1
打印 上一主题 下一主题
收起左侧

用16x8的LED点阵做的单片机贪吃蛇,汇编语言写的,有兴趣的可看看

[复制链接]
跳转到指定楼层
楼主
网上看到的一个贪吃蛇的文件,用的是16x8的点阵。

实物图:


电路原理图如下:


16x8led点阵程序流程图:


单片机源程序如下:
  1. ;
  2. ;  C51 Snakes ~~~~~~~~~
  3. ;
  4. ;  Created by Sandeep Sasi
  5. ;  Project Started:   10/09/09, 12:00PM
  6. ;  Project Completed: 30/09/09, 02:10AM

  7. ; File:       C51Snakes.asm
  8. ; Created by: Sandeep Sasi

  9. $TITLE ('C51Snakes')

  10. ; This file defines the Main Block and the entry point of the app.

  11. ; The app. uses polling technique to sequentially montior the state of the
  12. ; keys and then bring about the appropriate response. Since there are no blocking
  13. ; modules and each module will complete executuion within a deterministic time
  14. ; period, polling tech. proves more efficient over others in implementing this logic!!!





  15. ORG 0000H ; System reset sets PC to this addr.
  16.                                 SJMP ResetRecoveryISR ; After a sys. reset, pgm. exe. begins at this inst.
  17. ORG 0003H
  18.                                 SJMP ExternalInterrupt0ISR
  19. ORG 000BH ; Sets PC to this addr. when a Timer 0 overflow signal is raised
  20.                                 AJMP DisplayMuxISR ; Jump to the corresponding ISR's block
  21. ORG 0013H
  22.                                 SJMP ExternalInterrupt1ISR
  23. ORG 0020H ; All the supporting librarires are place in page one of ROM.
  24.           ; The game library (EggHandle.inc, GameBackEnd.inc and GameFrontEnd.inc) and
  25.           ; C51Snakes.asm will be moved to page two of ROM!!!




  26. $INCLUDE (ConstAndDataDefs.inc) ; Program Conts.s, redefinition of SFRs and Variable defs.
  27. $INCLUDE (SystemPower.inc)      ; Functions that handle system power down & program recovery after a system reset
  28. $INCLUDE (DisplayMuxing.inc)    ; Function and ISR for Disp. Muxing.
  29. $INCLUDE (SystemFunctions.inc)  ; Fns. to ctrl. disp., buzzer, scan keypad and delay the execution
  30. $INCLUDE (LowLevelGfx.inc)      ; Fuctions that give pixel level access to the disp.
  31. $INCLUDE (HighLevelGfx.inc)     ; Function to clear screen, print text and draw line
  32. $INCLUDE (WelcomeGfx.inc)       ; Functions to display an animation when the console is switched on
  33. $INCLUDE (DelaySettings.inc)    ; Functions to adjust the speed (prop. delay) of the snake
  34. $INCLUDE (StandByState.inc)     ; Fuctions that are called when the app. enters stand by
  35. $INCLUDE (PauseState.inc)       ; Functions that are called when the game is paused

  36. ORG 0801H ; The core game library functions; Beginning of page two of ROM

  37. $INCLUDE (EggHandle.inc)        ; Functions to handle generation and management of eggs for the snake
  38. $INCLUDE (GameBackEnd.inc)      ; Functions that not directly called from the Main block
  39.                                 ; The core game logic is handled by these functions
  40. $INCLUDE (GameFrontEnd.inc)     ; Functions that are called from Main block to notify various states of the game






  41. ; Beginning of the Main Block

  42. C51Main:
  43. GameStandBy:
  44.                                 LCALL _StandBy ; Enter stand by
  45. GameInit:
  46.                                 ACALL _NotifyGameBegin ; Initiate the operations before game begins
  47.                                 LCALL _WaitForAllActionKeyRelease
  48.                                 MOV PREV_KEY_STATES, #ALL_KEYS_PRESSED ; Force the user to release all keys
  49.                                                                        ; to start processing them in the polling loop
  50.                                 LCALL _ResetSystemIdleTime
  51. GameRun:
  52.                                 MOV CURR_KEY_STATES, KEYPAD_ACCESS_PORT ; Sample the state of all keys
  53. TestForAnyKeyPress:
  54.                                 MOV A, #ANY_KEY_MASK
  55.                                 LCALL ADetectedDepressedStateOfKeyMaskInA
  56.                                 JNZ BeginKeyScan ; If a key press is detected, start scanning the keys sequentially
  57.                                 LCALL AStepSystemIdleTimeAndNotifyOnExpiry ; If not, advance that system idle time
  58.                                 JZ EndOfKeyScan         ; If system idle period expires,
  59.                                 ACALL _NotifyGameFreeze ; then freeze the game and
  60.                                 LCALL _PowerDownSystem  ; initiate system power down

  61.                                 ACALL _NotifyGameRestore ; Recover game from power down state
  62.                                 LCALL _WaitForAnyKeyPress ; Wait for a user event
  63.                                 MOV CURR_KEY_STATES, #ALL_KEYS_PRESSED
  64.                                 SJMP EndOfKeyScan
  65. BeginKeyScan:
  66.                                 LCALL _ResetSystemIdleTime
  67. ScanGamePauseKey:
  68.                                 MOV A, #PAUSE_KEY_MASK
  69.                                 LCALL ADetectedDepressingEdgeOfKeyMaskInA ; Check whether the pause key is hit
  70.                                 JZ ScanDelaySettingsKey ; If not, check the state of Delay Settings key
  71.                                 ACALL _NotifyGameFreeze ; If yes, initiate a game freeze
  72.                                 LCALL AGamePause ; Enter paused state
  73.                                 JZ GameEnd; If the user wishes to quit, end the game and
  74.                                           ; display the game score
  75.                                 ACALL _NotifyGameRestore ; Else, initiate game resume
  76.                                 LCALL _WaitForAnyKeyPress ; After restoring the display to the state when the user
  77.                                                           ; hit the pause key, wait for a user event
  78.                                 MOV CURR_KEY_STATES, #ALL_KEYS_PRESSED
  79.                                 SJMP EndOfKeyScan ; Perform no more key processing in this iteration
  80. ScanDelaySettingsKey:
  81.                                 MOV A, #DEL_SET_KEY_MASK
  82.                                 LCALL ADetectedDepressingEdgeOfKeyMaskInA ; Check whether the pause key is hit
  83.                                 JZ ScanGameActionKeys ; If not, check the state of Game Action Keys
  84.                                 ACALL _NotifyGameFreeze ; If yes, initiate a game freeze
  85.                                 LCALL _DelaySettings ; Enter the mode that helps the user to adjust the snake's speed
  86.                                 ACALL _NotifyGameRestore ; Restore the game once the user has switched back to game mode
  87.                                 LCALL _WaitForAnyKeyPress ; After restoring the display to the state when the user
  88.                                                           ; hit the pause key, wait for a user event
  89.                                 MOV CURR_KEY_STATES, #ALL_KEYS_PRESSED
  90.                                 SJMP EndOfKeyScan ; Perform no more key processing in this iteration

  91. ScanGameActionKeys:
  92.                                 ACALL _ScanGameActionKeys
  93. EndOfKeyScan:
  94.                                 MOV PREV_KEY_STATES, CURR_KEY_STATES ; Init. PREV_KEY_STATES before next round of processing

  95.                                 MOV A, #GAME_TIME_STEP_PERIOD
  96.                                 LCALL _DelayABy100ms
  97.                                 ACALL ANotifyGameTimeStep ; Initiate a change of game state
  98.                                 JZ GameOver ; Collision detected; So, game over!
  99.                                 SJMP GameRun ; Loop around to Game Block
  100. GameOver:
  101.                                 ACALL _EndOfGameGfx ; Alert user about end of the game
  102. GameEnd:
  103.                                 ACALL _NotifyGameEnd
  104.                                 ACALL _DisplayGameScore
  105.                                 SJMP C51Main ; Loop around to Main Block
  106. END ; End of app.
复制代码

所有资料51hei提供下载:
C51Snakes.zip (2.18 MB, 下载次数: 13)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:241152 发表于 2017-10-20 17:07 | 只看该作者
非常好,谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表