Xin chào anh chị! Hiện tại em đang làm đồ án điều khiển thiết bị qua sms, em viết code bằng hitech, thầy có đưa ra một yêu cầu là lưu trạng thái khi mất nguồn. E không rành về phần này. Các anh chị có thể giúp em được không ạ. Em cảm ơn.
đây là phần lập trình của e về pic 16f887a
#include <htc.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "uart.h" // Cau hinh ket noi va khai bao lenh dung cho giao tiep UART.
#include "lcd.h" // Cau hinh ket noi va khai bao lenh dung cho LCD.
#include "internal_eeprom.h
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
#define _XTAL_FREQ 4000000
unsigned char buff_sms[90];
unsigned char dem,khoa,tinmoi;
unsigned char buff;
//************************************************** ************
// Khai bao cac thiet bi,nut nhan voi PIC
//************************************************** ************
#define DENVANG RE0
#define DENXANH RE1
//************************************************** ************
// KHAI BAO BIEN CHO LCD
//************************************************** ************
unsigned int i,j;
const char process[]="Processing......";
//************************************************** ************
// CTC delay - tao thoi gian tre
//************************************************** ************
void delay(unsigned char ms)
{
unsigned char i;
for(i=0;i<ms;i++)
__delay_ms(100);
}
//************************************************** ************
// CTC uart_AT xuat ki tu de giao tiep UART
//************************************************** ************
void uart_AT(const char *s)
{
uart_puts(s);
uart_putc(13);
}
//************************************************** ************
// CTC Cau hinh cho sim900
//************************************************** ************
void cauhinh_sim900()
{
uart_AT("AT");//kiem tra ket noi mang
delay(5);
uart_AT("ATZ");//reset modulm
delay(5);
uart_AT("ATZ");
delay(5);
uart_AT("ATE0");//tat che do echo
delay(5);
uart_AT("AT+ICF=3,3");// che do start-stop (8 Data 1 Stop)
delay(5);
uart_AT("AT+IPR=9600");// thiet lap toc do truyen
delay(5);
uart_AT("AT&W");//luu cau hinh hien tai
delay(5);
uart_AT("AT+CMGF=1");// dang du lieu dinh dang text de gui tin
delay(5);
uart_AT("AT+CNMI=2,0,0,0,0");//thiet lap che do nhan tin nhan tu mang
delay(5);
uart_AT("AT+CSAS");// luu thiet lap
delay(5);
}
//************************************************** ************
// CTC gui tin nhan
//************************************************** ************
void send_sms(const char *text)
{
uart_puts("AT+CMGS=");//lenh gui tin nhan AT+CMGS = "0933201339"
uart_putc(34);
uart_puts("0933201339");
uart_putc(34);
uart_putc(13);//<cr>
delay(3);
uart_puts(text);//data sms
delay(5);
uart_putc(26);
uart_putc(13);
delay(10);
}
//************************************************** ************
// CTC xoa tat ca cac tin nhan
//************************************************** ************
void xoa_het_sms()
{
uart_puts("AT+CMGDA =");
uart_putc(34);
uart_puts("DEL ALL");
uart_putc(34);
uart_putc(13);//<cr>
delay(5);
}
//************************************************** ************
// CTC Xoa buffer
//************************************************** ************
void clear_buff()
{
char i=0;
for(i=0;i<strlen(buff_sms);i++)
{
buff_sms[i]=0x00;
}
}
//************************************************** ************
// CTC Kiem tra so sanh 2 chuoi
//************************************************** ************
char test_string(const char buffer[], const char str[])
{
unsigned char leng_buff = 0;
unsigned char leng_str = 0;
char i, j;
leng_buff = strlen(buffer);
leng_str = strlen(str);
if (leng_buff >= leng_str)
{
for (i = 0; i <= leng_buff - leng_str; i++)
{
for (j = 0; j < leng_str; j++)
{
if (buffer[i + j] != str[j])
break;
}
if (j == leng_str)
return 1;
}
return 0;
}
return 0;
}
//************************************************** ************
// CT Ngat
//************************************************** ************
void interrupt isr()
{
unsigned char data;
//Ngat UART
if(RCIE && RCIF) // Kiem tra cho phep ngat thu cua UART.
{
RCIF = 0; // Xoa co ngat thu.
data=uart_getc();
//Cu phap dieu khien thiet bi
if(data=='*')
{ khoa=0;
tinmoi=1;}
if(khoa==1)
{ buff_sms[dem]=data;
dem++;}
if(data=='+')
{ khoa=1;
dem=0;}
}
}
//************************************************** ************
// CTC khoi dong he thong
// cau hinh SIM, hien thi trang thai ban dau
//************************************************** ************
void khoidong(void)
{
int i;
lcd_gotoxy(0,0);
lcd_puts("\f System\n Processing");
cauhinh_sim900(); //Cau hinh sim900
lcd_gotoxy(0,0);
lcd_puts("\f System\n OK");//hien thi 500ms
}//finish khoi dong
//************************************************** ************
// CTC DIEU KHIEN THIET BI QUA TN
//************************************************** ************
void dieukhien()
{
//DIEU KHIEN DEN XANH
if(test_string(buff_sms,"batxanh"))
{ DENXANH=1; //Bat den xanh
lcd_puts("\f DEN XANH\n Da duoc bat"); //Hien thi LCD
send_sms("DEN XANH da duoc bat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
if(test_string(buff_sms,"tatxanh"))
{ DENXANH=0; //Tat den xanh
lcd_puts("\f DEN XANH\n Da duoc tat"); //Hien thi LCD
send_sms("DEN XANH da duoc tat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
//DIEU KHIEN DEN VANG
if(test_string(buff_sms,"batvang"))
{ DENVANG=1; //Bat den vang
lcd_puts("\f DEN VANG\n Da duoc bat"); //Hien thi LCD
send_sms("DEN VANG da duoc bat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
if(test_string(buff_sms,"tatvang"))
{ DENVANG=0; //Bat den vang
lcd_puts("\f DEN VANG\n Da duoc tat"); //Hien thi LCD
send_sms("DEN VANG da duoc tat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
//BAT TAT CA CAC THIET BI
if(test_string(buff_sms,"onall"))
{ DENXANH=1;DENVANG=1;
lcd_puts("\f DEN XANH-VANG\n DA DUOC BAT");
send_sms("DEN XANH-DEN VANG\nDA DUOC BAT");
clear_buff();
xoa_het_sms();
return;
}
//TAT TAT CA CAC THIET BI
if(test_string(buff_sms,"offall"))
{ DENXANH=0;DENVANG=0;
lcd_puts("\f DEN XANH-VANG\n DA DUOC TAT");
send_sms("DEN XANH-DEN VANG\nDA DUOC TAT");
clear_buff();
xoa_het_sms();
return;
}
//KIEM TRA HE THONG
if(test_string(buff_sms,"kiemtra")) // KIEM TRA TINH TRANG CUA THIET BI
{
lcd_puts("\f Dang kiem tra ");
if(DENXANH==0&&DENVANG==0)
send_sms("DENXANH: OFF \nDENVANG: OFF");
if(DENXANH==0&&DENVANG==1)
send_sms("DENXANH: OFF \nDENVANG: ON");
if(DENXANH==1&&DENVANG==0)
send_sms("DENXANH: ON \nDENVANG: OFF");
if(DENXANH==1&&DENVANG==1)
send_sms("DENXANH: ON \n DENVANG: ON");
clear_buff();
xoa_het_sms();
return;
}
}//fin CTC dieukhien
//************************************************** ************
// CHUONG TRINH CHINH
//************************************************** ************
void main()
{
//khai bao cau hinh
ANSEL=0; //Cau hinh cac chan analog deu la digital I/O
ANSELH=0;
TRISA=0b111110;//Chon cac chan de I/O
// TRISB=0b00001111;
// PORTB=0X00;
TRISD=0b00000000;
TRISE=0b000;
PORTE=0b000;
uart_init();//khoi dong UART
lcd_init(); //khoi dong LCD theo cau hinh 4bit
//Cau hinh cho phep ngat Uart
// INTCON = | GIE | PEIE | T0IE | INTE | RBIE | T0IF | INTF | RBIF |
RCIE = 1; // Cho phep ngat RX (UART).
GIE = 1; // Cho phep ngat toan cuc.
PEIE = 1; // Cho phep ngat ngoai vi.
//CHUONG TRINH
khoidong();
while(1)
{
if(tinmoi==1)
dieukhien();
}
}
đây là phần lập trình của e về pic 16f887a
#include <htc.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "uart.h" // Cau hinh ket noi va khai bao lenh dung cho giao tiep UART.
#include "lcd.h" // Cau hinh ket noi va khai bao lenh dung cho LCD.
#include "internal_eeprom.h
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
#define _XTAL_FREQ 4000000
unsigned char buff_sms[90];
unsigned char dem,khoa,tinmoi;
unsigned char buff;
//************************************************** ************
// Khai bao cac thiet bi,nut nhan voi PIC
//************************************************** ************
#define DENVANG RE0
#define DENXANH RE1
//************************************************** ************
// KHAI BAO BIEN CHO LCD
//************************************************** ************
unsigned int i,j;
const char process[]="Processing......";
//************************************************** ************
// CTC delay - tao thoi gian tre
//************************************************** ************
void delay(unsigned char ms)
{
unsigned char i;
for(i=0;i<ms;i++)
__delay_ms(100);
}
//************************************************** ************
// CTC uart_AT xuat ki tu de giao tiep UART
//************************************************** ************
void uart_AT(const char *s)
{
uart_puts(s);
uart_putc(13);
}
//************************************************** ************
// CTC Cau hinh cho sim900
//************************************************** ************
void cauhinh_sim900()
{
uart_AT("AT");//kiem tra ket noi mang
delay(5);
uart_AT("ATZ");//reset modulm
delay(5);
uart_AT("ATZ");
delay(5);
uart_AT("ATE0");//tat che do echo
delay(5);
uart_AT("AT+ICF=3,3");// che do start-stop (8 Data 1 Stop)
delay(5);
uart_AT("AT+IPR=9600");// thiet lap toc do truyen
delay(5);
uart_AT("AT&W");//luu cau hinh hien tai
delay(5);
uart_AT("AT+CMGF=1");// dang du lieu dinh dang text de gui tin
delay(5);
uart_AT("AT+CNMI=2,0,0,0,0");//thiet lap che do nhan tin nhan tu mang
delay(5);
uart_AT("AT+CSAS");// luu thiet lap
delay(5);
}
//************************************************** ************
// CTC gui tin nhan
//************************************************** ************
void send_sms(const char *text)
{
uart_puts("AT+CMGS=");//lenh gui tin nhan AT+CMGS = "0933201339"
uart_putc(34);
uart_puts("0933201339");
uart_putc(34);
uart_putc(13);//<cr>
delay(3);
uart_puts(text);//data sms
delay(5);
uart_putc(26);
uart_putc(13);
delay(10);
}
//************************************************** ************
// CTC xoa tat ca cac tin nhan
//************************************************** ************
void xoa_het_sms()
{
uart_puts("AT+CMGDA =");
uart_putc(34);
uart_puts("DEL ALL");
uart_putc(34);
uart_putc(13);//<cr>
delay(5);
}
//************************************************** ************
// CTC Xoa buffer
//************************************************** ************
void clear_buff()
{
char i=0;
for(i=0;i<strlen(buff_sms);i++)
{
buff_sms[i]=0x00;
}
}
//************************************************** ************
// CTC Kiem tra so sanh 2 chuoi
//************************************************** ************
char test_string(const char buffer[], const char str[])
{
unsigned char leng_buff = 0;
unsigned char leng_str = 0;
char i, j;
leng_buff = strlen(buffer);
leng_str = strlen(str);
if (leng_buff >= leng_str)
{
for (i = 0; i <= leng_buff - leng_str; i++)
{
for (j = 0; j < leng_str; j++)
{
if (buffer[i + j] != str[j])
break;
}
if (j == leng_str)
return 1;
}
return 0;
}
return 0;
}
//************************************************** ************
// CT Ngat
//************************************************** ************
void interrupt isr()
{
unsigned char data;
//Ngat UART
if(RCIE && RCIF) // Kiem tra cho phep ngat thu cua UART.
{
RCIF = 0; // Xoa co ngat thu.
data=uart_getc();
//Cu phap dieu khien thiet bi
if(data=='*')
{ khoa=0;
tinmoi=1;}
if(khoa==1)
{ buff_sms[dem]=data;
dem++;}
if(data=='+')
{ khoa=1;
dem=0;}
}
}
//************************************************** ************
// CTC khoi dong he thong
// cau hinh SIM, hien thi trang thai ban dau
//************************************************** ************
void khoidong(void)
{
int i;
lcd_gotoxy(0,0);
lcd_puts("\f System\n Processing");
cauhinh_sim900(); //Cau hinh sim900
lcd_gotoxy(0,0);
lcd_puts("\f System\n OK");//hien thi 500ms
}//finish khoi dong
//************************************************** ************
// CTC DIEU KHIEN THIET BI QUA TN
//************************************************** ************
void dieukhien()
{
//DIEU KHIEN DEN XANH
if(test_string(buff_sms,"batxanh"))
{ DENXANH=1; //Bat den xanh
lcd_puts("\f DEN XANH\n Da duoc bat"); //Hien thi LCD
send_sms("DEN XANH da duoc bat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
if(test_string(buff_sms,"tatxanh"))
{ DENXANH=0; //Tat den xanh
lcd_puts("\f DEN XANH\n Da duoc tat"); //Hien thi LCD
send_sms("DEN XANH da duoc tat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
//DIEU KHIEN DEN VANG
if(test_string(buff_sms,"batvang"))
{ DENVANG=1; //Bat den vang
lcd_puts("\f DEN VANG\n Da duoc bat"); //Hien thi LCD
send_sms("DEN VANG da duoc bat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
if(test_string(buff_sms,"tatvang"))
{ DENVANG=0; //Bat den vang
lcd_puts("\f DEN VANG\n Da duoc tat"); //Hien thi LCD
send_sms("DEN VANG da duoc tat"); //Bao cao
clear_buff(); //Xoa bo dem
xoa_het_sms(); //Xoa tin nhan
return;
}
//BAT TAT CA CAC THIET BI
if(test_string(buff_sms,"onall"))
{ DENXANH=1;DENVANG=1;
lcd_puts("\f DEN XANH-VANG\n DA DUOC BAT");
send_sms("DEN XANH-DEN VANG\nDA DUOC BAT");
clear_buff();
xoa_het_sms();
return;
}
//TAT TAT CA CAC THIET BI
if(test_string(buff_sms,"offall"))
{ DENXANH=0;DENVANG=0;
lcd_puts("\f DEN XANH-VANG\n DA DUOC TAT");
send_sms("DEN XANH-DEN VANG\nDA DUOC TAT");
clear_buff();
xoa_het_sms();
return;
}
//KIEM TRA HE THONG
if(test_string(buff_sms,"kiemtra")) // KIEM TRA TINH TRANG CUA THIET BI
{
lcd_puts("\f Dang kiem tra ");
if(DENXANH==0&&DENVANG==0)
send_sms("DENXANH: OFF \nDENVANG: OFF");
if(DENXANH==0&&DENVANG==1)
send_sms("DENXANH: OFF \nDENVANG: ON");
if(DENXANH==1&&DENVANG==0)
send_sms("DENXANH: ON \nDENVANG: OFF");
if(DENXANH==1&&DENVANG==1)
send_sms("DENXANH: ON \n DENVANG: ON");
clear_buff();
xoa_het_sms();
return;
}
}//fin CTC dieukhien
//************************************************** ************
// CHUONG TRINH CHINH
//************************************************** ************
void main()
{
//khai bao cau hinh
ANSEL=0; //Cau hinh cac chan analog deu la digital I/O
ANSELH=0;
TRISA=0b111110;//Chon cac chan de I/O
// TRISB=0b00001111;
// PORTB=0X00;
TRISD=0b00000000;
TRISE=0b000;
PORTE=0b000;
uart_init();//khoi dong UART
lcd_init(); //khoi dong LCD theo cau hinh 4bit
//Cau hinh cho phep ngat Uart
// INTCON = | GIE | PEIE | T0IE | INTE | RBIE | T0IF | INTF | RBIF |
RCIE = 1; // Cho phep ngat RX (UART).
GIE = 1; // Cho phep ngat toan cuc.
PEIE = 1; // Cho phep ngat ngoai vi.
//CHUONG TRINH
khoidong();
while(1)
{
if(tinmoi==1)
dieukhien();
}
}