# 微算機_lab1
[TOC]
---
# lab1-1 MPLAB下載、安裝、建專案
## 下載&安裝
到[此網址](http://www.microchip.com.tw/Data_CD/)下載MPLAB® X IDE **v5.25**
:::warning
一定要下載到對的版本v5.25!!!!
下載要滿久的,建議大家有空就先下載
:::

開始安裝





等待安裝完成即可
## 建專案
1. FILE -> new project

2. Standalone Project

3. family選PIC18,型號選PIC18F4520

4. tool**目前先選simulator**
(以後課堂若開始用到板子,依型號選擇pickit4或pickit3)
(前幾個lab暫時還用不到板子)

5. compiler 選 mpasm

6. 打上專案名稱,編碼改成 UTF8

7. 在左側欄位找到Source File,右鍵到new欄位新增AssemblyFile.asm

:::warning
**如果找不到AssemblyFile.asm**

選擇other,再去Assembler的資料夾下找

:::
8. 填上檔案名稱,完成!可以開始寫code了!

## DEMO影片小重點
方便大家在寫程式的時候了解到指令怎麼運作或是用來debug
* 設中斷點
* 怎麼看register內的value
:::info

上課的時候應該會講解pic18怎麼存放data
* 主要就是將資料放在data memory
* 而data memory就是好多個register
* register裡面用來放值
往後的lab或課堂上會更詳細的跟大家說這些register怎麼操作
(如果還是不太了解的話,可以先把這些register想像成是既有的一些變數,我們要對這些已存在的變數存值、取值)
:::
## 注意事項
* 一定要確認.asm檔在source file之下,否則無法順利執行
* 同個project有兩個以上.asm 檔時,若只要執行其中某個,要把其他的.asm檔exclude
* 如果有多個project,切換的時候要記得
對著想執行的專案右鍵->Set as main project
如果專案無法順利執行,先確認上面三項是否正確無誤
兩個重要的參考網址
* [Instruction Set ](http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/)
* [Data sheet](https://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf)
---
# lab1-2 [Instruction set](http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/)簡介
outline:
* 小前言:PIC18、MPLAB、這個學期的實驗
* 簡單介紹指令集
* 介紹WREG
* demo:
* 基本指令
* 用這些指令實作loop
Lab1主要是讓同學們知道怎麼開始在MPLAB寫組合語言和執行
## Instruction set

不懂的話一定要自己查,要學著看懂
http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/
## WREG
[What is WREG?](https://www.microchip.com/forums/m150959.aspx)

* The W register is a special register in the PIC architecture
* used as one of the 2 operands for ALU operations
* can be the destination for any ALU operation.
**WREG=working register**
* PIC18中,可以經常用來當作運算元的register
* 常常用來在運算的時候,暫時存放data
## 補充
(這次介紹的一些指令,後面有一些參數是關於access bank、BSR...往後實驗會再詳細介紹,有興趣的同學也可以自己先參考看看下面的資料)
**描述PIC18的memory架構**
* The PIC18 Memory Organization
* A memory location is referred to as an information unit.
* A memory location in the PIC18 holds **eight bits** of information.
* An information unit has two components: its address and its contents
* 
* Separation of **Data Memory** and **Program Memory**
* The PIC18 MCU assigns **data** and **program** to different memory spaces
* 
* PIC18 **Data Memory**
* Each location in the data memory is also referred to as a **register** or **file register**
* Supports 4096 bytes(8 bits) of data memory. It requires 12 bits of address to select one of the data registers. (要用12bits才能分辨現在用的是哪個register)
* Because the limited length of the PIC instruction,**only eight bits** of the PIC18 instruction are used **to specify the file
register.**
* As a result, the PIC designers divided the 4096 file registers into 16 banks. Only one bank of 256 file registers is active at any time.
* An additional four bits are placed in a special register called bank select register (**BSR**) to **select the bank to be active.**
* 如果沒有指定BSR,通常就是預設access bank的register
* 
* registers可以分成兩個種類:
* general-purpose registers (**GPR**s)
hold dynamic data when the CPU is executing a prog.
(運算的時候可以用來存放值、讀值...等等)
* specialfunction registers (**SFR**s)
control the desired operation of the MCU
(就是可以有一些特殊用途,往後lab會慢慢去用到這些比較特別的register)