Vì em mới nghiên cứu mảng này nên không rõ sữa lỗi.Mong mấy anh text lại em đoạn code này.
#include "16F877A.h"
#define <16f877a.h>
#device *=16 adc=10
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#include <LCD.C>
#define enter RB0
#define tang RB1
#define giam RB2
int8 read1,read2,m,thuc,dat_pc,dat,kp=100,kd=10,ki=2,er ror,pre_error;
int16 P,I,D,sum,duty;
#INT_RDA
void serial_isr()
{dat_pc=getc();
}
void send_to_pc(int8 giatri)
{ printf("%2u\r",giatri);
}
void phim_bam(void);
void main ()
{
set_tris_a(0xFF);
set_tris_b(0b00000111);
set_tris_c(0x00);
set_tris_d(0x00);
dat=39;
//=====================khoi tao ngat ngoai=================
enable_interrupts (INT_RDA);
//ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
//=====================khoi tao ADC ====================
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
delay_ms(50);
//=====================khoi tao PWM ====================
setup_ccp1(ccp_pwm);
lcd_init();
Printf(LCD_putc,"\fLOP 43DDK TNUT");
delay_ms(200);
Printf(LCD_putc,"\nDO AN NHUNG");
delay_ms(200);
Printf(LCD_putc,"\f Waiting........");
delay_ms(200);
while(1)
{
phim_bam();
set_ADC_channel(0);//Doc ADC tu LM35
delay_ms(10);
read2=read_adc();
thuc =read2*0.488758553;
//============= chong trinh PID so =======================
error = dat-thuc; // tinh sai so
P = KP*error; // tinh thanh phan ti le
I = I+KI*(pre_error+error)/4; // thoi gian lay mau la 0.5s
D = KD*(error-pre_error)*2;
pre_error= error; // luu lai gia tri sai so truoc do
sum = P+I+D; // tin hieu tong khi qua khau PID
duty = sum; // cap nhat gia PID vao bo phat PWM
if (duty>1023)
duty=1023; // gia tri lon nhat cua thanh ghi PWM
// if (error==0)
// I=255; // duy tri nhiet do khi ngo ra dat yeu cau
if (error<0)
duty =0; // tat PWM khi ngo ra vot lo
set_pwm1_duty(duty); // cho phep phat PWM voi gia tri duty cu the
// if(m!=0)
// { duty=0;
// error=0;
//}
//=========Gui du lieu ra LCD ==================
lcd_init();
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
// delay_us(200);
//=======================Gui du du lieu ra PC============
send_to_pc(thuc);
}
}
void phim_bam()
{
lcd_init();
if (input(pin_b0) ==0)
{
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
while(input(pin_b0) == 0)
{
}
}
if (input(pin_b2) == 0)
{
dat = dat - 1;
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
while(input(pin_b2) == 0)//cho nha phim
{
}
delay_ms(10);//chong nay phim
}
if (input(pin_b1) == 0)
{
dat = dat + 1;
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
while(input(pin_b1) == 0)//cho nha phim
{
}
delay_ms(10);//chong nay phim
}}
đây là đoạn code thuyết trình mô phỏng máy ấp trứng.mong mấy anh sửa một số lỗi giúp em.Em xin cảm ơn
#include "16F877A.h"
#define <16f877a.h>
#device *=16 adc=10
#use delay(clock=20000000)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#include <LCD.C>
#define enter RB0
#define tang RB1
#define giam RB2
int8 read1,read2,m,thuc,dat_pc,dat,kp=100,kd=10,ki=2,er ror,pre_error;
int16 P,I,D,sum,duty;
#INT_RDA
void serial_isr()
{dat_pc=getc();
}
void send_to_pc(int8 giatri)
{ printf("%2u\r",giatri);
}
void phim_bam(void);
void main ()
{
set_tris_a(0xFF);
set_tris_b(0b00000111);
set_tris_c(0x00);
set_tris_d(0x00);
dat=39;
//=====================khoi tao ngat ngoai=================
enable_interrupts (INT_RDA);
//ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
//=====================khoi tao ADC ====================
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
delay_ms(50);
//=====================khoi tao PWM ====================
setup_ccp1(ccp_pwm);
lcd_init();
Printf(LCD_putc,"\fLOP 43DDK TNUT");
delay_ms(200);
Printf(LCD_putc,"\nDO AN NHUNG");
delay_ms(200);
Printf(LCD_putc,"\f Waiting........");
delay_ms(200);
while(1)
{
phim_bam();
set_ADC_channel(0);//Doc ADC tu LM35
delay_ms(10);
read2=read_adc();
thuc =read2*0.488758553;
//============= chong trinh PID so =======================
error = dat-thuc; // tinh sai so
P = KP*error; // tinh thanh phan ti le
I = I+KI*(pre_error+error)/4; // thoi gian lay mau la 0.5s
D = KD*(error-pre_error)*2;
pre_error= error; // luu lai gia tri sai so truoc do
sum = P+I+D; // tin hieu tong khi qua khau PID
duty = sum; // cap nhat gia PID vao bo phat PWM
if (duty>1023)
duty=1023; // gia tri lon nhat cua thanh ghi PWM
// if (error==0)
// I=255; // duy tri nhiet do khi ngo ra dat yeu cau
if (error<0)
duty =0; // tat PWM khi ngo ra vot lo
set_pwm1_duty(duty); // cho phep phat PWM voi gia tri duty cu the
// if(m!=0)
// { duty=0;
// error=0;
//}
//=========Gui du lieu ra LCD ==================
lcd_init();
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
// delay_us(200);
//=======================Gui du du lieu ra PC============
send_to_pc(thuc);
}
}
void phim_bam()
{
lcd_init();
if (input(pin_b0) ==0)
{
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
while(input(pin_b0) == 0)
{
}
}
if (input(pin_b2) == 0)
{
dat = dat - 1;
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
while(input(pin_b2) == 0)//cho nha phim
{
}
delay_ms(10);//chong nay phim
}
if (input(pin_b1) == 0)
{
dat = dat + 1;
printf(lcd_putc,"\f\rNhietdodat: %u\nNhietdothuc: %u",dat,thuc);
while(input(pin_b1) == 0)//cho nha phim
{
}
delay_ms(10);//chong nay phim
}}
đây là đoạn code thuyết trình mô phỏng máy ấp trứng.mong mấy anh sửa một số lỗi giúp em.Em xin cảm ơn
Comment