Thursday, January 23, 2014

Menu Collapses when Using PageViewer (Action Bar Tabs)

寫Android App時,使用viewpager會在actionbar上頭產生tab,而如果使用R.menu內的xml檔案,我們可以在右上角產生action button,但我發現他們都疊在一起了(collapse),根據文件,應該在設定成ifRoom之後在有空間情況下會直接出現在action bar而非下拉選單。

於是我上網找詢問解決方法,得到以下:


在tag設定上,我們應該注意showAsAction前的namespace應該用一個同於第二行xmlns底下的名稱。相同之後問題就解決了!

Reference


Wednesday, January 22, 2014

ctags 操作筆記

"I/O and Drivers" 這堂課教授推薦可以使用Source Insight軟體trace code,而我則好奇linuxers多半是用什麼樣的工具,一問之下知道有ctags這樣的東西,配合上vim十分方便,以下是幾個基本操作。

  • ctags *.c
  • ctrl + ] 查看函式的定義
  • ctrl + t 返回查看函式定義之前的位置
  • :ta main 跳到指定函式名稱
  • :ta /^main 跳至所有相似名稱的函式
    • :ts 列表
    • :tn 下一個
    • :tp 前一個
    • :tf 第一個
    • :tl 最後一個

另外可裝Tag List工具

安裝方法
  • cd ~/Downloads
  • wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701
  • cd ~/.vim  (若不存在要建立)
  • unzip ~/Downloads/taglist.zip
使用方法(需要先安裝ctags並順利執行)
  • vim 一份程式碼
  • :TlistOpen

Saturday, January 18, 2014

瞭解 DMA (Direct Memory Access)

故事是這樣的。

從前有個老大覬覦著國王的寶藏庫,朝朝夜夜只想要把寶藏庫裡的東西搬到自己家裡頭;而當老大的,總是有個不愛自己露臉的個性,所以有個稱職又講禮貌的小弟服務他。

這天終於來了,老大告訴小弟:「誒,去搜刮寶藏庫裡面的五號珍藏品,它放在第七號櫃子上」,沒想到這個講禮貌的小弟竟然跑去跟國王說他要用外頭搬運貨物的道路來搬寶藏,更沒想到的是,這昏庸的國王看看發現沒有人在使用這個道路,於是竟然就答應了!

這下好了,小弟馬上跑去寶藏庫跟管理員說他要七號櫃子的寶藏,然後又跑去跟老大說事情搞定了。都講好之後,五號珍藏品就從寶藏庫搬運出來了,一路搬到老大他家,這時老大也笑哈哈的把這珍藏品收起來。

小弟似乎有禮貌過頭,老大把珍藏品都收好之後,他竟然又跑去跟國王說寶藏庫已經使用完了,那條送貨的路可以還給你用了,而昏庸的國王則點點頭說知道了。寶藏就在國王沒親自插手的情況下搬到老大家了!

以上就是DMA的運作介紹

角色

  • 國王:CPU
  • 老大:I/O Device with Interface
  • 小弟:DMA
  • 寶藏庫:(System) Memory
  • 搬運貨物的道路:Address Bus


流程示意圖

* 影像來自課本講義

配合示意圖中的流程編號,我們可以將本來故事編上編號:

故事是這樣的。

從前有個老大覬覦著國王的寶藏庫,朝朝夜夜只想要把寶藏庫裡的東西搬到自己家裡頭;而當老大的,總是有個不愛自己露臉的個性,所以有個稱職又講禮貌的小弟服務他。

這天終於來了,老大告訴小弟:「誒,去搜刮寶藏庫裡面的五號珍藏品,它放在第七號櫃子上」(1),沒想到這個講禮貌的小弟竟然跑去跟國王說他要用外頭搬運貨物的道路來搬寶藏(2),更沒想到的是,這昏庸的國王看看發現沒有人在使用這個道路,於是竟然就答應了(3)

這下好了,小弟馬上跑去寶藏庫跟管理員說他要七號櫃子的寶藏(4),然後又跑去跟老大說事情搞定了(5)。都講好之後,五號珍藏品就從寶藏庫搬運出來了,一路搬到老大他家(6),這時老大也笑哈哈的把這珍藏品收起來(7)

