Mình đang lập trình cho PIC16F877A bằng CCS, có đoạn code này mình muốn hỏi xem phần tính trung bình bằng vòng lặp for có đúng không hay phải làm thế nào khác:
#include <16F877A.h>
#include <def_877a.h>
//#device *=16
#include <math.h>
#include <stdlib.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal 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=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)
#include <lcd_lib_4bit.c>
unsigned int32 count=0,tb=0,tong=0;
unsigned int32 a[11];
void hv();
void average();
//------------------------------------
#int_EXT
void EXT_isr()
{
count=get_timer1();
set_timer1(0);
//printf(" %6ld\n",count);
hv();
}
//-------------------------------------
void average() //Ctrinh con tinh gia tri trung binh
{
int8 i;
tong=0;
tb=0;
for(i=0;i<=10;i++)
{
a[i]=count;
tong+=a[i];
tb=tong/11; //Hoi ve phan nay
}
}
//=====================================
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_DIV_256|RTCC_INTERNAL);
setup_timer_1(T1_DIV_BY_1|T1_EXTERNAL);
setup_timer_2(T2_DISABLED,0,1);
setup_vref(FALSE);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
ext_int_edge(H_TO_L);
//set_timer0(5);
set_timer1(0);
TRISA=0x00;
TRISB=0x01;
TRISE=0x00;
TRISC=0x01;
// TODO: USER CODE!!
tong=0;
tb=0;
hv();
lcd_init();
while(TRUE)
{
hv();
average();
printf(" %6ld\n",tb);
lcd_putcmd(0x01);
lcd_putcmd(0x80);
lcd_putchar("START");
lcd_putcmd(0xC0);
lcd_putchar("GET VALUE");
delay_ms(500);
lcd_putcmd(0x01);
}
}
//====================================
void hv() //ctrinh con dieu khien cao ap
{
output_low(PIN_C1);
output_high(PIN_C1);
}
//------------------------------------
Ở đây biến count nhận giá trị liên tục từ bộ đếm timer1 (cỡ vài micro giây). Để tính trung bình 11 giá trị liên tiếp của count nhận được thì làm như mình có được chưa? nếu chưa được thì cần sửa đoạn nào để có thể gán count cho từng thành phần của mảng a[i] rồi tính trung bình và reset mảng về 0 để tiếp tục gán lần tiếp theo?
Cảm ơn các bạn!
#include <16F877A.h>
#include <def_877a.h>
//#device *=16
#include <math.h>
#include <stdlib.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal 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=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)
#include <lcd_lib_4bit.c>
unsigned int32 count=0,tb=0,tong=0;
unsigned int32 a[11];
void hv();
void average();
//------------------------------------
#int_EXT
void EXT_isr()
{
count=get_timer1();
set_timer1(0);
//printf(" %6ld\n",count);
hv();
}
//-------------------------------------
void average() //Ctrinh con tinh gia tri trung binh
{
int8 i;
tong=0;
tb=0;
for(i=0;i<=10;i++)
{
a[i]=count;
tong+=a[i];
tb=tong/11; //Hoi ve phan nay
}
}
//=====================================
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_DIV_256|RTCC_INTERNAL);
setup_timer_1(T1_DIV_BY_1|T1_EXTERNAL);
setup_timer_2(T2_DISABLED,0,1);
setup_vref(FALSE);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
ext_int_edge(H_TO_L);
//set_timer0(5);
set_timer1(0);
TRISA=0x00;
TRISB=0x01;
TRISE=0x00;
TRISC=0x01;
// TODO: USER CODE!!
tong=0;
tb=0;
hv();
lcd_init();
while(TRUE)
{
hv();
average();
printf(" %6ld\n",tb);
lcd_putcmd(0x01);
lcd_putcmd(0x80);
lcd_putchar("START");
lcd_putcmd(0xC0);
lcd_putchar("GET VALUE");
delay_ms(500);
lcd_putcmd(0x01);
}
}
//====================================
void hv() //ctrinh con dieu khien cao ap
{
output_low(PIN_C1);
output_high(PIN_C1);
}
//------------------------------------
Ở đây biến count nhận giá trị liên tục từ bộ đếm timer1 (cỡ vài micro giây). Để tính trung bình 11 giá trị liên tiếp của count nhận được thì làm như mình có được chưa? nếu chưa được thì cần sửa đoạn nào để có thể gán count cho từng thành phần của mảng a[i] rồi tính trung bình và reset mảng về 0 để tiếp tục gán lần tiếp theo?
Cảm ơn các bạn!

Comment