星期一, 9月 19, 2005

普誠 LCD button controller : PT6961

提供類似SPI的interface (SDI/SDO/CLK/CS)和MCU溝通。
可以用掃描的方式驅動7段顯示器,用掃描的方式讀取keyboard。
SPI的格式是第一個byte command,之後N個byte data (IN/Out)。
Command的最高兩個bit是Command Index,之後的bite是command的設定內容。
Command後的byte都是data byte

Command 1: Display Mode Setting
七段顯示器掃描有18個控制線,可以選擇是6 digits + 12 segment 或是 7 digits + 11 segments。
一般的七段顯示器因為一個digit只有8個segment。所以選兩個都一樣。

Command 2: Data Setting Command
用來讀寫PT6961內部的memory (register ?)。用一個bit來決定是Read還是write。
Command之後就是Data (in/out)。
Read Command代表讀取Scan Key的內容,
KeyScan一次Scan K1,K2,K3三個key。
Scan Line有SG1 - SG10。
Read Data依照Scan的內容,每一個byte只有用到前6個bit,代表兩個Scan Line。
所以keyscan總共有5個byte。
Write Command代表寫入七段顯示輸出記憶體
一個七段顯示器需要12個bit,使用2個byte。
所以write command使用的memory有 7 x 2 = 14 byte


Command 3: Address Setting Command
設定開始顯示的memory address
顯示memory也是一樣,因為PT6961支援一個digit最多支援12 segment。
所以memory一個digit需要2個byte (16 bit),但是最高4 bit不使用。只用12 bit.

Command 4: Display Control Command
開啟/關閉七段顯示器的掃描動作。
設定顯示的duty cycle(調整亮度)。


Sample Code from PTC :
Command 0x03 1 : 7 digit 11 seg
Command 0x8F 4 : Display On, Pulse width 14/16
----clear ram----
Command 0x40 2 : Normal, Increment, Write to disp
Command 0xC0 - Data 0x00 repeat 14. 3 : write 14 datas to ram, start from 0
----load---------
Command 0x44 2 : Normal, Fix, Write to disp
----address:data set
Command 0x44 2 : Normal, Fix, Write to disp
Command Addr - Data Data
-0xC0 : 0x38 write 0x38 on 0
-0xC2 : 0x18 write 0x18 on 2
-0xC4 : 0x38 write 0x38 on 4
-0xC6 : 0x2C
-0xC8 : 0x3C
-0xCA : 0x1C
-0xCC : 0x30
REPEAT 4 Times
----set ram
Command 0x40 2 : Normal, Increment, Write to disp
Command 0xC0 Data 0xFF repeat 14 3 : Write 14 0xFF to disp starts from 0
----read key
Command 0x42 Read Data repeat 4 2 : Normal, Increment, Read 4 data.


spec的recommand algorithm:
Command 2
Command 3 Clear Memory
Command 1
Command 4 Disp OFF
Command 1
Command 4 Disp ON
Loop -
Command 2 Write Data
Command 3
Command 1
Command 4


使用PIC內建SPI來作沒有動作,改用IO控制後OK。
Initial的Code如下

void Pt6961Init(void)
{
char i;

STB=1;
SCL=1;

// command 2 : memory r/w, address inc
STB=0;
sout(0x40);
STB=1;

// command 3 : address set, and data
STB=0;
sout(0xC0);
for(i=0;i<14;i++)
sout(0x00);
STB=1;

// command 1 : display mode : digit & seg
STB=0;
sout(0x02);
STB=1;

// command 4 : disp on/off, disp duty
STB=0;
sout(0x87);
STB=1;

// command 1 : display mode
STB=0;
sout(0x02);
STB=1;

// command 4 : disp on/off, disp duty
STB=0;
sout(0x8F);
STB=1;

}

其中sout是:

void sout(char data)
{
char i;
for(i=0;i<8;i++){
SCL=0;
SDO = data&0x01;
SCL=1;
data = data >> 1;
}
}

將內容送出到LED的Code是

void LedPrint(char *str)
{
char data;
char i=0;

// command 2
STB=0;
sout(0x40); // write mode, auto inc address
STB=1;

// command 3
STB=0;
sout(0xC0); // set write start address=0
sout(str[0]); // 1st digit
sout(0x00); // 1 digit occupy 2 bytes
sout(str[1]); // 2nd digit
sout(0x00);
sout(str[2]); // 3rd digit
sout(0x00);
STB=1;
}

讀取KeyScan的Code是: data[5]就是讀出的結果,但是每個byte只有前6個bit有效

// Read
STB=0;
sout(0x42); // read mode, auto inc address
data[0]=sin(); // read in 1st scan line only 0-5 useable
data[1]=sin(); // 2nd
data[2]=sin(); // 3rd
data[3]=sin(); // 4th
data[4]=sin(); // 5th
STB=1;

sin() 是

char sin(void)
{
char i;
char rc=0;

for(i=0;i<8;i++){
SCL=0;
rc = rc>>1;
SCL=1;
if(SDI)
rc |= 0x80;
}

return rc;
}



所以datasheet應該要改一下,這一顆chip的protocol是固定將STB Low後第一個byte作Command,之後的都是Data,所以在任何Command後都可以加Data。 至於Data的動作就由上次Command 2的Read/Write bit來決定是Read Data或是Write Data。
如果是Write Data,Write Target Address就依照上次 Command 2中address auto increment bit的狀態跟Command 3 : target address的設定值決定。

沒有留言:

網誌存檔