Thông báo

Collapse
No announcement yet.

[Help] Giúp mình xem lỗi code build bằng MPLab-CCS với

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • [Help] Giúp mình xem lỗi code build bằng MPLab-CCS với

    Mình mới tập viết code cho pic được 1 tuần, hiện đang thử viết một số code đơn giản. nhưng khi mình mô phỏng, lúc đầu nó chạy bình thường, sau một thời gian thì nó bị lỗi thế này:


    Còn đây là code của nó

    Code:
    #include <16F877A.h>
    #include "def_16F877A.h"
    #include "lcd_lib_8bit.c"
    #include "ds1307.c"
    
    #FUSES NOWDT //No Watch Dog Timer
    #FUSES HS //High speed Osc (> 4mhz)
    #FUSES NOPUT //No Power Up Timer
    #FUSES NOPROTECT //Code not protected from reading
    #FUSES NODEBUG //No Debug mode for ICD
    #FUSES NOBROWNOUT //No brownout reset
    #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
    #FUSES NOCPD //No EE protection
    
    #use delay(clock=20000000)
    #use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
    
    #define SET PIN_C0 // Nut menu-set
    #define UP PIN_C1 // Nut tang
    #define DOWN PIN_C2 // Nut giam
    #define MODE PIN_C6 // Nut chuyen che do Time & Count
    #define ZERO PIN_C7
    #define SPEAKER PIN_A3
    
    //===================== Khai bao bien ===========================
    int8 secon,minute,hour,date,wday,month,year;
    unsigned char time[17],days[17];
    
    //======================== Khai bao ham =========================
    void  update(int8 address,int8 var);
    void ds1307_def();
    void load_time();
    void display_hr();
    void set_incr(int address, char var);
    //====================== Ham chinh ========================
    void main()
    {
    //
    days[0]=' ';days[1]=' ';days[5]=' ';days[8]='/';days[11]='/';days[12]='2';days[13]='0';days[16]='\0';
    time[0]=' ';time[1]=' ';time[2]=' ';time[3]=' ';time[4]=' ';time[7]=':';time[10]=':';time[13]=' ';time[13]=' ';
    time[14]=' ';time[15]=' ';time[16]='\0';
    //
       set_tris_b(0x00);
       lcd_init();
       delay_ms(50);
       lcd_cmd(0x80);
       printf(lcd_putchar," DIGITAL CLOCK ");
       lcd_cmd(0xC0);
       printf(lcd_putchar," DESIGN BY TLG ");
       delay_ms(100);
       lcd_cmd(0x01);
       delay_ms(3);
       ds1307_def();
       while(TRUE)
       {  
          load_time();
          display_hr();
          set_incr(1,minute);
       }
    
    }
    
    
    //=================== Ham thiet lap thoi gian ban dau ========================
    void ds1307_def()
    {
    // Thiet lap mac dinh: 00:00:00 Sun 18/08/2013
    update(0x00,0b00000000);
    update(0x01,0b00000000);
    update(0x02,0b00000000);
    update(0x03,0b00000001);
    update(0x04,0b00011000);
    update(0x05,0b00001000);
    update(0x06,0b00010011);
    }
    
    //================ Ham up date thong tin =======================
    void  update(int8 address,int8 var) // address=dia chi, var=bien
    {
    i2c_start();
    i2c_write(0xD0);
    i2c_write(address);
    i2c_write(var);
    i2c_stop();
    }
    
    //
    void load_time()
    {
    i2c_start();
    i2c_write(0xD0);        //Gui dia chi cua slave
    i2c_write(0x00);        //Thiet lap lai con tro - set register pointer
    i2c_stop();
    i2c_start();
    i2c_write(0xD1);        // gui lenh doc du lieu
    secon  = i2c_read(1);    // starts w/last address stored in register pointer
    minute  = i2c_read(1);
    hour   = i2c_read(1);    //che do 24h ko can AND
    wday   = i2c_read(1);
    date  = i2c_read(1);
    month = i2c_read(1);
    year   = i2c_read(0);
    i2c_stop();
    /////
    switch (wday){
       case 2: {days[2]='M';days[3]='o';days[4]='n';}
       break;
       case 3: {days[2]='T';days[3]='u';days[4]='e';}
       break;
       case 4: {days[2]='W';days[3]='e';days[4]='d';}
       break;
       case 5: {days[2]='T';days[3]='h';days[4]='u';}
       break;
       case 6: {days[2]='F';days[3]='r';days[4]='i';}
       break;
       case 7: {days[2]='S';days[3]='a';days[4]='t';}
       break;
       case 1: {days[2]='S';days[3]='u';days[4]='n';}
       break;
    }
    /////
    time[12] = (secon&0x0F)+0x30;  // Tach' 4 bit thap roi tao ma hien thi tren lcd
    time[11] = swap(secon&0x70)+0x30; // Tach 4 bit cao roi tao ma hien thi tren lcd
    time[9] = (minute&0x0F)+0x30;
    time[8] = swap(minute&0x70)+0x30;
    time[6] = (hour&0x0F)+0x30;
    time[5] = swap(hour&0x70)+0x30;
    days[7] = (date&0x0F)+0x30;
    days[6] = swap(date&0x31)+0x30;
    days[10] = (month&0x0F)+0x30;
    days[9] = swap(month&0x10)+0x30;
    days[15] = (year&0x0F)+0x30;
    days[14] = swap(year&0xF0)+0x30;
    /////
    }
    
    void display_hr()
    {
    lcd_cmd(0x80);
    LCD_String(days,0);
    lcd_cmd(0xC0);
    LCD_String(time,0);
    }
    //======================= ham thu nut' bam =======================
    void set_incr(int address, char var)
    {
    int8 temp;
    int8 temph,templ;
    
    if(address==0||address==1){
    templ=(var&0x0F);
    temph=swap(var&0x70);
    }
    if(address==2){
    templ=(var&0x0F);
    temph=swap(minute&0x70);
    }
    if(address==3){
    templ=var;
    temph=0x00;
    }
    if(address==4){
    templ=(var&0x0F);
    temph=swap(var&0x30);
    }
    if(address==5){
    templ=(var&0x0F);
    temph=swap(var&0x10);
    }
    if(address==6){
    templ=(var&0x0F);
    temph=swap(var&0xF0);
    }
    if(!input(SET)){
    templ+=0x01;
       if(address==0||address==1){
          if(templ>0x09) {templ=0x00; temph+=0x01;}
          if(temph>=0x06) temph=0x00;
       }
       if(address==2){
          if(templ>0x09) {templ=0x00; temph+=0x01;}
          if(temph==0x02&&templ>0x03) {templ=0x00; temph=0x00;}
       }
       if(address==3){
          if(templ>0x07) templ=0x01;
       }
       if(address==4){
          if(templ>0x09) {templ=0x00; temph+=0x01;}
          if(temph>0x03) {temph=0x00; templ=0x01;}
       }
       if(address==5){
          if(templ>0x09) {templ=0x00; temph+=0x01;}
          if(temph>0x01) {temph=0x00; templ=0x01;}
       }
       if(address==6){
          if(templ>0x09) {templ=0x00; temph+=0x01;}
          if(temph>0x09) {temph=0x00;}
       }
    temp=swap(temph)|templ;
    update(address,temp);
    }
    
    }
    À, cho mình hỏi luôn, mình muốn vẽ một character riêng lên CGRAM trống rồi hiện lên lcd thì làm thế nào vậy?
    Ví dụ như:

    const char speaker_off[] = {0,4,13,30,28,12,20,0};
    hoặc
    const byte speaker_on[] = {0x00,0x04,0x0C,0x1C,0x1C,0x0C,0x04,0x00};


    Mong mọi người giúp đỡ.


    Click image for larger version

Name:	error.jpg
Views:	1
Size:	142.3 KB
ID:	1418442
    Last edited by ttlg59; 22-08-2013, 01:48.

Về tác giả

Collapse

ttlg59 Tìm hiểu thêm về ttlg59

Bài viết mới nhất

Collapse

Đang tải...
X