Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home...

27
EE2024(E) Programming for Computer Interface Assignment 2(AY2013/14 Semester 1) KYAW SOE HEIN (A0103612Y) KAUNG SITHU (A0103596A) Programming for Computer Interface

Transcript of Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home...

Page 1: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

EE2024(E)Programming for Computer Interface

Assignment 2(AY2013/14 Semester 1)

KYAW SOE HEIN (A0103612Y)

KAUNG SITHU (A0103596A)

Programming for Computer Interface

Page 2: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Objective

The project aims to fulfill the requirements of smart home system integration by using LPCxpresso IDE, with LPCXpresso Baseboard RevB, LPC1769 LPCXpresso Development Board RevB. In this project, one will

- be able to apply system design approaches to design embedded applications, - understand the interfaces between microcontrollers and peripherals, - be able to develop C embedded programming controller based applications.

Programming for Computer Interface

Page 3: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Overview

Ultimately, it is tasked to implement the user-friendly interface, which can update the environmental information of, home and control functions such as control lighting, air-conditioning, heating and ventilation.

In general, students are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system, as well as detects fire and generates fire alarm. The control system works in two modes: MONITORING mode (default) and ALARM mode. It uses a temperature sensor and a light sensor to detect events and generate necessary control signals and alarms.

As mentioned above, the control system can be approached more clearly, if it is broken down into simple components, namely

Detected Events:

- Light Luminance Level - High/Low/Normal Temperature- Fire

Control signals & alarms

- Turn on/off Lights- Turn on/off the Air con/Heater/Fan- Generate Fire Alarms

Programming for Computer Interface

Page 4: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Specifications & Projects Requirements

Firstly, it would be meaningful to describe the specifications and requirements of the project briefly, before going deep into the codes and implementations. It is important to fully understand the projects requirements so that “Codes” could be well written and flow of the program will be smoothly handled. As well as, nature of required sensors and study of its nature- communication means, related input/output devices would be discussed briefly here.

Most importantly, the program will be executed in a way that the lights sensors will be handled with interrupt functions and temperature sensor will be handled without interrupt functions. However, both will be monitored throughout the execution of the program and execute related functions accordingly.

Light Sensor

The system needs to control the luminance level in the home, which is specified by the user as DESIRED LIGHT. Therefore, manipulation of luminance level read from environment is needed to achieve the desired light level .This would be done by PID controller which will adjust light levels (illumination LEDs in this project).

The communication mode of light sensor is I2C. The outputs from Light sensor will feedback to microcontroller and the intended device (Display purpose) needed is LED7 to LED19 of the 16 LEDs connected to PCA9532.

Temperature Sensor

The temperature sensor will be regulated the ambient temperature, the reading will be check against COLD, and as well as HOT temperature set point. The Heater will be turned on if temperature is COLD, LED4 of pca9532. The Air-con will be turned on if temperature is HOT, LED5 of pca9532. Otherwise, FAN will be turned on for ventilation, LED6 of pca9532. The communication mode of the temperature sensor is I2C as well.

Buzzer

The buzzer will be acted as indication if the program goes into ALARM mode. However, it is rather simple because there is no communication between other devices and buzzer. It just simply acted when program called for it. The communication mode of Buzzer is SPI.

PCA95322

As one could possible noticed, in this project, PCA9532 16 bit LED Dimmer plays an important role. This will indicate the light, output indication of the light sensor, LED7 to LED9 of the 16 LEDs. The status of HEATER, AIR-CON, & FAN will be indicated by LED4, LED5 and LED6 respectively. This handling of PCA9532 is rather complex which will be discussed later part of the report.

Programming for Computer Interface

Page 5: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

96x64 pixel OLED & 7segment Display

The OLED will always display the current status of the system, MONITORING/ALARM MODE, NUMER OF LEDS ON, HEATER/AIR-CON/ FAN ON, SLOW ALARM/FAST ALARM.

The 7 segment LED Display will display the count down time. The count down time is 8 second which will act as a timer for transition of SLOW ALARM MODE to FAST ALARM MODE.

Flow of the Program

Mentioning about the flow of the program, as per requirements, there are 3 modes of operations, namely MONITORING, SLOW ALARM, FAST ALARM. In this program, they are well handled by using programming technique (Switch case).

There are 2 interrupts required for the projects, SW3 & “Light read” from light sensor. Therefore, interrupts pins were declared and passed to handler function. On top of it, the setting the priority of these 2 interrupts is vital. Logically, SW3 act like a master reset, therefore, it is required to set the priority level of SW3 higher than that of Light sensor.

And the conditions of light as well as the temperature is defined so that program would notice when to go to ALARM MODE from MONITORING MODE. Moreover, it is coded in a way that when to turn on HEATER/FAN/AIR-CON if the conditions are met. The display of system status also has to include in the program when its conditions are met accordingly.

Professionally, the arrangement of displaying LEDs, handling interrupts of various devices and passing the perimeters from one function to another by coding will be well observed in this project.

Having mentioned that, the flow of the Program is rather simple and rigid. By means of switch case, and called for different functions (written in different C file) whenever there is needed. In terms of UML design, as attached to this report, it would be more reflective to this session generally.

Programming for Computer Interface

Page 6: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Detailed Code Implementations & Explanations

In this session of the report, the well-written codes will be thoroughly explained. Hopefully, this will bring clear understanding of how the requirements of project are full-filled. However, explanation of simple initializations of peripheral devices, constants declaration, header files and perimeters of sensors will be neglected.

First of all, the desired luminance level will be handled by this PDI function. However, one need to go through device data sheet for setting threshold luminance level and under what conditions the interrupts signal is out from which data line. This is vital for implementing this project. The main point according to the project requested is that the light will only be read and updated to program only if there’s interrupt output from light sensor device.

“GPIO_SetDir(2,1<<5,0);

light = light_read();y = light_read();”

This is how the light is read and pass to PID control function.

#define DESIRED_LIGHT 40

double PIDcontrol(double en, unsigned start){----- if (sn>desiredLight*10.0) sn=desiredLight*10.0; else if (sn<-(desiredLight*10.0)) sn=-(desiredLight*10.0); ----}

PDI controller will take the light_read value and it will be checked against the Desired Light and adjust until it is met desired level.

For temperature sensor, it is monitored by polling (always) and read the value every 1 millisecond. Therefore, it is rather simple by doing initializing temperature sensor and read the value by the code.

“temp = temp_read()/10” . However, the catch point is that one must use Systick_Handler function to get right value as temperature function is dependent on it.

“SysTick_Config(SystemCoreClock / 1000);

temp_init(&getMsTicks);”

Moreover, it would be nice to take a glance @ how the interrupt handler is written and how to check if there’s interrupt from particular port.

void EINT3_IRQHandler(void){-------------“codes are omitted”

if ((LPC_GPIOINT->IO2IntStatF >> 5) & 0x1){

LPC_GPIOINT->IO2IntClr |= 1<<5;

light_clearIrqStatus();

As one can notice, the code “LPC_GPIOINT->IO2IntStatF >> 5) & 0x1” is there to check if there’s any interrupt from Port 2.5 for light sensor.

Programming for Computer Interface

Page 7: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

As well as, the code “LPC_GPIOINT->IO2IntStatF >> 10) & 0x1” to check if there’s any interrupt from Port2.10 for temperature sensor.Then, the program will execute accordingly if there’s interrupt.

Another main requirement of the project is that when SW3 is pressed, it would over write everything and reset most of the time. It doesn’t entirely true for every situation. However, it is still required to check if its still too hot and too bright, if it is, it won’t go back to MONITORING MODE. It will be still @ ALARM MODE.

This needs the skills of setting priority group in the code as it is shown here.

For light > / Enable GPIO Interrupt P2.5

“LPC_GPIOINT->IO2IntEnF |= 1<<5;

uint32_t ans, PG=5, PP=0b11, SP=0b000;”

This priority level will be @ 0x60.

For SW3> / Enable GPIO Interrupt P2.10

“LPC_GPIOINT->IO2IntEnF |= 1<<10;

uint32_t ans, PG=5, PP=0b10, SP=0b000;”

This priority level will be @ 0x40.

Priority level of 0x40 is higher than that of 0x60. Therefore, SW3 interrupt has higher priority level than Light sensor.

Remarkable manipulation - 16 LEDs if PCA9532

Another code worth explaining is that how it is arranged to use the 16 LEDs of PCA9532 (Sharing to show status of luminous level and FAN, HEATER, AIR-CON). Obviously, when one status is shown, another will be affected anyhow because it is single LEDs driver. Therefore, masking off the undesired LEDs is needed.

Let’s study these codes

if(lightControl > 35){

ledState = pca9532_getLedState(0);

ledState |= 0x0007;

pca9532_setLeds(ledState<<1, 0xfff8)}

“omitted some codes”

else if(lightControl < -50){

Programming for Computer Interface

Page 8: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

ledState = pca9532_getLedState(0);

pca9532_setLeds(ledState>>1, 0xfff8);}

Here, ledState |= 0x0007; is written to mask the very first 3 LEDs of 16LEDs.

The LEDs are in the form of 0000 0000 0000 0111. i.e exclude the FAN, HEATER & AIR-CON status. It will just show according to the adjustment of luminous levels without affecting the rest 3LEDs by “OR” or “Equal to” 0x0007 code with shifting light up LED left or right.

Observe here again these codes,

“if(temp < TEMP_COLD){ ledState = pca9532_getLedState(0);

ledState |= 0x0001; ledState &= 0xfff9;}”

If temperature is colder than desired, turn on HEATER. This status is shown by 0xfff9 (1111 1111 1111 1001), i.e mask the 13 LEDs for luminous level. However, concurrently, ledstate /= 0x0001 enables the status of luminous level updated.

Same thing goes for “else if(temp > TEMP_HOT){ ledState = pca9532_getLedState(0);

ledState |= 0x0002; ledState &= 0xfffa;}”

If temperature is hotter than desired, turn on Air-con. This status is shown by 0xfffa (1111 1111 1111 1010), i.e mask the 13 LEDs for luminous level. However, concurrently, ledstate /= 0x0002 enables the status of luminous level updated.

Lastly, “else{ ledState = pca9532_getLedState(0);

ledState |= 0x0004; ledState &= 0xfffc;}”

If temperature falls between the limits, FAN will be on for ventilation. It is shown by 0xfffc(1111 1111 1111 1100) i.e mask the 13 LEDs again for light.

ALARM countdown

Another piece of work is remarkable. It is how to get the perfect timing of the ALARM countdown. This is done by step by step logics and with the help of preloaded “msTicks” function. The “count_down” is predefined as 8 secs.

msTicks++;

“msTick ++” function will be called every microsecond since the start of execution of mains. The value will be loaded to startTick when program goes into ALARM mode.

“case ALARM_SLOW: {---codes are omitted

startTick = msTicks;

alarm(500000, 2000000);}”

Programming for Computer Interface

Page 9: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Furthermore, it is written in a way that it is counted every 1 minute by taking modulus by 1000 as below

