main.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * main.c
  3. *
  4. * Copyright 2024 dh33ex <dh33ex@riseup.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301, USA or visit <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. */
  23. #include <msp430g2553.h>
  24. #include "hc-sr04.h"
  25. int main(void) {
  26. WDTCTL = WDTPW | WDTHOLD;
  27. /* use 1 MHz */
  28. if (CALBC1_1MHZ != 0xFF) {
  29. DCOCTL = 0;
  30. BCSCTL1 = CALBC1_1MHZ;
  31. DCOCTL = CALDCO_1MHZ;
  32. }
  33. P1DIR |= BIT0;
  34. P1OUT &= ~BIT0;
  35. HC_SR04_init(BIT1, BIT2);
  36. while (1) {
  37. __delay_cycles(10000);
  38. float distance = HC_SR04_mesure(BIT1, BIT2);
  39. if (distance > 20) {
  40. P1OUT |= BIT0;
  41. } else {
  42. P1OUT &= ~BIT0;
  43. }
  44. }
  45. return 0;
  46. }