TA的每日心情 | 无聊 2016-12-23 14:05 |
---|
签到天数: 7 天 连续签到: 1 天 [LV.3]偶尔看看II
|
#include "msp430f5309.h"
#include "Sys_clk/sys_clk.h"
unsigned char j;
unsigned char irda_receive_data;
void irda_gpio_init(void)
{
//发射引脚
P1DIR |= BIT5;
P1SEL &= ~BIT5;
P1OUT |= BIT5;
P1DS |= BIT5;
//接受引脚
P1SEL &= ~BIT6;
P1DIR &= ~BIT6;
P1IE |= BIT6;
P1IES |= BIT6;
}
//产生38KHZ的高电平
void timer_a1_init(void)
{
TA1CTL |= TASSEL__SMCLK + MC__STOP + TACLR + ID_2 ;
//TA1CCTL0 |= CCIE;
//TA1CCR0 = 0;
}
void timer_a2_init(void)
{
TA2CTL |= TASSEL__SMCLK + MC__STOP + TACLR + ID_2 ;
//TA2CCTL0 |= CCIE;
//TA2CCR0 = 0;
}
void irda_send_char(unsigned char irda_send_data)
{
unsigned char i;
//9ms的高电平
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
TA1CCR0 = 52;
TA2CTL |= MC__UP;
TA2CCTL0 |= CCIE;
TA2CCR0 = 36000;
j = 1;
while(j);
j = 1;
TA1CTL |= MC__STOP;
TA1CCTL0 &= ~CCIE;
//4ms的低电平
TA2CCR0 = 16000;
P1OUT |= BIT5;
j = 1;
while(j);
j = 1;
for(i=0;i<8;i++)
{
//0.5ms的高电平
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
//TA1CCR0 = 52;
TA2CCR0 = 2000;
while(j);
j = 1;
TA1CTL |= MC__STOP;
TA1CCTL0 &= ~CCIE;
P1OUT |= BIT5;
if(irda_send_data&0x80)
{
//TA2CTL |= MC__UP;
TA2CCR0 = 6000;
while(j);
j = 1;
}
else
{
//TA2CTL |= MC__UP;
TA2CCR0 = 2000;
while(j);
j = 1;
}
irda_send_data <<= 1;
}
TA2CCTL0 &= ~CCIE;
TA2CTL |= MC__STOP;
//结束标志
//高电平
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
TA2CTL |= MC__UP;
TA2CCTL0 |= CCIE;
TA2CCR0 = 10000;
//TA2CCR0 = 5000;
j = 1;
while(j);
j = 1;
TA1CTL |= MC__STOP;
TA1CCTL0 &= ~CCIE;
TA2CCTL0 &= ~CCIE;
TA2CTL |= MC__STOP + TACLR;
P1OUT |= BIT5;
}
void irda_send_char_test(unsigned char irda_send_data)
{
unsigned char i;
j = 1;
//9ms的高电平
P1OUT &= ~BIT5;
TA2CTL |= MC__UP;
TA2CCTL0 |= CCIE;
TA2CCR0 = 36000;
while(j);
j = 1;
//4ms的低电平
TA2CCR0 = 16000;
P1OUT |= BIT5;
while(j);
j = 1;
for(i=0;i<8;i++)
{
//0.5ms的高电平
P1OUT &= ~BIT5;
//TA1CCR0 = 52;
TA2CCR0 = 2000;
while(j);
j = 1;
P1OUT |= BIT5;
if(irda_send_data&0x80)
{
//TA2CTL |= MC__UP;
TA2CCR0 = 6000;
while(j);
j = 1;
}
else
{
//TA2CTL |= MC__UP;
TA2CCR0 = 2000;
while(j);
j = 1;
}
irda_send_data <<= 1;
}
TA2CCTL0 &= ~CCIE;
TA2CTL |= MC__STOP;
//结束标志
//9ms的高电平
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
TA2CTL |= MC__UP;
TA2CCTL0 |= CCIE;
TA2CCR0 = 10000;
j = 1;
while(j);
j = 1;
TA1CTL |= MC__STOP;
TA1CCTL0 &= ~CCIE;
TA2CCTL0 &= ~CCIE;
TA2CTL |= MC__STOP + TACLR;
P1OUT |= BIT5;
}
void USCIA1_ByteSend(unsigned char SendData)
{
while (!(UCA1IFG&UCTXIFG)); // USCI_A3 TX buffer ready?
UCA1TXBUF = SendData; // TX -> RXed character
}
void irda_receive(void)
{
//检测先导码
unsigned int low_v_time,high_v_time,i;
P1IE &= ~BIT6;
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
TA1CTL |= TACLR;
TA1CCR0 = 50000;
while(!(P1IN&BIT6))
{
if(TA1R > 49000)
break;
}
low_v_time = TA1R;
TA1CTL |= MC__STOP;
TA1CCTL0 &= ~CCIE;
TA1CTL |= TACLR;
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
while((P1IN&BIT6))
{
if(TA1R > 49000)
break;
}
high_v_time = TA1R;
TA1CTL |= MC__STOP;
TA1CCTL0 &= ~CCIE;
//TA1CTL |= TACLR;
if((high_v_time>10000)&&(high_v_time<20000)&&(low_v_time<40000)&&(low_v_time>25000))
{
for(i=0;i<8;i++)
{
irda_receive_data <<= 1;
while(!(P1IN&BIT6))
{
if(TA1R > 49000)
break;
}
TA1CTL |= TACLR;
TA1CTL |= MC__UP;
TA1CCTL0 |= CCIE;
while((P1IN&BIT6))
{
if(TA1R > 49000)
break;
}
high_v_time = TA1R;
if(high_v_time>4000)
{
irda_receive_data |= 0x01;
}
}
USCIA1_ByteSend(irda_receive_data);
}
P1IE |= BIT6;
}
void irda_send_string(unsigned char *ptr,unsigned char length)
{
unsigned int k;
while(length>0)
{
irda_send_char(*ptr);
ptr++;
length--;
k = 10000;
while(k--);
}
}
void uart_init(void)
{
P4SEL |= BIT4 | BIT5; // P3.3,4 = USCI_A0 TXD/RXD
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL_2; // SMCLK
//UCA1BR0 = 18;
UCA1BR0 = 139; // 1MHz 115200 (see User's Guide)
UCA1BR1 = 0; // 1MHz 115200
UCA1MCTL |= UCBRS_1 | UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
//UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
int k;
unsigned char irda_sen_string[11] = "12345678abc";
WDTCTL = WDTPW + WDTHOLD;
//P1DIR |= BIT0;
set_sys_clk();
irda_gpio_init();
uart_init();
USCIA1_ByteSend('a');
timer_a1_init();
timer_a2_init();
__bis_SR_register(GIE);
while(1)
{
// while(!(UCA1IFG&UCRXIFG));
// P1IE &= ~BIT6;
// irda_sen_string[i] = UCA1RXBUF;
// uart_rx_data = UCA1RXBUF;
// irda_send_char(UCA1RXBUF);
// while(1)
// {
//irda_send_char(uart_rx_data);
// irda_send_char(0x32);
/*
//按键发射
while(P1IN&BIT0);
//P1IE &= ~BIT6;
irda_send_string((unsigned char *)irda_sen_string,11);
k = 10000;
while(k--);
//P1IE |= BIT6;
*/
}
}
#pragma vector = TIMER1_A0_VECTOR
__interrupt void TIMERA1_A0_INT()
{
P1OUT ^= BIT5;
}
#pragma vector = TIMER2_A0_VECTOR
__interrupt void TIMERA2_A0_INT()
{
j = 0;
}
#pragma vector = PORT1_VECTOR
__interrupt void PORT1_INT()
{
irda_receive();
}
|
|