if(mode == ALARM_SLOW){

if((msTicks - startTick) % 1000 == 0 && countdown > 0){-----“codes are omitted” countdown--;}

Allow explaining briefly, let’s saying if “startTick” is 800 microseconds when program enters ALARM mode, msTickets continues counting and it reaches 1800microseconds. By taking modulus by 1000 of (1800-800), program knows one-second time has passed. So, it will give us exact 8 seconds time we need for slow alarm mode. After that, if no button is pressed, it will move on to Fast ALARM mode.

MASTER RESET SW3 – PRIORITY GROUPPING

This piece of codes enables the SW3 interrupt to take over the light sensor interrupt and the whole program. This is one of the most important aspects of the projects requirement. Allow to explain thoroughly for this part with table.

LPC_GPIOINT->IO2IntEnF |= 1<<5;

uint32_t ans, PG=5, PP=0b11, SP=0b000;

NVIC_SetPriorityGrouping(5);

ans = NVIC_EncodePriority(PG,PP,SP);

NVIC_SetPriority(EINT3_IRQn,ans);

NVIC_EnableIRQ(EINT3_IRQn);

// Enable GPIO Interrupt P2.10

PG=5, PP=0b10, SP=0b000;

NVIC_SetPriorityGrouping(5);

ans = NVIC_EncodePriority(PG,PP,SP);

NVIC_SetPriority(EINT3_IRQn,ans);

NVIC_EnableIRQ(EINT3_IRQn);

LPC_GPIOINT->IO2IntEnF |= 1<<10;

SW3 Light SensorPriority Group PG=5 PG=5Preempt PP=0b10 PP=0b11 #NoteSub priority SP=0b000 SP=0b00Priority level 0x40 0x60 SW3 has higher

priority level.

Programming for Computer Interface

Page 10: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

To conclude, since the priority level of the SW3 interrupt is higher than that of Light sensor, the switch button will act as a master reset. However, the condition is not entirely true, when SW3 is pressed, conditions will be check against Too Bright or Too Low before going back to monitoring mode.

if(temp >= TOO_HOT && light >= TOO_BRIGHT){mode = ALARM_SLOW; startTick = msTicks;}

Programming for Computer Interface

Page 11: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Extension (Bonus Part for the project)

After going through requirements of the project, it would be more advanced to put in Extension to the DESIRED LIGHT which can be set by occupants of the smart home. Project specified to use luminance of 50 lux , or (otherwise user-defined value below 500 lux). Therefore, it is decided to upgrade this part in the coding.

Mentioned that user can be easily set the desired light by using simple Rotary switch which is available on the Base Board. By turning Right (rotary switch), the desired light level will be up by 10 (this value could be adjusted in the code too). By turning Left (rotary switch), the desired light level will be down by 10.

Learn the function (coding) here.

void setDesiredLight(uint8_t rotary){if(rotary == ROTARY_RIGHT){ if (LIGHT_LOW >= 500) LIGHT_LOW = 500; else LIGHT_LOW+=10;

LIGHT_HIGH = LIGHT_LOW + 100;DESIRED_LIGHT = LIGHT_LOW + 50;

}else if(rotary == ROTARY_LEFT){

if (LIGHT_LOW <= 50) LIGHT_LOW = 50; else LIGHT_LOW-=10;LIGHT_HIGH = LIGHT_LOW + 100;DESIRED_LIGHT = LIGHT_LOW + 50;

}light_setHiThreshold(LIGHT_HIGH);light_setLoThreshold(LIGHT_LOW);printf("Light_low = %d\n", LIGHT_LOW);

}

As one could possibly notice that by setting the desired light, low & high threshold also will change. Therefore, it is clear that it deviates from the project requirements; set point is the middle of Lower and Upper threshold level.

Moreover, as declared by project requirement, the desired luminance level should not be higher than 500 lux and should not be lower than 50 lux. This requirement also full filled by using if else conditions.For user-friendly, the set point (Desired Light level) will also be shown on the OLED.

// Enable GPIO Interrupt P0.24LPC_GPIOINT->IO0IntEnF |= 1<<24;PG=5, PP=0b11, SP=0b100;NVIC_SetPriorityGrouping(5);ans = NVIC_EncodePriority(PG,PP,SP);NVIC_SetPriority(EINT3_IRQn,ans);NVIC_EnableIRQ(EINT3_IRQn);

// Enable GPIO Interrupt P0.25LPC_GPIOINT->IO0IntEnF |= 1<<25;

Programming for Computer Interface

Page 12: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

PG=5, PP=0b11, SP=0b100;NVIC_SetPriorityGrouping(5);ans = NVIC_EncodePriority(PG,PP,SP);NVIC_SetPriority(EINT3_IRQn,ans);NVIC_EnableIRQ(EINT3_IRQn);

As described by the above mention code, GPIO interrupt of rotary switch is also enabled and set priority. However, their priority level should not be higher than light sensor and SW3.

Thus, as encouraged, the extension is successfully well-done and this idea would be more helpful in the user-experience.

Programming for Computer Interface

Page 13: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

Problems Encounters & Learning points

The project requirements are intricate and complex coding is required such as interrupt handlers, timing countdown for alarm, different communication modes, indication status of the light as well as HEATER, FAN, AIR-CON.

Out of them, high-lighted learning will be mentioned here.

This includes the handling of PCA9532 16 LEDS, which is shared by light levels and status of HEATER/AIR-CON & FAN.

It is important that, LED7 to LED9 is running independently from LED4, 5, 6 while LIGHT and temperature sensing feeding information concurrently. Therefore, Masking off necessaries LEDS are well-handled in this project. The coding is well explained in the above, section “Detailed Coding & Implementations”.

Another difficulty encountered will be printing of count_down timer in the slow alarm mode. As described below coding, count down is to be used as character, however, when it is manipulated, and update on the led 7 segment, it would not display as desired.Therefore, it is required to study type casting of the character so it can be calculated with integer. As a result, the updated count down can be displayed correctly in the 7 segment LED.

void SysTick_Handler(void){char char_countdown;

msTicks++; if(mode == SLOW_ALARM){ if((msTicks - startTick) % 1000 == 0 && countdown > 0){ printf("countdown : %d\n",countdown); char_countdown = (char)(countdown + (int)'0'); // type casting led7seg_setChar(char_countdown, FALSE); countdown--; } }

Moreover, as suggested, for light control, it is required to PID controller as developed for earlier project. However, it doesn’t work perfect here; therefore, debugging is done and applied the PID controller in correct way. This takes some collect data for different ranges of light level and logics.However, this is also successfully done and works perfectly.

Programming for Computer Interface

Page 14: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

CONCLUSION

To conclude, the system integration of light control, temperature sensing and alarm detection is well coded in this project. All the project requirements are full filled by hard work of the project members.

Moreover, extension of the project with rotary switch is also included so as to offer better user friendly interface. Learning is solid and interrupt handling are also gone through as well as how to apply each peripheral devices on the baseboard – linked with micro controller as required. The project shall be treated as blessing for occupants and it well deserved in every smart homes.

All in all, the developing of applications, by LPC 1769 is thrilling and if time permits, there is highest possibility of putting in more devices and functions. Therefore, this project brings fruitful enriched experiences to the project members.

Programming for Computer Interface

Page 15: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

AppendixMain Codes#include "lpc17xx_gpio.h"#include "lpc17xx_timer.h"

#include "pca9532.h"#include "light.h"#include "temp.h"#include "oled.h"#include "led7seg.h"#include "acc.h"#include "rotary.h"

#include "functions.h"#include "pid.h"

#include <stdio.h>

/*** Constants ***/#define TOO_BRIGHT 1000#define TEMP_HOT 45#define TEMP_COLD 35#define TOO_HOT 32#define COUNTDOWN 8#define TONE 1275 //g 784 Hz#define TONE_DURATION 100/*** Constants ***/

#define NOTE_PIN_HIGH() GPIO_SetValue(0, 1<<26);#define NOTE_PIN_LOW() GPIO_ClearValue(0, 1<<26);

//operating modestypedef enum{MONITORING, SLOW_ALARM, FAST_ALARM} mode_t;

//Global variables

uint32_t msTicks=0;mode_t mode = MONITORING;static int light = 0;uint32_t startTick = 0;static int countdown = COUNTDOWN;static int lightControl = 30;

int32_t LIGHT_LOW = 50; //max 500int32_t LIGHT_HIGH = 150;int32_t DESIRED_LIGHT = 100;

// This function is called every 1usvoid SysTick_Handler(void){

char char_countdown; msTicks++; if(mode == SLOW_ALARM){ if((msTicks - startTick) % 1000 == 0 && countdown > 0){

Programming for Computer Interface

Page 16: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

printf("countdown : %d\n",countdown); char_countdown = (char)(countdown + (int)'0'); // type casting led7seg_setChar(char_countdown, FALSE); countdown--; } }}uint32_t getMsTicks(void){

return (msTicks);}

void EINT3_IRQHandler(void){double sp, u, e;uint32_t y;int i;unsigned st;

if ((LPC_GPIOINT->IO2IntStatF >> 5) & 0x1){LPC_GPIOINT->IO2IntClr |= 1<<5;light_clearIrqStatus();// C version sp = DESIRED_LIGHT; u = 0.0; for (i=0; i<10; i++){ if (i==0) st=1; else st=0;

//y = plant(u,st,-0.8,0.2); // Do NOT change the plant parameters

y = light_read(); e = sp - y; // st = 1; // PID controller written in C u = PIDcontrol(e, st, DESIRED_LIGHT*10); } lightControl = u; light = light_read();

return;}// SW3 interrupt Checkif ((LPC_GPIOINT->IO2IntStatF >> 10) & 0x1){

LPC_GPIOINT->IO2IntClr |= 1<<10;if(temp_read()/10 < TOO_HOT){

if((light = light_read()) < TOO_BRIGHT){mode = MONITORING;led7seg_setChar('0', FALSE); // 7seg initially set

to 0}

}return;

}//Rotatry Interuppt Check !

if((LPC_GPIOINT->IO0IntStatF >> 24) & 0x3){LPC_GPIOINT->IO0IntClr |= 0x3<<24;if(mode==MONITORING){

printf("rotary=%d\n", rotary_read());setDesiredLight(rotary_read());

}return;

Programming for Computer Interface

Page 17: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

}}

void setDesiredLight(uint8_t rotary){if(rotary == ROTARY_RIGHT){ if (LIGHT_LOW >= 500) LIGHT_LOW = 500; else LIGHT_LOW+=10;

LIGHT_HIGH = LIGHT_LOW + 100;DESIRED_LIGHT = LIGHT_LOW + 50;

}else if(rotary == ROTARY_LEFT){

if (LIGHT_LOW <= 50) LIGHT_LOW = 50; else LIGHT_LOW-=10;LIGHT_HIGH = LIGHT_LOW + 100;DESIRED_LIGHT = LIGHT_LOW + 50;

}light_setHiThreshold(LIGHT_HIGH);light_setLoThreshold(LIGHT_LOW);printf("Light_low = %d\n", LIGHT_LOW);

}

void alarm(uint32_t dur, uint32_t pause){ //duration and pause in milliseconds

uint32_t t = 0;pca9532_setLeds(0xffff, 0xffff); //Turn on all LEDswhile (t < (TONE_DURATION*1000)) { //function to play alarm NOTE_PIN_HIGH(); Timer0_us_Wait(TONE / 2);

NOTE_PIN_LOW(); Timer0_us_Wait(TONE / 2);

t += TONE;}Timer0_us_Wait(dur);

NOTE_PIN_LOW();pca9532_setLeds(0x0000, 0xffff);Timer0_us_Wait(pause);

}

int main(void){int i, ledOn = 0;uint8_t sw3 = 1;int temp, ledState;char buffer[50];

init_i2c();pca9532_init();light_enable();SW3_Init();init_ssp();

oled_init(); led7seg_init(); acc_init(); rotary_init();

Programming for Computer Interface

Page 18: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

SysTick_Config(SystemCoreClock / 1000);temp_init(&getMsTicks);oled_clearScreen(OLED_COLOR_BLACK);

/* ---- Speaker ------> */

GPIO_SetDir(2, 1<<0, 1); GPIO_SetDir(2, 1<<1, 1);

GPIO_SetDir(0, 1<<27, 1); GPIO_SetDir(0, 1<<28, 1); GPIO_SetDir(2, 1<<13, 1); GPIO_SetDir(0, 1<<26, 1);

GPIO_ClearValue(0, 1<<27); //LM4811-clk GPIO_ClearValue(0, 1<<28); //LM4811-up/dn GPIO_ClearValue(2, 1<<13); //LM4811-shutdn

/* <---- Speaker ------ *//* Set up Light sensor */light_setHiThreshold(LIGHT_HIGH);light_setLoThreshold(LIGHT_LOW);light_setRange(LIGHT_RANGE_4000);light_setIrqInCycles(LIGHT_CYCLE_8);

LPC_GPIOINT->IO2IntClr = 1<<5;//temperatureLPC_GPIOINT->IO2IntClr = 1<<10;//SW3

//Rotary

LPC_GPIOINT->IO0IntClr = 1<<24;LPC_GPIOINT->IO0IntClr = 1<<25;

GPIO_SetDir(2,1<<5,0);light = light_read();light_clearIrqStatus();led7seg_setChar('0', FALSE); // 7seg initially set to 0

// Enable GPIO Interrupt P2.5LPC_GPIOINT->IO2IntEnF |= 1<<5;

uint32_t ans, PG=5, PP=0b11, SP=0b000;NVIC_SetPriorityGrouping(5);ans = NVIC_EncodePriority(PG,PP,SP);NVIC_SetPriority(EINT3_IRQn,ans);NVIC_EnableIRQ(EINT3_IRQn);

// Enable GPIO Interrupt P2.10PG=5, PP=0b10, SP=0b000;NVIC_SetPriorityGrouping(5);ans = NVIC_EncodePriority(PG,PP,SP);NVIC_SetPriority(EINT3_IRQn,ans);NVIC_EnableIRQ(EINT3_IRQn);LPC_GPIOINT->IO2IntEnF |= 1<<10;

// Enable GPIO Interrupt P0.24

Programming for Computer Interface

Page 19: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

LPC_GPIOINT->IO0IntEnF |= 1<<24;PG=5, PP=0b11, SP=0b100;NVIC_SetPriorityGrouping(5);ans = NVIC_EncodePriority(PG,PP,SP);NVIC_SetPriority(EINT3_IRQn,ans);NVIC_EnableIRQ(EINT3_IRQn);

// Enable GPIO Interrupt P0.25LPC_GPIOINT->IO0IntEnF |= 1<<25;PG=5, PP=0b11, SP=0b100;NVIC_SetPriorityGrouping(5);ans = NVIC_EncodePriority(PG,PP,SP);NVIC_SetPriority(EINT3_IRQn,ans);NVIC_EnableIRQ(EINT3_IRQn);

//lightControl = 30;//DESIRED_LIGHT;while(1){

switch(mode){case MONITORING: {

temp = temp_read()/10;printf("u=%d\nlight=%d\ntemp=%d\n", lightControl,

light_read(),temp);if(lightControl > 80){//(DESIRED_LIGHT-LIGHT_LOW)){

ledState = pca9532_getLedState(0);ledState |= 0x0007; //pca9532_setLeds(ledState<<1, 0xfff8);ledOn++;if(ledOn >= 13)

ledOn = 13;

} else if(lightControl < -10){//(DESIRED_LIGHT-LIGHT_HI)){

ledState = pca9532_getLedState(0);pca9532_setLeds(ledState>>1, 0xfff8);ledOn--;if(ledOn <= 0)

ledOn = 0;}if(temp < TEMP_COLD){ //COLD. Turn Heater on

ledState = pca9532_getLedState(0);ledState |= 0x0001; ledState &= 0xfff9;pca9532_setLeds(ledState, 0xffff);/* OLED output */oled_clearScreen(OLED_COLOR_WHITE);sprintf(buffer,"Monitoring Mode\n");

oled_putString(0,0,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"Heater On");

oled_putString(0,20,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"%d LEDs On", ledOn);

oled_putString(0,30,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"SetLight:%d", DESIRED_LIGHT);

oled_putString(0,40,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);

Programming for Computer Interface

Page 20: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

//printf("temp : %d \r\n",temp);//printf("light : %d \n", light);printf("Monitoring Mode\n");printf("Heater on\n");printf("%d LEDs On\n\n", ledOn);

}else if(temp > TEMP_HOT){ //HOT. Turn Air-con onledState = pca9532_getLedState(0);ledState |= 0x0002; ledState &= 0xfffa;pca9532_setLeds(ledState, 0xffff);

/* OLED output */oled_clearScreen(OLED_COLOR_WHITE);sprintf(buffer,"Monitoring Mode\n");

oled_putString(0,0,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"Air-con On");

oled_putString(0,20,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"%d LEDs On", ledOn);

oled_putString(0,30,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"SetLight:%d", DESIRED_LIGHT);

oled_putString(0,40,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);//printf("temp : %d \r\n",temp);//printf("light : %d \n", light);printf("Monitoring Mode\n");printf("Air-con on\n");printf("%d LEDs On\n\n", ledOn);

} else{ //Normal. Turn Fan on for ventilationledState = pca9532_getLedState(0);ledState |= 0x0004; ledState &= 0xfffc;pca9532_setLeds(ledState, 0xffff);//pca9532_setLeds(0x0004, 0xffff);oled_clearScreen(OLED_COLOR_WHITE);sprintf(buffer,"Monitoring Mode\n");

oled_putString(0,0,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"Fan On");

oled_putString(0,20,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"%d LEDs On", ledOn);

oled_putString(0,30,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"SetLight:%d", DESIRED_LIGHT);

oled_putString(0,40,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);//printf("temp : %d \r\n",temp);//printf("light : %d \n", light);printf("Monitoring Mode\n");printf("Fan on\n");printf("%d LEDs On\n\n", ledOn);

}

if(temp >= TOO_HOT && light >= TOO_BRIGHT){mode = SLOW_ALARM;startTick = msTicks;

Programming for Computer Interface

Page 21: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

countdown = COUNTDOWN;}

break;}case SLOW_ALARM: {

ledOn = 0;

oled_clearScreen(OLED_COLOR_WHITE);sprintf(buffer,"Alarm Mode\n");

oled_putString(0,0,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"Slow Alarm");

oled_putString(0,20,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"%d LEDs On", ledOn);

oled_putString(0,30,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"SetLight:%d", DESIRED_LIGHT);

oled_putString(0,40,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);pca9532_setLeds(0x0000, 0xffff);startTick = msTicks;alarm(500000, 2000000);

if(countdown <= 0) {mode = FAST_ALARM;

}printf("Alarm Mode\n");printf("Slow Alarm\n");printf("%d LEDs On\n\n", ledOn);break;

}case FAST_ALARM: {

ledOn = 0;oled_clearScreen(OLED_COLOR_WHITE);sprintf(buffer,"Alarm Mode\n");

oled_putString(0,0,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"Fast Alarm");

oled_putString(0,20,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"%d LEDs On", ledOn);

oled_putString(0,30,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);sprintf(buffer,"SetLight:%d", DESIRED_LIGHT);

oled_putString(0,40,buffer,OLED_COLOR_BLACK,OLED_COLOR_WHITE);

alarm(200000, 500000);printf("Alarm Mode\n");printf("Fast Alarm\n");printf("%d LEDs On\n\n", ledOn);break;

}

}//end of switch}//end of while

Programming for Computer Interface

Page 22: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

return 0;}

Functions.c#include "lpc17xx_pinsel.h"#include "lpc17xx_i2c.h"#include "lpc17xx_ssp.h"#include "lpc17xx_gpio.h"#include "functions.h"

void init_i2c(void) //Initialize I2C devices{

PINSEL_CFG_Type PinCfg;

/* Initialize I2C2 pin connect */PinCfg.Funcnum = 2;PinCfg.Pinnum = 10;PinCfg.Portnum = 0;PINSEL_ConfigPin(&PinCfg);PinCfg.Pinnum = 11;PINSEL_ConfigPin(&PinCfg);

// Initialize I2C peripheralI2C_Init(LPC_I2C2, 100000);

/* Enable I2C1 operation */I2C_Cmd(LPC_I2C2, ENABLE);

}

void init_ssp(void) //Initialize SSP devices. For OLED is used in SPI mode{

SSP_CFG_Type SSP_ConfigStruct;PINSEL_CFG_Type PinCfg;

/* * Initialize SPI pin connect * P0.7 - SCK; * P0.8 - MISO * P0.9 - MOSI * P2.2 - SSEL - used as GPIO */PinCfg.Funcnum = 2;PinCfg.OpenDrain = 0;PinCfg.Pinmode = 0;PinCfg.Portnum = 0;PinCfg.Pinnum = 7;PINSEL_ConfigPin(&PinCfg);PinCfg.Pinnum = 8;PINSEL_ConfigPin(&PinCfg);PinCfg.Pinnum = 9;PINSEL_ConfigPin(&PinCfg);PinCfg.Funcnum = 0;PinCfg.Portnum = 2;PinCfg.Pinnum = 2;PINSEL_ConfigPin(&PinCfg);

SSP_ConfigStructInit(&SSP_ConfigStruct);

Programming for Computer Interface

Page 23: Objective - Kyaw Soe Hein - Home · Web viewstudents are required to simulate this smart home system, which controls the lighting system, heating, air-conditioning and fan system,

// Initialize SSP peripheral with parameter given in structure aboveSSP_Init(LPC_SSP1, &SSP_ConfigStruct);

// Enable SSP peripheralSSP_Cmd(LPC_SSP1, ENABLE);

}

void SW3_Init(void){PINSEL_CFG_Type PinCfg;

PinCfg.Funcnum = 1; //Use interrupt functionPinCfg.Pinnum = 10;PinCfg.Portnum = 2;PinCfg.Pinmode = 0;PinCfg.OpenDrain = 0;PINSEL_ConfigPin(&PinCfg);

GPIO_SetDir(2,1<<10,0);}

PID.C function

#include "pid.h"

double PIDcontrol(double en, unsigned start, double limit){

static double Kp=0.25, Ki=0.1, Kd=0.8, sn, enOld, un; if (start) sn = enOld = 0.0;

sn = sn + en;

if (sn>limit) sn=limit;

else if (sn<=0) sn=0;

un = Kp*en + Ki*sn + Kd*(en-enOld); enOld = en; return(un);}

Programming for Computer Interface