HERE IS MY C PROGRAMMING ASSIGNMENT
CNET232 Week 1 Assignments TECH153 L05A – GPIO Overview While the Raspberry Pi is not specifically designed to be a microcontroller, it is based upon the most dominant microcontroller architecture today, the ARM. This is a family of microcontrollers manufactured under licence by many companies, and most often implemented as a System-On-a-Chip (SOC). In addition to the traditional logic and memory controlling circuity, a modern microcontroller has many other input/output features, which in the past might have required an external, dedicated integrated circuit. On the RPI, the ARM microcontroller also makes available ‘peripheral functions’ such as timers, general purpose input/output pins (GPIO), communications functions such as I2C, SPI, UART (Universal Asynchronous Receiver Transmitter), PWM (Pulse Width Modulation) etc. These functions are controlled by reading and writing individual bits at fixed memory locations. C allows changes to individual bits in memory to control these peripherals. We typically call these fixed areas of memory, used to control the hardware, peripheral registers. This is as opposed to the other more general areas of memory, the system’s RAM (Random Access Memory), and ROM (Read Only Memory), and the fixed memory locations used by the computer’s arithmetic and logic unit (ALU) to execute the machine language, which are also known as registers. Our version of the RPI, has a 40 pin connector that interfaces to various elements of this SOC including a number of General Purpose Input/Output pins (GPIO), that we may set with software to either a high or a low state. In this lab we are going to control a bi-directional light emitting diode (LED), with two of these pins. Depending on how we control these pins, the LED will emit different colours. Note that there are significant differences in the labels/pins found for this connector, as compared to the actual pins on the microcontroller. Keep this in mind if you are doing any research by yourself with technical documentation for the ARM microcontroller, as opposed to this RPI header pin, and the pin numbers or labels used by different supporting libraries. We could attach to these pins directly, however we have a custom hardware interface board, the Humber Sensor Hat (HSH), that has a bidirectional LED attached to GPIO pins 17 and 18 (GEN0, GEN1). The HSH also has several devices that are accessed with a serial protocol I2C. These include a real time clock (RTC), a pin extender (MP23017), a 4 channel analog-to-digital converter (ADC) plus 1 channel digital-to-analog converter (DAC) chip (PCF8591), and a temperature, relative humidity, and pressure sensor chip (BME280). While we could learn how to access these registers directly, there are a number of freely available C functions that others have written to simplify this process. Most notably in the Raspberry Pi universe are the wiringPi libraries, which have been pre-installed on your RPI software image. As with other C software, you access these functions by including a header file ( wiringPi.h) in your c source code files, and make a reference to the corresponding machine code library files in your linker commands in the makefile. Task 1 – Program Organization As with previous labs, this program will be distributed among a number of files. Your first task is to set up the development file structure and to create a makefile for compiling and linking the program. 1. The main function will be placed in a file called lab05.c 2. The SetLED function will be put in ghutils.h/c 3. There are changes as well to GhControllerInit, which must call the wiringPiSetup() function before it returns. The SetLED function will not work unless this is done. 4. The makefile needs modification to build these new functions. Notice the new entry in the linker line. lab05: lab05.o ghutils.o ghcontrol.o gcc -g -o lab05 lab05.o ghutils.o ghcontrol.o -lwiringPi lab05.o: lab05.c ghutils.h ghcontrol.h gcc -g -c lab05.c ghutils.o: ghutils.c ghutils.h ghcontrol.h gcc -g -c ghutils.c ghcontrol.o: ghcontrol.c ghcontrol.h gcc -g -c ghcontrol.c Task 2 – Turn on an LED The function prototypes is as follows: void SetLED(int colour); This function makes use of the wiringPi libraries, so it requires including the
file. Note the brackets used, as this is an installed library that is in the search path like the standard libraries. For this function we only use two wiringPi functions, wiringPiSetup, and digitalWrite. The wiringPiSetup function is called once to allow the library functions access to the peripheral registers. The digitalWrite function has two parameters. The first is an integer representing the pin you will control, and the second is the value that will be passed to it. (e.g. digitalWrite(0,0) ). For our lab we will use the following constants with our functions, and the constants HIGH, LOW, and OUTPUT from the wiringPi header file: #define GEN00 0 #define GEN01 1 #define NOCOLOUR 0 #define RED 1 #define GREEN 3 Task 3 – Detailed Function Specifications int GhControllerInit(void) Modify this function to return and integer and return a 1 as the last statement. After initializing the random number generator, call wiringPiSetup() int main(void) In the main check the status returned by GhcontrollerInit and if it is 1 then use the SetLED function to set the LED to green. Otherwise set the LED to red and do a return 0 to exit the program. void SetLED(int colour) This sets the colour of the LED using the constants GEN00, GEN01, OUTPUT, LOW, HIGH, RED, GREEN: 1. Use the wiringPi function pinMode to set the GPIO pins for outputs. pinMode(GEN00, OUTPUT); pinMode(GEN01, OUTPUT); 2. Clear both pins GEN00 and GEN01. digitalWrite(GEN00, LOW); digitalWrite(GEN01, LOW); 3. If the colour is RED, set pin GEN00 to HIGH using the digitalWrite function. 4. Else if the colour is GREEN set the GEN01 pin to HIGH using the digitalWrite function. Note: To run a program that uses wiringPi functions you must have supervisor privileges. This is done with the command line: $sudo ./lab05 Task 4 – Collecting Your Results 1. Use the WinSCP program to transfer your lab05 folder on the RPI and its contents to your H: network drive using the same folder structure as with the RPI. 2. Open up a new Word document and make a section title Lab05 Part A 3. Copy and paste the source code from lab05.c, makefile, ghcontrol.c, ghcontrol.h, ghutils.h, and ghutils.c into this document. 4. Take a screen shot of the output of the program and paste it into this document. 5. Save the document with the file name LastName153S18L05, where you substitute your last name as viewed in Blackboard for the ‘LastName’ characters. Save the document to your H: drive in the lab05 folder there. We will be adding to this document in part B of this lab before submitting it on Blackboard CNET232 Week 1 Assignments TECH153 L05B – I2C/MCP23017 Programming Overview Our initial use of an LED indicator used the pins called GEN0, and GEN1 to control a bi-directional LED. Once we added our HSH board, we also gained access to a GPIO pin extending chip, the MCP23017 that provides for 16 digital pins that can providing either 0V or 5V outputs, and capable of sensing the same as inputs. Along with the other devices on the HSH, these pins are accessed via a serial port that supports the I2C protocol. I²C (pronounced I-squared-C) created by Philips Semiconductors and commonly written as 'I2C' stands for Inter-Integrated Circuit and allows communication of data between I2C devices over two wires. It sends information serially using one line for data (SDA) and one for clock (SCL). The Phillips I2C protocol defines the concept of master and slave devices. A master device is simply the device that is in charge of the bus at the present time and this device controls the clock and generates START and STOP signals. Slaves simply listen to the bus and act on controls and data that they are sent. In our case, the RPI is the master and can send data to a slave or receive data from a slave - slaves do not transfer data between themselves. While there can be some more complexity, we access our I2C devices using wiringPi routines, that configure our devices using the hardware based I2C address, and an offset to a series of 8 bit data points, that can either be thought of as pins having the digital value of 0 or 1, or as 8 bit data values. The wiringPi I2C routines offer digitalRead, and digitalWrite commands for the 0 or 1 values, and analogRead, and analogWrite commands for the 8 bit data values. Additionally, the wiringPi setup for these devices uses the I2C address, and then sets up a virtual ‘pin’ address for the device. These are arbitrarily chosen by the programmer. Our project sets the pin base address for the MCP23017 device at 100, the PCF8591 device at 200, and the BME280 device at 300. On the HSH there is also an NXP PCF8591 IC, which has a single DAC (digital to analogue) converter and four ADCs (analogue to digital converters), accessible via the I2C bus. The DAC on the PCF8591 has a resolution of 8-bits, so it can generate a theoretical signal of between zero volts and the reference voltage (Vref 5V) in 255 steps. Each of the four PCF8591 ADC channels can read a voltage between zero and the Vref and it will return a value of between zero and 255 which is directly proportional to zero and the Vref (5V). For example, measuring 3.3V should return 168. For our greenhouse controller, we are going to sense the ambient light with a light-dependent resistor (LDR), and use this reading to control the intensity of supplemental lighting. We will hook up the LDR to channel 0 of the PCF8591. At some point will simulate the supplemental lighting by passing connecting a LED to the DAC channel, and vary its intensity. Task 1 – LED Control Indicators The first task requires adding two LED as indicators for our heater and humidifier controls. If you have LEDs with the built-in current limiting resistor, they may simply be plugged into Port A, pins 0 and 1. The positive or anode of the LED goes on the pin to the inside of the board, and the cathode to the digital ground, which is opposite to it on the outside. If you need an external resistor then this should be in series with the LED