Sunday 26 August 2012

WEEK 26 - WEEK 29

Objective:

  • Program for the push button for set the time

Contents:
  • Make a programming for push button to select the time 
  • Load to PIC16F887 


 switch(mode){
 case 0: case 1:{
  lcd_goto(0,0);                  //select first line in LCD // will show at first line LCD
  lcd_string("Time     : ");
  if((set==1)&(counter[0]<PULSE/2)) lcd_string("  ");
  else lcd_number(hrs,10,2);
  lcd_string(":");
  if((set==2)&(counter[0]<PULSE/2)) lcd_string("  ");
  else lcd_number(min,10,2);
  lcd_string(":");
  if((set==3)&(counter[0]<PULSE/2)) lcd_string("  ");
  else  lcd_number(sec,10,2);
  lcd_string(" ");

  lcd_goto(0,1);              //select second line in LCD // will show at second line LCD
  lcd_string("Date     : ");
  if((set==4)&(counter[0]<PULSE/2)) lcd_string("  ");
  else  lcd_number(date,10,2);
  lcd_string("/");
  if((set==5)&(counter[0]<PULSE/2)) lcd_string("  ");
  else  lcd_number(month,10,2);
  lcd_string("/");
  if((set==6)&(counter[0]<PULSE/2)) lcd_string("  ");
  else   lcd_number(year,10,2);
  lcd_string(" ");

  if(mode==0){
   if(button==3){
    set=1;
    mode++;
    delay(200);}}
  else if(mode==1){
   switch(button){

                         case 1:{
                             mode++;
                             set=0;
                             delay(200);
                             break;}

                             case 2:{
if(set<6) set++;
else set=1;
delay(200);
break;}

case 4:{
switch(set){
case 1:{
if(hrs<23) hrs++;
else hrs=0;
rtc_set(2,hrs);               //set hours
break;}

case 2:{
if(min<59) min++;
else min=0;
rtc_set(1,min);           //set min
break;}

case 3:{
sec=0;
rtc_set(0,sec);           //set sec
break;}

case 4:{
if(date<mo[month-1]) date++;
else date=1;
rtc_set(4,date);                       //set date
break;}

case 5:{
if(month<12) month++;
else month=1;
rtc_set(5,month);               //set month
break;}

case 6:{
if(year<99) year++;
else year=0;
rtc_set(6,year);                  //set year
break;}
}
delay(200);
break;}

case 8:{
switch(set){

case 1:{
if(hrs>0) hrs--;
else hrs=23;
rtc_set(2,hrs);              //set hours
break;}

case 2:{
if(min>0) min--;
else min=59;
rtc_set(1,min);         //set min
break;}

case 3:{
sec=0;
rtc_set(0,sec); //set sec
break;}

case 4:{
if(date>1) date--;
else date=mo[month-1];
rtc_set(4,date);              //set date
break;}

case 5:{
if(month>1) month--;
else month=12;
rtc_set(5,month);                //set month
break;}

case 6:{
if(year>0) year--;
else year=99;
rtc_set(6,year);                     //set year
break;}
}
delay(200);
break;}
}
}
break;}

Conclusion:
              The programming for the push button has been successful to load into PIC. For next step, i will programming for the GSM.
  

Saturday 4 August 2012

Week 23- Week 25

Objectives: 

  • Start to programming for LCD string
  • To show output in LCD


Contents: 

  • For make sure this programming suitable in LCD 4x16



//Project: GSM Solar Monitoring
//Programmer:Khairul Hasnadi Mohmad
//PIC: PIC16F887
//Crystal Frequency: 20MHz
//Compiler: HI-TECH ANSI C V9.83
//Last Modified: 14 August 2012
//Website: http://hasnady.blogspot.com

#define PUSH_BUTTON
#define ADC
#define UART
#define I2C
#define HD44780
#define GSM_MODEM
#define PULSE 9000
#define SEL RC0

#include <htc.h>
#include "LibraryHardware.h"
#include "LibraryPeripheral.h"
#include "LibraryUniversalDisplay.h"
#include "LibraryAtCommand.h"
#include "LibraryDS1307.h"

void pic_init(void);
char read_button(void);
char sms(void);

unsigned int counter[3];

__EEPROM_DATA('0','1','3','6','2','8','9','8');                     //memory data can save 256 Byte
__EEPROM_DATA('4','4',1,1,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);
__EEPROM_DATA(0,0,0,0,0,0,0,0);

static void interrupt isr(void)
{
if(RCIF==1){                                                                     // Receive message 
counter[0]=0;
get_phone_message();}
if(TMR0IF==1){                                                                // Time Receive message 
TMR0IF=0;
reset_yz();
if(counter[0]<PULSE) counter[0]++;
else counter[0]=0;
}
}

main()
{
unsigned char button,temp,disp=0,mode=0;
unsigned char set=0,mo[12]={31,28,31,30,31,30,31,31,30,31,30,31};
unsigned char sec,min,hrs,date,month,year,time[2];
int i,j,voltage,current;
pic_init(); //initialize PIC
uart_init(9600); //initialize UART
i2c_init(10); //initialize I2C
lcd_init(); //initialize LCD
phone[0]='+';
phone[1]='6';
for(i=0;i<=9;i++) phone[i+2]=eeprom_read(i);
phone[12]=0xFF;
for(i=0;i<=1;i++) time[i]=eeprom_read(i+10);

lcd_goto(0,0); //select first line
lcd_string("     GSM MODEM      ");                          // Show the " GSM MODEM" in first line
while(gsm_ready()==0) continue;
lcd_goto(0,1); //select second line
lcd_string("       READY        ");                                                 // Show "READY" in second line
sms_delete(1);
delay(1000);

#if defined (PCD8544)
/* initialise the GLCD */                                                         //we use the LCD 4x16
void glcd_init(void)
{
GLCD_RST=1;                                                                      // graphical LCD
__delay_ms(15);
glcd_write(0x21); //function set
glcd_write(0xC0); //set Vlcd
glcd_write(0x13); //set Bias System
glcd_write(0x20); //function set
glcd_write(0x0C); //sets display configuration
}

/* write serial byte to the GLCD */
void glcd_write(unsigned char c)
{int i;
GLCD_CS=0;
for(i=0;i<=7;i++){
GLCD_SDA=(c>>(7-i))&1;
GLCD_SCLK=0;
GLCD_SCLK=1;}
GLCD_CS=1;
}

/* go to the specified x&y location */
void glcd_goto(char x, char y)
{
GLCD_DC=0;
glcd_write(0x80+x);
glcd_write(0x40+y);
GLCD_DC=1;
}

/* write a character to the LCD */
void glcd_char(char c)
{int i;
if((c<32)|(c>127)) c=' ';
for(i=0;i<5;i++) glcd_write(FontLookup[c-32][i]);
glcd_write(0x00);
}

/* write a string of chars to the GLCD */
void glcd_string(const char *s)
{
GLCD_DC=1;
while(*s)
glcd_char(*s++);
}

void glcd_number(unsigned int no, char base, char digit)
{char i,j,di[10];
for(i=0;i<=9;i++) di[i]=0;
i=0;
do{
di[i]=no%base;
no=no/base;
i=i+1;}
while(no!=0);
for(j=digit;j>0;j--){
if(di[j-1]<=9) glcd_char(di[j-1]+'0');
else glcd_char(di[j-1]-10+'A');}
}
#endif

Result&Analysis



  • Programming successful for LCD screen 2X16 when the compiler in mp lab not detect another problem in the programming.

Conclusion
  • As a conclusion, i know how to program of the LCD screen in PIC 16F887. For next step, i need to program for the push button for select the time.