Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ECHO BIT1 //configure Pins
- #define TRIG BIT2
- #define SER BIT0
- #define RCLK BIT1
- #define SRCLK BIT2
- #include <msp430g2553.h> // include necessary templates
- #include <intrinsics.h>
- #include <stdint.h>
- volatile unsigned int distance_in_cm=0; // set up intgers for measuring
- volatile unsigned int start_time;
- volatile unsigned int total_time;
- volatile unsigned int up=0;
- int y;
- char array[8][8] ={ // set up matrix
- {1,0,0,0,0,0,0,0},
- {1,1,0,0,0,0,0,0},
- {1,1,1,0,0,0,0,0},
- {1,1,1,1,0,0,0,0},
- {1,1,1,1,1,0,0,0},
- {1,1,1,1,1,1,0,0},
- {1,1,1,1,1,1,1,0},
- {1,1,1,1,1,1,1,1}
- };
- void init (void){
- WDTCTL = WDTPW | WDTHOLD; //kill watchdog timer
- TACCTL0 |= CM_3 + SCS + CAP + CCIE + CCIS_0; // set Timer A0 in capture mode
- TA0CTL = TASSEL_2 + ID_0 + MC_2; // configure timer A0
- P1DIR = TRIG; // set trigger as output
- P1SEL = ECHO; // set echo as input (capture mode)
- P2OUT &= ~(SER | RCLK | SRCLK); // preset SER RCLK and SRCLK of the 74HC595 off
- P2DIR |= SER | RCLK | SRCLK; // set SER RCLK and SRCLK of the 74HC595 as output
- TA1CTL = TASSEL_2 | ID_3 | MC_1 | TACLR; // configure timer A1
- TA1CCR0 = 100;
- int i;
- for(i=0; i<=16; i++){ // clear bit register
- while ((TA1CTL & TAIFG) == 0){
- }
- TACTL &= ~TAIFG;
- P2OUT ^= SRCLK;
- }
- }
- //--------------------------------------------------------
- void Delay (long ms){ // Delay
- int i;
- for (i=0; i<=ms;i++)
- {
- __delay_cycles(1000);
- }
- }
- //-------------------------------------------------------
- void ClockCycle (void){ // clockcyle void for the 74HC595
- int i;
- for (i=0;i<=1;i++){
- while ((TA1CTL & TAIFG) ==0 ){
- }
- TA1CTL &= ~TAIFG;
- P2OUT ^= SRCLK;
- }
- }
- //-------------------------------------------------------
- void Show (void) // show void for the 74HC595
- {
- P2OUT |= RCLK;
- P2OUT &= ~RCLK;
- }
- //-------------------------------------------------------
- void Set (void){ // Set high void for the 74HC595
- P2OUT |= SER;
- ClockCycle(); // always one clockcycle high
- P2OUT &= ~SER;
- }
- //-------------------------------------------------------
- void SetPattern (char arr[8][8]) // void that sets pattern depending on chosen row of matrix
- {
- int x;
- for (x=0; x<=7; x++){ // checks al coloms of matrix
- if (arr[y][x] != 0){ // if not zero, then set, else dont set (clockcycle)
- Set();
- }
- else {
- ClockCycle();
- }
- }
- Show(); // show the setted array
- Delay(5); // wait five milliseconds
- }
- //-------------------------------------------------------
- void main(void) {
- init();
- __enable_interrupt(); // enabling interrupts
- while (1) // do this forever
- {
- up = 1; // when pulse is send, up =1 (high edge), TACCR0 detects rising and falling edges
- P1OUT |= TRIG; // sending pulses for the HC-SR04
- _delay_cycles(20);
- P1OUT &= ~TRIG;
- _delay_cycles(60000); // wait long enough for the ISR to kick in
- }
- }
- //------------------------------------------------------
- #pragma vector=TIMER0_A0_VECTOR
- __interrupt void TIMERA0_ISR (void){
- if (up){
- start_time = TACCR0; // check for high edge, if so, TACCR0 is the start time
- }
- else{
- total_time = TACCR0 - start_time; // when up = 0, your time the pulse went and came back is TACCR0- start time
- distance_in_cm = total_time/58; // distance is the time divided by 58
- if (distance_in_cm > 32 ){
- y=0;
- }
- else if (distance_in_cm < 32 && distance_in_cm > 25){ // checking distance, depending on what the distance is, it
- y=1; // configures a diffrent row of the matrix
- }
- else if (distance_in_cm < 25 && distance_in_cm > 19){
- y=2;
- }
- else if (distance_in_cm < 19 && distance_in_cm > 13){
- y=3;
- }
- else if (distance_in_cm < 13 && distance_in_cm > 9){
- y=4;
- }
- else if (distance_in_cm < 9 && distance_in_cm > 6){
- y=5;
- }
- else if (distance_in_cm < 6 && distance_in_cm > 3){
- y=6;
- }
- else if (distance_in_cm < 3 ){
- y=7;
- }
- SetPattern (array);
- }
- up=!up; // after this, up needs to be opposite as it was
- TA0CTL &= ~TAIFG; // clear timer flag
- TACCTL0 &= ~CCIFG; // clear CCI flag
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement