 |
โดยทั่วไปแล้วการควบคุม 7'segments จะต้องใช้ 2 ports คือ Data port กับ Scan port ลองเปลี่ยนวิธีการ โดยใช้ I2C ร่วมกับ SAA1064 เพื่อควบคุม 7'segments. ซึ่งจะทำงานได้ง่ายกว่าเนื่องจาก SAA1064 จะสแกน 7'segments ให้เองโดยอัตโนมัติ เราเพียงแต่ส่งข้อมูลที่ต้องการแสดงผลเขาไปไว้ที่ memory ของ SAA1064 เท่านั้น
|
อุปกรณ์ในการทดลอง
- SNAP Board and Baseboard
- 7'segments Board
|
 |
ความรู้เบื้องต้นของโครงงาน
|
"I2C" (ไอ-สแคว-ซี) หรือ ชื่อเต็มๆ ว่า "Inter-Integrated Circuit" เป็นผลงานการวิจัยของ บริษัท Philips Semiconductors เืืพื่อใช้สำหรับติดต่อกับอุปกรณ์ภายนอกที่มีความเร็วต่ำ เนื่องจากได้ออกแบบลักษณะของการสื่อสารเป็น แบบอนุกรม (Synchronous) โดยใช้สายสัญญาณ เีพียง 2 เส้นเท่านั้นคือ
- Serial Data (SDA)
- Serial Clock (SCL)
|  |
| ทำให้สามารถต่ออุปกรณ์ภายนอก ไปบน Bus เดียวกันได้สุงสุดที่ 112 ตัว โดยมีความเร็วในการรับส่งข้อมูลใน Standard mode ที่ 100Kbits/s และ Low-speed mode ที่ 10Kbits/s สามารถอ่านเพิ่มเิติ่มได้จาก Wikipedia และ Thaimicrotron.com
|
7'segments จะเป็นตัวที่แสดงข้อมูลให้อยู่ในรูปแบบของตัวเลขฐานสิบหก(0 ถึง F) โดยการทดลองนี้กำหนดให้มีการ Mapping ระหว่าง Digits ของ 7'segment กับ Databit ไว้ดังตารางข้างล่างนี้
| DIGITs |
a |
b |
c |
d |
e |
f |
g |
dt |
| DATA(bit) |
D0 |
D1 |
D2 |
D3 |
D4 |
D5 |
D6 |
D7 |
|
 |
Mapping 7'segment display.
 |
 |
 |
 |
 |
 |
 |
 |
|
| 00111111 |
00000110 |
01011011 |
01001111 |
01100110 |
01101101 |
01111101 |
00000111 |
|
| 0x3F |
0x06 |
0x5B |
0x4F |
0x66 |
0x6D |
0x7D |
0x07 |
|
 |
 |
 |
 |
 |
 |
 |
 |
| 01111111 |
01101111 |
01110111 |
01111100 |
00111001 |
01011110 |
01111001 |
01110001 |
| 0x7F |
0x6F |
0x77 |
0x7C |
0x39 |
0x5E |
0x79 |
0x71 |
รายละเิอียดของโครงงาน
เนื่องจาก SNAP ได้รับการออกแบบให้ใช้กับ TILT400 ซึ่งมีการซ้อนทับระหว่างขา I2C กับขา CAN bus ซึ่งทางบริษัทเห็นว่าไม่สะดวก ในการใช้งานจึงได้ออกแบบ Baseboard ใหม่เพื่อให้สามารถใช้งาน I2C ได้พร้อมๆ กับ Can bus ดังนั้นจึงต้องมีการเพิ่มคำสั่งเข้าไปในขั้นตอนของการสร้าง Constructor ของ คลาส CTRLLED ด้วยคำสั่งข้างล่าง
| SNAP.setI2CPinSet(SNAP.I2C_ALTPINS); |
|
โครงงานนี้ประกอบไปด้วยโปรแกรม 2 ตัว ซึ่งมีชื่อและหน้าที่การทำงานดังต่อไปนี้
- CTRLLED.java
เริ่มจากการประกาศ CTRLLED extends จาก I2CPort
public class CTRLLED extends I2CPort{
} |
สร้าง Constructor และ อ้างถึง super() function เพืออ้างถึงคุณสมบัติทั้งหมด ของ I2CPort
public CTRLLED(){
super();
SNAP.setI2CPinSet(SNAP.I2C_ALTPINS); //ย้ายขาที่ทับกันระหว่าง Canbus กับ I2C
setClockDelay(8);
setAddress((byte) (ADDRESS >> 1));
InitialLED();
} |
เริ่มต้นจากการกำหนดหมายเลข Address ให้กับ SAA1064, ตำแหน่งของ Memory, และ Control, Databuffer สำหรับ โปรแกรม
final byte ADDRESS = 0x70; //slave address
final byte SUBADDR0= 0x00;
final byte SUBADDR1= 0x01;
final byte SUBADDR2= 0x02;
final byte SUBADDR3= 0x03;
final byte SUBADDR4= 0x04;
private byte CONTROL = 0x00;
private byte DIGIT1= 0x00;
private byte DIGIT2= 0x00;
private byte DIGIT3= 0x00;
private byte DIGIT4= 0x00;
|
ทำความเข้าใจกับตำแหน่งของ control register และ control bits บน SAA1064
 |
- S = start condition
- P = stop condition
- A = acknowledge
- X = don?t care
|
- A1, A0 = programmable address bits
- SC, SB, SA = subaddress bits
- C6 to C0 = control bits
- PR = POWER RESET flag
|
Subaddressing
ใช้สำหรับการกำหนดว่าเราจะเขียน ข้อมูลไปที่ Control Register หรือ ว่าส่งข้อมูลไปแสดงผลที่ 7'segment Digit1, Digit2, Digit3, Digit4 ตามตารางข้างล่าง
-
-
| SC |
SB |
SA |
Function |
|
| 0 |
0 |
0 |
Control Register |
|
| 0 |
0 |
1 |
Digit 1 |
|
| 0 |
1 |
0 |
Digit 2 |
|
| 0 |
1 |
1 |
Digit 3 |
|
| 1 |
0 |
0 |
Digit 4 |
|
|
Control bits ซึ่งแต่ละบิทมีความหมายและหลักการทำงานดังต่อไปนี้
- C0
- '0' = static mode, i.e. continuous display of digits 1 and 2
- '1' = dynamic mode, i.e. alternating display of digit 1 + 3 and 2 + 4 [selected]
- C1
- '0' = digits 1 + 3 are blanked
- '1" = digits 1 + 3 are not blanked [selected]
- C2
- '0' = digits 2 + 4 are blanked
- '1' = digits 2 + 4 are not blanked [selected]
- C3
- '1' = all segment outputs are switched-on for segment test
- C4
- '1' = adds 3 mA to segment output current
- C5
- '1' = adds 6 mA to segment output current [selected]
- C6
- '1' = adds 12 mA to segment output current [selected]
C4, C5, C6 เป็นบิทสำหรับปรับค่าความสว่างของ 7'segment โดย ถ้าเป็น '1' หมด จะให้ความสว่างสูงสุด และ ถ้าเป็น '0' หมด จะให้ความสว่างน้อยที่สุด
|
เริ่ม initial SAA1064 ด้วย Function "InitialLED()"
private void InitialLED(){
byte[] buf = new byte[6];
SetDynamicMode(); // dynamic mode
SetDigit13(); // digits 1 + 3 are not blanked
SetDigit24(); // digits 2 + 4 are not blanked
SetBright(3); // setting bright level 3
buf[0] = 0x00;
buf[1] = CONTROL;
buf[2] = 0x00; //set Digit 1 OFF;
buf[3] = 0x00; //set Digit 2 OFF;
buf[4] = 0x00; //set Digit 3 OFF;
buf[5] = 0x00; //set Digit 4 OFF;
WriteManyByte(buf);
} |
Mapping Charactor '0' to 'F' เข้า กับ Display Digit บน 7'segment. ตามที่ได้อธิบายและยกตัวอย่างไว้ข้างบน
private byte ConvertCharToSegment(char ch){
byte result = 0x00;
switch (ch) {
case ' ': result= 0x00; break;
case '0': result= 0x3F; break;
case '1': result= 0x06; break;
case '2': result= 0x5B; break;
case '3': result= 0x4F; break;
case '4': result= 0x66; break;
case '5': result= 0x6D; break;
case '6': result= 0x7D; break;
case '7': result= 0x07; break;
case '8': result= 0x7F; break;
case '9': result= 0x6F; break;
case 'A': result= 0x77; break;
case 'B': result= 0x7C; break;
case 'C': result= 0x39; break;
case 'D': result= 0x5E; break;
case 'E': result= 0x79; break;
case 'F': result= 0x71; break;
case 'N': result= 0x54; break;
case 'O': result= 0x5C; break;
case 'Y': result= 0x6E; break;
default: result= 0x40;
}//switch (ch);
return(result);
}
|
- TestLED.java
public class TestLED {
public static void main(String[] args) {
CTRLLED LED = new CTRLLED();
LED.ShowDisplay('1','2','3','4');
}
}
|
|
|