Part Number: MSP430G2553
Hi everyone.I am begginer at MSP430.;
I would like to bliknk two different leds via pressing two different buttons using IR.
I think transmitter works propperly.But something is wrong with my receiver.
RX:
#include <msp430.h>
void init(void);
void configClock(void);
void configTimer(void);
void startTimer(void);
void stopTimer(void);
void clearTimer(void);
unsigned int readTimer(void);
void listen_IR(void);
unsigned int time_in_low;
unsigned int time_in_high;
int A;
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR = BIT0|BIT6;
P1REN = BIT3;
P1OUT = BIT3;
init();
while(1)
{
listen_IR();
}
}
void listen_IR(void)
{
if((P1IN & BIT3)!=BIT3) { //falling edge
startTimer();}
if((P1IN & BIT3)==BIT3){ //rising edge
stopTimer();
A=readTimer(); //read the timer to detect PWM length
clearTimer();
}
if (A>8000 && A<10000){ //if transmitter sends 9ms PWM
P1OUT^=0x01;
__delay_cycles(500000);
}
if (A>3000 && A<6000){
P1OUT^=0x40; // if transmitter sends 4.5ms PWM
__delay_cycles(500000);
}
}
void init(void)
{
configTimer();
configClock();
}
void configClock(void)
{
//1Mhz
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
}
void configTimer(void)
{
TACCR1 = 20000;
}
void clearTimer(void)
{
TACTL |= TACLR;
}
void stopTimer(void)
{
TACTL = 0;
}
void startTimer(void)
{
TACCTL1 &= ~CCIFG; // clear the time out flag
TACTL = TASSEL_2 + MC_2; // clock source: SMCLK, mode 2: count up to 0xFFFF
}
unsigned int readTimer(void)
{
return TAR;
}
TX:
#include <msp430.h>
void init(void);
void configClock(void);
void configTimer(void);
void startTimer(void);
void stopTimer(void);
void clearTimer(void);
unsigned int readTimer(void);
void listen_IR(void);
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
P1DIR = 0x00;
P1REN= 0xFF;
P2DIR = BIT0;
P2OUT=0x00;
int i;
while(1)
{
if (!(P1IN & 0x08)) {
for(i=0;i<692;i++){ // 38kHz, 9ms %50 dutycycle PWM signal period=26us
P2OUT^=0x01;
__delay_cycles(10);
}
}
if (!(P1IN & 0x01) ){
for(i=0;i<381;i++){ //38kHz, 9ms %50 dutycycle PWM signal, period=26us
P2OUT^=0x01;
__delay_cycles(10);
}
}
}
}