PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 12-08-2011, 03:46 PM   #1
princedx
Nhập môn đệ tử
 
Tham gia ngày: Oct 2010
Bài gửi: 2
:
cho em hỏi ạ

#include <16F877A.h>
#include <def_877a.h>
#device *=16 adc=10
#include <string.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)
#include <lcd_lib_4bit1.c> // file giao tiep LCD

#define l1 RD0
#define l2 RD1
#define l3 RD2
#define l4 RD3
#define l5 RD4
#define rl1 RC0
#define rl2 RC1
//#define bt RC4

unsigned char str[60],str1[20],str2[20],str3[13];
char ch; int is1=0;
int kt,is,isti,ngat,idex,idex1,index,index1;
int32 read,temp,as; int nd;int16 high1,high,low;
int32 temp1,temp2; int isas=0;
//=============================================
#INT_RDA
Receive_isr()
{
ch = getc();
if(isti==1) //cho phep nhan khi da khoi tao thanh cong
{
if(ch==10 && index ==0) is=1; //bat dau cho phep nhan chuoi
if(ch==10 && index>0) {idex=index; ngat=1; isti = 2; } //KHI KET THUC MOT CHUOI
if(ch!=10 && ch!=13 && is == 1) { str[index] = ch; index++; }
}
if(isti==2) //cho phep nhan khi da khoi tao thanh cong
{
if(ch==10 && index1 ==0) is=1; //bat dau cho phep nhan chuoi
if(ch==10 && index1>0) {idex1=index1; ngat=2; isti = 0; } //KHI KET THUC MOT CHUOI
if(ch!=10 && ch!=13 && is == 1) { str1[index1] = ch; index1++; }
}
}
//------------
void send_sms()
{
putc('A'); putc('T'); putc('+'); putc('C'); putc('M'); putc('G'); putc('S'); putc('='); putc(34);
putc('0'); putc('9'); putc('7'); putc('5'); putc('1'); putc('9'); putc('1'); putc('4'); putc('0');
putc('9'); putc(34); putc(13); delay_ms(200);
putc(str3[0]); putc(str3[1]); putc(str3[2]); putc(str3[3]); putc(str3[4]); putc(str3[5]);
putc(str3[6]); putc(str3[7]); putc(str3[8]); putc(str3[9]); putc(str3[10]); putc(str3[11]); putc(str3[12]);putc(26); putc(13);
delay_ms(200);
putc('A'); putc('T'); putc('+'); putc('C'); putc('M'); putc('G'); putc('S'); putc('='); putc(34);
putc('0'); putc('9'); putc('3'); putc('7'); putc('7'); putc('0'); putc('5'); putc('3'); putc('7');
putc('9'); putc(34); putc(13); delay_ms(200);
putc(str3[0]); putc(str3[1]); putc(str3[2]); putc(str3[3]); putc(str3[4]); putc(str3[5]);
putc(str3[6]); putc(str3[7]); putc(str3[8]); putc(str3[9]); putc(str3[10]); putc(str3[11]); putc(str3[12]);putc(26); putc(13);
//----------------
int kiemtra()// lenh tu dt de dk role
{
int j,k;
for(j=0;j<idex;j++)
{
if(str2[0]==str[j])
{
for(k=0;k<Strlen(str2);k++)
{
if(str2[k] != str[j+k]) break;
}
if(k==strlen(str2)) { idex=0; index = 0; return 1; }
}
}
idex=0; index = 0; return 0;
}
//-------------------
int kiemtra1()
{
int j,k;
for(j=0;j<idex1;j++)
{
if(str2[0]==str1[j])
{
for(k=0;k<strlen(str2);k++)
{
if(str2[k] != str1[j+k]) break;
}
if(k==strlen(str2)) { return 1; }
}
}
return 0;
}
void chuyen(int32 x)
{
high1 = x/100;
high=(x%100)/10;
low=x%10; //chia lay phan du, so hang don vi

low = low + 0x30;
high = high + 0x30;
high1 = high1 + 0x30;
}
//-------
void nhietdothap()
{
LCD_putcmd(0x80);
Printf(LCD_putchar,"NHIET DO THAP ");
LCD_putcmd(0xC0);
Printf(LCD_putchar," SE CANH BAO");
delay_ms(500); l1 = 1; rl1 = 1; delay_ms(600); l1 = 0; delay_ms(600); l1 = 1; delay_ms(600); l1 = 0; delay_ms(600);
l1 = 1; delay_ms(600); l1 = 0; delay_ms(600);
//gui tin nhan
delay_ms(500);
isti = 0; //
strcpy(str3,"NHIET DO THAP"); send_sms();delay_ms(500);
LCD_putcmd(0xC0); Printf(LCD_putchar,"ND thap dang goi");
delay_ms(200);
puts("atd 0975191409;");
delay_ms(20000);
puts("ath");
isti = 1;
rl1 = 0;
}
void nhietdocao()
{
LCD_putcmd(0x80);
Printf(LCD_putchar,"NHIET DO CAO ");
LCD_putcmd(0xC0);
Printf(LCD_putchar," SE CANH BAO");
delay_ms(500); l5 = 1;rl1 = 1; delay_ms(600); l5 = 0; delay_ms(600); l5 = 1; delay_ms(600); l5 = 0; delay_ms(600);
l5 = 1; delay_ms(600); l5 = 0; delay_ms(600);
//gui tin nhan
delay_ms(500);
isti = 0; //
strcpy(str3,"NHIET DO CAO "); send_sms(); delay_ms(500);// day chuoi ki tu qua tap lenh sms
LCD_putcmd(0xC0); Printf(LCD_putchar,"ND cao dang goi ");
delay_ms(200);
puts("atd 0975191409;");
delay_ms(20000);
puts("ath");
isti = 1;
rl1 = 0;
}
void baoasyeu()
{
LCD_putcmd(0x80);
Printf(LCD_putchar,"DO SANG YEU ");
LCD_putcmd(0xC0);
Printf(LCD_putchar,"GUI TIN CANH BAO");
delay_ms(500); l2 = 1; rl1 = 1; delay_ms(600); l2 = 0; delay_ms(600); l2 = 1; delay_ms(600); l2 = 0; delay_ms(600);
l2 = 1; delay_ms(600); l2 = 0; delay_ms(600);
//gui tin nhan
delay_ms(500);
isti = 0; //
strcpy(str3,"DO SANG YEU "); send_sms(); delay_ms(500);
isti = 1;
rl1 = 0;
}
//===========Chuong trinh chinh================
void main()
{


//===========================================
TRISA=0xff; // Thiet lap chan vao ra
TRISB=0x00;
TRISC=0xF0;
TRISD=0x00;
TRISE=0x00;
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
delay_ms(50);
enable_interrupts(int_rda);
ENABLE_INTERRUPTS(GLOBAL);
delay_ms(10);
//===================
l1 = l2 = l3 = l4 = l5 = 0; rl1 = rl2 = 0;
//=====================
LCD_init();
Printf(LCD_putchar,"D_KHIEN QUA SMS ");
LCD_putcmd(0xC0);
Printf(LCD_putchar,"Dang khoi tao...");
delay_ms(1200);
while(true)
{
index = 0; is = 0; isti = 0;
puts("AT"); putc(13); isti = 1; delay_ms(300);
Printf(LCD_putchar,str);
strcpy(str2,"OK");
if(kiemtra()==1)
{
LCD_putcmd(0xC0);
Printf(LCD_putchar,"Da khoi tao xong");
puts("ATE0"); putc(13); delay_ms(100);
puts("AT+CMGF=1"); putc(13); delay_ms(300);
puts("AT+CMGD=1"); putc(13); delay_ms(300);
break;
}
delay_ms(700);
}
while(true)
{
LCD_putcmd(0x80);
Printf(LCD_putchar,"NHIET DO LA: ");
LCD_putcmd(0x8F);
Printf(LCD_putchar,"C");
set_adc_channel(0);
read=read_adc();
delay_ms(500);
temp = (int)read*500/102,3;
temp1 = (temp / 100);
temp = temp % 100;
temp2 = (temp / 10);
nd = temp1 * 10 + temp2;
LCD_putcmd(0x8D);
chuyen(nd); LCD_putchar(high); LCD_putchar(low);
LCD_putcmd(0xC0); Printf(LCD_putchar,"DO SANG: ");
set_adc_channel(1);
read=read_adc();
delay_ms(500);
temp = read*500/1023;
as = temp*2;
if(as<400)
{ chuyen(as);LCD_putcmd(0xC9); LCD_putchar(high1); LCD_putchar(high); LCD_putchar(low);
Printf(LCD_putchar," lux"); if(isas==0) { baoasyeu(); isas = 1; }
}
else
{
LCD_putcmd(0xC0); Printf(LCD_putchar,"DO SANG >400 lux"); isas = 0;
}

if(nd<20) nhietdothap(); if(nd>50) nhietdocao();
if(ngat!=0) //co tin hieu dieu khien
{
isti = 0;// bien kt tin hieu dk
LCD_putcmd(0xC0);
Printf(LCD_putchar,"Co tin hieu dk ");
strcpy(str2,"CMTI");
if(kiemtra()==1)
{
kt = 1;
while(kt<2)
{
idex = 0; idex1 = 0; index = 0; index1 = 0; ngat = 0; isti = 0; is = 0;
puts("at+cmgr=1"); putc(13); isti = 1;
while(ngat != 2) {} isti = 0;
//------------
strcpy(str2,"BATDEN");
if(kiemtra1()==1)
{
strcpy(str3,"DA BAT DEN "); send_sms(); delay_ms(300); RL2 = 1; is1 = 1; kt=2;
}
else
{
strcpy(str2,"TATDEN");
if(kiemtra1()==1)
{
strcpy(str3,"DA TAT DEN "); send_sms(); delay_ms(300); RL2 = 0; is1 = 0; kt=2;
}
}
//------------------
kt++;
if(kt==2) { strcpy(str3,"SAI CU PHAP"); send_sms(); delay_ms(300); }
puts("at+cmgd=1"); putc(13); delay_ms(800);
} //end kt < 2
} //end kt CMTI
idex = 0; idex1 = 0; index1 = 0; index = 0; ngat = 0; isti = 1; is = 0;
} //end co tin hieu dk
}
}
//========= END MAIN ==============


đoạn code trên viết cho pic với sim 300 mấy anh pro có thể giải thích cho em 2 khúc được ko ạ?
Đoạn thứ 1
Receive_isr()
{
ch = getc();
if(isti==1) //cho phep nhan khi da khoi tao thanh cong
{
if(ch==10 && index ==0) is=1; //bat dau cho phep nhan chuoi
if(ch==10 && index>0) {idex=index; ngat=1; isti = 2; } //KHI KET THUC MOT CHUOI
if(ch!=10 && ch!=13 && is == 1) { str[index] = ch; index++; }
}
if(isti==2) //cho phep nhan khi da khoi tao thanh cong
{
if(ch==10 && index1 ==0) is=1; //bat dau cho phep nhan chuoi
if(ch==10 && index1>0) {idex1=index1; ngat=2; isti = 0; } //KHI KET THUC MOT CHUOI
if(ch!=10 && ch!=13 && is == 1) { str1[index1] = ch; index1++; }
}
đoạn thứ 2:
\putc('A'); putc('T'); putc('+'); putc('C'); putc('M'); putc('G'); putc('S'); putc('='); putc(34);
putc('0'); putc('9'); putc('7'); putc('5'); putc('1'); putc('9'); putc('1'); putc('4'); putc('0');
putc('9'); putc(34); putc(13); delay_ms(200);
putc(str3[0]); putc(str3[1]); putc(str3[2]); putc(str3[3]); putc(str3[4]); putc(str3[5]);
putc(str3[6]); putc(str3[7]); putc(str3[8]); putc(str3[9]); putc(str3[10]); putc(str3[11]); putc(str3[12]);putc(26); putc(13);
mấy cái putc(34); putc(13); delay_ms(200);
putc(str3[0]); putc(str3[1]); putc(str3[2]); putc(str3[3]); putc(str3[4]); putc(str3[5]);
putc(str3[6]); putc(str3[7]); putc(str3[8]); putc(str3[9]); putc(str3[10]); putc(str3[11]); putc(str3[12]);putc(26); putc(13);
có nghĩa là gì vậy ạ?
princedx vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 13-11-2013, 06:04 PM   #2
cubom_sg
Nhập môn đệ tử
 
Tham gia ngày: Nov 2013
Bài gửi: 4
:
anh! day la code dieu khien thiet bi qua sms dung modul sim 300.
e cung dang lam de tai nay.
nhung ccs cua e bi thieu thu vien "lcd_lib_4bit1.c" nen k dich hex dc.
anh cho e xin cai thu vien ay duoc k?
mail: cubom_sg@yahoo.com.vn
cam on anh nhiu.
cubom_sg vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 13-11-2013, 09:14 PM   #3
unisa
Đệ tử 2 túi
 
Tham gia ngày: May 2005
Bài gửi: 27
:
đoạn code trên viết cho pic với sim 300 mấy anh pro có thể giải thích cho em 2 khúc được ko ạ?
Đoạn thứ 1
Code:
Receive_isr()  //Chương trình ngắt UART, khi PIC nhận được data truyền đến từ SIM300 thông qua giao thức UART, nó sẽ nhảy vào ctrình ngắt UART này để xử lý
{
  ch = getc(); // lệnh get() sẽ nhận các bye data được truyền đến từ SIM300
  if(isti==1)   //cho phep nhan khi da khoi tao thanh cong
   {
      if(ch==10 && index ==0) is=1;  //bat dau cho phep nhan chuoi
      if(ch==10 && index>0) {idex=index; ngat=1; isti = 2; }    //KHI KET THUC MOT CHUOI
      if(ch!=10 && ch!=13 && is == 1) { str[index] = ch;  index++; }
   }
   if(isti==2)   //cho phep nhan khi da khoi tao thanh cong
   {
      if(ch==10 && index1 ==0) is=1;  //bat dau cho phep nhan chuoi
      if(ch==10 && index1>0) {idex1=index1; ngat=2; isti = 0; }    //KHI KET THUC MOT CHUOI
      if(ch!=10 && ch!=13 && is == 1) { str1[index1] = ch; index1++; }
   }
đoạn thứ 2:
Code:
\putc('A'); putc('T'); putc('+'); putc('C'); putc('M'); putc('G'); putc('S'); putc('='); putc(34);
   putc('0'); putc('9'); putc('7'); putc('5'); putc('1'); putc('9'); putc('1'); putc('4'); putc('0');
   putc('9'); putc(34); putc(13); delay_ms(200);
   putc(str3[0]); putc(str3[1]); putc(str3[2]); putc(str3[3]); putc(str3[4]); putc(str3[5]);
   putc(str3[6]); putc(str3[7]); putc(str3[8]); putc(str3[9]); putc(str3[10]); putc(str3[11]); putc(str3[12]);putc(26); putc(13);
mấy cái putc(34); putc(13); delay_ms(200);
   putc(str3[0]); putc(str3[1]); putc(str3[2]); putc(str3[3]); putc(str3[4]); putc(str3[5]);
   putc(str3[6]); putc(str3[7]); putc(str3[8]); putc(str3[9]); putc(str3[10]); putc(str3[11]); putc(str3[12]);putc(26); putc(13); 

=> putc() là lệnh truyền data từ PIC ra thiết bị khác thông qua UART (RX,TX), ở đây cụ thể là giao tiếp với SIM300.
 VD cụ thể: 
 putc('A') có nghĩa là truyền ký tự A đến Sim300
 Sim300 khi hoạt động nó dựa vào tập lệnh để chạy, tập lệnh này đc truyền từ VDK đến SIM300 tùy theo ý đồ của ng lập trình. Bạn nên tham khảo datasheet của Sim300 để biết rõ tập lệnh.
-
Bạn nên tìm đọc CCS và PIC cơ bản để có khái niệm rõ hơn.
[/quote]
unisa vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 10:34 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam