/***** STB-- P3.1
***** DIO-- P3.5 P3.4
***** CLK-- P3.0
****************************************/
#include "msp430f5438a.h"
#define STB_1 P3OUT |= BIT1
#define STB_0 P3OUT &= ~BIT1
/****
#define CLK_1 P3OUT |= BIT0
#define CLK_0 P3OUT &= ~BIT0
#define DIO_1
#define DIO_0
#define DIO_IN
#define DIO_OUT
****/
#define RED_TM1638 0x01 //RED Led
#define GRE_TM1638 0x02 //Green Led
#define DATA_WRITE_INCR_ADDR 0x40 //地址自动增加写数据命令 Command to switch TM1638 for automatic increment address mode
#define DATA_WRITE_FIX_ADDR 0x44 //固定地址写数据命令 Command to switch TM1638 for fix address mode
#define DATA_READ_KEY_SCAN_MODE 0x42 //读取按键值命令 Command for read key code from TM1638
#define ADDRSET 0xC0 //地址从00H开始 Command to set address 0x00
#define DISP_ON 0x8F //打开显示 最大亮度Command to Display ON and set (max) brightness
#define DISP_OFF 0x80 //关闭显示 Command to Display OFF
#define DISP_ON 0x8F //打开显示 最大亮度Command to Display ON and set (max) brightness
#define DISP_OFF 0x80 //关闭显示 Command to Display OFF
unsigned int Num[]= //Code table of symbols
{
0x3F, //0
0x06, //1
0x5B, //2
0x4F, //3
0x66, //4
0x6D, //5
0x7D, //6
0x07, //7
0x7F, //8
0x6F, //9
0x40, //Minus
0x63, //Degree
0x39, //"C"
0x00, //Blank
0x3E //"U"
};
unsigned int ERROR_DATA[] = {
0x79, // E
0x50, // r
0x50, // r
0x5C, // o
0x50, // r
0,
0,
0
};
void init_Ports()
{
P3OUT = 0x00; // P1 setup for LED & STROBE output
//P2OUT = 0x00; // P2 setup for output
P3DIR = BIT1;// P1.0 P1.5 P1.6 都是输出方向
P3SEL = BIT0 + BIT4 + BIT5; // 打开P1.1 P1.2 P1.4 的第二功能 Set secondary functions for PORT1
//P1SEL2 = BIT0 + BIT4 + BIT5; // P3.4 - TXD, P3.5 - RXD
STB_1; // STB = 1 Chip Select, Set STROBE = "1"
}
void init_WDT()
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog
}
void init_SPI()
{
UCA0CTL0 |= UCCKPL + UCMST + UCSYNC; // 第一个边沿更新数据,第二个边沿捕获数据(先输出后输入!!!) ,不工作状态是高电平
// 3-pin, 8-bit SPI master
// 主机模式,同步通讯
UCA0CTL1 |= UCSSEL_2; // fBRCLK = SMCLK = 1.045MHz
UCA0BR0 |= 0x02; // fBitClock = fBRCLK/2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void SendCommand(unsigned char Command) //Transmit Command
{
STB_0; //Set STROBE = "0"
_NOP();
_NOP();
STB_1; //Set STROBE = "1"
_NOP();
_NOP();
STB_0; //Set STROBE = "0"
_NOP();
UCA0TXBUF = Command; // 把命令发出去
while (!(UCA0IFG & UCTXIFG)); //等待命令发送完毕
STB_1; //Set STROBE = "1"
}
void SendData(unsigned int address, unsigned int data) // Transmit Data 向指定地址address中,写入数据data
{
unsigned int t;
SendCommand(DATA_WRITE_FIX_ADDR); //地址自动增加模式写数据
STB_0; //Set STROBE = "0"
UCA0TXBUF = (0xC0 | address); //发送开始地址adderssc
for(t=0; t<8; t++) //连续写入八个地址的数据
{
while (!(UCA0IFG & UCTXIFG));
UCA0TXBUF = data; // 一个字节整体写入
}
while (!(UCA0IFG & UCTXIFG));
STB_1; //Set STROBE = "1"
}
void ShowDig(int position, int Data, int Dot) //show single digit
{
SendData(position << 1, Data | (Dot ? 0x80 : 0) );
}
void ClearDig(unsigned int pos, unsigned int dot)
{
ShowDig(pos, 0x00, dot);
}
void ShowLed(int Number, int Color)
{
SendData((Number << 1)-1, Color);
}
void ShowLeds(int color)
{
unsigned int i;
for (i=1; i<9; i++)
{
ShowLed (i, color);
}
}
void DisplayClean() //Clean RAM of TM1638 清空数码管显示
{
int i;
for (i=0; i<17; i++)
{
SendData(i, 0x00);
}
}
void SetupDisplay(char active, char intensity)
{
SendCommand (0x80 | (active ? 8 : 0) | intensity); //显示控制命令写入
STB_1; // Set STROBE = "1"
}
void init_Display()
{
__delay_cycles(100000); //Time to initial TM1638
DisplayClean(); //Clean display
SendCommand(DISP_OFF); //Display off
SendCommand(DATA_WRITE_FIX_ADDR); //Set address mode
SendCommand(ADDRSET); //Set first adress
}
void ShowError()
{
unsigned int j;
for (j=0; j<8; j++)
{
ShowDig(j, ERROR_DATA[j], 0);
}
}
int GetKey()
{
unsigned int KeyData = 0;
unsigned int i;
STB_0; // Set STROBE = "0"
UCA0TXBUF = DATA_READ_KEY_SCAN_MODE;
while (!(UCA0IFG & UCTXIFG));
__delay_cycles(20); //wait to ready (see datasheet) ???????????????????????????
UCA0TXBUF = 0xff; //Send dummy byte
while (!(UCA0IFG & UCTXIFG)); //wait for buffer ready
// 1'st reseiving byte = bad
for (i=0; i<4; i++)
{
UCA0TXBUF = 0xff; //配合相位模式UCCKPH 先输出再输入
while (!(UCA0IFG & UCTXIFG));
KeyData |= UCA0RXBUF << i; // 总共读入四个字节的数据,这个为什么需要移位呢????????????
}
STB_1; //Set STROBE = "1"
return KeyData;
}
void main()
{
unsigned int j;
unsigned int Keys = 0;
init_WDT();
init_Ports();
init_SPI();
init_Display();
// Example program:
while(1)
{
for (j=0; j<8; j++)//example set "01234567" on display
{
ShowDig(j, Num[j], 0);
}
}
SetupDisplay(1, 7); // Display ON, max brightness
while(1)
{
ShowLeds(0); //switch all LEDs off
switch(GetKey())
{
case 1: Keys = 1; break;
case 2: Keys = 2; break;
case 4: Keys = 3; break;
case 8: Keys = 4; break;
case 16: Keys = 5; break;
case 32: Keys = 6; break;
case 64: Keys = 7; break;
case 128: Keys = 8; break;
default: Keys = 0; break;
}
if (Keys)
{
ShowLed(Keys, GRE_TM1638);
}
ShowDig(7, Num[Keys], 1);
}
//end of example program
}
|