46

Click here to load reader

User Manual - Final Project 2015

Embed Size (px)

Citation preview

Page 1: User Manual - Final Project 2015

Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

CHAFIC’S ELECTRO-MECH TECHNICAL SOLUTIONS

Solar Power Regulator User’s Manual

Page 2: User Manual - Final Project 2015

Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

By: Chafic Bouchakra

Submitted to: Stephen Ryan

ELN9204

Section: 010

Date: April 21, 2015

Page 3: User Manual - Final Project 2015

Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Table of Contents

Section Page

1.0 General Information ................................................................................................................... 1

1.0 general information............................................................................................................................ 2

1.1 System Overview ................................................................................................................................ 2

1.2 Point of Contact .................................................................................................................................. 2

1.3 Organization of the Manual ............................................................................................................... 2

2.0 System Summary ............................................................................................................... 3

2.0 System Summary ................................................................................................................................ 4

2.1 Hardware ............................................................................................................................................ 4

2.2 Software ............................................................................................................................................. 4

2.3 Computer ........................................................................................................................................... 4

3.0 Visuals of the Hardware ..................................................................................................... 5

3.0 Containment Unit ............................................................................................................................... 6

3.1 Main Circuit Board.............................................................................................................................. 6

3.2 Clock Chip Circuit Board ..................................................................................................................... 7

3.3 Temperature Chip Circuit Board ........................................................................................................ 7

4.0 Electrical Circuit Schematic ........................................................................................................ 8

4.0 Schematic ........................................................................................................................................... 9

5.0 Issues and Limitations ................................................................................................................ 10

5.0 Hardware ............................................................................................................................................ 11

5.1 Software ............................................................................................................................................. 11

6.0 Program Flowchart ..................................................................................................................... 12

6.0 Software Flowchart ............................................................................................................................ 13

7.0 Program Code ............................................................................................................................. 14

7.0 Main Program ..................................................................................................................................... 15

7.1 Switchc program ................................................................................................................................. 18

7.2 KBD_press Program ............................................................................................................................ 20

7.3 Set_time Program .............................................................................................................................. 22

7.4 BCD Program ...................................................................................................................................... 24

7.5 Init_time Program .............................................................................................................................. 26

7.6 Init_rtc Program ................................................................................................................................. 29

7.7 Init_KBD Program ............................................................................................................................... 33

7.8 Init_LCD Program ............................................................................................................................... 37

7.9 Prototype.H Program ......................................................................................................................... 42

Page 4: User Manual - Final Project 2015

1.0 General Information

Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015 Page 1

1.0 General Information

Page 5: User Manual - Final Project 2015

1.0 General Information

Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015 Page 2

1.0 GENERAL INFORMATION

This product is a solar power regulator that is installed as a bridge point between your active solar panels and the power contain-

ment unit (PCU) (or in other terms the battery pack). It is designed to monitor the voltage flow between the panels and the PCU,

displaying live updates on the LCD screen. It has the functionality to power on an internal fan to the PCU to keep it under a spe-

cific temperature threshold and to disconnect the voltage line of between the panels and the PCU if the intake voltage exceeds

the recommended voltage of the PCU or the temperature of the PCU exceeds the critical threshold.

1.1 System Overview

This solar power regulator is a self contained unit and required no additional hardware or wiring to be fully operational*.

It has a built in LCD screen, and can be either powered with batteries or an external power source.

Using a PIC16F877A Microcontroller chip this solar power regulator is designed monitor and control the flow of electricity to the

PCU while monitoring the internal temperature of the PCU.

The LCD screen on the regulator displays the time of day using the clock chip, while continuously displaying the live voltage, and

amp readings of the flow of electricity from the panel, and displays the live temperature reading of the internal PCU. While con-

tinuously checking the voltage and temperature, the internal programming will trigger the relays to disconnect the power if the

voltage or temperature exceeds the allowable threshold** in addition it powers on a fan in the PCU to maintain a suitable tem-

perature range. The voltage relay can be reset using the user interface after the manual reset button is pressed.

This product is fully operational.

*Additional hardware and software may be required to modify the programming or create a personalized custom configuration.

**The thresholds are default set to 12 volts, for the power and 80° F for the temperature. The thresholds may be reprogrammed

through the computer, using the required software and hardware (not included).

1.2 Point of Contact

At any point if you require additional assistance in the setup, programming or trouble shooting of the Solar Power Regulator, you

may contact our lead technician Chafic directly at 613-864-4454 or via email him at [email protected].

1.3 Organization of the Manual

This user’s manual consists of 7 sections: General Information. System Summary, Visuals of the Hardware, Electrical Circuit Sche-

matic, Issues and Limitations, Program Flowchart, and Program Code.

General Information sections explains the system, the purpose and gives contact information where more information can be

found.

System Summary section gives a quick rundown on how the program works, and lists the requirements for the hardware and

software to troubleshoot and reprogram the chip.

Visuals of the Hardware section shows the actual parts built and what the Solar Power Regulator looks like inside and out.

Electrical Circuit Schematic section shows the entire Schematic for the Solar Power Regulator.

Issues and Limitations section outlines any issues I had with the project and limitations for both the hardware and software.

Program Flowchart section is a visual display of how the program is expected to run in a flowchart form.

Program Code section is the coding for the main program and any subprograms that are required to run the regulator.

Page 6: User Manual - Final Project 2015

2.0 System Summary

Page 3 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

2.0 System Summary

Page 7: User Manual - Final Project 2015

2.0 System Summary

Page 4 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

2.1 Hardware

1 - PIC16F877A

1 - NE555N - Timer Chip

1 - 6355ED - Serial I/O Real Time Clock

1 - DS1631 - Temperature Chip

1 - 4X3 Keypad

1 - FOX - 20.0MHz Crystal Oscillator

2 - 7805CT - 5 Volt Regulator

1 - 7809CT - 9 Volt Regulator

1 - JHD 162A - 2X16 LCD Screen

2 - 100kΩ Resistor

1 - 22kΩ Resistor

1 - 20kΩ Resistor

6 - 10kΩ Resistor

1 - 2kΩ Resistor

4 - 1kΩ Resistor

1 - 330Ω Resistor

1 - 10Ω Resistor

1 - 10KΩ Potentiometer

1 - 100μF Capacitor

1 - 0.01μF Capacitor

1 - 0.001μF Capacitor

2 - 3904 Transistor

4 - IN5231B - Zener Diode

2- HLS-4078-DC5V - Relay

1 - Push Button Switch

3 - 9V Battery Connectors

3 - 9V Battery

1 - 9V DC Fan

2.2 Software

PIC C Compiler

PICkit 2

2.3 Computer

Minimum Requirements:

Pentium 1 Processor

Keyboard

Monitor

80 GB of storage Space

2.0 SYSTEM SUMMARY

When the Regulator is initialized it will ask you for the hour and than the minute, input the time using the keypad. It will then auto-

matically show your all the data. If it enters critical mode it will tell you to press ’*’ to reset, all you have to do then it hit ’*’ on the

keypad after you checked to see that the voltage is level and it will reset the voltage connection. The voltage connection will not

reset if the voltage and/or temperature are still past the threshold.

Page 8: User Manual - Final Project 2015

3.0 Visuals of the Hardware

Page 5 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

3.0 Visuals of the hardware

Page 9: User Manual - Final Project 2015

3.0 Visuals of the Hardware

Page 6 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

3.0 Containment Unit

This is the visual of the external containment unit.

The LCD, reset push button, keypad, visual LED and voltage

intake are all easily accessible.

Has a slot to include access to a temperature chip to meas-

ure temperature of the units environment.

3.1 Main Circuit Board

This is the main circuit PCB board it contains the microcon-

troller, it’s own 5 volt power supply, and a 9 volt power

supply to power the fan. The two relays to cut the intake

voltage and power the fan. Control the contras of the LCD

with the potentiometer on this board

All other components connect to this board.

Page 10: User Manual - Final Project 2015

3.0 Visuals of the Hardware

Page 7 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

3.2 Clock Chip Circuit Board

This is the 6355ED - Serial I/O Real Time Clock circuit board

with the NE555N timer.

This has its own 5 volt closed circuit power to reduce noise

caused by the NE555N timer to the rest of the circuits.

3.3 Temperature Chip Circuit Board

This is the DS1631 - Temperature Chip circuit board, It is

separated from the rest so that it can be placed within the

PCU to read its internal temperature.

Page 11: User Manual - Final Project 2015

4.0 Electrical Circuit Schematic

Page 8 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

4.0 Electrical circuit Schematic

Page 12: User Manual - Final Project 2015

4.0 Electrical Circuit Schematic

Page 9 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 13: User Manual - Final Project 2015

5.0 Issues and Limitations

Page 10 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

5.0 Issues and Limitations

Page 14: User Manual - Final Project 2015

5.0 Issues and Limitations

Page 11 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

5.0 Hardware

Quick steps to ensure you do not run into any issues with the hardware and ways to figure out how to determine what the problem

is.

A chip is not working properly?

Check power supply

Check wiring

Check for cold and incomplete solders

Ensure any transistors and/or diodes are facing the right direction

Best way to determine if a specific chip or section of your circuit is working is by building it in bits and pieces and making sure it

works before adding the next section.

Test any questionable parts in a working circuit with the proper programming to ensure that it is functioning properly otherwise

replace the part.

Having the LCD wired with all 8 data pins reduced the number of ports on the PIC16 that can be used for other hardware. Reducing

the number of pins to 4 and changing the programming allows more peripherals on the PIC

I had issues getting the NE555N Timer to meet the required frequency (32.768KHz) with a duty cycle above 50% with the specific

timer circuit that was built in this project. As the 6355ED seconds need that frequency to give out accurate time readings. My sug-

gestion for this is get a proper crystal oscillator that runs at that frequency or find a clock chip with a built in precise oscillator.

5.1 Software

Space... The biggest limiting factor with the PIC and software, ensuring the software all fit within the total Rom space was essential.

Changing values and using int and char helped to save space.

Printf statements required tremendous amounts of space so limiting those is essential.

Using the switch program to when thresholds were triggered ensured flawless transition into different modes.

Breaking out of switch modes was a challenge as creating a kbd_press program was essential to create that pause in the program

to wait for a user to reset the program.

Putting the Init files on a separate chip to initialize the LCD, 6355ED, and DS1631, would save you tons of space as well, as long as

those parts maintain power and do not turn on. Which makes it very challenging to do this with this setup. So in this project they

had to stay on the main chip for simplicity and time sake, which meant I was highly limited in the complexity and depth of this soft-

ware.

Page 15: User Manual - Final Project 2015

6.0 Program Flowchart

Page 12 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

6.0 Program Flowchart

Page 16: User Manual - Final Project 2015

6.0 Program Flowchart

Page 13 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 17: User Manual - Final Project 2015

7.0 Program Code

Page 14 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

7.0 Program Code

Page 18: User Manual - Final Project 2015

7.0 Program Code

Page 15 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 19: User Manual - Final Project 2015

7.0 Program Code

Page 16 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

#include <prototype.h>

#include <ds1631.c>

#include <nju6355.c>

#include <stdlib.h>

/***************************************************************************

Main Program

***************************************************************************/

void main()

{

int hour, min, sec, last_sec, volt, curr; // Sets Int for; hour, min, sec, last_sec, volt, curr

unsigned long int voltage_reading, current_reading; // Sets Unsigned long int for; voltage_reading, current_reading

unsigned long int value=0; // Sets unsigned long int for value

lcd_init(); // Initialized LCD

kbd_init (); // Initialized keypad

rtc_init (); // Initialized clock chip

init_temp(); // Initialized temperature chip

set_time (); // Prompts set time sequence

while(1) // While true execute this Loop

{

rtc_get_time (hour, min, sec ); // Retrieves time from chip

if (( sec!=last_sec )) // If Sec does not equal Last_sec then execute the following loop

{

set_adc_channel ( 1 ); // Sets Channel to Port A1

delay_us (10); // Delays 10 µs

voltage_reading = read_adc (); // Reads Voltage from A1

delay_us (10); // Delay 10 µs

volt = ((voltage_reading*12)/100) ; // Converts Voltage Reading to User Readable Number

// Sets Readable Number as Volt Int

set_adc_channel ( 3 ); // Sets Channel to Port A3

delay_us (10); // Delays 10 µs

current_reading = read_adc (); // Reads Current from A3

delay_us (10); // Delays 10 µs

Page 20: User Manual - Final Project 2015

7.0 Program Code

Page 17 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

curr = ((current_reading*12)/1000); // Converts Current Reading to User Readable Number

// Sets Readable Number as Curr Int

value = read_full_temp (); // Sets temp reading as value

value /= 100;

delay_ms (100); // Delay 100 ms

printf (lcd_putc, "\f%c%c:%c%c:%c%c ", // Displays time on the LCD

display_first_bcd ( hour), display_second_bcd (hour),

display_first_bcd (min), display_second_bcd (min),

display_first_bcd (sec), display_second_bcd (sec));

printf (lcd_putc, "\nTemp%4.2ld ", value); // Displays temp on LCD

printf (lcd_putc, "\t %d V ", volt); // Displays volt on LCD

printf (lcd_putc, "\U %d mA ", curr); // Displays curr on LCD

last_sec = sec; // Sets Last_sec as the Current Sec

If (volt >= 12) // If volt >= 12 then Switchc case 0 is activated

{

Switchc (0);

}

If (value >= 78) // If temp value >= 78 then fan turns ON

{

output_high (Pin_D0);

}

If (value >= 80) // If temp value >= 80 then Switchc case 1 is activated

{

Switchc (1);

}

if (value <= 77) // If temp value <= 77 then fan turns OFF

{

Output_low (Pin_D0);

}

}

}

}

