}
void Init_SystemClock()
{
SCFQCTL |=BIT0+BIT1+BIT2+BIT3+BIT4;//即时钟模块功能打开,DCO主频为DCOPLUS=0: fDCOCLK = (N + 1) . fcrystal,这里N为默认
//SCFI0 =默认全为0
//SCFI1 =默认全为0
//FLL_CTL0 |=按默认的来处理,后面的初始化LCD和BasicTimer都对这里的匹配电容做了修改。
FLL_CTL1 |= XT2OFF; //SMCLK is on,XT2 is off it is not used for MCLK or SMCLK,select DCO as MCLK source,select DCO as SMCLK source,ACLK 不频率
}
//******************************************************************************
//*****************************BLE程序段****************************************
//******************************************************************************
void Init_BLE()
{
char Bluetooth_data[100];
char Count_Data=0;
char Ble_data=0;
//************************************
#define En_0 P4OUT &=~BIT2;
#define En_1 P4OUT |= BIT2;
#define BRTS_0 P3OUT &=~BIT0;
#define BRTS_1 P3OUT |= BIT0;
#define BCTS_IN P3IN&BIT7;
//#define BCTS_1 P3OUT |= BIT7;//没有意义了,因为BCTS脚是接收模块的信号,是输入状态
//************************宏定义************************************************
char *LCD=LCDMEM; //LCDMEM为LCD寄存器的起始地址0x0091h
//定义每段对应的值
#define SEG_a 0x01
#define SEG_b 0x02
#define SEG_d 0x04
#define SEG_g 0x08
#define SEG_c 0x10
#define SEG_f 0x20
#define SEG_h 0x40
#define SEG_e 0x80
//定义每个字符对应点亮的段
#define CHAR_ALL (SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f|SEG_g|SEG_h)
#define CHAR_SPACE 0
#define CHAR_MINUS (SEG_g)
#define CHAR_0 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f)
#define CHAR_1 (SEG_b|SEG_c)
#define CHAR_2 (SEG_a|SEG_b|SEG_d|SEG_e|SEG_g)
#define CHAR_3 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_g)
#define CHAR_4 (SEG_b|SEG_c|SEG_f|SEG_g)
#define CHAR_5 (SEG_a|SEG_c|SEG_d|SEG_f|SEG_g)
#define CHAR_6 (SEG_a|SEG_c|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_7 (SEG_a|SEG_b|SEG_c)
#define CHAR_8 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_9 (SEG_a|SEG_b|SEG_c|SEG_d|SEG_f|SEG_g)
#define CHAR_E (SEG_a|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_R (SEG_e|SEG_g)
#define CHAR_B (SEG_c|SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_A (SEG_b|SEG_c|SEG_e|SEG_f|SEG_a|SEG_g)
#define CHAR_T (SEG_d|SEG_e|SEG_f|SEG_g)
#define CHAR_F (SEG_e|SEG_f|SEG_a|SEG_g)
#define CHAR_L (SEG_d|SEG_e|SEG_f)
#define CHAR_H (SEG_b|SEG_c|SEG_e|SEG_f|SEG_g)
const char digit[20] =
{
CHAR_0, // "0"
CHAR_1, // "1"
CHAR_2, // "2"
CHAR_3, // "3"
CHAR_4, // "4"
CHAR_5, // "5"
CHAR_6, // "6"
CHAR_7, // "7"
CHAR_8, // "8"
CHAR_9, // "9"
CHAR_E, // "10"
CHAR_R, // "11"
CHAR_B, // "12"
CHAR_A, // "13"
CHAR_T, // "14"
CHAR_F, // "15"
CHAR_L, // "16"
CHAR_H, // "17"
CHAR_ALL, //"18"
CHAR_SPACE, //"19"
};
void Init_LCD()
{
unsigned int i;
//设置LCD
FLL_CTL0 |=XCAP14PF; //设置装载电容
for(i=0;i<10000;i++); //设置延时
//LCD清零
for (i = 0; i < 20; i++)
LCDMEM[i] = 0; //LCD Memory分为20行8列的数组
P5SEL |=(BIT2|BIT3|BIT4); //P5端口与4619相连
P5DIR |=(BIT2|BIT3|BIT4);
LCDACTL = LCDFREQ_128 | LCD4MUX | LCDSON | LCDON; //设置LCD_A Control Register
LCDAPCTL0 = LCDS0 | LCDS4 | LCDS8 | LCDS12 | LCDS16 | LCDS20 | LCDS24; //设置LCD_A Port Control Register 0
LCDAPCTL1 = 0; //设置LCD_A Port Control Register 1(即设置最左边的两个显示的字符0)
}
//LCD显示
void Display(int data1)
{
unsigned x1,x2,x3,x4;
x1=data1%10; //个位
x1=x1/1;
x2=data1%100; //十位
x2=x2/10;
x3=data1%1000; //百位
x3=x3/100;
x4=data1%10000; //千位
x4=x4/1000;
LCDMEM[2] = digit[x1];
}
void Init_SystemClock()
{
SCFQCTL |=BIT0+BIT1+BIT2+BIT3+BIT4;//即时钟模块功能打开,DCO主频为DCOPLUS=0: fDCOCLK = (N + 1) . fcrystal,这里N为默认
//SCFI0 =默认全为0
//SCFI1 =默认全为0
//FLL_CTL0 |=按默认的来处理,后面的初始化LCD和BasicTimer都对这里的匹配电容做了修改。
FLL_CTL1 |= XT2OFF; //SMCLK is on,XT2 is off it is not used for MCLK or SMCLK,select DCO as MCLK source,select DCO as SMCLK source,ACLK 不频率
}
//******************************************************************************
//*****************************BLE程序段****************************************
//******************************************************************************
void Init_BLE()
{
P4DIR |=BIT2;//4.2IO口连接En端,En端置低,模块开始广播,置高,模块休眠
P4SEL &=~BIT2;
P3DIR |=BIT0;//3.0IO口连接BRTS端口
P3SEL &=~BIT0;
P3DIR &=~BIT7;//3.7IO口连接BCTS端口,
BRTS_1;
}
void Init_Usci_A0() //异步串口通信UART模块初始化
{
//UCA0CTL0 |= UCPEN; // 8 位字符,奇校验
UCA0CTL1 &= ~UCSWRST; // 进入操作模式
UCA0CTL1 |= UCSSEL1 ; // 时钟源为SMCLK,1M
UCA0BR0 =0x6D; // 设置拨特率为1M/109=9600
UCA0BR1 =0x00;
UCA0MCTL = UCBRF_2; // 小数部分调整
P2SEL |= BIT4+BIT5; // P2.4,P2.5 用作USART0 TXD/RXD
IE2 |= UCA0RXIE; // 使能USART0接收中断
}
void Ble_AT(char *data,char len)
{
En_0;
BRTS_0;
__delay_cycles(10485/5);
for(char i=0;i<len;i++)
{
UCA0TXBUF=*(data++);
while ((IFG2 & UCA0TXIFG)==0);
}
__delay_cycles(10485/2);
BRTS_1;
}
void Ble_Data(char *data,char len)
{
En_0;
BRTS_0;
__delay_cycles(10485/5);
for(char i=0;i<len;i++)
{
UCA0TXBUF=*(data++);
while ((IFG2 & UCA0TXIFG)==0);
}
__delay_cycles(10485/2);
BRTS_1;
}
//******************************************************************************
//*****************************以上为BLE程序段**********************************
//******************************************************************************
void main()
{
WDTCTL=WDTPW+WDTHOLD;
Init_SystemClock();
Init_LCD();
Init_BLE();
Init_Usci_A0();
_EINT(); // 使能中断
Ble_AT("TTM:MAC-?",10);
__delay_cycles(1048576);
Ble_AT("TTM:TPL-(+4)",13);
__delay_cycles(1048576);
while(1)
{
Ble_Data("hedongsheng",12);
Ble_data=Bluetooth_data[Count_Data-1];
Display(Ble_data);
__delay_cycles(1048576);
}
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void usart0_recieve()
{
if(Count_Data>99)
{
Count_Data=0;
}
Bluetooth_data[Count_Data++]= UCA0RXBUF; //接收到的数据
while ((IFG2 & UCA0RXIFG)); //判断是否recieve完毕
IFG2 &= ~UCA0RXIFG;
}
一周热门 更多>