📄 msp147flashuart.c
字号:
#include <msp430x14x.h>
void write_SegA (unsigned char *); //函数声明
void Send_AT_Command(unsigned char * ),Send_One_Byte(unsigned char );
void delay(void),init(void);
unsigned char recbuff[1800]={0x31,0x32};
unsigned int j,sendlong,total_sendlong ;
char *Flash_ptr;
unsigned char * p;
void main(void)
{ init();
j=0;
sendlong=0;
total_sendlong=0;
while(1)
{ if(j>3000)
{j=0;
}
j++;
if(sendlong>=1024)
{//_DINT();
total_sendlong = 1024;
p=&recbuff[0];
Flash_ptr = (char *) 0x0d000; // 初始化指针
write_SegA(p); // 写段A
p=(char *)0x0d000;
Send_AT_Command(p);
p=&recbuff[512];
Flash_ptr = (char *) 0x0d200; // 初始化指针
write_SegA(p); // 写段B
p=(char *)0x0d200;
Send_AT_Command(p);
//_EINT();
sendlong=0;
}
// if((j>200)&&(total_sendlong>0)) //2000
/*if((j>2000)&&(sendlong>1)) //2000
{ //_DINT();
if(total_sendlong>1024)
{
total_sendlong =1024+ sendlong;
sendlong=0;
p=(char *)0x0d000;
Send_AT_Command(p);
//p=(char *)0x0d100;
//Send_AT_Command(p);
if((total_sendlong>1024)&&(total_sendlong<1536))
{for(j=1024;j<total_sendlong;j++)
{ Send_One_Byte(recbuff[j]);
}
}
else
{ for(j=1024;j<1536;j++)
{ Send_One_Byte(recbuff[j]);
}
for(j=0;j<sendlong;j++)
{ Send_One_Byte(recbuff[j]);
}
}
}
else
{for(j=0;j<sendlong;j++)
{ Send_One_Byte(recbuff[j]);
}
sendlong=0;
}
// for(j=0;j<sendlong;j++)
// { Send_One_Byte(recbuff[j]);
// }
// sendlong=0;
total_sendlong=0;
sendlong=0;
Flash_ptr = (char *) 0x0d000; // 初始化指针
j=0;
}
*/
//_EINT();
// _BIS_SR(LPM3_bits) ; /* Enter Low Power Mode 3 */
}
}
interrupt[UART0RX_VECTOR] void usart0_rx (void)
{ // while ((IFG1 & UTXIFG0) == 0); // USART0 TX buffer ready?
// TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0
recbuff[sendlong++]=RXBUF0;
j=0;
if(sendlong>1536)
{sendlong=0;
}
// _BIC_SR_IRQ(LPM3_bits) ;/* Exit Low Power Mode 3 */
}
interrupt[UART1RX_VECTOR] void usart1_rx (void)
{ // while ((IFG2 & UTXIFG1) == 0); // USART1 TX buffer ready?
// TXBUF1 = RXBUF1; // RXBUF1 to TXBUF1
recbuff[sendlong++]=RXBUF1;
// TXBUF1 = recbuff[i];
// *Flash_ptr++=recbuff[i];
j=0;
// _BIC_SR_IRQ(LPM3_bits) ;/* Exit Low Power Mode 3 */
}
void init(void)
{unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT停止看门狗
BCSCTL1 |= XTS; // ACLK = LFXT1 = HF XTAL
P1DIR |= 0xff; // Set P1.0 to output direction
P1OUT=0xff;
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
}
while ((IFG1 & OFIFG) == OFIFG); // OSCFault flag still set?
//*************************************************************0
BCSCTL2 |= SELM1+SELM0; // MCLK = LFXT1 (safe)
UCTL0 = CHAR; // 8-bit character
UTCTL0 = SSEL0; // UCLK = ACLK
//UBR00 = 0x74; // 3.58Mhz/9600 - 372
//UBR10 = 0x01; //
// UBR00 = 0x00; // 1.8432MMhz/1200 - 96
//UBR00 = 0x60; // 1.8432MMhz/19200 - 96
UBR00 = 0x20; // 1.8432MMhz/57600 - 32
// UBR10 = 0x06; // 1200
UBR10 = 0x00; //19200//57600
UMCTL0 = 0x00; // no modulation
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
IE1 |= URXIE0; // Enable USART0 RX interrupt
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
P3DIR |= 0x10; // P3.4 output direction
//*********************************************************1
UCTL1 = CHAR; // 8-bit character
UTCTL1 = SSEL0; // UCLK = ACLK
UBR01 = 0x60; // 1.8432MMhz/19200 - 96
//UBR01 = 0x20; // 1.8432MMhz/57600 - 32
UBR11 = 0x00; //
UMCTL1 = 0x00; // no modulation
ME2 |= UTXE1 + URXE1; // Enable USART0 TXD/RXD
// IE2 |= URXIE1; // Enable USART1 RX interrupt
P3SEL |= 0xc0; // P3.6,7 = USART1 TXD/RXD
P3DIR |= 0x40; // P3.6 output direction
_EINT(); // Enable interrupts
// FCTL2 = FWKEY + FSSEL0 + FN0; // 定义FLASH时序发生器输入时钟
}
void Send_AT_Command(unsigned char *p1)
{ unsigned int ii=0;
do
{ Send_One_Byte(*p1);
p1++;
ii++;
}while(ii<512);//*p1!='\0');
// Send_One_Byte(0x0d);
}
void Send_One_Byte(unsigned char SendByte)
{//while ((IFG2 & UTXIFG1) == 0); // USART1 TX buffer ready?
// TXBUF1 = SendByte;
while ((IFG1 & UTXIFG0) == 0); // USART0 TX buffer ready?
TXBUF0 = SendByte; // to TXBUF0
}
void write_SegA (unsigned char *p2)
{ //char *Flash_ptr;
unsigned int ii=0;
//Flash_ptr = (char *) 0x0f000; // 初始化指针
FCTL1 = FWKEY + ERASE; // 使能擦除操作
FCTL3 = FWKEY; // FLASH解锁
*Flash_ptr = 0; // 空写,启动擦除
FCTL1 = FWKEY + WRT; // 允许写操作
//******* ************first512
// while(*p!='\0')
for (ii=0; ii<512; ii++)
{ *Flash_ptr++ = *p2++; // 循环写FLASH 256字节(一个信息段的长度)
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK; //写完了,锁定
/****************second 512
Flash_ptr = (char *) 0x0f200; // 初始化指针
FCTL1 = FWKEY + ERASE; // 使能擦除操作
FCTL3 = FWKEY; // FLASH解锁
*Flash_ptr = 0; // 空写,启动擦除
FCTL1 = FWKEY + WRT; // 允许写操作
for (; i<longth; i++)
{ *Flash_ptr++ = *p++; // 循环写FLASH 128字节(一个信息段的长度)
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK; //写完了,锁定
*/
}
void delay(void)
{ unsigned int i;
i=0;
do (i++);
while (i != 10);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -