2016年3月23日 星期三

8051 Timer0 Mode0 (13 bits)

8051 Timer0 Mode0 (TMOD = 0x00) 是 13 bit timer, 要注意的是這裡 TL0 是 5 bits !!

TH0 (8 bits): 0~255
TL0 (5 bits): 0~31

所以在計算 overflow 時, 把計算值除 32 才是要給 TH0, 餘數給 TL0.
例如要在 1000 count overflow :

TH0 = (8192-1000)/32;
TL0 = (8192-1000)%32;

利用 python 經由 serial port 輸出資料


Python 需安裝 Python Serial Port Extension
  https://pypi.python.org/pypi/pyserial

安裝方式:
. 下載 pyserial-3.0.1.tar.gz
. 解開 pyserial-3.0.1.tar.gz
. 進入解開壓縮檔的目錄下, 執行:
  python setup.py install
 
開始寫程式:
# coding=UTF-8

import time
import serial


ptn = [0xAA,0x50,0x60,0x70]

def main():
   
    ser = serial.Serial('COM3', 2400, timeout=0.5)
    ary = bytearray(ptn)
    ser.write(ary)
    time.sleep(0.1)
    ser.close()   

if __name__ == "__main__":
    main()














ref : https://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/installing-python-and-pyserial

2016年3月21日 星期一

Arduino bootloader hex file

Arduino bootloader hex code 就位在 arduino 安裝目錄下的 \hardware\arduino\avr\bootloaders 中
至於要選擇那一個檔案, 參考 boards.txt 裡的設定
例如 Arduino Uno:
..
uno.bootloader.tool=avrdude
uno.bootloader.low_fuses=0xFF
uno.bootloader.high_fuses=0xDE
uno.bootloader.extended_fuses=0x05
uno.bootloader.unlock_bits=0x3F
uno.bootloader.lock_bits=0x0F
uno.bootloader.file=optiboot/optiboot_atmega328.hex
...


2016年3月2日 星期三

TI CCStudio Bit-Field Definitions

Bit-Field Definitions
- Bit field members are stored from right to left in memory. (LSB 開始放置)

例如:
struct SCICCR_BITS {        // bit description
    Uint16 SCICHAR:3;       // 2:0 Character length control (LSB)
    Uint16 ADDRIDLE_MODE:1; // 3 ADDR/IDLE Mode control
    Uint16 LOOPBKENA:1;     // 4 Loop Back enable
    Uint16 PARITYENA:1;     // 5 Parity enable
    Uint16 PARITY:1;        // 6 Even or Odd Parity
    Uint16 STOPBITS:1;      // 7 Number of Stop Bits
    Uint16 rsvd1:8;         // 15:8 reserved
};


- The C28x compiler limits the maximum number of bits within a bit field to the size of an integer; no bit field can be greater than 16 bits in length.
 - If the total number of bits defined by bit fields within a structure grows above 16 bits, then the next bit field is stored consecutively in the next word of memory.

要注意的是, 例如寫入單一個成員變數, CPU 是執行 read-modify-write; 也就是說在有些 register 寫入 1 是清除的指令, 當成員變數在同一個 structure 中時, 寫入 A 成員, 可能會造成 B 成員也被清除. 這時候可能要用整個 structure 寫入的方式.
 以下的 register 在執行 read-modify-write 動作時, 要注意:
1) Registers with bits that hardware can change after the read, but before the write
2) Registers with write 1-to-clear bits
3) Registers that have bits that must be written with a value different from what the bits read back

ref: TI SPRAA85D, Programming TMS320x28xx and 28xxx Peripherals in C/C++ (Rev. D)
p.16