/******************************************************
程序说明:LED显示
时 间:2014.6.26
HT1621显示数字:0 1 2 3 4 5 6 7 8 9
程序功能:接收来自PC的字符,然后用8个LED灯显示出来
8个LED从高到低,组成8位二进制数.
通信格式:N.8.1, 9600
无校验,8个数据位,1个停止位,波特率9600
*******************************************************/
#include
#include
#define uint unsigned int
#define uchar unsigned char
unsigned char table[] = { 0xEB, 0x60, 0xC7, 0xE5, 0x6C, 0xAD, 0xAF, 0xE0, 0xEF,
0xED };
#define BIAS 0x52 //0b1000 0101 0010 1/3duty 4com
#define SYSDIS 0X00 //0b1000 0000 0000 关振系统荡器和LCD偏压发生器
#define SYSEN 0X02 //0b1000 0000 0010 打开系统振荡器
#define LCDOFF 0X04 //0b1000 0000 0100 关LCD偏压
#define LCDON 0X06 //0b1000 0000 0110 打开LCD偏压
#define XTAL 0x28 //0b1000 0010 1000 外部接时钟
#define RC256 0X30 //0b1000 0011 0000 内部时钟
#define TONEON 0X12 //0b1000 0001 0010 打开声音输出
#define TONEOFF 0X10 //0b1000 0001 0000 关闭声音输出
#define WDTDIS1 0X0A //0b1000 0000 1010 禁止看门狗
#define SECOND 20
#define SYSTEM_CLOCK
uchar Data_Length=3,counter=0,sec=0;
uchar LED_STATUS=1,RXD_STATUS=1; //显示状态
uint RXD_COM[3],LED_COM=0,LCD_COM=0,LCD_COM_TIME=0; //
//-----------------------------------------------------------------------------------------
//函数名称:Delay()
//功 能:延时子程序
//-----------------------------------------------------------------------------------------
void Delay(uchar us) //5,7,9
{
while (--us)
;
}
//-----------------------------------------------------------------------------------------
//函数名称:DelayMS()
//功 能:延时子程序
//-----------------------------------------------------------------------------------------
void DelayMS(uint iMs) {
uint i, j;
for (i = 0; i < iMs; i++)
for (j = 0; j < 65; j++) {
Delay(1);
}
}
//-----------------------------------------------------------------------------------------
//函数名称:Ht1621Wr_Data()
//功 能:写数据函数,cnt为传送数据位数,数据传送为低位在前
//-----------------------------------------------------------------------------------------
void Ht1621Wr_Data(uchar Data, uchar cnt) {
uchar i;
for (i = 0; i < cnt; i++) {
P1OUT &= ~0x40;
if ((Data & 0x80) == 0x80) {
P1OUT |= 0x04;
} else {
P1OUT &= ~0x04;
}
P1OUT |= 0x40;
Data <<= 1;
}
}
//-----------------------------------------------------------------------------------------
//函数名称:void Ht1621WrCmd(uchar Cmd)
//功能描述: HT1621命令写入函数
//参数说明:Cmd为写入命令数据
//说 明:写入命令标识位100
//-----------------------------------------------------------------------------------------
void Ht1621WrCmd(uchar Cmd) {
P1OUT &= ~0x80;
Ht1621Wr_Data(0x80, 4); //写入命令标志100
Ht1621Wr_Data(Cmd, 8); //写入命令数据
P1OUT |= 0x80;
}
//-----------------------------------------------------------------------------------------
//函数名称:void Ht1621WrOneData(uchar Addr,uchar Data)
//功能描述: HT1621在指定地址写入数据函数
//参数说明:Addr为写入初始地址,Data为写入数据
//说 明:因为HT1621的数据位4位,所以实际写入数据为参数的后4位
//-----------------------------------------------------------------------------------------
void Ht1621WrOneData(uchar Addr, uchar Data) {
P1OUT &= ~0x80;
Ht1621Wr_Data(0xa0, 3); //写入数据标志101
Ht1621Wr_Data(Addr << 2, 6); //写入地址数据
Ht1621Wr_Data(Data << 4, 4); //写入数据
P1OUT |= 0x80;
}
//-----------------------------------------------------------------------------------------
//函数名称:void Ht1621WrAllData()
//功能描述: HT1621连续写入方式函数
//说 明:HT1621的数据位4位,此处每次数据为8位,写入数据总数按8位计算
//-----------------------------------------------------------------------------------------
void Ht1621WrAllData(void) {
uchar i;
P1OUT &= ~0x80;
Ht1621Wr_Data(0xa0, 3); //写入数据标志101
Ht1621Wr_Data(0x00 << 2, 6); //写入地址数据
for (i = 0; i < 16; i++) {
Ht1621Wr_Data(0x00, 8); //写入数据
}
P1OUT |= 0x80;
}
//-----------------------------------------------------------------------------------------
//函数名称:void Ht1621_Init(void)
//功能描述: HT1621初始化
//说 明:初始化后,液晶屏所有字段均显示
//-----------------------------------------------------------------------------------------
void Ht1621_Init(void) {
DelayMS(20); //延时使LCD工作电压稳定
Ht1621WrCmd(BIAS);
Ht1621WrCmd(RC256); //使用内部振荡器
Ht1621WrCmd(SYSDIS);
Ht1621WrCmd(WDTDIS1);
Ht1621WrCmd(SYSEN);
Ht1621WrCmd(LCDON);
}
//-----------------------------------------------------------------------------------------
//函数名称:Initial()
//功 能:初始化子程序
//-----------------------------------------------------------------------------------------
void Initial(void) {
DCOCTL=0X60;
BESCTL1=OX86;
BESCTL1=OX00;
BCSCTL1 |= DIVA_2; // ACLK/4
WDTCTL = WDT_ADLY_1000; // WDT 1s/4 interval timer
TACTL=TASSEL_2+ID0+TACLR;//ACLK=32768HZ,CLEAR TAR ,2分频
CCTL0=CCIE;
CCR0=819;//(1/32768)*819=20MS
TACTL|MC0;//增计数
IE1 |= WDTIE; // Enable WDT interrupt
P1DIR = 0xFF; // All P1.x outputs
P1OUT = 0; // All P1.x reset
P2DIR = 0xFF; // All P2.x outputs
P2OUT = 0; // All P2.x reset
P3SEL |= 0x30 + BIT6 + BIT7 ;
ME2 |= UTXE1 + URXE1;
U1CTL |= CHAR;
U1TCTL |= SSEL0;
U1BR0 = 0x03;
U1BR1 = 0x00;
U1MCTL = 0x4A;
U1CTL &= ~SWRST;
IE2 |= URXIE1;
_EINT();
}
//-----------------------------------------------------------------------------------------
//函数名称:void Ht1621WrByteData(uchar address,uchar what)
//功 能:在指定的地址写入一个字节数,注意高字节先写,每次写入奇数地址
//-----------------------------------------------------------------------------------------
void Ht1621WrByteData(uchar address, uchar what) {
P1OUT &= ~0x80;
Ht1621Wr_Data(0xa0, 3); //写入数据标志101
Ht1621Wr_Data(address << 2, 6); //写入地址数据
Ht1621Wr_Data(what, 8); //写入数据
P1OUT |= 0x80;
}
//-----------------------------------------------------------------------------------------
//函数名称:Ht1621_Display(uint interval_time)
//功 能:时间间隔显示函数
//-----------------------------------------------------------------------------------------
void Ht1621_Display(uint interval_time)
{
int qiang = 0, bai = 0, shi = 0, ge = 0;
qiang = interval_time / 1000;
bai = interval_time % 1000 / 100;
shi = interval_time % 100 / 10;
ge = interval_time % 10;
Ht1621_Init(); //上电初始化LCD
Ht1621WrByteData(0x08, table[qiang]);
Ht1621WrByteData(0x06, table[bai]);
Ht1621WrByteData(0x04, table[shi]);
Ht1621WrByteData(0x02, table[ge] | 0x10); //带小数点
Ht1621WrByteData(0x00, 0x00);
}
void Led_Display(uchar led_com)
{
P2OUT = led_com;
DelayMS(100);
P2OUT = 0x00;//熄灭
}
void Command_Process(uchar * com_data) //取出三个字节
{
LED_COM = com_data[0]; //LED编码:01 02 04 08 16 32 64 128
LCD_COM_TIME= com_data[1]; //LED间隔时间:01 02 03 04 05 06 07 08
LCD_COM_BIT = com_data[2]; //LED位数:01 02 03 04 05 06 07 08
}
//-----------------------------------------------------------------------------------------
//函数名称:main()
//功 能:主函数
//-----------------------------------------------------------------------------------------
void main(void) {
Initial(); //初始化子程序
Ht1621_Init(); //上电初始化LCD
DelayMS(10); //延时一段时间
Ht1621WrAllData();
while (1)
{
Command_Process(RXD_COM); //处理串口数据
if(LED_STATUS==1) //接收到数据则开始下一次显示,定时器中断中改变状态led的状态
{
Led_Display(LED_COM); //将LED显示指令发送至LED
LED_STATUS=0; //改变显示状态
}
Lcd_Display(LCD_COM); //显示时间间隔
}
}
//-----------------------------------------------------------------------------------------
//函数名称:UART1_RXISR()
//功 能:UART1的接收中断服务函数
//-----------------------------------------------------------------------------------------
#pragma vector = UART1RX_VECTOR
__interrupt void UART1_RXISR(void)
{
RXD_COM[Data_Length++] = RXBUF1;
if(Data_Length==2)
{
Data_Length=0;
}
}
//-----------------------------------------------------------------------------------------
//函数名称:TIMERA1_VECTOR()
//功 能:定时器A1产生50毫秒的中断
//-----------------------------------------------------------------------------------------
#pragma vector = TIMERA1_VECTOR
__interrupt void timer_a(void)
{
counter++;
if(counter==SECOND)
{
counter=0;
sec++;
if(sec==LCD_COM_TIME)
{
LED_STATUS-0;
}
}
}
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void) {
_BIC_SR_IRQ(LPM3_bits);
}
你能帮我改改这个程序吗 大神