Advertisement
Mark2020H

How blink leds correctly using RPI and BCM header files

Mar 20th, 2024 (edited)
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.31 KB | Software | 0 0
  1. /*
  2.  
  3. MD Harrington  London  Kent UK  20-03-2024   Note The Makefile  for this you find at this address https://pastebin.com/tAK7Ni8h
  4.  
  5.  
  6. The correct  methods  of   how  use threads and pointers to functions
  7.  
  8. Demonstrating   how to flash  leds using the RPI  and associated <bcm2835.h>
  9. Header files
  10.  
  11. Note  ***   Not quite what they  have shown you to date  which
  12.  
  13. 1: locks up  
  14. 2: causes  segmentation  issues
  15. 3: Wont read user input  etc
  16.  
  17. 4: Does not clean up correctly or  shuts down the port correctly on the raspberry pi
  18.  
  19. This  took me a good few days to work this out as to why points 1 to 4
  20. would  not work
  21.  
  22. Here  ARE some pointers  into how  which library  they used  i.e  wiringPI AND THEIR EXAMPLES
  23.  
  24. https://youtu.be/mLcZJMCcIBE  Watch all of thee tutorials   carefully  because not one of them
  25. show  you this or explain  why you cannot interrupt that flash  loop  using thier approach
  26.  
  27. https://youtu.be/Gw-LdBWKxmM
  28.  
  29.  
  30.  
  31. Below pin mapping using  gpio readall  explanations   also contained in code
  32.  
  33.  +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
  34.  | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
  35.  +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
  36.  |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
  37.  |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
  38.  |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
  39.  |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT5 | TxD     | 15  | 14  |
  40.  |     |     |      0v |      |   |  9 || 10 | 1 | ALT5 | RxD     | 16  | 15  |
  41.  |  17 |   0 | GPIO. 0 |  OUT | 1 | 11 || 12 | 0 | OUT  | GPIO. 1 | 1   | 18  |
  42.  |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
  43.  |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
  44.  |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
  45.  |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
  46.  |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
  47.  |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
  48.  |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
  49.  |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
  50.  |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
  51.  |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
  52.  |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
  53.  |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
  54.  |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
  55.  |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
  56.  +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
  57.  | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
  58.  +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
  59.  
  60. */
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <bcm2835.h>
  65. #include <pthread.h>
  66.  
  67. #define LED RPI_GPIO_P1_11
  68. #define LED_2 RPI_GPIO_P1_15
  69.  
  70. unsigned int blink = 0;
  71.  
  72. void *flashLED() {
  73.    
  74.    
  75.    
  76.     while (1) {
  77.         if (blink) {
  78.             bcm2835_gpio_write(LED, HIGH);
  79.             bcm2835_gpio_write(LED_2, LOW);
  80.             bcm2835_delay(500);// Delay for 500ms (0.5 seconds)
  81.             bcm2835_gpio_write(LED, LOW);
  82.             bcm2835_gpio_write(LED_2, HIGH);
  83.             bcm2835_delay(500);// Delay for 500ms (0.5 seconds)
  84.         } else {
  85.             bcm2835_gpio_write(LED, LOW);
  86.             bcm2835_gpio_write(LED_2, LOW);
  87.             bcm2835_delay(500); // Delay for 100ms (0.1 seconds)
  88.         }
  89.     }
  90. }
  91.  
  92.  
  93. void cleanup() {
  94.     blink = 0;
  95.     bcm2835_gpio_write(LED_2, LOW);
  96.     bcm2835_gpio_write(LED, LOW);
  97.     bcm2835_close();
  98. }
  99.  
  100. void *menu() {
  101.     int choice;
  102.     do {
  103.         printf("\nChoose an option:\n");
  104.         printf("1: Flash the LED\n");
  105.         printf("2: Stop LED flashing\n");
  106.         printf("3: Exit\n");
  107.         printf("Enter your choice: ");
  108.  
  109.         scanf("%d", &choice);
  110.  
  111.         switch (choice) {
  112.             case 1:
  113.                 printf("You chose to flash the LED.\n");
  114.                 blink = 1; // Stop LED flashing
  115.                 break;
  116.             case 2:
  117.                 printf("You chose to stop LED flashing.\n");
  118.                 blink = 0; // Stop LED flashing
  119.                 break;
  120.             case 3:
  121.                 printf("You chose to exit.\n");
  122.                 cleanup();
  123.                 exit(0);
  124.             default:
  125.                 printf("Invalid choice. Please try again.\n");
  126.         }
  127.     } while (choice != 0);
  128.  
  129.     return NULL;
  130. }
  131.  
  132. int main(void) {
  133.     printf("Raspberry Pi blink\n");
  134.  
  135.     if (!bcm2835_init()) {
  136.         printf("bcm2835_init failed. Are you running as root?\n");
  137.         return 1;
  138.     }
  139.  
  140.     bcm2835_gpio_fsel(LED, BCM2835_GPIO_FSEL_OUTP);
  141.     bcm2835_gpio_fsel(LED_2, BCM2835_GPIO_FSEL_OUTP);
  142.  
  143.     pthread_t led_thread, menu_thread;
  144.     pthread_create(&led_thread, NULL, flashLED, NULL);
  145.     pthread_create(&menu_thread, NULL, menu, NULL);
  146.  
  147.     pthread_join(menu_thread, NULL);
  148.  
  149.  
  150.     return 0;
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement