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

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 23-04-2010, 08:01 AM   #1
v1t2m3
Nhập môn đệ tử
 
Tham gia ngày: Mar 2010
Bài gửi: 3
:
Red face code đo điện áp và dòng điện của em báo lỗi!

em đang viết 1 đoạn chương trình cho pic6f877a, em dựa trên tài liệu và hướng dẫn của mọi người trong diễn đàn để viết,trong đó có bài viết của anh linhnc, ban đầu em viết đo điện áp thì chạy được và mô phỏng được,nhưng sau em làm theo cách anh linhnc để lấy giá trị trung bình cuả điện áp hiển thị lên LCD thì ko được mong các anh,các bạn giúp mình,khi chạy xong báo lỗi nhưng mình tìm hoài ko ra.
Xin lỗi mọi ngươi vì mình chưa biết viết code trong khung:
code:

Code:
#include <16F877A.h>
#include <DEFS_16F877A.h>
#device *=16 ADC=10
#include <math.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#define RS RB3
#define RW RB4
#define E RB5
#byte lcd_data = PORTD
#define INTS_PER_SECOND0  4    // (20000000/(4*256*256))
#define INTS_PER_SECOND2  64       // (20000000/(4*16*256))

int16 nghin,tram,chuc,donvi,du1,du2;
int16 a[15],v[15],a1,v1;
byte int_count0,int_count2;

void do_dien_ap();
void do_dong_dien();
void xuat_ra_lcd();
void temp_1_buff();
void temp_2_buff();
void lcd_init();
void lcd_gotoxy();
void lcd_putc();
void lcd_send_byte();
//==================================

//============Ngat Timer 0 2=============
#int_rtcc                          // This function is called every time
void clock0_isr() {                 // the RTCC (timer0) overflows (255->0).
                                   // For this program this is apx 76 times
    if(--int_count0==0) {           // per second.
      temp_2_buff();
      temp_1_buff();
      int_count0=INTS_PER_SECOND0;
    }

}
#INT_TIMER2                        // This function is called every time
void clock1_isr() {                 // timer 1 overflows (65535->0), which is
                                   // approximately 19 times per second for
    if(--int_count2==0) {           // this program.
      xuat_ra_lcd();
      int_count2 = INTS_PER_SECOND2;
    }
}
//==================================
void main() 
{
set_tris_B(0);
set_tris_D(0);
set_tris_A(0xFF);
lcd_init();
delay_ms(100);
lcd_gotoxy(1,1);
lcd_putc("HE THONG KICH TU");
lcd_gotoxy(3,2);
lcd_putc("MAY PHAT");
delay_ms(100);
lcd_send_byte(0,1);//xoa LCD
lcd_send_byte(0,6);
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
delay_us(10);

//---------------------------
   int_count0=INTS_PER_SECOND0;
   int_count2=INTS_PER_SECOND2;
while(true)
   {
   do_dien_ap();
   do_dong_dien();
    }
}
//------------lay gia tri read_adc 15 lan-------------------
void temp_1_buff() {
set_adc_channel(0);
delay_us(10);
   v[15]=v[14];
   v[14]=v[13];
   v[13]=v[12];
   v[12]=v[11];
   v[11]=v[10];
   v[10]=v[9];
   v[9]=v[8];
   v[8]=v[7];
   v[7]=v[6];
   v[6]=v[5];
   v[5]=v[4];
   v[4]=v[3];
   v[3]=v[2];
   v[2]=v[1];
   v[1]=v[0];
   v[0]=read_adc();
}
void temp_2_buff() {
set_adc_channel(1);
delay_us(10);
   a[15]=a[14];
   a[14]=a[13];
   a[13]=a[12];
   a[12]=a[11];
   a[11]=a[10];
   a[10]=a[9];
   a[9]=a[8];
   a[8]=a[7];
   a[7]=a[6];
   a[6]=a[5];
   a[5]=a[4];
   a[4]=a[3];
   a[3]=a[2];
   a[2]=a[1];
   a[1]=a[0];
   a[0]=read_adc();
}
//-----------------------------------
void convert_bcd(int16 x)
  {
   nghin = x / 1000 + 0x30;
   du1  = x % 1000;
   tram  = du1 / 100 + 0x30;
   du2  = du1 % 100;
   chuc  = du2 / 10 + 0x30;
   donvi = du2 % 10 + 0x30;
  }
 //------------lenh cho LCD----------- 
  byte lcd_read_byte()
{
byte read_byte;
set_tris_D(0xFF); // PORTD = input
RW = 1;
delay_cycles(1);
E = 1;
delay_cycles(1);
read_byte = lcd_data;
E = 0;
set_tris_D(0x00); // PORTD = output
return(read_byte);
}
/* Goi 1byte den LCD */
void lcd_send_byte( byte address, byte n )
{
RS = 0;
while ( bit_test(lcd_read_byte(),7) ) ;
RS = address;
delay_cycles(1);
RW = 0;
delay_cycles(1);
E = 0;
lcd_data = n;
delay_cycles(1);
E = 1;
delay_us(2);
E = 0;
}

/* Khoi tao ban dau cho LCD */
void lcd_init()
{
byte const lcd_init_string[4] = {0x38, 0x0C, 1 , 6};
byte i;
set_tris_D(0x00);
RS = 0;
RW = 0;
E = 0;
delay_ms(15);
for(i=1;i<=3;++i)
{
lcd_data = 3;
delay_cycles(1);
E = 1;
delay_us(2);
E = 0;
delay_ms(5);
}
lcd_data = 2;
delay_cycles(1);
E = 1;
delay_us(2);
E = 0;
delay_ms(5);
for(i=0;i<=3;++i)
{
lcd_send_byte(0,lcd_init_string[i]);
}
}
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;

if(y!=1)
address=0x40;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1);
delay_ms(2);
break;
case '\n' : lcd_gotoxy(1,2); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
//===================================tinh trung binh read_adc
int16 cal_a() {
return((a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9])+a[11]+a[12]+a[13]+a[14]+a[15]/15);
}
int16 cal_v() {
return((v[0]+v[1]+v[2]+v[3]+v[4]+v[5]+v[6]+v[7]+v[8]+v[9]+v[10]+v[11]+v[12]+v[13]+v[14]+v[15])/15);
}


void do_dien_ap()
{
   set_timer0(0);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); // Tran = 13.1ms
   set_timer1(0);
   setup_timer_2(T2_DIV_BY_16,255,2);
   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_TIMER2);
   set_adc_channel(0);

}

void do_dong_dien()
{
   set_timer0(0);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); // Tran = 13.1ms
   set_timer2(0);
   setup_timer_2(T2_DIV_BY_16,255,2);
   enable_interrupts(INT_RTCC);
   enable_interrupts(INT_TIMER2);
   set_adc_channel(1);
}
void xuat_ra_lcd() {
int16 a1,v1;
   a1 = cal_a();
   a1=a1*10/1023;
   convert_bcd(a1);
   lcd_gotoxy(1,1);
   lcd_putc("I=");
   lcd_putc(nghin);
   lcd_putc(".");
   lcd_putc(tram);
   lcd_putc(chuc);
   lcd_putc("A");   
   v1= cal_v();
   v1=v1*5000/1023;
   convert_bcd(v1);
   lcd_gotoxy(10,1);
   lcd_putc("U=");
   lcd_putc(nghin);
   lcd_putc(tram);
   lcd_putc(chuc);
   lcd_putc("V");
}

thay đổi nội dung bởi: namqn, 25-04-2010 lúc 07:03 PM. Lý do: định dạng code
v1t2m3 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


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à 09:44 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