顯示具有 MCU 標籤的文章。 顯示所有文章
顯示具有 MCU 標籤的文章。 顯示所有文章

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;

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

2015年9月18日 星期五

ATMEGA328 port 的操作

原因: 使用 arduino 的 digitalWrite() 及 digitalRead() 的速度不夠快, 直接對 register 控制.
用到的 register:
   PORTD: set data output
   DDRD  : set input/output mode
   PIND    : read data input . if write 1 , the port will toggle.

方法:

-- 找出對應的 pin 腳 --
input pin:
  digital pin 6, PORTD6

output pin:
  digital pin 2, PORTD2

-- 設定 --
 output mode:
    DDRD |= (0x04);    // 1: output, 0:input
  
 input mode:
   DDRD &= (~0x40)
   PORTD |= (0x40);  // input pull-high

-- 讀值 --
   Data = PIND;     // read PORTD input data

ex: // 反向輸出
..
noInterrupts();
while(1){
    
    if(PIND & 0x04){
        PORTD &= (~0x40);
    }
    else{
        PORTD |= (0x40);
    }
}