完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include
#define UART_TXD 0x02 // TXD on P1.1 (timer0_A.OUT0) #define UART_RXD 0x04 // RXD on P1.2 (Timer0_A.CCI1A) #define UART_TBIT_DIV_2 (1000000 / (9900 * 2)) #define UART_TBIT (1000000 / 9900) unsigned int txData; // UART internal variable for TX unsigned char rxBuffer; // Received UART character char data[40]=""; unsigned int i=0; unsigned char nc,num; unsigned char alm1=0; unsigned char alm2=0; unsigned char alm3=0; unsigned char det=1; unsigned char status=0; unsigned char sms=0; unsigned char smscount=0; unsigned char bakstatus=0; void TimerA_UART_init(void); void TimerA_UART_tx(unsigned char byte); void TimerA_UART_print(char *string); void senda(); int sendmessage(); void delay(int n); void write(unsigned char *string); void writecmd(unsigned char cmd); void alARM(char n); void initlcd(void); void smsalm(void); void detectinit(void); void main(void) { volatile unsigned int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer DCOCTL = 0x00; // Set DCOCLK to 1MHz BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; P1DIR |= 0x43; // P1.0 output P1DIR &= ~UART_RXD; P1SEL = UART_TXD+UART_RXD; P1OUT=0x00; P1IE |= 0x10; // P1.4 interrupt enabled P1IES |= 0x10; // P1.4 Hi/lo edge P1IFG &= ~0x10; // P1.4 IFG cleared P2SEL &= ~0xC0; P2IE |= 0xC0; P2IES |= 0xC0; P2IFG &= ~0xC0; detectinit(); initlcd(); /*P1OUT = 0x00; // Initialize all GPIO P1SEL = UART_TXD + UART_RXD; // Timer function for TXD/RXD pins P1DIR = 0xFF & ~UART_RXD; // Set all pins but RXD to output P2OUT = 0x00; P2SEL = 0x00; P2DIR = 0xFF;*/ __enable_interrupt(); //smsalm(); for (;;){} } void detectinit(void) { CCTL0 = CCIE; // CCR0 interrupt enabled CCR0 = 0xffff; TACTL = TASSEL_2 + MC_1 + ID_3; // SMCLK, upmode } void smsalm(void) { sms=1; TimerA_UART_init(); // Start Timer_A UART //senda(); __enable_interrupt(); sendmessage(); } void delay(int n) { unsigned int i,j,k; for (i=0;i for (k=0;k<100;k++){;} } int sendmessage() { while ((data[5]!='O')||(data[6]!='K')) { TimerA_UART_print("atr"); //TimerA_UART_print("atr"); delay(30); i=0; } TimerA_UART_print("AT+CMGF=0r"); delay(30); //if ((data[12]!='O')||(data[13]!='K')) //return 2; i=0; TimerA_UART_print("AT+CMGS=43r"); delay(30); //if (data[24]!=0x3e) //return 3; i=0; TimerA_UART_print("0011000D91688147403467f80008A01C6D7768E0"); delay(30); TimerA_UART_print("516C5BD3003553F7697C7740706BFF0C8BF76C42"); delay(30); TimerA_UART_print("655163F4"); delay(30); //if ((data[0]!='t')||(data[1]!='e')||(data[2]!='s')||(data[3]!='t')) //return 4; i=0; senda(); delay(100); i=0; sms=0; detectinit(); return 0; } //------------------------------------------------------------------------------ // Function configures Timer_A for full-duplex UART operation //------------------------------------------------------------------------------ void TimerA_UART_init(void) { TACCTL0 = OUT; // Set TXD Idle as Mark = '1' TACCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int TACTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode } //------------------------------------------------------------------------------ // Outputs one byte using the Timer_A UART //------------------------------------------------------------------------------ void TimerA_UART_tx(unsigned char byte) { while (TACCTL0 & CCIE); // Ensure last char got TX'd TACCR0 = TAR; // Current state of TA counter TACCR0 += UART_TBIT; // One bit time till first bit TACCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU0, Int txData = byte; // Load global variable txData |= 0x100; // Add mark stop bit to TXData txData <<= 1; // Add space start bit } void senda() { TimerA_UART_tx(0x1A); } //------------------------------------------------------------------------------ // Prints a string over using the Timer_A UART //------------------------------------------------------------------------------ void TimerA_UART_print(char *string) { while (*string) { TimerA_UART_tx(*string++); } } void write(unsigned char *string) { unsigned char i=0; while (*string) { data[i++]=0xfa; data[i++]=((*string)&0xf0); data[i++]=(((*string++)<<4)&0xf0); } num=i; nc=0; USICNT = 8; USICTL0 &= ~USISWRST; // USI released for operation USICTL1 |= USIIE; // Counter interrupt, flag remains set } void writecmd(unsigned char cmd) { data[0]=0xF8; data[1]=(cmd&0xf0); data[2]=((cmd<<4)&0xf0); num=3; nc=0; USICNT = 8; USICTL0 &= ~USISWRST; // USI released for operation USICTL1 |= USIIE; // Counter interrupt, flag remains set } void initlcd(void) { USICTL0 |= USIPE7 + USIPE6 + USIPE5 + USIMST + USIOE; // Port, SPI master USICKCTL = USIDIV_7 + USISSEL_2; // /16 SMCLK USICNT = 8; // init-load counter num=0; nc=0; _EINT(); writecmd(0x30); delay(1); writecmd(0x01); delay(1); writecmd(0x80); delay(1); write(" 火灾报警系统"); delay(1); writecmd(0x90); delay(1); write(" 一号正常"); delay(1); writecmd(0x88); delay(1); write(" 二号正常"); delay(1); writecmd(0x98); delay(1); write(" 三号正常"); delay(1); writecmd(0x0c); delay(1); } void alarm(char n) { _EINT(); nc=0; if (n==1) { writecmd(0x90); delay(1); write(" 一号着火"); delay(1); } if (n==2) { writecmd(0x98); delay(1); write(" 三号着火"); delay(1); } if (n==3) { writecmd(0x88); delay(1); write(" 二号着火"); delay(1); } writecmd(0x0c); delay(1); } // USI interrupt service routine #pragma vector=USI_VECTOR __interrupt void universal_serial_interface(void) { if (0x10 & USISRL) P1OUT |= 0x01; else P1OUT &= ~0x01; if (nc USISRL = data[nc]; nc++; } else { USICTL1 &= ~USIIE; // Counter interrupt, flag remains set USICTL0 |= USISWRST; } //USISRL=0x35; USICNT = 8; // re-load counter } //------------------------------------------------------------------------------ // Timer_A UART - Transmit Interrupt Handler //------------------------------------------------------------------------------ #pragma vector = TIMERA0_VECTOR __interrupt void Timer_A0_ISR(void) { static unsigned char txBitCnt = 10; if (!sms) { if (alm1++==6) { P1OUT|=0x40; det=0; if (!(status&0x10)) { status|=0x10; alarm(1); } } if (alm2++==6) { P1OUT|=0x40; det=0; if (!(status&0x20)) { status|=0x20; alarm(2); } } if (alm3++==6) { P1OUT|=0x40; det=0; if (!(status&0x40)) { status|=0x40; alarm(3); } } if (smscount++==10) { smscount=0; if (bakstatus!=status) { bakstatus=status; smsalm(); } } } if (sms) { TACCR0 += UART_TBIT; // Add Offset to CCRx if (txBitCnt == 0) { // All bits TXed? TACCTL0 &= ~CCIE; // All bits TXed, disable interrupt txBitCnt = 10; // Re-load bit counter } else { if (txData & 0x01) { TACCTL0 &= ~OUTMOD2; // TX Mark '1' } else { TACCTL0 |= OUTMOD2; // TX Space '0' } txData >>= 1; txBitCnt--; } } } //------------------------------------------------------------------------------ // Timer_A UART - Receive Interrupt Handler //------------------------------------------------------------------------------ #pragma vector = TIMERA1_VECTOR __interrupt void Timer_A1_ISR(void) { static unsigned char rxBitCnt = 8; static unsigned char rxData = 0; switch (__even_in_range(TAIV, TAIV_TAIFG)) { // Use calculated branching case TAIV_TACCR1: // TACCR1 CCIFG - UART RX TACCR1 += UART_TBIT; // Add Offset to CCRx if (TACCTL1 & CAP) { // Capture mode = start bit edge TACCTL1 &= ~CAP; // Switch capture to compare mode TACCR1 += UART_TBIT_DIV_2; // Point CCRx to middle of D0 } else { rxData >>= 1; if (TACCTL1 & SCCI) { // Get bit waiting in receive latch rxData |= 0x80; } rxBitCnt--; if (rxBitCnt == 0) { // All bits RXed? rxBuffer = rxData; // Store in global variable rxBitCnt = 8; // Re-load bit counter TACCTL1 |= CAP; // Switch compare to capture mode data[i]=rxBuffer; i++; //__bic_SR_register_on_exit(LPM0_bits); // Clear LPM0 bits from 0(SR) } } break; } } //------------------------------------------------------------------------------ /*// Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) { if ((alm1++==6)||(alm2++==6)||(alm3++==6)) { P1OUT|=0x40; det=0; alarm(); } }*/ // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { if (P1IFG&0x10) { alm1=0; P1OUT ^= 0x01; // P1.0 = toggle P1IFG &= ~0x10; // P1.4 IFG cleared } } #pragma vector=PORT2_VECTOR __interrupt void Port_2(void) { if (P2IFG&0x40) { P1OUT ^= 0x01; alm2=0; P2IFG &= ~0x40; } if (P2IFG&0x80) { P1OUT ^= 0x01; alm3=0; P2IFG &= ~0x80; } } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
3032个成员聚集在这个小组
加入小组2928 浏览 1 评论
MSP430FR5994 使用库函数 定时器触发AD问题请教
3601 浏览 2 评论
请问怎么把下面51单片机的代码改成msp430 g2 pocket的代码,还有改下时间变成30秒
2341 浏览 1 评论
4804 浏览 1 评论
2573 浏览 1 评论
1328浏览 3评论
MSP430FR5994 使用库函数 定时器触发AD问题请教
3601浏览 2评论
2928浏览 1评论
1510浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 18:45 , Processed in 1.130159 second(s), Total 80, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号