小弟似乎有禮貌過頭,老大把珍藏品都收好之後,他竟然又跑去跟國王說寶藏庫已經使用完了(8),那條送貨的路可以還給你用了,而昏庸的國王則點點頭說知道了(9)。寶藏就在國王沒親自插手的情況下搬到老大家了!

Wednesday, January 15, 2014

Bluetooth Remote Control Car - Microprocessor System Lab Final Project

Preface

Finally, it’s the end of this semester. We’ve learned a lot of 8051 assembly code programming from this course, including simple I/Os, interrupts, timers, UART, etc. This time, we are trying to improving our 8051 coding skill to next level. Therefore, “Remote Control Car” becomes our challenge for the final project. We got to bring out the coolest toy car!

Abstraction

They are 4 DC motors, which drives the car forward, backward, turn left, and turn right, on the remote controlled car we designed. Our main chip, 8051, receives the signals from Bluetooth UART module, and control those motors by NPN transistors and relays. It turns out that we can control the car via Bluetooth on our laptop.

Development Flowchart




In the other words, we tried to rebuild the remote control car by using Arduino Duemilanove with HC-06 Bluetooth module first. Then, added transistors before the implementation on 8051.

Components

Before we get started, I would like to list out the components we are using in this project.

89S51

What’s the difference between 89S51 and 89C51?

The biggest difference is that 89S51 is offering ISP feature. “ISP” is the ability of some programmable logic devices, microcontrollers, and other embedded devices to be programmed while installed in a complete system, rather than requiring the chip to be programmed prior to installing it into the system (quote from Wikipedia).
That means that we can program the chip without other tools but simple serial port instead. And, it’s not only more convenient but also cheaper.

Why 89S51 instead of 89C51?

Since the remote control car need a place for it to drive around and a longer developing time, we think it will be better if we develop in our own place. To setup an environment for 8051 developing, we first have to find out a way to burn our code into the 8051 chip in our own place. Here are the solutions we had before we get started:

Solution
Advantage
Disadvantage
Use ICE Emulator in EC222
No extra equipment.
Not able to put on our car and EC222 doesn’t offer flexible open hours.
Use 89S51 & use ISP to burn the program
Able to develop in our own place.
We have to buy extra experiment board.

Since 89S51 offers ISP feature, it’s much easier for us to write code on it instead of using ICE Emulator.

What’s our developing environment if using 89S51?

We are using “TE-S51A” experiment board this time. It’s much cheaper than buying the ICE emulator, and it offers a simple ISP burner which save our time to build our own ISP driver. TE-S51A is connected with the laptop via USB cable, which is simple.

Motors and Batteries




4 DC motors are applied in this project. Each of them requires at least 3V power supply. Therefore, we’ve wired each of them up with one pair of batteries. DC motors have a simple mechanical design, but they are powerful for moving the car fast.

Usage
Motor
Required Input Voltage
Forward
Back Motor
3V
Backward
Back Motor
3V
Turn Left
Front Motor
3V
Turn Right
Front Motor
3V



Relays


The motor are using high-current circuits, but the circuit for 8051/Arduino is not. These two kinds of circuits should be separated to avoid conflicts. Relays total solve this problem here. See one of our circuit design here:
The “Motor Control” wire can be set by the controller (8051 or Arduino); if it’s set, the motor will start to work. We apply this design for all the motors, so we need 4 pairs of batteries and 4 relays in total.


NPN Transistors




When we start to use 8051 instead Arduino, we found that they don’t have the same behavior. In Arduino, we can simply pull up one of the pin, and switch on the relay, but it’s different when I work on 8051. I pull up one of the pin on 8051 and checked it’s around 4.5V; however, when my relay is connected, it comes to around 0V. This tells me that 8051 is not offering enough current to the relay, and it’s not able to switch on the relay.
To solve this problem, we added “NPN transistors”. By using the transistors, only small current is needed to turn on the relay.

Bluetooth Module (HC-06)




In order to build a “wireless” remote car, we decided to use bluetooth to communicate with our laptop. It’s advantage is light and cheap, and disadvantage is that the time for pairing take a little long.
Using the Bluetooth module HC-06 is pretty simple. There are only four pins on it as below:

VCC
Working Voltage ~= 5V
GND
Ground
RX
UART Receive Port
TX
UART Transfer Port



And this is how we connect HC-06 with our 8051.

Arduino Duemilanove