Page 21: User Manual - Final Project 2015

7.0 Program Code

Page 18 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 22: User Manual - Final Project 2015

7.0 Program Code

Page 19 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

/***************************************************************************

Switch Program for "Voltage Overload" and "Temperature Crititcal"

***************************************************************************/

void Switchc ( int SC )

{

switch ( SC )

{

case 0:

{ // Case 0

Output_high (Pin_A2); // Disconnects input voltage

Printf (lcd_putc, "\b****Overload****"); // Displays on LCD

Printf (lcd_putc, "\nPress * to reset"); // Displays on LCD

Kbd_press (); // Waits for * to be pressed on keypad

Output_low (Pin_A2); // Reconnects input voltage

break; // Returns to main program

}

case 1:

{ // Case 1

Output_high (Pin_A2); // Disconnects input voltage

Output_high (Pin_D0); // Turn fan ON

Printf (lcd_putc, "\b Temp CRITICAL"); // Displays on LCD

Printf (lcd_putc, "\nPress * to reset"); // Displays on LCD

Kbd_press (); // Waits for * to be pressed on keypad

Output_low (Pin_D0); // Turns fan OFF

Output_low (Pin_A2); // Reconnects input voltage

break; // Returns to main program

}

}

}

Page 23: User Manual - Final Project 2015

7.0 Program Code

Page 20 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 24: User Manual - Final Project 2015

7.0 Program Code

Page 21 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

/***************************************************************************

Kbd_press

Here the program will cycle in a blank loop waiting for * to be pressed then

released before going back to the previous program

***************************************************************************/

void Kbd_press (void)

{

while(kbd_getc () != '*') // While * is not press, closed loop rotation

{

}

while(kbd_getc () == '*') // While * is press, closed loop rotation

{

} // Once * is released, breaks out of Kbd_press.

} // Returns to previous program

Page 25: User Manual - Final Project 2015

7.0 Program Code

Page 22 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 26: User Manual - Final Project 2015

7.0 Program Code

Page 23 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

/***************************************************************************

Set Time

***************************************************************************/

void set_time ()

{

int hour, min; // Sets hour, min as an Int value

Delay_ms (500);

printf ( lcd_putc, "\bHour: "); hour = get_bcd();

printf ( lcd_putc, "\bMin: " ); min = get_bcd ();

rtc_set_datetime ( 0, 0, 0, 0, hour, min );

} // end void set_time

char display_first_bcd (byte n )

{

return n/16+'0' ;

} // end char display_first_bcd

char display_second_bcd (byte n )

{

return n%16+'0';

} // end char display_second_bcd

Page 27: User Manual - Final Project 2015

7.0 Program Code

Page 24 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 28: User Manual - Final Project 2015

7.0 Program Code

Page 25 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

/***************************************************************************

BCD

***************************************************************************/

byte get_bcd()

{

char first, second;

do

{

first=kbd_getc(); // Note first is gathered from kbd_getc() Keypad Press

} while ((first<'0') || (first>'9'));

putc(first);

do

{

second=kbd_getc(); // Note first is gathered from kbd_getc() Keypad Press

} while ((second<'0') || (second>'9') && (second!='\r'));

putchar(second);

if (second=='\r')

return (first-'0');

else

return (((first-'0') << 4) | (second-'0'));

}

void display_bcd (byte n )

{

putc ( n/16+'0' );

putc ( n%16+'0' );

}

Page 29: User Manual - Final Project 2015

7.0 Program Code

Page 26 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 30: User Manual - Final Project 2015

7.0 Program Code

Page 27 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

#ifndef DAL_SCL

#define DAL_SCL PIN_E1

#define DAL_SDA PIN_E0

#endif

#define read_temp read_full_temp // for backwards compatability

#use i2c(master, sda=DAL_SDA, scl=DAL_SCL)

void temp_config(BYTE data)

{

i2c_start();

i2c_write(0x90);

i2c_write(0xac);

i2c_write(data);

i2c_stop();

}

void init_temp()

{

output_high(DAL_SDA);

output_high(DAL_SCL);

i2c_start();

i2c_write(0x90);

i2c_write(0x51);

i2c_stop();

temp_config(0xc);

}

signed long read_full_temp()

{ // Returns hundreths of degrees F (-67 to 257)

signed int datah, datal;

signed long data;

i2c_start();

i2c_write(0x90);

Page 31: User Manual - Final Project 2015

7.0 Program Code

Page 28 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

i2c_write(0xaa);

i2c_start();

i2c_write(0x91);

datah=i2c_read();

datal=i2c_read(0);

i2c_stop();

data=(signed long)datah*100;

data=data+(((datal >> 4 )*(long)50)/16);

data=data*9;

data = (data / 5) + 3200;

return(data);

}

Page 32: User Manual - Final Project 2015

7.0 Program Code

Page 29 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 33: User Manual - Final Project 2015

7.0 Program Code

Page 30 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

#ifndef RTC_DATA // If RTC_DATA defined set pins

#define RTC_DATA PIN_B0 // Data pin to port B0

#define RTC_CLK PIN_C4 // CLK pin to port C4

#define RTC_CE PIN_D3 // CD pin to port D3

#define RTC_IO PIN_D2 // IO pin to port D2

#endif // end if

void rtc_init() {

output_low(RTC_CE);

output_low(RTC_IO);

}

void write_rtc_byte(BYTE data_byte, BYTE number_of_bits) {

BYTE i;

for(i=0; i<number_of_bits; ++i) {

if((data_byte & 1)==0)

output_low(RTC_DATA);

else

output_high(RTC_DATA);

data_byte=data_byte>>1;

output_high(RTC_CLK);

output_low(RTC_CLK);

}

}

BYTE read_rtc_byte(BYTE number_of_bits) {

BYTE i,data;

for(i=0;i<number_of_bits;++i) {

output_high(RTC_CLK);

shift_right(&data,1,input(RTC_DATA));

output_low(RTC_CLK);

}

return(data);

}

void rtc_set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow,

Page 34: User Manual - Final Project 2015

7.0 Program Code

Page 31 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

BYTE hour, BYTE min){

output_low(RTC_CLK);

output_high(RTC_IO);

output_high(RTC_CE);

write_rtc_byte(year,8);

write_rtc_byte(mth,8);

write_rtc_byte(day,8);

write_rtc_byte(dow,4);

write_rtc_byte(hour,8);

write_rtc_byte(min,8);

output_low(RTC_CE);

output_low(RTC_IO);

}

void rtc_get_date(BYTE& day, BYTE& mth, BYTE& year, BYTE& dow) {

output_low(RTC_CLK);

output_low(RTC_IO);

output_high(RTC_CE);

year=read_rtc_byte(8);

mth=read_rtc_byte(8);

day=read_rtc_byte(8);

dow=read_rtc_byte(4)>>4;

read_rtc_byte(8*3);

output_low(RTC_CE);

output_low(RTC_IO);

}

void rtc_get_time(BYTE& hr, BYTE& min, BYTE& sec) {

output_low(RTC_CLK);

output_low(RTC_IO);

output_high(RTC_CE);

read_rtc_byte(8*3+4);

hr=read_rtc_byte(8);

min=read_rtc_byte(8);

Page 35: User Manual - Final Project 2015

7.0 Program Code

Page 32 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

sec=read_rtc_byte(8);

output_low(RTC_CE);

output_low(RTC_IO);

}

Page 36: User Manual - Final Project 2015

7.0 Program Code

Page 33 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 37: User Manual - Final Project 2015

7.0 Program Code

Page 34 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

/***************************************************************************

Kbd Initialization Program

Defines Keypad Buttons

***************************************************************************/

#define col0 PIN_B3

#define col1 PIN_B2

#define col2 PIN_B1

#define row0 PIN_B7

#define row1 PIN_B6

#define row2 PIN_B5

#define row3 PIN_B4

char const KEYS[4][3] = {{'1','2','3'}, // Keypad layout:

{'4','5','6'},

{'7','8','9'},

{'*','0','#'}};

#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where

// n is the number of times you expect

// to call kbd_getc each second

void kbd_init() {}

short int ALL_ROWS (void)

{

if (input (row0) & input (row1) & input (row2) & input (row3))

return (0);

else

return (1);

}

char kbd_getc( ) {

static byte kbd_call_count;

static short int kbd_down;

static char last_key;

static byte col;

Page 38: User Manual - Final Project 2015

7.0 Program Code

Page 35 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

byte kchar;

byte row;

kchar='\0';

if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {

switch (col) {

case 0 : output_low(col0);

output_high(col1);

output_high(col2);

break;

case 1 : output_high(col0);

output_low(col1);

output_high(col2);

break;

case 2 : output_high(col0);

output_high(col1);

output_low(col2);

break;

}

if(kbd_down)

{

if(!ALL_ROWS())

{

kbd_down=false;

kchar=last_key;

last_key='\0';

}

}

else

{

if(ALL_ROWS())

{

if(!input (row0))

row=0;

Page 39: User Manual - Final Project 2015

7.0 Program Code

Page 36 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

else if(!input (row1))

row=1;

else if(!input (row2))

row=2;

else if(!input (row3))

row=3;

last_key =KEYS[row][col];

kbd_down = true;

}

else

{

++col;

if(col==3)

col=0;

}

}

kbd_call_count=0;

}

return(kchar);

}

Page 40: User Manual - Final Project 2015

7.0 Program Code

Page 37 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 41: User Manual - Final Project 2015

7.0 Program Code

Page 38 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

/***************************************************************************

Define Ports for LCD

***************************************************************************/

#define LCD_DB4 PIN_D4

#define LCD_DB5 PIN_D5

#define LCD_DB6 PIN_D6

#define LCD_DB7 PIN_D7

#define LCD_E PIN_C5

#define LCD_RS PIN_C1

#define lcd_line_two 0x40

#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines

/***************************************************************************

int const LCD_INIT_STRING[5]

***************************************************************************/

int const LCD_INIT_STRING[4] =

{

0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots

0xc, // Display on

1, // Clear display

6 // Increment cursor

};

void lcd_send_nibble(int8 nibble)

{ // Note: !! converts an integer expression

// to a boolean (1 or 0).

output_bit(LCD_DB4, !!(nibble & 1));

output_bit(LCD_DB5, !!(nibble & 2));

output_bit(LCD_DB6, !!(nibble & 4));

output_bit(LCD_DB7, !!(nibble & 8));

delay_cycles(1);

output_high(LCD_E);

delay_us(2);

output_low(LCD_E);

Page 42: User Manual - Final Project 2015

7.0 Program Code

Page 39 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

}

/***************************************************************************

void lcd_send_byte(int address, int n)

***************************************************************************/

void lcd_send_byte(int address, int n)

{

output_low (LCD_RS);

delay_us (60);

if(address)

output_high (LCD_RS);

else

output_low (LCD_RS);

output_low(LCD_E);

delay_cycles(1);

lcd_send_nibble(n >> 4);

lcd_send_nibble(n & 0xf);

} // end lcd_send_byte

/***************************************************************************

void lcd_init(void)

***************************************************************************/

void lcd_init(void)

{

int i;

output_low(LCD_RS);

output_low(LCD_E);

delay_ms(15);

Page 43: User Manual - Final Project 2015

7.0 Program Code

Page 40 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

for(i=0 ;i < 3; i++)

{

lcd_send_nibble(0x03);

delay_ms(5);

} // end for

lcd_send_nibble(0x02);

for(i=0; i < sizeof(LCD_INIT_STRING); i++)

{

lcd_send_byte(0, LCD_INIT_STRING[i]);

} // end for

} // end lcd_init

/***************************************************************************

void lcd_putc(char c)

***************************************************************************/

void lcd_gotoxy(int8 x, int8 y)

{

int8 address;

if(y != 1)

address = lcd_line_two;

else

address=0;

address += x-1;

lcd_send_byte(0, 0x80 | address);

}

void lcd_putc(char c)

{

switch(c)

{

Page 44: User Manual - Final Project 2015

7.0 Program Code

Page 41 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

case '\b':

lcd_send_byte(0,1);

delay_ms(2);

break;

case '\f':

lcd_send_byte(0, 0x80 );

break;

case '\n':

lcd_send_byte(0, 0xc0 );

break;

case '\t':

lcd_send_byte(0, 0x89 );

break;

case '\U':

lcd_send_byte(0, 0xc9 );

break;

default:

lcd_send_byte(1,c);

break;

} // end switch

} // end lcd_putc

Page 45: User Manual - Final Project 2015

7.0 Program Code

Page 42 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

Page 46: User Manual - Final Project 2015

7.0 Program Code

Page 43 Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015

#include <16f877A.h>

#fuses HS, NOLVP, NOWDT, PUT

#use delay ( clock = 20000000 )

#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)