element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RoadTests & Reviews
  • Products
  • More
RoadTests & Reviews
RoadTest Forum some c code for raspberry pi
  • Blogs
  • RoadTest Forum
  • Documents
  • RoadTests
  • Reviews
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join RoadTests & Reviews to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 0 replies
  • Subscribers 2562 subscribers
  • Views 258 views
  • Users 0 members are here
  • home
  • automation
Related

some c code for raspberry pi

Former Member
Former Member over 12 years ago

hi everyone

if any of u knows c or python caould pls give me some c code to control six relay via gpio through raspberry pi

ill sho wu wht ive got so far underneath

 

#include <stdio.h>

#include <fcntl.h>

#include <sys/mman.h>

 

 

#define IOBASE   0x20000000

 

 

#define GPIO_BASE (IOBASE + 0x200000)

 

 

#define GPFSEL0    *(gpio.addr + 0)

#define GPFSEL1    *(gpio.addr + 1)

#define GPFSEL2    *(gpio.addr + 2)

#define GPFSEL3    *(gpio.addr + 3)

#define GPFSEL4    *(gpio.addr + 4)

#define GPFSEL5    *(gpio.addr + 5)

// Reserved @ word offset 6

#define GPSET0    *(gpio.addr + 7)

#define GPSET1    *(gpio.addr + 8)

// Reserved @ word offset 9

#define GPCLR0    *(gpio.addr + 10)

#define GPCLR1    *(gpio.addr + 11)

// Reserved @ word offset 12

#define GPLEV0    *(gpio.addr + 13)

#define GPLEV1    *(gpio.addr + 14)

 

 

#define BIT_17 (1 << 17)

 

 

#define PAGESIZE 4096

#define BLOCK_SIZE 4096

 

 

struct bcm2835_peripheral {

    unsigned long addr_p;

    int mem_fd;

    void *map;

    volatile unsigned int *addr;

};

 

 

struct bcm2835_peripheral gpio = {GPIO_BASE};

 

 

// Some forward declarations...

int map_peripheral(struct bcm2835_peripheral *p);

void unmap_peripheral(struct bcm2835_peripheral *p);

 

 

int gpio_state = -1;

 

 

////////////////

//  main()

////////////////

int main(int argc, char *argv[]) {

 

 

    if(argc == 2) {

        if(!strcmp(argv[1], "on"))

            gpio_state = 1;

        if(!strcmp(argv[1], "off"))

            gpio_state = 0;

    }

 

 

    if(map_peripheral(&gpio) == -1) {

        printf("Failed to map the physical GPIO registers into the virtual memory space.\n");

        return -1;

    }

 

 

    /* Set GPIO 17 as an output pin */

    GPFSEL1 &= ~(7 << 21); // Mask out bits 23-21 of GPFSEL1 (i.e. force to zero)

    GPFSEL1 |= (1 << 21);  // Set bits 23-21 of GPFSEL1 to binary '001'

 

 

    if(gpio_state == 0)

        GPCLR0 = BIT_17;

    else if(gpio_state == 1)

        GPSET0 = BIT_17;

 

 

    usleep(1);    // Delay to allow any change in state to be reflected in the LEVn, register bit.

 

 

    printf("GPIO 17 is %s\n", (GPLEV0 & BIT_17) ? "high" : "low");

 

 

    unmap_peripheral(&gpio);

 

 

    // Done!

}

 

 

// Exposes the physical address defined in the passed structure using mmap on /dev/mem

int map_peripheral(struct bcm2835_peripheral *p)

{

   // Open /dev/mem

   if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {

      printf("Failed to open /dev/mem, try checking permissions.\n");

      return -1;

   }

 

 

   p->map = mmap(

      NULL,

      BLOCK_SIZE,

      PROT_READ|PROT_WRITE,

      MAP_SHARED,

      p->mem_fd,  // File descriptor to physical memory virtual file '/dev/mem'

      p->addr_p      // Address in physical map that we want this memory block to expose

   );

 

 

   if (p->map == MAP_FAILED) {

        perror("mmap");

        return -1;

   }

 

 

   p->addr = (volatile unsigned int *)p->map;

 

 

   return 0;

}

 

 

void unmap_peripheral(struct bcm2835_peripheral *p) {

 

 

    munmap(p->map, BLOCK_SIZE);

    close(p->mem_fd);

}

 

this is wht ive got to control 1 of the relays

can u pls supply me with some to control another six via gpio 17 18 22 23 24 25

Attachments:
gpio_relay.c.zip
  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube