Quantcast
Channel: MSP low-power microcontrollers
Viewing all articles
Browse latest Browse all 63677

Forum Post: MSP430G2x interfacing with ultarsonic sensor and dot matrix led display

$
0
0
Hello, I am working on with MSP430G2553 interfacing with ultrasonic sensor and displaying the distance using 8x8 dot matrix led display. I done the program but I not connect ultrasonic sensor it starts to generate random values in led display unit . If any one work with the unit or any one faced this issue please help me.. Here my program #include #include #include #define Rx_ECHO BIT0 #define Tx_TRIGGER BIT1 #define DATA_PIN BIT2 // data for max7219 #define LOAD_PIN1 BIT3 // clock for max7219 #define LOAD_PIN2 BIT4 // clock for max7219 #define CLOCK_PIN BIT5 // load for max7219 void max7219_init(void); void max7219(unsigned char , unsigned char); void max7219_led2(unsigned char , unsigned char); void shiftout(unsigned char); void update_display(unsigned int); void calc(); void serialPrint(unsigned char * str); void serialPrintInt(unsigned int temp); volatile unsigned int measure; volatile unsigned int up=0; unsigned char buf[6]; unsigned int a=0; unsigned int sample[20]; unsigned int load; unsigned int avg1=0; unsigned int temp1[20]; unsigned int second=0,third=0; const char disp1[10][8]={ {0X00,0X7E,0X81,0X81,0X81,0X7E,0X00,0X00},//0 {0x00,0x84,0x82,0xFF,0XFF,0X80,0X80,0X00},//1 {0X00,0X00,0X72,0X52,0X52,0X52,0X5E,0X00},//2 {0X00,0X49,0X49,0X49,0X49,0X7F,0X00,0X00},//3 {0X00,0X18,0X14,0X12,0X11,0X7F,0X10,0X00},//4 {0X00,0X4F,0X49,0X49,0X49,0X79,0X00,0X00},//5 {0X00,0X7F,0X49,0X49,0X49,0X79,0X00,0X00},//6 {0X00,0X01,0X01,0X01,0X01,0X7F,0X00,0X00},//7 {0X00,0X7F,0X49,0X49,0X49,0X4F,0X00,0X00},//8 {0X00,0X8F,0X89,0X89,0X89,0XFF,0X00,0X00},//9 }; int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT if (CALBC1_1MHZ==0xFF) // If calibration constant erased { while(1); // do not load, trap CPU!! } DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; /*GPIO FOR ULTRASONIC SENSOR */ P2DIR = Tx_TRIGGER; P2OUT &= ~(Tx_TRIGGER); P2SEL = Rx_ECHO; /* TIMER CONFIGURATION*/ TA1CCTL0 |= CM_3 + CCIS_0 + CAP + CCIE; //rising & falling edge + synchronous + P2.0 (CCI1A) + capture + capture/compare interrupt enable TA1CTL |= TASSEL_2 + MC_2 + ID_0; //select smclock for timer a source + make ta1ccr0 count continously up + no division /* UART CONFIGURATION*/ P1SEL = BIT1 + BIT2 ; //P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2 ; //P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_2; //SMCLK UCA0BR0 = 104; //1MHz 9600 UCA0BR1 = 0; //1MHz 9600 UCA0MCTL = UCBRS0; //Modulation UCBRSx = 1 UCA0CTL1 &= ~UCSWRST; //**Initialize USCI state machine** /****************MAX7219 GPIO init section***************/ P2DIR |= (DATA_PIN | LOAD_PIN1 | CLOCK_PIN |LOAD_PIN2 ); P2OUT |= DATA_PIN; P2OUT |= LOAD_PIN1; P2OUT |= LOAD_PIN2; P2OUT &= ~CLOCK_PIN; max7219_init(); /*******************************************************/ CCTL0 = CCIE; // CCR0 interrupt enabled CCR0 = 60000; TACTL = TASSEL_2 + MC_1; // SMCLK, upmode __bis_SR_register(GIE); while (1){ if(up==0){ if((measure>25) && (measure!=0) ){ sample[a]=measure; a++; if(a==20){ calc(); a=0;} } } else { memset(sample, 0 , sizeof(sample)); calc(); } } } /******************************************************************************/ #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A (void) //tirgger for the sensor { P2OUT |= Tx_TRIGGER; up = 1; P2OUT &= ~Tx_TRIGGER; TA0CTL &= ~TAIFG; } unsigned int measure_1 = 0; unsigned int measure_2 = 0; //Timer1_A Capture #pragma vector=TIMER1_A0_VECTOR __interrupt void Timer1A0(void) // measuring the echo { if(up){ //rising edge measure_1=TA1CCR0; } else{ //falling edge measure_2=TA1CCR0; measure=(measure_2-measure_1)/58.2; } up=!up; TA1CTL &= ~TAIFG; } void update_display(unsigned int dist) { unsigned int i=0; second=0; third=0; second=(dist%100)/10; third=(dist%1000)/100; for(i=1;i =0) { sample[j+1] = sample[j]; --j; } sample[j+1]=temp; } k=0; /* finding tolerance of 20%*/ for(b=0;b =0) && (diff 0) { serialPrintInt(avg1); update_display(avg1); } else{ avg1=0; update_display(avg1); } avg1=0; } /////////***************UART TRANSMISSION TO PC*****************////////////////////// void serialPrint(unsigned char *str) { unsigned int i; for (i=0;str[i] != '#'; ++i){ while (!(IFG2 & UCA0TXIFG)); UCA0TXBUF = str[i]; } } /*ITOA CONVERTION */ void serialPrintInt(unsigned int temp) { unsigned int n=0; unsigned int num=0,rem=0; unsigned int len=0,j; n = temp; num=temp; while (n != 0){ len++; n /= 10; } for (j = 0; j < len; j++){ rem = num % 10; num = num / 10; buf[len - (j + 1)] = rem + '0'; } buf[len] = ','; buf[len+1]='#'; serialPrint( buf ); memset(buf, '\0' , sizeof(buf)); } If any one had a better algorithm please suggest me. This my first project with msp430. Here in this program I continuously take 20 distance as sample the to avoid glitch value. Regards praveen

Viewing all articles
Browse latest Browse all 63677

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>