2. Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
By: Chafic Bouchakra
Submitted to: Stephen Ryan
ELN9204
Section: 010
Date: April 21, 2015
3. 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
4. 1.0 General Information
Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015 Page 1
1.0 General Information
5. 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 bouc0261@algonquinlive.com.
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.
6. 2.0 System Summary
Page 3Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
2.0 System Summary
7. 2.0 System Summary
Page 4Chafic 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.
8. 3.0 Visuals of the Hardware
Page 5Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
3.0 Visuals of the hardware
9. 3.0 Visuals of the Hardware
Page 6Chafic 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.
10. 3.0 Visuals of the Hardware
Page 7Chafic 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.
13. 5.0 Issues and Limitations
Page 10Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
5.0 Issues and Limitations
14. 5.0 Issues and Limitations
Page 11Chafic 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.
15. 6.0 Program Flowchart
Page 12Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
6.0 Program Flowchart
17. 7.0 Program Code
Page 14Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
7.0 Program Code
18. 7.0 Program Code
Page 15Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
19. 7.0 Program Code
Page 16Chafic 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
20. 7.0 Program Code
Page 17Chafic 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);
}
}
}
}
21. 7.0 Program Code
Page 18Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
22. 7.0 Program Code
Page 19Chafic 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
}
}
}
23. 7.0 Program Code
Page 20Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
24. 7.0 Program Code
Page 21Chafic 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
25. 7.0 Program Code
Page 22Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
26. 7.0 Program Code
Page 23Chafic 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
27. 7.0 Program Code
Page 24Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
28. 7.0 Program Code
Page 25Chafic 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' );
}
29. 7.0 Program Code
Page 26Chafic Bouchakra, 040-622-744, Interfacing Final Project Winter 2015
30. 7.0 Program Code
Page 27Chafic 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);