Before we got our hand dirty on 8051, we first used Arduino for testing. Arduino offers much better platform for developers, and we can test our circuit designs in a short time. However, it seems that this board is not desired in our lab, so we have to switch back to 8051 after our testing.

System Structure

System Flowchart


By combining all the components, we can build up our remote control car. The signal flowchart of the whole system can be learned from the following graph.




There are four types of signals for controlling the car in order to give different commands for the car.

Signal Name
8051: UART Input
Arduino: UART Input
Forward
arrow up
‘w’
Backward
arrow down
‘s’
Turn Left
arrow left
‘a’
Turn Right
arrow right
‘d’

8051/Arduino have different reaction on different signals, and actually the code is quite simple.

ASM code for 8051

MOV TMOD, #20H
MOV TH1, #-3 ; 9600 baud
MOV SCON, #50H
SETB TR1
MOV P2, #00000000b
HERE: JNB RI, HERE;
MOV A, SBUF
; A is the input char
CJNE A, #01000001b, NOT_CASE1
ACALL FORWARD
JMP CONTINUE
NOT_CASE1:
CJNE A, #01000010b, NOT_CASE2
ACALL BACKWARD
JMP CONTINUE
NOT_CASE2:
CJNE A, #01000100b, NOT_CASE3
ACALL LEFT
JMP CONTINUE
NOT_CASE3:
CJNE A, #01000011b, ELSE_CASE
ACALL RIGHT
JMP CONTINUE
ELSE_CASE:
ACALL CLEAR
CONTINUE:
CLR RI
SJMP HERE
;
CLEAR:
MOV P2, #00000000b
RET
FORWARD:
MOV P2, #00000001b
RET
BACKWARD:
MOV P2, #00000010b
RET
LEFT:
MOV P2, #00000100b
RET
RIGHT:
MOV P2, #00001000b
RET
END

C code for Arduino

char INBYTE;
char GO_IN = 'w';
char BACK_IN = 's';
char LEFT_IN = 'a';
char RIGHT_IN = 'd';

int GO_PIN = 12;
int BACK_PIN = 11;
int LEFT_PIN = 10;
int RIGHT_PIN = 9;

void setup() {
 Serial.begin(9600);

 pinMode(LED, OUTPUT);
 pinMode(GO_PIN, OUTPUT);
 pinMode(BACK_PIN, OUTPUT);
 pinMode(LEFT_PIN, OUTPUT);
 pinMode(RIGHT_PIN, OUTPUT);
}

void loop() {

 while (!Serial.available());   // stay here so long as COM port is empty   

 INBYTE = Serial.read();        // read next available byte
 if( INBYTE == GO_IN )
   digitalWrite(GO_PIN, HIGH);
 else
   digitalWrite(GO_PIN, LOW);
 
 if( INBYTE == BACK_IN )
   digitalWrite(BACK_PIN, HIGH);
 else
   digitalWrite(BACK_PIN, LOW);

 if( INBYTE == LEFT_IN )
   digitalWrite(LEFT_PIN, HIGH);
 else
   digitalWrite(LEFT_PIN, LOW);
 
 if( INBYTE == RIGHT_IN )
   digitalWrite(RIGHT_PIN, HIGH);
 else
   digitalWrite(RIGHT_PIN, LOW);

 delay(50);
}

Further Challenges


We’ve finally built our own remote control car using Bluetooth on 8051. However, we think that it’s is not stable enough at this point. And, we also believe that there’s more possibilities behind this project.

Not Stable Enough

Since we are building almost everythings on our own, including soldering the wires, circuit designs, and the assembly code. Some of them don’t always give out a stable work. The car will stop working if one of the problems occurs. Here, I am listing some of them out:
  1. Wires don’t connect well.
  2. Motor is blocked by other stuffs since there’s too much things on the car.
  3. Batteries are out of charge.



More Improvements

Here are some improvements we can think of, and hope that we can continue on these in the future.
  1. Video Streaming: Actually, we’ve put one cell phone on the car, and streamed the video back using Skype, so users can see the view from the current when operating it.
  2. Mobile App Integration: Another reason for using Bluetooth instead of IR is that we can write an app to control the car in the future. People are playing car racing game on mobile phones, and we want to make the same thing but the car in the game is real. What’s more, we believe that using G-sensor, which is already embedded in our smart phones, is a better way to control the car.